Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

patent10021

macrumors 68040
Original poster
Apr 23, 2004
3,552
834
I'm coming from Swift and this is my first time encountering this in Obj-C.
Code:
if (currentOperation == 0) result = currentNumber;

    else {
Normally we'd see this:

Code:
if (currentOperation == 0) {

} else {

}


What's going on and is there a name for this particular type of if statement? Of course it's like an if else statement but what's happening when we put result = currentNumber after the braces and before the first curly bracket? Basically the if statement seems to be missing its own curly braces but the else statement has its own.

Is it just saying if currentOperation = 0 then result = currentNumber else...???
 
Last edited:
Personally, I avoid this from a maintain ability POV.

This is good to mention. If you are writing the code for yourself it's probably not a big deal. Often times programmers get caught up in trying to use the least amount of lines as possible, but if someone else is working on your code or modifies it later that can cause problems because they often have to decipher what you were trying to do.
 
if someone else is working on your code or modifies it later that can cause problems because they often have to decipher what you were trying to do.

Someone else is relative.

On collaborative code projects I am often faced with getting upset about some code only to realize it was written by myself 6 months ago!

B
 
  • Like
Reactions: Mascots and chown33
Someone else is relative.

On collaborative code projects I am often faced with getting upset about some code only to realize it was written by myself 6 months ago!
I continue to be amazed at what a terse and unclear twit the past me was, as well as said twit being unaware of what a forgetful and inattentive clod the future me will become. One would hope that the current me might have long since learned this about the temporally altered versions of me, but no, it's a lesson we must keep learning.
 
  • Like
Reactions: firewood and balamw
That's right, that's a single line if statement.

I did use them in Objective-C apps, but never with an if-else statement. I would only use single-line if statements if there was no 'else' condition necessary.

I'm sort of sad (and happy at the same time) that they got rid of single line if statements in Swift. On one hand, if used wrong, they can be VERY confusing. But they can also make your code shorter if you know how to use them correctly.

Overall though code is much more readable in Swift most of the time.

The only thing I do miss a lot is nil checking. I liked being able to do this:

Code:
NSString *test = @"hello";

if (test) {
     //do something with test
}

Instead, in Swift, you actually have to compare it against nil.

Code:
let test = "hello";

if (test != nil) {
     //do something with test
}

It's not a huge deal but to this day I still find myself needing to add the != nil part after creating if statements.
 
That's right, that's a single line if statement.

I did use them in Objective-C apps, but never with an if-else statement. I would only use single-line if statements if there was no 'else' condition necessary.
This is exactly why it threw me off. There was else but the if didn't have braces.

But, what I'm more concerned about is how is this statement read? Is there an inferred 'and' in there?

if (currentOperation == 0) AND result = currentNumber;
[doublepost=1456474157][/doublepost]
Someone else is relative.

On collaborative code projects I am often faced with getting upset about some code only to realize it was written by myself 6 months ago!

B
This is very funny because just today I was reading about why we need private ivars as they relate to @Property.

A public member can be accessed from outside the class, which for practical considerations means "potentially anywhere". If something goes wrong with a public field, the culprit can be anywhere, and so in order to track down the bug, you may have to look at quite a lot of code.

A private member, by contrast, can only be accessed from inside the same class, so if something goes wrong with that, there is usually only one source file to look at.

In this context, "other programmers" include your future and past selves. Chances are you know now that you shouldn't do this thing X with your variable Y, but you're bound to have forgotten three months down the road when a customer urgently needs you to implement some feature, and you wonder why doing X breaks Y in obscure ways.

Our past selves piss off our future selves.
 
This is exactly why it threw me off. There was else but the if didn't have braces.

But, what I'm more concerned about is how is this statement read? Is there an inferred 'and' in there?

if (currentOperation == 0) AND result = currentNumber;
There are just invisible brackets there. That's how you should think of it. The (currentOperation == 0) is the only condition being checked. The result = currentNumber part is the code that gets executed if the condition is true.

So this:

Code:
if (currentOperation == 0) result = currentNumber;

Is exactly the same as this:

Code:
if (currentOperation == 0) {
    result = currentNumber;
}

Keep in mind though that this doesn't work in Swift, if my memory is correct. I believe Swift requires the { and } braces, which makes it more readable I think.
 
Isn't that the same as a ternary operator?

Code:
result = (currentOperation == 0) ? currentNumber : nil;

Some like it because it fits on one line.
 
There are just invisible brackets there.
Interesting you put it that way.

I tend to think of it in the exact opposite way for C derived languages. The curly braces are there to collect a block of code and make at look like a single statement.

You could achieve a similar effect by moving the contents of the code block code block to a function (with the braces) and replacing the bit in the if with a function call to the new function (without braces).

B
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.