Tag Archives: Style

Regarding Style

RibbitAs I’ve gotten older my coding style has evolved. I’m a fairly verbose coder which can drive some people absolutely crazy but it works for me. It makes code more readable.

Case in point. Something I get questioned about all the time is why do I write false if conditions like this.

if (nil == thing) {
    // Create a new thing
}

Instead of doing…

if (!thing) {
    // Create a new thing
}

Well, that’s easy. My eyes can pick it up instantly. One other thing about that syntax. It’s a hangover from over 20 years of writing C and C++ code. The compiler will bark if you try to a value to zero. In that way it served as a way to make sure you didn’t accidentally make a mistake that could take a while to find. Let the compiler help you where it can.

I really like it more for readability. Opinions vary.

A change in coding style

I’m a verbose guy when it comes to code. The compiler doesn’t care and it’s easier for me to read later on down the road. I’ve changed my style over the past few months, it started at Pelco because I was confusing folks with a particular syntax. I must admit, it even confused me on occasion.

When checking pointers I used to do this.

if (NULL != ptr)

That was confusing. I’ve since switched my tune back to a tried and true method of removing the NULL !=, like this

if (ptr)

Much easier for most to understand. I do, however, still check for explicit FALSE and NULL as the case may be, because I believe it to be a bit more clear. Here’s what that looks like.

if (NULL == ptr)

or

if (FALSE == value)

There is a statement some don’t like, that I love, and probably won’t switch away from. The ternary operator.

something = (value) ? value1 : value2;

My brain just likes it.