2

Looking to improve my IF statement, and I want to keep my code looking pretty

This is what I am currently doing, is it readable, any room for improvement?

SomeObject o = LoadSomeObject();


if( null == o
    ||
    null == o.ID || null == o.Title
    ||
    0 == o.ID.Length || 0 == o.Title.Length
 )

I don't have anyone to ask around. That's why I came here in first place. Please don't close my question :(

Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • "null.Title"? you're looking at the Title member of the null object? Shome mishtake, Shirley? – The Archetypal Paul Nov 26 '08 at 20:30
  • This is completely subjective and has been asked (and closed) before. – EBGreen Nov 26 '08 at 20:31
  • @EBGreen - I think it was asked in a reasonable way (genuinely seeking advise on improvement) and there have been a number of different answers yet I don't think any were argumentative. – Draemon Nov 26 '08 at 20:34
  • I think this is the question that's quite similar if it helps. http://stackoverflow.com/questions/217710/formatting-an-if-statement-for-readability – brabster Nov 26 '08 at 20:41
  • I don't think it is argumentative, but it is hgihly subjective and there is no way to close it as just subjective. – EBGreen Nov 26 '08 at 21:08
  • http://stackoverflow.com/questions/11099/what-is-the-prefered-style-for-single-decision-and-action-statements – EBGreen Nov 26 '08 at 21:11
  • http://stackoverflow.com/questions/97506/formatting-of-if-statements – EBGreen Nov 26 '08 at 21:14
  • http://stackoverflow.com/questions/254864/would-it-be-bad-form-to-put-braces-on-the-same-line-as-the-statement-for-single – EBGreen Nov 26 '08 at 21:15
  • http://stackoverflow.com/questions/184432/whats-your-preferred-indent-style-closed – EBGreen Nov 26 '08 at 21:16
  • http://stackoverflow.com/questions/108522/indenting-styles – EBGreen Nov 26 '08 at 21:16
  • http://stackoverflow.com/questions/159366/is-there-a-best-coding-style-for-identations-same-line-next-line – EBGreen Nov 26 '08 at 21:17
  • All in less that 5 minutes of searching. The question is subjective. It may not be a literal duplicate but it is definitely a spiritual duplicate. – EBGreen Nov 26 '08 at 21:18
  • And as for not closing it because you don't have people to talk to, there are plenty of discussions to read. – EBGreen Nov 26 '08 at 21:19
  • @EBGreen - I didn't realise that. I think that should be changed (I'll poddle over to uservoice). I'm fine with it being closed as a dupe, and it clearly is subjective. But then I'm never sure why some subjective Q's get closed and others don't. – Draemon Nov 26 '08 at 21:31
  • It is a subjective process. :) – EBGreen Nov 26 '08 at 21:34
  • And while you are there, the wording to close for duplication is "Exact Duplicate" which this isn't altough as I said it really is a dupe imo. – EBGreen Nov 26 '08 at 21:35

12 Answers12

13

I always try and avoid complex boolean expressions for the sake of the next guy, but if I had to write an expression that didn't easily go on one line I would format it as follows:

if (value1 == value2 ||
    value3 == value4 ||
    value5 == value6 ||
    value7 == value8) {

  executeMyCode();
}
brabster
  • 42,504
  • 27
  • 146
  • 186
  • The operators won't line up so nicely if the variable names aren't the same length. – Albert Jan 21 '09 at 14:53
  • Is not that important that the operators line up. The most important is to have a single condition per line and leave the operators at the end of line for better readability. – Gabriel Dec 21 '22 at 15:27
9

Your verbosity is leading to a less readable code, I think the following format is best:

if ( null == o || null == o.ID || null.Title || 0 == o.ID.Length || 0 == o.Title.Length )
{
  // do stuff
}

We all have high resolution/widescreen displays for a reason, there's no reason to lock your code at some horribly short syntax. Also, I would simply create a function named IsIDEmpty so that the code could look like

if ( IsIDEmpty(o) )
{
  // do stuff
}

to keep the code simpler & cleaner. The function would perform the actual checks and return a boolean. I'm sure this is something you might have re-use for anyways, plus it serves as a simple way for code to be more self documenting/commenting.

TravisO
  • 9,406
  • 4
  • 36
  • 44
  • Perhaps, but there will eventually be a line that isn't remotely sensible to put on one line. – Draemon Nov 26 '08 at 20:32
  • I really wouldn't want to be the one partially sighted guy in your office! – brabster Nov 26 '08 at 20:35
  • 1
    "there's no reason to lock your code at some horribly short syntax". And of course, on my screen, Stackoverflow presents it with the last few characters right hand side not displayed and a scroll bar! – The Archetypal Paul Nov 26 '08 at 20:36
  • I'm not too keen on having everything in one line, but do agree putting complex logic in a separate function makes it a lot more readable. – Robo Nov 26 '08 at 21:20
  • 1
    @Paul - not just your screen. SO's column is fixed at a certain number of ems wide. I assume this is because Jeff Atwood hates people who say, "well, I code using a decent editor on a decent monitor, and the few remaining troglodytes who don't deserve to die painful deaths". Or words to that effect. – Steve Jessop Nov 27 '08 at 00:30
  • Even with a nice high resolution monitor, the extra information panes on an IDE can effectively limit the editor to 80-90 columns (They can be turned off but I find the information is worth the screen real estate). – Albert Nov 30 '08 at 16:31
  • People have a hard time tracking lines of text horizontally over very great a distance. That's why newspapers and magazines break their content into columns, and why websites like this one do, too. If you use very long lines, your eye may skip over important details without your knowledge. – Adam Bellaire Jan 15 '09 at 15:33
  • Java coding standards say to limit line length to 100 characters. https://source.android.com/source/code-style.html#limit-line-length – Edward Falk Oct 29 '15 at 20:17
8

For the simplest formatting of what you have, I would go with one per line.

if(null == o
  || null == o.ID
  || null == o.Title
  || 0 == o.ID.Length
  || 0 == o.Title.Length)

Even better would be if you could refactor the condition such that it fits on one line. I find that a large number of || or && is usually difficult to read. Perhaps you can refactor it out into a function and be left with:

if(myFunction(...))
Albert
  • 536
  • 3
  • 7
  • 16
  • 2
    +1 on using the operators in the beginning of the continued line. This makes it easy and fast for a programmer to notify that the line is a continuation of the previous and not the first line of code inside the if – David Rodríguez - dribeas Nov 26 '08 at 21:33
4

My rule of thumb: Avoid any format that a semi-smart auto-formatter can't reproduce.

Having a defined set of formats and a automated tool/template/configuration that actually produces code in that format is a big plus, in my opinion.

And if your code is still unreadable after it has been auto-formatted, then chances are that you need to refactor anyway.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
3

I would either put it all on one line if it fits (which this one clearly doesn't). With this, I would put the || consistently at the start or end of line:

if( null == o ||
    null == o.ID ||
    null == o.Title ||
    0 == o.ID.Length ||
    0 == o.Title.Length
)

or

if( null == o
    || null == o.ID
    || null == o.Title
    || 0 == o.ID.Length
    || 0 == o.Title.Length
)

You could have >1 condition on a line, the positioning of || is more important I think.

I'm ignoring the fact that null.Title doesn't seem to make much sense

Draemon
  • 33,955
  • 16
  • 77
  • 104
2

I find it quite distracting, to be honest. Mostly because the '||' start making funny patters.

I much rather prefer something like

if ( o == null || o.ID == null || null.Title ||
     o.ID.Length == 0 || o.Title.Length )

or even better, keep it in a single line if possible.

Ismael C
  • 111
  • 2
  • 6
2

I think it's pretty unreadable.

The affection of putting the constant first always seems a bit odd to me - most compilers can be persuaded to warn if they find an assignment in a conditional.

Then you're testing for null for two different things, then for zero length for two different things - but the important thing is not the length check, but the member you're checking. So I'd write it as

if (o == null       ||
    o.ID == null    || o.ID.length == 0 ||
    o.Title == null || o.Title.Length == 0) 
The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134
2

Rather than making up a standard for just this one question - I would suggest adopting an existing coding standard for whatever language you are using.

For example:

GNU Coding Standards
http://www.gnu.org/prep/standards/

Code Conventions for the Java Programming Language
http://java.sun.com/docs/codeconv/

.NET Framework General Reference Design Guidelines for Class Library Developers
http://msdn.microsoft.com/en-us/library/czefa0ke.aspx

Kelvin Meeks
  • 217
  • 2
  • 14
1

Generally I'm with TravisO on this, but if there are so many conditions in your if() statement that it just gets crazy long, consider putting in its own little function instead:

bool wereTheConditionsMet()
{
  if( NULL == 0 )
    return true;
  if( NULL == o.ID )
    return true;
  :  :   // and so on until you exhaust all the affirmatives
  return false;
}

if ( wereTheConditionsMet() )
{
  // do stuff
}

It is a lot easier to convey the intent of a well-named predicate function than an endless string of ||s and &&s.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
0

It is not readable. This is how I do Really Long Ifs(or those I have to twiddle a lot).

if(
  o == null  ||
  o.ID == null || 
  o.Title == null ||
  o.ID.Length == 0 || 
  o.Title.Length == 0
 )

For yours, I would do a single line.

if(o == null  || o.ID == null || o.Title == null || o.ID.Length == 0 ||  o.Title.Length == 0)

Or, if you are using C++, I'd do something like this:

if(!o)
{}
if(! (o.ID && o.Title && o.Length))
{}

...since it separates creation from correctness.

However, caveat emptor, I've been accused of bloated LOC due to my fondness for newlines.

Paul Nathan
  • 39,638
  • 28
  • 112
  • 212
0
  1. Use an automated code formatter, and configure it appropriately.
  2. Write a method like isPresent(String) that checks the String argument for not null and for not empty (zero length).
  3. Rewrite the original conditional to use the new isPresent(String) method, probably all on one line.
Rob Williams
  • 7,919
  • 1
  • 35
  • 42
-1

I usually do something like:

if(x < 0 || x >= width
|| y < 0 || y >= height)
{
    /* Coordinate out of range ... */
}

The first y and x line up in a monospace font, which is nice, and I'm not confused by half-indentions.

This method works best when doing similar comparisons. Otherwise, I usually split up my if's.

strager
  • 88,763
  • 26
  • 134
  • 176