How important is it to reduce the number of lines in code?
programmers.stackexchange.com
programmers.stackexchange.com
Will I be able to read my code tomorrow or next week and know what in the heck I did?
People will go to endless lengths to make their code more terse and/or "self-documenting", but almost nobody takes the five extra minutes it takes to write an intelligent comment. The comments are far more important.
// p.xyz += f.xyz * timeStep
__m128 ts = _mm_load1_ps(&timeStep);
__m128 x, y, z;
__m128 fx, fy, fz;
for(int i = 0; i < NUM_PARTICLES; i+=4) {
x = _mm_load_ps(px + i);
y = _mm_load_ps(py + i);
z = _mm_load_ps(pz + i);
fx = _mm_load_ps(pfx + i);
fy = _mm_load_ps(pfy + i);
fz = _mm_load_ps(pfz + i);
x = _mm_add_ps(_mm_mul_ps(fx, ts), x);
y = _mm_add_ps(_mm_mul_ps(fy, ts), y);
z = _mm_add_ps(_mm_mul_ps(fz, ts), z);
_mm_store_ps(px + i, x);
_mm_store_ps(py + i, y);
_mm_store_ps(pz + i, z);
}
Sometimes, it's because you're doing something that looks surprising, but is there for some reason, so that the next time someone looks at that code, they know why something is done in that fashion. val loginForm = Form(
tuple(
"email" -> text,
"password" -> text,
"rememberMe" -> optional(text) // jfim: HACK For some reason, having this as boolean causes a None.get in Play 2.1.0
)
)
Comments should be there to explain why the code does something or what it does in a more readable fashion(because it's faster to read plain English than code), neither of which can be covered by tests.How many times in your codebase have you seen a comment like:
// This exists because that dumb thing happened,
// and during the migration from Y to Z we had to
// support these two systems simultaneously. This
// component mediates between the two . . .
And someone had added some common logic into the mediator, then someone else had hooked into that, and now there's a snarl.The most important thing is that the system be as simple as it can while still performing its intended functions. We have all made a first cut at something that ended up being three times as verbose or complicated as our second. You want a system made out of pieces of the second kind. That's even more important than comments.
I can definitely understand the desire to reduce lines of code, because it's often a good proxy for understanding how complicated a system is. Not always, but most of the time, something a quarter as long that does the same job is doing it a simpler way.
In your example, even if that comment is outdated, it's still incredibly useful. It tells you, in under 5 seconds, the original intent of a piece of code, and how it was supposed to fit into the entire system. That's something you'll never get without a comprehensive understanding of the project, which takes far more than 5 seconds of reading. The real problem here is not that there is a comment -- the problem is that a bad coder fucked up by not updating the comment. There's no excuse.
That all of the commenters below will spend paragraphs explaining how they shouldn't have to document their code with the occasional descriptive comment is an amazing testament to the overwhelming power of human laziness and self-delusion.
Obviously, both will have a part in any well written an maintained system. But the person I'm responding to held good comments up as the most important aspect of system maintainability, and that is simply not the case.
I'm not saying that comments are the "most important" aspect of system maintainability, but that they're one of a few things that are all of top importance.
Obviously, if you write spaghetti code, your system won't be maintainable, no matter how many comments you write. But that's a false dichotomy: you need to write good code and you need to write good comments. Neglecting either one leads to unmaintainable systems.
Even so, there are some times when I need to write some mentally dense code to accomplish several competing goals. It is then where I explain what's going on and why the code was built that way.
It's a rule of thumb, and snark will put you over that easily, but IME, it can suggest you're getting overzealous about commenting when you've got more than that.
in C
#include <studio.h>#include <stdlib.h>
won't work. In perl, strings with literal new lines or here docs will break (terminator must be on a line by itself)Any language with 'comment till end of line' would break.
Any language that does not use semicolons as statement terminator or separator such as Forth, Postscript, and XML-based languages wouldn't like the inserted semicolons.
Even pascal would break, as it has a few places where repeated semicolons are illegal, as in the third line of
type
T = record
a : integer;
end;
Algol has a problem changing if x=4 then y:=5
else y:=6;
into if x=4 then y:=5;else y:=6;
Because a semicolon is a statement separator in Algol (I think pascal has this problem, too)So, can anybody suggest a language where this would work?
* BIG NOTE * This excludes code which is artificially short due to 'clever' coding.
In other words, if you're thinking "that guy must be productive, he wrote twice as many lines as this other guy" or "this code must be slower since it's 3 times longer than that code" you're most likely wrong in both cases. Or, in the best case, very naive.
And what's up with Ars Technica just copy-pasting SO (or prog.SE) answers into an article?
Another example would be from PHP, where you can many times do tasks in several different ways, and so you may have this code (straight from the manual) for reading the contents of a file:
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
And this isn't even very descriptive, unless you have a background in other languages such as C to grok that filesize($filename) might mean "read to the end of the file".OTOH, this code does the exact same thing and is extremely explicit about what it's doing:
$contents = file_get_contents("/usr/local/something.txt");The only thing it will do is cause you to crush your lines wherever possible and code horizontally instead of vertically, which generally conflicts with readability. In fact, I'd actually impose a limit on line length to prevent just that.
Instead, write code that makes sense when you read it. Avoid cleverness, and DRY. Repeating yourself in the way that violates DRY principles can often make reading code more difficult because you're essentially forced to re-read repetitive code because you can't assume it's all identical.
The only exception to this is code standards for consistency in the code base that make sense, such as use a '{' on the same line as the definition or omit the '{}' block if the body is just 1 statement in cases where you won't violate a width constraint, etc. Anything that isn't completely reasonable as a code standard is optimizing for line count and is probably the wrong thing to do.
Be thankful downvotes are anonymous. You are very likely incredibly incompetent, and I fear for anyone who has to read your code or worse, use it.
for instance, say you notice you've got multiple for loops iterating over matrixes of pixel data and running transformation functions on member elements. The first level would be to abstract this into an each function taking the array and the function to call, the next step would be to pack the each into an even more general function, and now all those lines have been compacted into a single call pixel_data.update_array_matrix.
I found his use of the word "crisp" very interesting. I believe it means to keep your code short and to the point, without any extra embellishment.
I work differently now. I mostly work in Clojure and I write very few comments but I like long and descriptive function and variable names. I think that a concise language like Clojure or Ruby, combined with very good semantic naming works much better for me. I can look at year old code, and fall right into it.
Of course, some complexity/warts will be needed.