Code Complete by Steve McConnell is an excellent book
and I agree with almost everything in it. Including 
the advice about functions.

But as I said earlier the concept of multiple return 
values in Python is similar to returning a struct in C.
The key thing is that the function has a single purpose 
so if returning multiple values or a struct the values 
within that group should be related to the single purpose

In other words don't write a function called

def getLengthOrContent(dataStream, flag)
    data = readData(dataAStream)      
    if flag:
       return len(data)
    else: 
       return data

Thats what I think is meant by bad practice in returning 
multiple values. The function returns two completely different 
things depending on some input flag.
 Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/





________________________________
From: Albert-Jan Roskam <fo...@yahoo.com>
To: tutor@python.org; Alan Gauld <alan.ga...@btinternet.com>
Sent: Monday, 30 November, 2009 9:58:21
Subject: Re: [Tutor] Python best practices


I'm currently reading the book "Code Complete" (I don't know the author name), 
which gives a lot of useful best practices. It's not specifically about one 
programming language. The author stresses that the majority of those practices 
are a matter of consensus/consistency and not a matter of religion. There is no 
one best way to do it.
 
Re: functions, the author recommends that they have one purpose and one purpose 
only, and that a function returns only one result.

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the face of ambiguity, refuse the temptation to guess.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Mon, 11/30/09, Alan Gauld <alan.ga...@btinternet.com> wrote:


>From: Alan Gauld <alan.ga...@btinternet.com>
>Subject: Re: [Tutor] Python best practices
>To: tutor@python.org
>Date: Monday, November 30, 2009, 1:57 AM
>
>
>"spir" <denis.s...@free.fr> wrote
>
>>> > - functions should return one value (im not 100% of this one)
>>> 
>>> I 100% disagree or with this one.
>> 
>> Could you explain this bit, Lie? I'm very interested.
>> I use multiple-value result myself, for it's so practicle in given cases.
>
>My take on this is that in Python its OK to return multiple values if it
>is as a tuple - because a tuple is really a single value. Its like returning
>a record in Pascal or a struct in C or a List in Lisp...
>
>> But it makes me uneasy; also have the impression (why?) it
>> reveals wrong design.
>
>Its better than....
>
>> a function both to have an effect (eg assignment outside the func scope) and
>> return a value.
>
>Side effects in functions are
> nearly always bad news and are always
>avoidable if you can return multiple values (or pass arguments by reference).
>
>> "Command-Query Separation Principle" (Eiffel) & Pascal "procedure vs 
>> function".
>
>You can have Command/Query separation without having side-effects.
>A Query is a procedure or function that doesn't change anything but just
>returns a result. A Command changes something, but it can be the thing
>passed to it - or if a method of a class the internal data of the class.
>Again a command can be a function or a procedure. Those are separate
>concepts. (I very rarely write procedures in real programs - there is nearly
>always something useful you can return - and in an object that's usually
>a minimum of self! (Returning self is the default idiom in Smalltalk - it
>allows chaining of methods)
>
>HTH,
>
>Alan G. 
>
>_______________________________________________
>Tutor maillist  - 
> Tutor@python.org
>To unsubscribe or change subscription options:
>http://mail.python.org/mailman/listinfo/tutor
> 
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to