Go to Post In a game full of tanks I think the Jeeps will win the all-terrain battle. - JesseK [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
 
Thread Tools Rating: Thread Rating: 6 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 23-02-2016, 17:37
King Nerd III's Avatar
King Nerd III King Nerd III is offline
Chief Programmer/Head of Autonomous
AKA: Isaac
FRC #1410 (The Kraken)
Team Role: Programmer
 
Join Date: Jan 2014
Rookie Year: 2014
Location: Denver, CO
Posts: 113
King Nerd III is an unknown quantity at this point
Thoughts On Comments In Team Code

I was busy rewriting my teams code today, just cleaning up some things and adding comments, and I realized that I tend to over comment things (at least in my opinion) when I write code for robotics. Upon realizing this, I decided that in reality, it's good that I over comment my code, as we always have some new programmers each year, and I think it's good for all the comments to be there so that they know what's going on and can hopefully learn how the code works so that they can then go and write it on their own. Here's a sample of the amount of comments I tend to write:
Code:
void DriveBase::DriveTank(float left_speed, float right_speed){
	//This method drives each side of the robot using set values
	//The values are not changed in any way
	//Since the right motors are opposite of the left motors, their values are inverted in the command
	//Because of this, when the speeds are output to the SmartDashboard we invert the inverted value
	fl_motor->Set(left_speed); //Sets the front left motor to the left speed
	fr_motor->Set(right_speed); //Sets the front right motor to the right speed
	bl_motor->Set(left_speed); //Sets the back left motor to the left speed
	br_motor->Set(right_speed); //Sets the back right motor to the right speed
	SmartDashboard::PutNumber("Left Speed", left_speed); //Puts the left speed on SmartDashboard
	SmartDashboard::PutNumber("Right Speed", right_speed * -1); //Puts the inverted right speed on SmartDashboard
}
So here's my question to you, the programmers on Chief Delphi: What's your opinion on commenting robot code? Should there be an insane amount of comments so that new kids understand what's going on, or do you prefer to live by the mantra always repeated to me, "good code should explain itself"?
Let me know what you guys think, I'm very interested in knowing your opinions on the subject.
__________________
Isaac
Chief of Programming and Head of Autonomous Control
FRC Team 1410
  #2   Spotlight this post!  
Unread 23-02-2016, 17:40
Alex Webber Alex Webber is offline
Programming / Web Site / Scouting
FRC #1810 (Jaguar Robotics)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2015
Location: Lenexa, KS
Posts: 102
Alex Webber is on a distinguished road
Re: Thoughts On Comments In Team Code

Our team doesn't put cmments inside our methods. We Java Doc our code, with easily explainable wording so others can understand what is going on.

During development we typically do, but we transfer to Java Docs sooner or later.
__________________
Alex Webber | Programming and Scout Master
Jaguar Robotics FRC 1810
2015 | Greater Kansas City Regional 16/54

Last edited by Alex Webber : 23-02-2016 at 17:56.
  #3   Spotlight this post!  
Unread 23-02-2016, 17:50
cadandcookies's Avatar
cadandcookies cadandcookies is offline
Director of Programs, GOFIRST
AKA: Nick Aarestad
FTC #9205 (The Iron Maidens)
Team Role: College Student
 
Join Date: Jan 2012
Rookie Year: 2009
Location: Minnesnowta
Posts: 1,496
cadandcookies has a reputation beyond reputecadandcookies has a reputation beyond reputecadandcookies has a reputation beyond reputecadandcookies has a reputation beyond reputecadandcookies has a reputation beyond reputecadandcookies has a reputation beyond reputecadandcookies has a reputation beyond reputecadandcookies has a reputation beyond reputecadandcookies has a reputation beyond reputecadandcookies has a reputation beyond reputecadandcookies has a reputation beyond repute
Re: Thoughts On Comments In Team Code

Quote:
Originally Posted by Alex Webber View Post
Our team doesn't put code inside our methods.
I'm curious as to how you manage to do that and still have a functional robot
__________________

Never assume the motives of others are, to them, less noble than yours are to you. - John Perry Barlow
tumblr | twitter
'Snow Problem CAD Files: 2015 2016
MN FTC Field Manager, FTA, CSA, Emcee
FLL Maybe NXT Year (09-10) -> FRC 2220 (11-14) -> FTC 9205(14-?)/FRC 2667 (15-16)
VEXU UMN (2015-??)
Volunteer since 2011
2013 RCA Winner (North Star Regional) (2220)
2016 Connect Award Winner (North Super Regional and World Championship) (9205)
  #4   Spotlight this post!  
Unread 23-02-2016, 17:55
Alex Webber Alex Webber is offline
Programming / Web Site / Scouting
FRC #1810 (Jaguar Robotics)
Team Role: Programmer
 
Join Date: Jan 2015
Rookie Year: 2015
Location: Lenexa, KS
Posts: 102
Alex Webber is on a distinguished road
Re: Thoughts On Comments In Team Code

Quote:
Originally Posted by cadandcookies View Post
I'm curious as to how you manage to do that and still have a functional robot
Oops. Should have been comments. Spell check lol.
__________________
Alex Webber | Programming and Scout Master
Jaguar Robotics FRC 1810
2015 | Greater Kansas City Regional 16/54
  #5   Spotlight this post!  
Unread 23-02-2016, 17:59
kylelanman's Avatar
kylelanman kylelanman is offline
Programming Mentor
AKA: Kyle
FRC #2481 (Roboteers)
Team Role: Mentor
 
Join Date: Feb 2008
Rookie Year: 2007
Location: Tremont Il
Posts: 185
kylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to allkylelanman is a name known to all
Re: Thoughts On Comments In Team Code

I encourage the students to comment the 'why' not the 'what'. When students are writing code the 'what' should be obvious. If not then it should probably be re-written. Commenting 'why' something is done is much more useful to someone reading the code.
__________________
"May the coms be with you"

Is this a "programming error" or a "programmer error"?

  #6   Spotlight this post!  
Unread 23-02-2016, 19:32
King Nerd III's Avatar
King Nerd III King Nerd III is offline
Chief Programmer/Head of Autonomous
AKA: Isaac
FRC #1410 (The Kraken)
Team Role: Programmer
 
Join Date: Jan 2014
Rookie Year: 2014
Location: Denver, CO
Posts: 113
King Nerd III is an unknown quantity at this point
Re: Thoughts On Comments In Team Code

Quote:
Originally Posted by kylelanman View Post
I encourage the students to comment the 'why' not the 'what'. When students are writing code the 'what' should be obvious. If not then it should probably be re-written. Commenting 'why' something is done is much more useful to someone reading the code.
That's a great idea, I'll keep that in mind. I think based off of your advice I'll start having kids comment their code in that way, but I'm going to keep writing my comments as I have been, because some kids like to learn by reading my code, which means I feel like they would benefit from seeing the 'what' along with the 'why'. Thanks for the input!
__________________
Isaac
Chief of Programming and Head of Autonomous Control
FRC Team 1410
  #7   Spotlight this post!  
Unread 23-02-2016, 17:52
Foster Foster is offline
Engineering Program Management
VRC #8081 (STEMRobotics)
Team Role: Mentor
 
Join Date: Jul 2007
Rookie Year: 2005
Location: Delaware
Posts: 1,369
Foster has a reputation beyond reputeFoster has a reputation beyond reputeFoster has a reputation beyond reputeFoster has a reputation beyond reputeFoster has a reputation beyond reputeFoster has a reputation beyond reputeFoster has a reputation beyond reputeFoster has a reputation beyond reputeFoster has a reputation beyond reputeFoster has a reputation beyond reputeFoster has a reputation beyond repute
Re: Thoughts On Comments In Team Code

So I comment my code a lot, but I don't comment the obvious. Like setting the speed to the motors is pretty clear from the code. But I did change the variable names on the code to make it clear it was front_left and not f1. (See you looked to "is that a one or an L"). Same thing with the dashboard. The code is clear, so the "set the dashboard" comments don't add any value. A reminder about the inversion isn't needed since we saw the comment 7 lines before, but it's a little weird, so worth a comment.

And if this is where the drive speed is actually set then I'd do the speed inversion here. That way the entire stack on top is going Foward is positive (+) Reverse is negative (-) and does not need to do the "well the right motors need to be inverted" every place else. That would also get rid of the need to re-invert it back for the display. That would reduce some code complexity.

My 2 cents since you asked, your mileage WILL vary.


Code:
void DriveBase::DriveTank(float left_speed, float right_speed){
	//This method drives each side of the robot using set values
	//The values are not changed in any way
	//Since the right motors are opposite of the left motors, their values are inverted in the command
	//Because of this, when the speeds are output to the SmartDashboard we invert the inverted value

	front_left_motor->Set(left_speed); //Set the motor speeds
	front_right_motor->Set(right_speed);
	back_left_motor->Set(left_speed);
	back_right_motor->Set(right_speed);

	SmartDashboard::PutNumber("Left Speed", left_speed); 
	SmartDashboard::PutNumber("Right Speed", right_speed * -1); //inverted right speed to make the number positive on the display
}
And with the inversion done here

Code:
void DriveBase::DriveTank(float left_speed, float right_speed){
	//This method drives each side of the robot. Forward is positive speeds, reverse is negative speeds.
	//The values are not changed in any way
	//Since the right motors are opposite of the left motors, their values are inverted when we set them

	front_left_motor->Set(left_speed); //Set the motor speeds
	front_right_motor->Set(right_speed * (-1) );
	back_left_motor->Set(left_speed);
	back_right_motor->Set(right_speed * (-1) );

	SmartDashboard::PutNumber("Left Speed", left_speed); 
	SmartDashboard::PutNumber("Right Speed", right_speed); 
}
__________________
Foster - VEX Delaware - 17 teams -- Chief Roboteer STEMRobotics.org
2010 - Mentor of the Year - VEX Clean Sweep World Championship
2006-2016, a decade of doing VEX, time really flies while having fun
Downingtown Area Robotics Web site and VEXMen Team Site come see what we can do for you.

Last edited by Foster : 23-02-2016 at 18:02. Reason: Too much time on my hands
  #8   Spotlight this post!  
Unread 23-02-2016, 19:30
King Nerd III's Avatar
King Nerd III King Nerd III is offline
Chief Programmer/Head of Autonomous
AKA: Isaac
FRC #1410 (The Kraken)
Team Role: Programmer
 
Join Date: Jan 2014
Rookie Year: 2014
Location: Denver, CO
Posts: 113
King Nerd III is an unknown quantity at this point
Re: Thoughts On Comments In Team Code

Quote:
Originally Posted by Foster View Post
So I comment my code a lot, but I don't comment the obvious. Like setting the speed to the motors is pretty clear from the code. But I did change the variable names on the code to make it clear it was front_left and not f1. (See you looked to "is that a one or an L"). Same thing with the dashboard. The code is clear, so the "set the dashboard" comments don't add any value. A reminder about the inversion isn't needed since we saw the comment 7 lines before, but it's a little weird, so worth a comment.

And if this is where the drive speed is actually set then I'd do the speed inversion here. That way the entire stack on top is going Foward is positive (+) Reverse is negative (-) and does not need to do the "well the right motors need to be inverted" every place else. That would also get rid of the need to re-invert it back for the display. That would reduce some code complexity.

My 2 cents since you asked, your mileage WILL vary.


Code:
void DriveBase::DriveTank(float left_speed, float right_speed){
	//This method drives each side of the robot using set values
	//The values are not changed in any way
	//Since the right motors are opposite of the left motors, their values are inverted in the command
	//Because of this, when the speeds are output to the SmartDashboard we invert the inverted value

	front_left_motor->Set(left_speed); //Set the motor speeds
	front_right_motor->Set(right_speed);
	back_left_motor->Set(left_speed);
	back_right_motor->Set(right_speed);

	SmartDashboard::PutNumber("Left Speed", left_speed); 
	SmartDashboard::PutNumber("Right Speed", right_speed * -1); //inverted right speed to make the number positive on the display
}
And with the inversion done here

Code:
void DriveBase::DriveTank(float left_speed, float right_speed){
	//This method drives each side of the robot. Forward is positive speeds, reverse is negative speeds.
	//The values are not changed in any way
	//Since the right motors are opposite of the left motors, their values are inverted when we set them

	front_left_motor->Set(left_speed); //Set the motor speeds
	front_right_motor->Set(right_speed * (-1) );
	back_left_motor->Set(left_speed);
	back_right_motor->Set(right_speed * (-1) );

	SmartDashboard::PutNumber("Left Speed", left_speed); 
	SmartDashboard::PutNumber("Right Speed", right_speed); 
}
In my experience, it doesn't really change much if you have it in the method or the command, as either way it's just one or two * -1 somewhere in the code. The reason we do it this way is because it just makes things easier when calling it in different commands, but to each their own!
__________________
Isaac
Chief of Programming and Head of Autonomous Control
FRC Team 1410
  #9   Spotlight this post!  
Unread 23-02-2016, 17:54
SamCarlberg's Avatar
SamCarlberg SamCarlberg is offline
GRIP, WPILib. 2084 alum
AKA: Sam Carlberg
no team
Team Role: Mentor
 
Join Date: Nov 2015
Rookie Year: 2009
Location: MA
Posts: 43
SamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to beholdSamCarlberg is a splendid one to behold
Re: Thoughts On Comments In Team Code

Quote:
Originally Posted by Alex Webber View Post
Our team doesn't put code inside our methods. We Java Doc our code, with easily explainable wording so others can understand what is going on.

During development we typically do, but we transfer to Java Docs sooner or later.
Javadoc explains what the method does and anything that's needed for it to work properly. It doesn't explain reasoning behind the logic inside, which is very unhelpful when trying to debug. It doesn't need to be as zealous as OP's example, but you should at least have comments explaining non-obvious code
__________________
WPILib
GRIP, RobotBuilder
  #10   Spotlight this post!  
Unread 24-02-2016, 11:44
Caleb Sykes's Avatar
Caleb Sykes Caleb Sykes is offline
Registered User
FRC #4536 (MinuteBots)
Team Role: Mentor
 
Join Date: Feb 2011
Rookie Year: 2009
Location: St. Paul, Minnesota
Posts: 1,029
Caleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond repute
Re: Thoughts On Comments In Team Code

We use javadocs as much as we can. In addition, we usually add comments explicitly stating units of variables. Besides that, we occasionally add in comments explaining what a single line does, but this is rare.

Putting a comment on every single line is a waste of human resources and just clutters the code.
  #11   Spotlight this post!  
Unread 24-02-2016, 12:25
aeastet aeastet is offline
Programming Mentor
AKA: Tim Easterling
FRC #6043 (Allegan Tigers Robotics)
Team Role: Coach
 
Join Date: Jan 2015
Rookie Year: 2011
Location: Holland, MI
Posts: 113
aeastet is an unknown quantity at this point
Re: Thoughts On Comments In Team Code

There is no such thing as too many comments. You have to remember that you will not always be there to explain what you were thinking. The more comments the better chance for getting help or your code surviving after you are gone. I have never seen code with too many comments. I don't believe that is possible.
  #12   Spotlight this post!  
Unread 24-02-2016, 12:37
virtuald's Avatar
virtuald virtuald is offline
RobotPy Guy
AKA: Dustin Spicuzza
FRC #1418 (), FRC #1973, FRC #4796, FRC #6367 ()
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2003
Location: Boston, MA
Posts: 1,031
virtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant futurevirtuald has a brilliant future
Re: Thoughts On Comments In Team Code

I suggest this short reading for those who believe comments to be unnecessary: http://catb.org/esr/writings/unix-koans/prodigy.html
__________________
Maintainer of RobotPy - Python for FRC
Creator of pyfrc (Robot Simulator + utilities for Python) and pynetworktables/pynetworktables2js (NetworkTables for Python & Javascript)

2017 Season: Teams #1973, #4796, #6369
Team #1418 (remote mentor): Newton Quarterfinalists, 2016 Chesapeake District Champion, 2x Innovation in Control award, 2x district event winner
Team #1418: 2015 DC Regional Innovation In Control Award, #2 seed; 2014 VA Industrial Design Award; 2014 Finalists in DC & VA
Team #2423: 2012 & 2013 Boston Regional Innovation in Control Award


Resources: FIRSTWiki (relaunched!) | My Software Stuff
  #13   Spotlight this post!  
Unread 24-02-2016, 13:27
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,112
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: Thoughts On Comments In Team Code

Quote:
Originally Posted by aeastet View Post
There is no such thing as too many comments. You have to remember that you will not always be there to explain what you were thinking.
Comments that tell what a block of code does are good, as long as they don't get into too much detail. Comments that describe why the code does something are important. Comments that explain how it works at a high level are fine.

But comments that just duplicate the code in paraphrase do not help, and they can actively hurt if they fall out of sync with programming changes.

Quote:
I have never seen code with too many comments. I don't believe that is possible.
I have seen code with too many comments. It was like
Code:
ACHK  EQU *       ; check if ASCII input is a letter
      LDA CNV     ; get the configuration value in the accumulator
      SUB #64     ; subtract 64
      BLT SKIP2   ; skip if less than zero
      ...
The first line's comment is helpful, describing what the routine is intended to do. The rest of the line-by-line commentary adds absolutely no information. It serves only to clutter the source code.
  #14   Spotlight this post!  
Unread 24-02-2016, 13:32
Unsung FIRST Hero
Karthik Karthik is offline
VEX Robotics GDC Chairman
no team
Team Role: Mentor
 
Join Date: Apr 2002
Rookie Year: 1998
Location: Toronto, Ontario, Canada
Posts: 2,339
Karthik has a reputation beyond reputeKarthik has a reputation beyond reputeKarthik has a reputation beyond reputeKarthik has a reputation beyond reputeKarthik has a reputation beyond reputeKarthik has a reputation beyond reputeKarthik has a reputation beyond reputeKarthik has a reputation beyond reputeKarthik has a reputation beyond reputeKarthik has a reputation beyond reputeKarthik has a reputation beyond repute
Re: Thoughts On Comments In Team Code

Quote:
Originally Posted by Alan Anderson View Post


I have seen code with too many comments. It was like
Code:
ACHK  EQU *       ; check if ASCII input is a letter
      LDA CNV     ; get the configuration value in the accumulator
      SUB #64     ; subtract 64
      BLT SKIP2   ; skip if less than zero
      ...
The first line's comment is helpful, describing what the routine is intended to do. The rest of the line-by-line commentary adds absolutely no information. It serves only to clutter the source code.
It added plenty of information for me. I would not have understood that snippet of code without the comments.
__________________
:: Karthik Kanagasabapathy ::
"Enthusiasm is one of the most powerful engines of success. When you do a thing, do it with all your might. Put your whole soul into it. Stamp it with your own personality. Be active, be energetic, be enthusiastic and faithful and you will accomplish your object. Nothing great was ever achieved without enthusiasm" -- R.W. Emerson
My TEDx Talk - The Subtle Secrets of Success
Full disclosure: I work for IFI and VEX Robotics, and am the Chairman of the VEX Robotics and VEX IQ Game Design Committees
.
  #15   Spotlight this post!  
Unread 23-02-2016, 19:34
King Nerd III's Avatar
King Nerd III King Nerd III is offline
Chief Programmer/Head of Autonomous
AKA: Isaac
FRC #1410 (The Kraken)
Team Role: Programmer
 
Join Date: Jan 2014
Rookie Year: 2014
Location: Denver, CO
Posts: 113
King Nerd III is an unknown quantity at this point
Re: Thoughts On Comments In Team Code

Quote:
Originally Posted by Alex Webber View Post
Our team doesn't put cmments inside our methods. We Java Doc our code, with easily explainable wording so others can understand what is going on.

During development we typically do, but we transfer to Java Docs sooner or later.
I'm thinking from a developmental and real world practicing standpoint, this practice would be great, but I'm more wondering about the teaching value of over commenting (if there is any). But I do think from a developmental standpoint I'm going to try to shift my team towards a more 'why' commenting system and less of a 'what', as I said to someone else on this thread. Thank you for your input!
__________________
Isaac
Chief of Programming and Head of Autonomous Control
FRC Team 1410
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 17:31.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi