[Pharo-project] Return values of methods
Dear List, Consider the following method: -=-=-=-=-=-=-=-=-=-=-=-= AClass>>aMethod ^ self compute ifTrue: [ #aSelector ] ifFalse: [ false ] -=-=-=-=-=-=-=-=-=-=-=-= I read somewhere that having a method that may return different type of values is not of a good style. I first through it was in Kent's book, but I cannot find it. Is there a book that talk about this coding style? Cheers, Alexandre -- _,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;: Alexandre Bergel http://www.bergel.eu ^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;.
On Wed, Aug 19, 2009 at 5:29 PM, Alexandre Bergel <alexandre@bergel.eu>wrote:
Dear List,
Consider the following method: -=-=-=-=-=-=-=-=-=-=-=-= AClass>>aMethod ^ self compute ifTrue: [ #aSelector ] ifFalse: [ false ] -=-=-=-=-=-=-=-=-=-=-=-= I read somewhere that having a method that may return different type of values is not of a good style.
It would be worst if they are not only different type but also not polymorphic
I first through it was in Kent's book, but I cannot find it. Is there a book that talk about this coding style?
Cheers, Alexandre -- _,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;: Alexandre Bergel http://www.bergel.eu ^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;.
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
I read somewhere that having a method that may return different type of values is not of a good style. I first through it was in Kent's book, but I cannot find it. Is there a book that talk about this coding style?
There is a SmallLint rules that checks for this, see the class RBReturnsBooleanAndOtherRule. As a rational it states: "Checks for methods that return a boolean value (true or false) and return some other value such as (nil or self). If the method is suppose to return a boolean, then this signifies that there is one path through the method that might return a non-boolean. If the method doesn't need to return a boolean, you should probably rewrite it to return some non-boolean value since other programmers reading your method might assume that it returns a boolean." Cheers, Lukas -- Lukas Renggli http://www.lukas-renggli.ch
Thanks to all three for your answers. Cheers, Alexandre On 19 Aug 2009, at 14:55, csrabak@bol.com.br wrote:
Alexandre,
There are lots of more less similar guidelines, but I think is does not apply to this case as I see it more or less like when you ask for removing of an item of a collection.
In your particular example a point could be taken on whether is better to return false or nil. To me nil seems more like to an object (a Symbol in your example) than using false which I would expect of a isSomething method.
HTH
Em 19/08/2009 15:29, Alexandre Bergel < alexandre@bergel.eu > escreveu:
Dear List,
Consider the following method: -=-=-=-=-=-=-=-=-=-=-=-= AClass>>aMethod ^ self compute ifTrue: [ #aSelector ] ifFalse: [ false ] -=-=-=-=-=-=-=-=-=-=-=-= I read somewhere that having a method that may return different type of values is not of a good style. I first through it was in Kent's book, but I cannot find it. Is there a book that talk about this coding style?
Cheers, Alexandre -- _,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;: Alexandre Bergel http://www.bergel.eu ^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;.
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project< br /> _______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
-- _,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;: Alexandre Bergel http://www.bergel.eu ^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;.
Arguments and Results (1997) by James Noble (http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.50.7565) 2009/8/19 Alexandre Bergel <alexandre@bergel.eu>:
Dear List,
Consider the following method: -=-=-=-=-=-=-=-=-=-=-=-= AClass>>aMethod     ^ self compute         ifTrue: [ #aSelector ]         ifFalse: [ false ] -=-=-=-=-=-=-=-=-=-=-=-= I read somewhere that having a method that may return different type of values is not of a good style. I first through it was in Kent's book, but I cannot find it. Is there a book that talk about this coding style?
Cheers, Alexandre -- _,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;: Alexandre Bergel  http://www.bergel.eu ^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;.
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
participants (5)
-
Alexandre Bergel -
csrabak@bol.com.br -
Hernán Morales Durand -
Lukas Renggli -
Mariano Martinez Peck