Git Product home page Git Product logo

Comments (7)

mike-burns avatar mike-burns commented on August 18, 2024

We have this rule in Ruby, in case you're looking for an internal precedent.

from guides.

calebhearth avatar calebhearth commented on August 18, 2024

Related: #49

from guides.

seivan avatar seivan commented on August 18, 2024

So guys, ehm. What's the take here?
(Sorry, doing some cleaning with older issues and PR's)

from guides.

calebhearth avatar calebhearth commented on August 18, 2024

@gfontenot @halogenandtoast @21x9 @theocalmes

from guides.

gfontenot avatar gfontenot commented on August 18, 2024

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.

seivan avatar seivan commented on August 18, 2024

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.

gfontenot avatar gfontenot commented on August 18, 2024

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)

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.