Comments (7)
We have this rule in Ruby, in case you're looking for an internal precedent.
from guides.
Related: #49
from guides.
So guys, ehm. What's the take here?
(Sorry, doing some cleaning with older issues and PR's)
from guides.
@gfontenot @halogenandtoast @21x9 @theocalmes
from guides.
I agree with you for the most part, but I think there are a couple of situations that are unique to Objective-C where using multiple returns lead to much cleaner code. Custom init methods, for example, read better when you don't have to wrap the entire method in a conditional that checks to make sure self
was initialized properly:
- (instancetype)initWithFoo:(Foo *)foo
{
self = [super init];
if (!self) return nil;
_foo = foo;
// more setup
return self;
}
I've recently been having a debate about it with myself, but I think I'm liking the same pattern for lazily initialized objects:
- (Foo *)foo
{
if (_foo) return _foo;
_foo = [[Foo alloc] init];
// set up foo
return _foo;
}
The thing to note here is that the point of the early return isn't to return multiple objects. The point is to bail early if possible to avoid bloated conditionals. It should really only be used to catch exceptional cases, like when self
hasn't been initialized properly. For example, I wouldn't use multiple returns inside -tableView:cellForRowAtIndexPath:
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
switch (indexPath.row) {
case RowThatRequiresSpecialCell:
cell = [self helperMethodThatVendsACell];
break;
default:
cell = [self helperMethodForBasicCell];
break;
}
return cell;
}
from guides.
I personally prefer the conditionals inside an init, though I agree that there can be edge cases, where a simple return to kick out of the selector would be preferable, but that being said -> init is not one of them.
I agree about not doing so in cellForRow:. Btw, you'd wanna set cell to nil (since it's not a property). Best practices and etc. That being said, not sure if ARC is taking care of it, but I still tend to do so, old habit from high school C++.
from guides.
Under ARC, all pointer variables for Objective-C objects are set to nil
by default. So you don't need to do that anymore.
Thanks for your input though. We'll continue to discuss internally, but I don't think we're going to make this change to our style guide right now.
from guides.
Related Issues (20)
- Add accessibility resources to guides HOT 8
- YouTube Video Unavailable HOT 5
- Should we delete empty (seemingly duplicate) graphql guide? HOT 1
- Document SVG best practices HOT 5
- Update Backbone guide? HOT 2
- Update Ember guide? HOT 1
- Update how-to guide on feature-testing a Rails app’s Javascript? HOT 2
- Document Go Best Practices HOT 2
- Replace Ruby style guide with standard HOT 2
- Combine Best Practices and Style guides HOT 3
- Our testing guides are ruby/rails-centric HOT 2
- What layout should we suggest for test suite files in JavaScript / TypeScript projects?
- Audit guides for potential improvements
- Guides have inconsistent formatting
- Data guide should use description lists HOT 4
- Change master to main in thoughtbot/guides HOT 4
- Inquiring about JSONB column issues HOT 2
- rebase workflow link missing from Git guide HOT 1
- 404 link in relational-databases
- A11y template HOT 2
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 guides.