Empty block in if....else construct. OK?

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2006
Posts: 263
Reputation: Mushy-pea is an unknown quantity at this point 
Solved Threads: 1
Mushy-pea's Avatar
Mushy-pea Mushy-pea is offline Offline
Posting Whiz in Training

Empty block in if....else construct. OK?

 
0
  #1
Dec 16th, 2006
Hello everyone. I was just wondering if it is OK to do this:

  1. if ($beer == $wine) {# Some code}
  2. elsif ($beer == $vodka) {}
  3. else {die("Oh dear! Neither condition satisfied!")}

i.e. have an empty block attached to an if, elsif or else. It might sound like an odd question, but I don't want to put undefined behaviour into my program by breaking some obscure rule. Any advice appriciated.

Steven.
Last edited by Mushy-pea; Dec 16th, 2006 at 3:47 pm.
The one question you should not ask when teaching a new language structure is "Do you understand?". Do you understand?
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Empty block in if....else construct. OK?

 
0
  #2
Dec 16th, 2006
technically, as far as I know, it is OK. In general I would say it's not good practice unless it's clear why you needed to do that or put comments in the code explaining why you are doing that. Like in the CGI module you will see a sub routine that is just an empty block:

  1. # avoid autoloader warnings
  2. sub DESTROY {}

as the comment notes it's just there to avoid a warning message. In your case make sure it's really necessary, there is probably a better way to do whatever it is you are doing but can't tell by the sample code you posted.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 263
Reputation: Mushy-pea is an unknown quantity at this point 
Solved Threads: 1
Mushy-pea's Avatar
Mushy-pea Mushy-pea is offline Offline
Posting Whiz in Training

Re: Empty block in if....else construct. OK?

 
0
  #3
Dec 16th, 2006
Thanks Kevin. Actually, I have decided to release the application I am working on (perlBB) as open source software. So, if you want to have a peek at the code you can visit the project home page on Sourceforge (http://sourceforge.net/projects/perlbb ). The if....else construct I mentioned is in "sub update_list" in /perlbb_0_4/forum.pl.

Steven.
The one question you should not ask when teaching a new language structure is "Do you understand?". Do you understand?
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Empty block in if....else construct. OK?

 
0
  #4
Dec 16th, 2006
Good luck with the project.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: Empty block in if....else construct. OK?

 
0
  #5
Dec 16th, 2006
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 6
Reputation: nemo5 is an unknown quantity at this point 
Solved Threads: 0
nemo5 nemo5 is offline Offline
Newbie Poster

Re: Empty block in if....else construct. OK?

 
0
  #6
Dec 31st, 2006
It is perfectly ok what compiler regards, but I agree it is not a good coding style.
For a man without a destination no wind is favourable

http://www.vydavatelstvo-f.sk
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 66
Reputation: lordspace is an unknown quantity at this point 
Solved Threads: 4
lordspace's Avatar
lordspace lordspace is offline Offline
Junior Poster in Training

Re: Empty block in if....else construct. OK?

 
0
  #7
Jan 3rd, 2007
Hi,

you should be aware that using '==' you can compare numbers, not strings. Strings however can be compared with 'eq', 'ne' etc.

Svet
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 263
Reputation: Mushy-pea is an unknown quantity at this point 
Solved Threads: 1
Mushy-pea's Avatar
Mushy-pea Mushy-pea is offline Offline
Posting Whiz in Training

Re: Empty block in if....else construct. OK?

 
0
  #8
Jan 3rd, 2007
Thanks for the advice guys. I have rewritten the part of my program that contained the empty block to avoid it, as I agree its not good aesthetically.

Steven.
The one question you should not ask when teaching a new language structure is "Do you understand?". Do you understand?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 10
Reputation: zabina is an unknown quantity at this point 
Solved Threads: 0
zabina zabina is offline Offline
Newbie Poster

Re: Empty block in if....else construct. OK?

 
0
  #9
Jan 12th, 2007
I would say it's not good practice unless it's clear why you needed to do that or put comments in the code explaining why you are doing that.
Last edited by cscgal; Jan 12th, 2007 at 3:35 am. Reason: Fake signature snipped
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Perl Forum


Views: 3490 | Replies: 8
Thread Tools Search this Thread



Tag cloud for Perl
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC