What's the name for this kind of Obj-C if statement?

Discussion in 'iOS Programming' started by patent10021, Feb 24, 2016.

  1. patent10021, Feb 24, 2016
    Last edited: Feb 26, 2016

    patent10021 macrumors 68020

    patent10021

    Joined:
    Apr 23, 2004
    #1
    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...???
     
  2. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    It's sometimes referred to as a 'single-line' if statement.
     
  4. teagls macrumors regular

    Joined:
    May 16, 2013
    #4
    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.
     
  5. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #5
    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
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    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.
     
  7. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #7
    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.
     
  8. patent10021 thread starter macrumors 68020

    patent10021

    Joined:
    Apr 23, 2004
    #8
    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;
    --- Post Merged, Feb 26, 2016 ---
    This is very funny because just today I was reading about why we need private ivars as they relate to @Property.

    Our past selves piss off our future selves.
     
  9. AxoNeuron macrumors 65816

    AxoNeuron

    Joined:
    Apr 22, 2012
    Location:
    The Left Coast
    #9
    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.
     
  10. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #10
    Isn't that the same as a ternary operator?

    Code:
    result = (currentOperation == 0) ? currentNumber : nil;
    Some like it because it fits on one line.
     
  11. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #11
    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
     
  12. patent10021 thread starter macrumors 68020

    patent10021

    Joined:
    Apr 23, 2004

Share This Page