Readable code (Was: Re: [H-GEN] Which is better?)

Adrian Sutton adrian at intencha.com
Wed Apr 28 07:53:54 EDT 2004


> On the subject of readable code, will I have to get used to reading 
> code that looks like this:

heh.   You're trying to keep me busy writing contentious emails aren't 
you? :)  Coding style is a matter of personal preference.  Having said 
that, there are distinct advantages to using a familiar coding style.  
I have the firm belief that you should use the coding style most 
prevalent for the language you're using (barring any glaringly bad 
ideas in that style).  So for Java code I would follow the Sun Java 
coding convention:

if (orient == east) {
     if (x + radius > side) {
         if (clockwise) {
             orient = south;
         } else {
             orient = north;
         }
     } else {
         x = x + step;
     }
} else if (orient == south) {
....

Note the use of spaces around operators and the brace on the same line 
style.  Both of these styles are hallmarks of Java code.  There's no 
need to be too pedantic about the fine details though.

However, if I were writing in C, I would say the style that should be 
used would be the second one you quoted but probably with spaces around 
the operators.  Braces however would be the on the next line type.  Of 
course I don't actually follow that rule because I personally can't 
stand that style at all and I don't write C code that others have to 
read (just myself) so it's easier and more productive to follow the 
Java coding standards.  If I were to get a job writing C code I would 
switch to the more "C-like" style.

> You can easily see what code is executed during which if statement. I 
> know doing it that way would create long lines for deeply nested if 
> statements (or for loops). Is that why most of the code I see is 
> written the first way?

I agree, however many wouldn't.  I have no idea how people can consider 
that code more readable - I guess some people don't like whitespace or 
find it easier to read very densely packed text.  The two things that I 
really jump up and down about (again which many people don't):

if (condition) statement;

particularly if it's followed by an else:

if (condition) statement;
else statement;

I like the consistency of using block statements for all control 
structures.  Others prefer to save typing and/or don't like white 
space.  The other thing I really don't like is the use of "compound 
statements" like:

while (i++ < 10)

or

while (line = in.readLine())

Many people think saving a line is worth it but there is just too much 
confusion over what gets evaluated when that I figure it's better to 
write out the extra line and be explicit.  Somewhat hypocritically 
however I don't like excessive bracketing of expressions as I tend to 
find that harder to read:

((i + 1) - 3)  compared with i + 1 - 3

I do however try to avoid assuming that people know too much about 
operator precedence though, but tend to balance this against how many 
nested brackets are required to be explicit.  So I would write:

(i / 3) * 2

even though (in Java at least) the brackets could be omitted without 
changing the effect of the statement.  (Expressions are evaluated left 
to right and division and multiplication have the same precedence 
level).

Anyway, I would be interested to hear from people who like different 
styles to these as to why their preference goes that way, even if it's 
a very informal "I find whitespace makes it harder to parse chunks of 
code".

Regards,

Adrian Sutton.

----------------------------------------------
Intencha "tomorrow's technology today"
Ph: 38478913 0422236329
Suite 8/29 Oatland Crescent
Holland Park West 4121
Australia QLD
www.intencha.com





More information about the General mailing list