Comments (20)
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.
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.
@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.
@hollance Of the three options above, the second one.
from objective-c-style-guide.
-
we should these days return instancetype instead of id right?
-
I always use this one:
- (instancetype) init
{
self = [super init];
if (self) {
. . .
}
return self;
}
from objective-c-style-guide.
Yup. Agree with @icanzilb, we should always be using instancetype
from objective-c-style-guide.
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.
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.
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.
+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.
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.
I am not a fan of multiple returns. I almost always do like @ColinEberhardt.
from objective-c-style-guide.
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.
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.
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.
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.
(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.
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.
@ndubbs Yeah adding a section for that somewhere sounds great.
from objective-c-style-guide.
@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)
- Init section contradicts Golden path section HOT 2
- Can we lint to enforce some of this style guide? HOT 6
- Symbol prefix: 2 vs. 3 characters HOT 26
- English should be used, really? HOT 7
- What's the point on using properties instead of instance variables? HOT 8
- Direct access to properties. HOT 5
- License for this guide? HOT 1
- Not linking to Official Apple Style Guide HOT 1
- RWT prefix typo in constant definition HOT 1
- What is the recommended line width for Objective-C?
- Use style guide principles in examples
- arrayCount should be named something else HOT 4
- Contradiction between Init Methods and Golden Path
- Error handling
- I want make new branch for Korean ver.
- Google style guide URL is broken HOT 2
- Should self keyword should be used in init methods ?
- How do I indent something like this??? HOT 3
- Xcode behavior regarding indenting block
- 关于枚举(Enumerated Types)的想法
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from objective-c-style-guide.