coding-style

IF statement formatting best-practise, what's your style?


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 :(


Solution

  • 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.