GithubHelp home page GithubHelp logo

Comments (29)

icanzilb avatar icanzilb commented on July 24, 2024

I really prefer this myself, because it's just so clear to read:

NSString* str = [obj stringForblablabla];
if (str) {
  //do something with the string
}

from objective-c-style-guide.

funkyboy avatar funkyboy commented on July 24, 2024

For brevity I agree, but for learning and readability I prefer

if (str == nil)

On Fri, Nov 8, 2013 at 10:50 AM, Marin Todorov [email protected]:

I really prefer this myself, because it's just so clear to read:

NSString* str = [obj stringForblablabla];if (str) {
//do something with the string}


Reply to this email directly or view it on GitHubhttps://github.com//issues/22#issuecomment-28051637
.

Cesare Rocchi
http://studiomagnolia.com

from objective-c-style-guide.

moayes avatar moayes commented on July 24, 2024

Agree that it makes it easier to read but ...
it is a language feature, you don't have to compare against nil, nor you have to compare against 0.

from objective-c-style-guide.

pietrorea avatar pietrorea commented on July 24, 2024

I wouldn't write 'if (str == nil)' myself because it leaves me open to if (str = nil). But for someone who is new to Objective-C (or programming in general) this tiny bit of verbosity would be very helpful.

from objective-c-style-guide.

hollance avatar hollance commented on July 24, 2024

@moayes Yes, it's a language feature. A potentially confusing one.

Just because something is a language feature doesn't mean it should always be accepted. Some language features just aren't any good.

from objective-c-style-guide.

 avatar commented on July 24, 2024

I disagree. If(obj) is a standard way to check for non-nil values. Not showing that on the site isn't helping people learn anything, it is opening them up to not understanding any real code ever, because no one mentions nil in a check for nil.

I agree that checking ints should be explicit, but not objects.

Sent from my iPhone

On Nov 8, 2013, at 12:35 PM, Matthijs Hollemans [email protected] wrote:

@moayes Yes, it's a language feature. A potentially confusing one.

Just because something is a language feature doesn't mean it should always be accepted. Some language features just aren't any good.


Reply to this email directly or view it on GitHub.

from objective-c-style-guide.

moayes avatar moayes commented on July 24, 2024

Maybe for a beginner level tutorial it is better to say if (object != nil) but for an intermediate and above, I think it is better to say if (object).

from objective-c-style-guide.

icanzilb avatar icanzilb commented on July 24, 2024

@pietro - xcode produces a warning for if (str = nil)

from objective-c-style-guide.

icanzilb avatar icanzilb commented on July 24, 2024

@moayes I think that's unnecessary complication for the authors to change their style depending on the tutorial level ...

Did I say I love if (myObjec) { do stuff } ? It's the best

from objective-c-style-guide.

gregheo avatar gregheo commented on July 24, 2024

I don't like checking for nil explicitly. Next thing, you'll be saying if ((str == nil) == YES)!

Just kidding! Seriously though, I think checking for non-nil pointers (or non-NULL pointers going back to C) with if (!thing) is pretty idiomatic.

from objective-c-style-guide.

funkyboy avatar funkyboy commented on July 24, 2024

if (!str) is pretty clear
if(str) is not immediately intuitive.

I am fine either way, but I still think str == nil enhances readability.
To support my claim: Big Nerd Ranch teachers suggest the == nil, at least in the beginning.

I don't like checking for nil explicitly. Next thing, you'll be saying `if ((str == nil) == YES)'!

Just kidding! Seriously though, I think checking for non-nil pointers (or non-NULL pointers going back to C) with if (!thing) is pretty idiomatic.


Reply to this email directly or view it on GitHub.

from objective-c-style-guide.

 avatar commented on July 24, 2024

It doesn't need to be immediately intuitive the first time you see it. The COBOL language was very intuitive, but you wouldn't want to write anything in it today.

Adding == nil is simply something you should never expect to see. If our site is coding like that, anyone who knows how to code will assume we don't.

Sent from my iPhone

On Nov 8, 2013, at 2:20 PM, Cesare [email protected] wrote:

if (!str) is pretty clear
if(str) is not immediately intuitive.

I am fine either way, but I still think str == nil enhances readability.
To support my claim: Big Nerd Ranch teachers suggest the == nil, at least in the beginning.

I don't like checking for nil explicitly. Next thing, you'll be saying `if ((str == nil) == YES)'!

Just kidding! Seriously though, I think checking for non-nil pointers (or non-NULL pointers going back to C) with if (!thing) is pretty idiomatic.


Reply to this email directly or view it on GitHub.


Reply to this email directly or view it on GitHub.

from objective-c-style-guide.

icanzilb avatar icanzilb commented on July 24, 2024

To be honest, I do exactly the opposite. I always do:

if (str) {...};
if (str == nil) {...};

go figure ...

from objective-c-style-guide.

funkyboy avatar funkyboy commented on July 24, 2024

The point is that we are probably targeting people who do not know how to code in Obj-c or they are just getting started.

On Nov 8, 2013, at 8:25 PM, elephantronic [email protected] wrote:

It doesn't need to be immediately intuitive the first time you see it. The COBOL language was very intuitive, but you wouldn't want to write anything in it today.

Adding == nil is simply something you should never expect to see. If our site is coding like that, anyone who knows how to code will assume we don't.

Sent from my iPhone

On Nov 8, 2013, at 2:20 PM, Cesare [email protected] wrote:

if (!str) is pretty clear
if(str) is not immediately intuitive.

I am fine either way, but I still think str == nil enhances readability.
To support my claim: Big Nerd Ranch teachers suggest the == nil, at least in the beginning.

I don't like checking for nil explicitly. Next thing, you'll be saying `if ((str == nil) == YES)'!

Just kidding! Seriously though, I think checking for non-nil pointers (or non-NULL pointers going back to C) with if (!thing) is pretty idiomatic.


Reply to this email directly or view it on GitHub.


Reply to this email directly or view it on GitHub.

Reply to this email directly or view it on GitHub.

from objective-c-style-guide.

pietrorea avatar pietrorea commented on July 24, 2024

@elephantronic readers want to get through the tutorials as quickly as possible. if our readers have to do a double take of any of the code that ships with our tutorials and books, we failed them.

i would rather write code that is 100% intuitive and unambiguous and have people think i don't know how to code than the other way around.

from objective-c-style-guide.

 avatar commented on July 24, 2024

That's crazy talk. A double take means we've failed our readers? You guys all seem to think that our readers are in a semi-vegetative state or something. Give people some credit. You understand that code, right? Did you invent not being stupid? No? So maybe other people are capable of understanding this stuff, too.

if(obj) or if(!obj) IS the style we should be teaching because that's the style that people use in real life. We need to stop trying to invent our own style and teach the most commonly used practices, because people just may want to understand how to read code that isn't from this website, too.

Sent from my iPhone

On Nov 8, 2013, at 2:37 PM, Pietro Rea [email protected] wrote:

@elephantronic readers want to get through the tutorials as quickly as possible. if our readers have to do a double take of any of the code that ships with our tutorials and books, we failed them.

i would rather write code that is 100% intuitive and unambiguous and have people think i don't know how to code than the other way around.


Reply to this email directly or view it on GitHub.

from objective-c-style-guide.

hollance avatar hollance commented on July 24, 2024

Yes, if (obj) and if (!obj) are used in real projects. But so are if (obj != nil) and if (obj == nil). Personally I think the former are a defect in the language (one of the few things Java does better). But I must concede that its usage is idiomatic indeed.

But right now the guide says if (obj == nil) is bad, which I disagree with.

from objective-c-style-guide.

ndubbs avatar ndubbs commented on July 24, 2024

The thing to remember with this topic is the end result, what do we want the code to look like regardless of skill level. We have a broad range of readers from beginner to advanced. All the coding in our tutorials SHOULD be consistent and that is the reason for doing this style guide. It is the job of the tutorial writer to explain why the code is written in the manner it is.

@hollance We are going to change the word choices of good and bad. That should take care of the negative connotation that (obj == nil) is bad.

Everyone, let's have a vote on which way we prefer. Please reply with your choice. I think the reasons why have been covered. :)

from objective-c-style-guide.

pietrorea avatar pietrorea commented on July 24, 2024

(obj == nil)

from objective-c-style-guide.

funkyboy avatar funkyboy commented on July 24, 2024

(obj == nil)

Cesare Rocchi
studiomagnolia.com

On Nov 9, 2013, at 4:23 PM, Pietro Rea [email protected] wrote:

(obj == nil)


Reply to this email directly or view it on GitHub.

from objective-c-style-guide.

 avatar commented on July 24, 2024

if(obj)

Sent from my iPhone

On Nov 9, 2013, at 11:40 AM, Cesare [email protected] wrote:

(obj == nil)

Cesare Rocchi
studiomagnolia.com

On Nov 9, 2013, at 4:23 PM, Pietro Rea [email protected] wrote:

(obj == nil)


Reply to this email directly or view it on GitHub.

Reply to this email directly or view it on GitHub.

from objective-c-style-guide.

moayes avatar moayes commented on July 24, 2024

+1 if (obj)

from objective-c-style-guide.

icanzilb avatar icanzilb commented on July 24, 2024

yet again:

if (obj) ... 
if (obj == nil) ... 

this way you have only positive comparisons

from objective-c-style-guide.

hollance avatar hollance commented on July 24, 2024

Since you asked:

if (obj != nil)
if (obj == nil)

I don't think a vote on what everyone prefers is the same as finding the best way to teach programming, though. ;-)

from objective-c-style-guide.

mattjgalloway avatar mattjgalloway commented on July 24, 2024
if (obj)
if (!obj)

... for me. It's just cleaner, obvious and perfectly valid.

I do however use, and prefer this:

if (myArray.count > 0)

... because I think that it's better to be explicit about the fact you're checking that there are more than zero objects in that array.

from objective-c-style-guide.

rwenderlich avatar rwenderlich commented on July 24, 2024

My vote (but I'd be happy with whatever we decide):

if (obj)
if (!obj)

Because I think this is used more commonly in real life.

Perhaps this is another good one for the "suggested but optional" section ;]

from objective-c-style-guide.

gregheo avatar gregheo commented on July 24, 2024

+1 to checking bools and for nil or NULL: if (foo) and if (!foo)

But I agree with others that checking for numeric zero should be explicit: if (fooCount > 0)

from objective-c-style-guide.

ricardo-rendoncepeda avatar ricardo-rendoncepeda commented on July 24, 2024

+1 for

if(obj)
if(!obj)

I think it reads better for development tutorials when you are trying to make sense of new logic (if object, if not object - rather than using any double-negatives like "if object is not equal to nil")

from objective-c-style-guide.

ndubbs avatar ndubbs commented on July 24, 2024

The majority wins on this issue.

if (obj)
if (!obj)

from objective-c-style-guide.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.