Return values of methods

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Return values of methods

Alexandre Bergel
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
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: Return values of methods

Mariano Martinez Peck


On Wed, Aug 19, 2009 at 5:29 PM, Alexandre Bergel <[hidden email]> 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
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project


_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: Return values of methods

Lukas Renggli
In reply to this post by Alexandre Bergel
> 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

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: Return values of methods

csrabak
In reply to this post by Alexandre Bergel

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 < [hidden email] > 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
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project< br />

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: Return values of methods

Alexandre Bergel
Thanks to all three for your answers.

Cheers,
Alexandre


On 19 Aug 2009, at 14:55, [hidden email] 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 < [hidden email] >  
> 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
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project<  
> br />
> _______________________________________________
> Pharo-project mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project

--
_,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:
Alexandre Bergel  http://www.bergel.eu
^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;.






_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: Return values of methods

hernanmd
In reply to this post by Alexandre Bergel
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 <[hidden email]>:

> 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
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
>

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project