[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi] A matter of style
From: |
Greg Chicares |
Subject: |
[lmi] A matter of style |
Date: |
Thu, 22 Feb 2007 13:00:10 +0000 |
User-agent: |
Thunderbird 1.5.0.4 (Windows/20060516) |
Consider:
if(IsVoid())
return;
Something like that occurs many times in 'database_view_editor.cpp',
for example. In the past, I would have asked [1] that we write
if(IsVoid())
{
return;
}
instead, but I'm starting to think that
if(IsVoid())
{return;}
might be better. Reason: by saving two lines, that alternative
may make function bodies shorter and therefore easier to read,
particularly if there are multiple early exits.
It's possible, though probably not very likely, that someone
will want to add a line to an early exit someday:
if(IsVoid())
{
message_box("Hey, this shouldn't be void!");
return;
}
If so, the block can easily be split into multiple lines:
that's not much extra work, and fairly unlikely anyway.
I'm still reluctant to endorse
if(IsVoid())
return;
without any braces, for the reason given in the footnote below.
This change:
- return;
+ {return;}
costs only two characters and zero lines. I'll make such changes
in new code now, and in old code as it's revised.
---------
[1] From the lmi coding standard [which I'll revise]:
5.6 All dependent clauses must be blocks: always use {} for the
body of a control structure, even if it is only one line
if(' ' == *p++)
{
handle_space();
}
because someday you or someone else might add another line. This
example
if(' ' == *p++)
handle_space();
number_of_spaces++; // added later: intended to depend on 'if'
really means
if(' ' == *p++)
{
handle_space();
}
number_of_spaces++;
contrary to the intention.
- [lmi] A matter of style,
Greg Chicares <=