Skip to content
This repository has been archived by the owner on May 9, 2023. It is now read-only.

What about the conditional to check if an integer is higher than 0? #23

Closed
nuthinking opened this issue Aug 3, 2013 · 8 comments
Closed

Comments

@nuthinking
Copy link

Would you use:
if(text.length){
// do something
}
or
if(text.length>0){
// do something
}
?

@paulbruneau
Copy link
Contributor

I use the former

@chrisladd
Copy link
Contributor

Speaking of clarity "former" and "latter" are the parts-of-speech equivalents of ternary operators. ;)

@mbbischoff
Copy link
Contributor

I think

if (text.length) {
    [array addObject:text];
}

is the more common style on the team. Anyone have a problem if we write this one in?

Edit: Edited code example

@murdocdv
Copy link
Contributor

murdocdv commented Aug 4, 2013

I think - length is a method, so unless I misunderstood the dot-syntax rules, should be:

If ([text length] > 0) {
[array addObject:text];
}

I prefer the > 0 formulation because its more clear you're testing an integer value, and not a nil check.

On Aug 4, 2013, at 11:55 AM, Matthew Bischoff notifications@github.com wrote:

I think

if(text.length){
[array addObject:text];
}
is the more common style on the team. Anyone have a problem if we write this one in?


Reply to this email directly or view it on GitHub.

@mbbischoff
Copy link
Contributor

You're right. Updated my post to make it a method, but I'm still proposing no > 0.

@segiddins
Copy link

I disagree about it not being a nil check. It also will return 0 if text is nil, so it's an easy way to check both the existence of text and make sure its contents aren't empty.

-Samuel E. Giddins

On Aug 4, 2013, at 12:02 PM, Matthew Bischoff notifications@github.com wrote:

You're right. Updated my post to make it a method, but I'm still proposing no > 0.


Reply to this email directly or view it on GitHub.

@JThramer
Copy link

JThramer commented Aug 5, 2013

if ([text length]) {
    [thing doSomething];
}

is perfectly valid code, and will excecute [thing doSomething] on any value 1 or greater.

@mbbischoff
Copy link
Contributor

I’d recommend closing this issue.

@LMBHDU LMBHDU mentioned this issue Mar 25, 2018
3 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants