You are missing an option on that poll:
“I can’t even read my own code”
You are missing an option on that poll:
“I can’t even read my own code”
Sorry I didn’t specify, adding or removing code does not change the weight!!
Anyways, here’s some sample code from my joystick for FTC, this is how I like things whitespaced
while (true) { //whitespace after an important conditional or loop
getJoystickSettings(joystick); //single linebreak for basic commands and function calling
setMotors("default"); //double linebreak when another main conditional is about to appear
if (joystick.joy1_y1 > 40 || joystick.joy1_y1 < -40) { //single linebreak for a final conditional in nests
motor[motorD] = joystick.joy1_y1;
}
}
It actually makes more sense (to me) to have it on one line with a few spaces in between. Ideally, you’ve chosen descriptive variable names, so comments are not totally necessary.
Now, if a line is getting too long, I will break it between parameters to for readability.
And no, I haven’t had anyone complain about not enough whitespace yet. The parameters for my C++ class last semester were something like 1 extra line between groups of lines (say, between 2 for loops) and 3 lines between functions, plus the comment lines. I think I had about 650+ lines for a “voting machine” program, including whitespace and comment after comment after comment–and throw in some code for good measure.
Actually, one quote I’ve seen on here in the spotlights is: “A programmer’s job is to write documentation that just happens to compile.”, or something like that. Because it’s documentation, it needs to be readable. I’d have a hard time reading the code above, due to the fact that having all the parameters separate from the header makes them look like variable declarations within the function. That’s probably why they’re complaining–they can’t tell whether or not you’re declaring variables or parameters. If you do do it this way, you might want to indicate in the comment that it’s a parameter before anything else.
I usually only space out the parameter list when there’s a large number of parameters. In the example I tried to show that there were 20 parameters
What I prefer to do is to put a parameter list in the header comment. No extra comments… and the list is right behind it.
Then again, you should see some of my if statements and the conditions in them…
I would like to bring up the topic of the ternary operator. For those of you who do not know, in C you can use a?b:c; and have it as a completely valid statement so for example:
int foo(int bar)
{
return bar>0?1:-1;
}
The above is valid code and will return -1 if bar is negative and 1 otherwise. This is pretty clean code and, assuming you know the operator, is easy to read. Just curious what people think about it since we are talking about making code easier. I know that most of my college professors, though knowing the operator, do not like me using it.
ternary operator=blech. The problem with it is even if you know what it does(which not everyone does), I prefer syntax that actually reflects the logic of the process(like a standard if/else)
As for whitespace–I am known among some people for writing “MLA code”–basically, I code the same way that I would right an essay, with indents, paragraphs, etc.
Ditto that. Was wondering why there wasn’t a poll option for that…