help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-smalltalk] Re: Question regarding DatagramSocket...


From: Paolo Bonzini
Subject: Re: [Help-smalltalk] Re: Question regarding DatagramSocket...
Date: Sat, 12 Jun 2010 10:30:15 +0200

>> Just name it "size" please.  There is no <comment: ''> pragma in
>> methods.  Finally, please put a single ^ outside the if blocks.
>
> So no return from within the if? What is the reasoning? What would be
> preferred, assign to a local variable and then return, or put the return
> before the object ifNil check?

Answering both question with a citation from "Smalltalk Best Practice Patterns":

Assignment and return are often found in both branches of a
conditional. Look for opportunities to factor both to the outside of
the conditional. Here is an example of a return on both branches of a
conditional:

    cost
        self isInitialized
            ifTrue: [^self calculateCost]
            ifFalse: [^0]

If I write code like this, I don’t mean “here are two alternative
paths of execution,”, I mean, “here are two alternative values to be
returned.” Thus, a Conditional Expression expresses my intent more
clearly:

    cost
        ^self isInitialized
            ifTrue: [self calculateCost]
            ifFalse: [0]

Paolo



reply via email to

[Prev in Thread] Current Thread [Next in Thread]