|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools |
Rating:
|
Display Modes |
|
#1
|
||||
|
||||
|
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
}
Let me know what you guys think, I'm very interested in knowing your opinions on the subject. |
|
#2
|
|||
|
|||
|
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. Last edited by Alex Webber : 23-02-2016 at 17:56. |
|
#3
|
||||
|
||||
|
Re: Thoughts On Comments In Team Code
I'm curious as to how you manage to do that and still have a functional robot
![]() |
|
#4
|
|||
|
|||
|
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
}
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);
}
Last edited by Foster : 23-02-2016 at 18:02. Reason: Too much time on my hands |
|
#5
|
||||
|
||||
|
Re: Thoughts On Comments In Team Code
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
|
|
#6
|
|||
|
|||
|
Re: Thoughts On Comments In Team Code
Oops. Should have been comments. Spell check lol.
|
|
#7
|
||||
|
||||
|
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.
|
|
#8
|
||||
|
||||
|
Re: Thoughts On Comments In Team Code
Quote:
|
|
#9
|
||||
|
||||
|
Re: Thoughts On Comments In Team 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!
|
|
#10
|
||||
|
||||
|
Re: Thoughts On Comments In Team Code
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!
|
|
#11
|
|||||
|
|||||
|
Re: Thoughts On Comments In Team Code
Quote:
Seriously, don't do that. Comments that merely echo the code are distracting at best. They can actually be confusing and counterproductive, as they can give the reader the impression that the author is trying to explain something unobvious about what the code is doing. Keep putting the "block comments" at the beginning of sections, explaining what the code is for, and explain assumptions and possible side effects, but don't overdo the line-by-line commentary on what the code does. You can better document that kind of information by choosing appropriately detailed and helpful names for functions/methods and variables. For example, instead of the abbreviated fl_motor, make the reader's job easier by calling it front_left_motor. Instead of left_speed, be more explicit with left_speed_desired or left_drive_commanded. That way it is obvious that you're not talking about left_speed_measured or left_intake_commanded. |
|
#12
|
|||
|
|||
|
Re: Thoughts On Comments In Team Code
Our coding guidelines at work say this about comments:
Quote:
Code:
/**
* Operate the drive base using tank-style control, where a joystick is
* assigned to each side of the robot and independently controls its speed.
* @param left_speed Desired speed of left side of robot
* @param right_speed Desired speed of right side of robot.
**/
void DriveBase::DriveTank(float left_speed, float right_speed){
front_left_motor->Set(left_speed);
back_left_motor->Set(left_speed);
// Right motors operate in opposite direction of left, so we negate
// the values here to maintain the correct orientation.
front_right_motor->Set(-right_speed);
back_right_motor->Set(-right_speed);
SmartDashboard::PutNumber("Left Drive Speed", left_speed);
SmartDashboard::PutNumber("Right Drive Speed", -right_speed);
}
|
|
#13
|
|||||
|
|||||
|
Re: Thoughts On Comments In Team Code
From one who is not a programming person, comments are akin to the "show all your work" direction for math problems. However, when writing consider your audience.
|
|
#14
|
||||
|
||||
|
Re: Thoughts On Comments In Team Code
That's always been my approach to comments. I like to have every part of my thinking written out for me to review later, just like in math class.
|
|
#15
|
|||
|
|||
|
Re: Thoughts On Comments In Team Code
I'd echo what Alan said. Using clear variable names and avoiding confusing syntax is better practice than over commenting within code. Definitely have everything documented in Java Docs if you're using Java though.
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|