Annoying Code
This is ugly code:
if (cond)
{
stmt;
}
else if (othercond)
{
stmt;
}
else
{
stmt;
}
This is pretty code:
if (cond) {
stmt;
} else if (othercond) {
stmt;
} else {
stmt;
}
This is prettier code:
if cond:
stmt
elif othercond:
stmt
else:
stmt
How is the prettiness measured? By code density. Code looks horrible when it is not dense. Comments and language symbols too easily break up the density without really adding much to the value of the code. The last form is easily parsed by humans and contains no unnecessary symbols used for computer parsing convenience. The goal is to make the computer do the work. Compiled languages especially have no excuse for a better syntax.
The middle style is acceptable and the best style in languages with all those extra symbols like parentheses and braces and semicolons. It reads smoothly, and the code is densely packed. The first style, however, is just awful. There is no code density: every single brace breaks up the reading flow. It's twice as many lines as the middle form, and worse none of those extra lines helps in any way to the computer or to the programmer.
If I have a pet peeve for coding style, it is seeing the first form. It is disgusting. It should be abolished with all speed. It's so bad that just seeing it distracts me from the content of the code. Opening braces on their own lines after the start of a function or struct I can live with, but this is madness. Start nesting blocks and you get into even eviler territories. It's just nasty. If you do it, stop it at once. Please.
Addendum: I've seen a compromise of style 1 and style 2. I'm not even sure it's better than style 1, because it's still ugly; you decide:
if (cond)
{ stmt;
stmt;
} else
{ stmt;
stmt;
}
Posted on 2009-11-29 by Jach
Tags: programming
Permalink: https://www.thejach.com/view/id/51
Trackback URL: https://www.thejach.com/view/2009/11/annoying_code
Recent Posts
2025-03-15
2025-03-03
2025-02-13
2025-01-14
2025-01-10