User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 425,924 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,697 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 698 | Replies: 3
Reply
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Style question

  #1  
Sep 4th, 2006
A friend of mine just introduced me to the Eclipse IDE, which may or may not end up replacing IDLE for me.

Anyway, one of the features of Eclipse is error warnings as the code is *displayed*, rather than waiting for errors to pop up at runtime.

This code got flagged:
if self.tag == None:
   return ""
elif self.tag == "_text":   <-----
   return "\t"*tablvl + self.value
elif self.tag == "_toplevel":
   s = ""
   for i in self.children:
      s = s + "\n" + i.__str__(0)
   return s



the line marked <----- got flagged as "unnecessary else", since the return "" technically prevents the code from getting to 'elif self.tag == "_text":' unless the if falls through.

Stylistically, what is your opinion? Would you write the code like that, or like this:

if self.tag == None:
    return ""
 if self.tag == "_text":   <-----
    return "\t"*tablvl + self.value
 if self.tag == "_toplevel":
    s = ""
    for i in self.children:
       s = s + "\n" + i.__str__(0)
    return s
 
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 1,133
Reputation: Ene Uran is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 66
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Veteran Poster

Re: Style question

  #2  
Sep 4th, 2006
Lint Filters give warnings that are not always sensible. Using elif instead of all if is faster, since Python has to evaluate all the if, but stops at the first true elif condition.

In your case it doesn't make much difference since you have a return statement, which will stop Python from going through the rest of the if conditionals. So 'if' is less to type than 'elif'.
Last edited by Ene Uran : Sep 4th, 2006 at 1:56 pm.
drink her pretty
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: Style question

  #3  
Sep 4th, 2006
Thanks. My feeling was that the first was more human-readable. Do you agree?
Reply With Quote  
Join Date: Aug 2005
Posts: 1,133
Reputation: Ene Uran is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 66
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Veteran Poster

Re: Style question

  #4  
Sep 4th, 2006
I agree! Since you are just going down the line with the same variable, if/elif makes more sense IMHO. The if/elif ties the conditional together.
Last edited by Ene Uran : Sep 4th, 2006 at 2:26 pm.
drink her pretty
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 9:01 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC