Install BOOL immediately after checking

I work through a different codebase, and there are a few lines like this:

if (self.aBooleanProperty) { self.aBooleanProperty = YES; // Do some stuff } 

Does it make sense to install it on YES immediately after checking? Is something missing here?

+4
source share
4 answers
 if (self.aBooleanProperty) { self.aBooleanProperty = YES; // Do some stuff } 

In correctly written code, you don’t miss anything, and this setter line increases the paid lines of code with one non-op.

There are two reasons why this can be done for erroneous reasons.

As @HotLicks said, there may be side effects for the setter that may be required to run. But they had to be launched on the set, if the developer had no mistaken idea of ​​installing ivar directly everywhere, and then use the above to combine the cost of installation in one place. But it will be a remarkably fragile and stupid thing.

Another reason is that traditionally Objective-C BOOL is an illustrious char . Only it is not so glorified. Thus, comparing BOOL with YES really dangerous because YES has an explicit meaning.

 BOOL mmmmmK = 2; // this is valid if (mmmmmK == YES) { /* this won't execute */ } 

As with climbing, and something starts to fall, you do not shout “bottle”, “shoes”, “pebbles” or “prosthetic limb”, but you always shout ROCK .

So, perhaps the developer was thinking about normalizing the affirmative with an explicit YES. Again, it is highly doubtful and, even if this is so, then this should raise suspicion regarding the quality of the rest of the code base.

Uch.

+7
source

It's hard to say without having more code. I think everyone will agree that the code seems to be wrong, but what we see is a property of obj-c - the previous programmer could have done some kind of “smart” thing, for example. it is possible that the recipient of aBooleanProperty sets itself to NO when you call it.

Before changing the code, check the getters. It reminds me of schrödinbug

+2
source

I think that the person wrote YES incorrectly, instead he should have written NO, because he is already YES when he checked the condition. Or else there is no point on this line.

+1
source

if

  self.aBooleanProperty = YES; 

included in braces, it is not needed, and if it is

  self.aBooleanProperty = NO; 

it’s logical

0
source

Source: https://habr.com/ru/post/1479344/


All Articles