Click to See Complete Forum and Search --> : Long 'if' statements
Gnu/Vince
03-29-2002, 09:12 PM
What's the best/cleanest way to cut a long if statement?
a)
my_function (parameters)
if long long long long long
long long then
blah
b)
my_function (parameters)
if long long long long long
long long then
blah
c)
my_function (parameters)
if long long long long long
long long then
blah
?
Strogian
03-29-2002, 10:14 PM
Hmm.. What language? In C, I believe that GNU suggests something like this:
if (long + long + long + long + long
+ long + long == 10)
{
blah;
}
(note the conditional is split before the + operator, not after)
Wow, I just reread your message, and I see you actually used 'blah' as your statement, too. :)
GNU actually suggests you indent your braces? Heh. (not that this has to do with the subject.)
Well, I hope the don't mind if, among the various freedoms I allow myself, I take the freedom to choose my own coding style.
As for how far in to indent additional lines of an if statement, I generally do as Strogian posted... that is, line the beginning of each line up with the beginning of the "content" on the first line. Whether the plus is on the same line or the next depends on the situation.
bwkaz
03-29-2002, 11:26 PM
Originally posted by kmj:
<STRONG>GNU actually suggests you indent your braces? Heh. (not that this has to do with the subject.)</STRONG>
GNU code is the only C/C++ code on the planet (at least that I have ever seen) that does it that way, too.
I've seen this:
if(foo && bar && baz &&
foo2 && bar2 && baz2) {
blah;
}
(that's what I use if I can at all help it), and I've seen this:
if(foo && bar)
{
blah;
}
I've even occasionally seen this:
if(foo && bar)
{
blah;
}
But GNU code is the only code I've seen that put the braces midway between the outer and inner indent:
if(foo && bar)
{
blah;
}
:eek:
Hey, whatever, I guess, right?
Danger Fan
03-30-2002, 12:05 PM
if you only need to match 1 condition in many, you can use case:
case <condition>
do something;
break;
case <2nd condition>
do something else;
break;
so on and so forth...
Whipping Boy
03-30-2002, 06:38 PM
I just keep it all on one line, regardless of how long it is.
Originally posted by Whipping Boy:
<STRONG>I just keep it all on one line, regardless of how long it is.</STRONG>
<flashback to maintaining other people's code>
/me shudders
</flashback>