Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: empty 'else' block

by pg (Canon)
on Nov 20, 2004 at 06:44 UTC ( [id://409225]=note: print w/replies, xml ) Need Help??


in reply to empty 'else' block

It is a bad idea to put a last there.

  1. If this if statement is inside a loop, you will go out of the loop, which is not what you want;
  2. If this is not inside a loop, Perl does not like it;

Actually I sometime intentionally add blank else blocks in my code, simply to tell myself that, "I DID think about whether anything need to be done in the else branch, but really nothing need to be done here, I didn't foget".

To do that, simply leave it blank, or put a ; there.

use strict; use warnings; my $var; if ($var) { print $var; } else { ; } print "Hello World!";

Replies are listed 'Best First'.
Re^2: empty 'else' block
by FoxtrotUniform (Prior) on Nov 20, 2004 at 08:26 UTC

    I'm not usually a fan of putting semantics in comments, but here I think I might make an exception for:

    if(&predicate(...)) { &foo(...); } else { # do nothing }

    It's a bit more explicit to some poor maintenance programmer than a lone, apparently useless, semi. Of course, if lone, apparently useless, semis are a coding convention for "do nothing" where you hack, it's okay. :-)

    --
    Yours in pedantry,
    F o x t r o t U n i f o r m

    "Anything you put in comments is not tested and easily goes out of date." -- tye

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://409225]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-20 00:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found