String Length as Boolean Value in Object-C

String length is one of the most common condition that we often face. For me, I like to write it explicitly.

1
self.submitButton.enabled = [trimmedString length] > 0;

However, some people like to make it short and say this is the classic-c style.

1
self.submitButton.enabled = [trimmedString length];

I hate the above code as it is not explicit. We want to determine if the length of the string is more than zero, why don’t we write it that way? In addition, the second statement is harmful. When you do this in Objective-C, the value of the length will be modulo with 256 and the result is used to determine the boolean value. Thus, if the length is 256’s multiplier, the modulus gives 0, which result in a NO Bool value. This is not a grateful result.

Here are useful post of this issue.

Comments