4

I have the following code:

- (IBAction)buttonSectionPressed:(id)sender {

    if ([self.switchReloadOnlyDontToggleVissibility isOn]) {
        [self updateCells:self.section2Cells];
    } else {
        BOOL hide = ([sender tag] == 0);
        [self cells:self.section2Cells setHidden:hide];
    }

    [self reloadDataAnimated:([self.switchAnimated isOn])];
}

I have a question with

BOOL hide = ([sender tag] == 0);

Is it checking to see if (sender.tag == 0) then assign it to hide? So, (if sender.tag != 0), hide does not exist?

user1107173
  • 10,334
  • 16
  • 72
  • 117

1 Answers1

8

This expression works as follows:

  • Evaluates [sender tag]
  • Compares the result to zero
  • If the result is zero, hide is set to YES; otherwise, it is set to NO.

This could also be done with the equivalent expression that uses property syntax:

BOOL hide = (sender.tag == 0);

Finally, you can drop the hide variable altogether:

[self cells:self.section2Cells setHidden:(sender.tag == 0)];
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 4
    Or another way that I prefer is *BOOL hide= !sender.tag;* – Ramy Al Zuhouri Apr 30 '13 at 22:15
  • 3
    @RamyAlZuhouri This would be the best option if `sender.tag` were a boolean or had a yes/no syntax. However, since `sender.tag` is an `int` with a numeric meaning, comparing it to zero explicitly improves readability. – Sergey Kalinichenko Apr 30 '13 at 22:17
  • 1
    It's some very crappy code because it is not immediately clear and obvious what is happening. You can see this kind of thing in C a lot (and of course in other languages). You should rewrite it to be a proper `if` statement. `BOOL hide; if ([sender tag] == 0) { hide = YES; } else { hide = NO; }` You could use the ternary operator, but that is just as confusing to read. – uchuugaka May 01 '13 at 01:27
  • @ramy-al-zuhouri Your approach is valid, but introduces the side-effect of the long standing weakness of BOOL, allowing non-zero to be YES. It is much more reliable and less error prone to explicitly set BOOL to YES or NO. – uchuugaka May 01 '13 at 01:31
  • 4
    @uchuugaka Assigning a boolean expression to a boolean variable is perfectly natural in C, once you get used to it. Insisting on writing an extra `if` statement or a ternary operator where it's not necessary is similar to insisting on using `a = a + 1` in place of `a++` "for readability". – Sergey Kalinichenko May 01 '13 at 01:38
  • Hope this isn't flamebait. You are correct, it is not necessary, this is true, but IMHO, poor coding style and one of the weaknesses of C. Code needs to be maintained and readable. I would lean toward a few more keystrokes when there is no big change in overhead and it means somebody can comprehend the meaning of the code and side-effects better. The caveat about ensuring a BOOL is actually 1 or 0 is also a useful one that trips up a lot of people new to Objective-C. – uchuugaka May 01 '13 at 02:12
  • 1
    @uchuugaka I've heard this position many times, and I respect it. However, I know for sure that (1) your position is neither wrong nor right, and (2) with time, people tend to abandon it in favor of more elegant, readable code like the above. It's certainly not worth arguing about, let alone downvoting a perfectly good answer. – Sergey Kalinichenko May 01 '13 at 02:34
  • @dasblinkenlight Well, let's agree to disagree, but I will leave it downvoted due to position on the issue of code clarity. I don't see things that make people confused and generate frequently asked questions as elegant. Technical correctness != clarity of intent. – uchuugaka May 01 '13 at 02:50
  • 1
    @uchuugaka If the code isn't clear for you, why don't you just cast the result to a boolean type? I don't know why there are many programmers that tend to consider booleans an unknown, esoteric data type. If it was an *int* you weren't writing: if(a==1) b=1; else if(a==2) b=2; // and so on ... . Then why would you do it with a boolean? Isn't a plain assignment enough? – Ramy Al Zuhouri May 01 '13 at 18:49
  • @RamyAlZuhouri I think you are missing my point. It is clear to me, but it is a stumbling block for a lot of folks, and personally I prefer explicit clarity. boolean and BOOL are not the same. BOOL is a signed char type. This is surprising to a lot of people who come to Cocoa from other languages and may think a BOOL to be what it *seems* to be. If it weren't an issue, we wouldn't see the popularity of it... http://stackoverflow.com/questions/541289/objective-c-bool-vs-bool http://blog.bignerdranch.com/564-bools-sharp-corners/ http://nshipster.com/bool/ – uchuugaka May 02 '13 at 04:48
  • 1
    Isn't *BOOL* a boolean type? I am typing it in English, not in Objective-C. – Ramy Al Zuhouri May 02 '13 at 10:30