954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

Hello everyone. I was just wondering if it is OK to do this:

if ($beer == $wine) {# Some code}
elsif ($beer == $vodka) {}
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.

Mushy-pea
Posting Whiz in Training
275 posts since Jun 2006
Reputation Points: 47
Solved Threads: 1
 

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:

# avoid autoloader warnings
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.

KevinADC
Posting Shark
921 posts since Mar 2006
Reputation Points: 246
Solved Threads: 67
 

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.

Mushy-pea
Posting Whiz in Training
275 posts since Jun 2006
Reputation Points: 47
Solved Threads: 1
 

Good luck with the project.

KevinADC
Posting Shark
921 posts since Mar 2006
Reputation Points: 246
Solved Threads: 67
 
KevinADC
Posting Shark
921 posts since Mar 2006
Reputation Points: 246
Solved Threads: 67
 

It is perfectly ok what compiler regards, but I agree it is not a good coding style.

nemo5
Newbie Poster
6 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

Hi,

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

Svet

lordspace
Junior Poster in Training
90 posts since May 2006
Reputation Points: 18
Solved Threads: 6
 

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.

Mushy-pea
Posting Whiz in Training
275 posts since Jun 2006
Reputation Points: 47
Solved Threads: 1
 

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.

zabina
Newbie Poster
10 posts since Dec 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You