ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-list
python-list
Re: Good code patterns in Python
by Kirk Job-Sluder other posts by this author
Jul 2 2003 6:48AM messages near this date
Re: Good code patterns in Python | Re: Good code patterns in Python
Will Stuyvesant <hwlgw@[...].com>  wrote:
>  If you know that your source code is going to be used
>  later by others, then I feel that code with the pattern:
>  
>     if some_condition:
>         some_name = some_value
>     else:
>         some_name = other_value
>  
>  is often a mistake.  Much better, safer, would be:
>  
>     some_name = some_value
>     if not some_condition:
>         some_name = other_value
>  
>  Why?  

My personal opinion is that it depends on context.  The first idiom
is more clear when you are dealing with some kind of a switch.  The 
second idiom works better if you have a default value that needs to be 
overridden in some cases.

>  Because those people reusing your code might decide to
>  change or adapt the "if" part of the first example.  Or
>  the "else" part for that matter.  And then they could end
>  up with "some_name" being undefined, crashing other code
>  maybe 1000's of lines away.  This could happen for example
>  because they use exceptions in the "if" part that jump out
>  of it, etc.

Of course, another alternative is to combine both approaches by setting
a default value:

#set default for some_value
some_value = []
if condition1:
    some_value = function1()
else:
    some_value = function2()

And then if you really want to be safe the functions using
some_value should do reality checks as well.
def function3(some_value=[]):
    if len(some_value):
        do something
    else:
        do something else.

I think that an argument for the readability of if-else is that because
the logic is made more clear, it can be more effectively replaced.


>  There is the small overhead of assigning something twice
>  to some_name in the safer example, so if performance is
>  *that* important then the first example is better, but I
>  feel there should be comments with warnings around it:
>  **CREATING A NEW OBJECT REFERENCE HERE!** Hmm, now that
>  makes code ugly :-)

The problem here is that ***creating a new object reference*** is not
only ugly, but its ambiguous as well.  (What kind of object, what is it 
used for?) If indeed some_name is an important variable in your program,
then provide a full description.

#some_value (string) is used to hold the widgit that will 
#be passed to a wadget and eventually passed
#to standard output.  

-- 
Kirk Job-Sluder
http://www.jobsluder.net/~kirk/

-- 
http://mail.python.org/mailman/listinfo/python-list
Thread:
Will Stuyvesant
Ben Finney
Erik Max Francis
Bernhard Herzog
holger krekel
Kirk Job-Sluder
Erik Max Francis
Dennis Lee Bieber
Jiri Barton
Lulu of the Lotus-Eaters
Terry Reedy
Steven Taschuk
Michele Simionato

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved