GithubHelp home page GithubHelp logo

Comments (20)

 avatar commented on June 26, 2024

I use Apple's method exclusively

Sent from my iPhone

On 8 Nov 2013, at 07:59, Matthijs Hollemans [email protected] wrote:

How does everyone write their init methods?

I use:

  • (id)init
    {
    if ((self = [super init])) {
    . . .
    }
    return self;
    }
    The variations on this are endless. Apple seems to prefer this now:

  • (id)init
    {
    self = [super init];
    if (self) {
    . . .
    }
    return self;
    }
    But I also see people do this:

  • (id)init
    {
    self = [super init];
    if (self == nil) return nil;

    return self;
    }
    I like the first one because it just looks aesthetically pleasing to me.


Reply to this email directly or view it on GitHub.

from objective-c-style-guide.

ColinEberhardt avatar ColinEberhardt commented on June 26, 2024

I use this method for brevity:

- (id)init
{
  if (self = [super init]) {
    . . .
  }
  return self;
}

The need for extra parens ((...)) will certainly confuse, so if we use this form it really needs to be explained.

from objective-c-style-guide.

hollance avatar hollance commented on June 26, 2024

@micpringle Which Apple's method? The old one or the new one?

@ColinEberhardt You don't use extra parens? Doesn't that trigger a compiler warning?

from objective-c-style-guide.

 avatar commented on June 26, 2024

@hollance Of the three options above, the second one.

from objective-c-style-guide.

icanzilb avatar icanzilb commented on June 26, 2024
  1. we should these days return instancetype instead of id right?

  2. I always use this one:

- (instancetype) init
{
  self = [super init];
  if (self) {
    . . .
  }
  return self;
}

from objective-c-style-guide.

 avatar commented on June 26, 2024

Yup. Agree with @icanzilb, we should always be using instancetype

from objective-c-style-guide.

funkyboy avatar funkyboy commented on June 26, 2024

Good point.
+1.
@hollance will disagree :)

Cesare Rocchi
studiomagnolia.com

On Nov 8, 2013, at 10:42 AM, Mic Pringle [email protected] wrote:

Yup. Agree with @icanzilb, we should always be using instancetype


Reply to this email directly or view it on GitHub.

from objective-c-style-guide.

hollance avatar hollance commented on June 26, 2024

I'm OK with instancetype. I only use it on convenience constructors but it doesn't do any harm to put them on init methods as well. ;-)

from objective-c-style-guide.

gregheo avatar gregheo commented on June 26, 2024

I use the last one:

- (instancetype)init {
  self = [super init];
  if (!self) return nil;

  // other stuff here

  return self;
}

Rationale: it keeps with the "happy path on the left margin" guideline. I hate having important init code inside an if.

from objective-c-style-guide.

funkyboy avatar funkyboy commented on June 26, 2024

+1 on this.

On Nov 8, 2013, at 5:10 PM, Greg Heo [email protected] wrote:

I do the first one:

  • (instancetype)init {
    self = [super init];
    if (!self) return nil;

    // other stuff here

    return self;
    }
    Rationale: it keeps with the "happy path on the left margin" guideline. I hate having important init code inside an if.


Reply to this email directly or view it on GitHub.

from objective-c-style-guide.

ricardo-rendoncepeda avatar ricardo-rendoncepeda commented on June 26, 2024

I tend to use @ColinEberhardt 's method, but I am totally on board with @gregheo 's rationale and will be happy to stick to that

from objective-c-style-guide.

moayes avatar moayes commented on June 26, 2024

I am not a fan of multiple returns. I almost always do like @ColinEberhardt.

from objective-c-style-guide.

hollance avatar hollance commented on June 26, 2024

I am not a fan of the "happy path on the left margin" guideline, unless there are more than a couple of if's. Sometimes nested if's are clearer, sometimes multiple returns are clearer.

from objective-c-style-guide.

 avatar commented on June 26, 2024

I had never even heard of this "happy path" thing until this guide. I can tell you from experience debugging large systems, it usually works out better to have a single return statement, but I do occasionally have multiple returns if they are obvious and help the over all readability. (Or, quite frankly, if I feel like it at the moment.)

As for the init, I prefer the style where you assign self inside the condition, but that's just because it's easy to type and if statements don't bother me like they seem to bother my buddy @gregheo. I could see doing it the way apple does in their auto generated files, but definitely bit like Greg said. That's just insane. ;)

Sent from my iPhone

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

I am not a fan of the "happy path on the left margin" guideline, unless there are more than a couple of if's. Sometimes nested if's are clearer, sometimes multiple returns are clearer.


Reply to this email directly or view it on GitHub.

from objective-c-style-guide.

gregheo avatar gregheo commented on June 26, 2024

I stretched the truth a bit, @elephantronic – I do indeed use a super single line assignment like if (!(self = [super init])) return nil; but for the site, I figured it's better to be clearer and split up the assignment and the check.

I'm not a strict "happy pather" but I like it as a general rule to keep the common/expected code path non-indented. I also have no problem with multiple returns, especially for error conditions such as [super init] returning nil.

from objective-c-style-guide.

rwenderlich avatar rwenderlich commented on June 26, 2024

My vote is same as Matthijs:

- (id)init
{
  if ((self = [super init])) {
    . . .
  }
  return self;
}

Reasoning: that's what is used in the default Apple templates.

from objective-c-style-guide.

funkyboy avatar funkyboy commented on June 26, 2024

(I hope this does not start a flame on Apple's templates)

On Nov 10, 2013, at 5:16 PM, rwenderlich [email protected] wrote:

My vote is same as Matthijs:

  • (id)init
    {
    if ((self = [super init])) {
    . . .
    }
    return self;
    }
    Reasoning: that's what is used in the default Apple templates.


Reply to this email directly or view it on GitHub.

from objective-c-style-guide.

ndubbs avatar ndubbs commented on June 26, 2024

For this topic we should stick with Apple's template. This isn't necessarily covered in any detail. Should this be added as a new section?

from objective-c-style-guide.

rwenderlich avatar rwenderlich commented on June 26, 2024

@ndubbs Yeah adding a section for that somewhere sounds great.

from objective-c-style-guide.

mralexgray avatar mralexgray commented on June 26, 2024

@gregheo +1 re: if (self != [super init]) return nil;

or EVEN _BETTER_...

- (id) init { return self = super.init ? _aProp = @"aVal", self : nil; }

Relish in it.. don't flame it, lovers ✌️...

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.