Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Coding styles using if/else

by superfrink (Curate)
on Apr 06, 2007 at 06:50 UTC ( [id://608613]=note: print w/replies, xml ) Need Help??


in reply to Coding styles using if/else

I used to work at a company that had a policy of using the first method. ie of leaving off the else. (They did code reviews.) It was claimed that this lead to more "linear" flow rather than deeply nested flow.

The idea is that the example if block is checking for (and dealing with) an exceptional situation. That check/deal step is a logical unit with respect to whatever the code it is part of is trying to accomplish.

Once that check/deal step is done the program continues on with the next step. Including an else clutters the code and slightly hides the main focus of the code.

Another effect of using else where it is not needed is it increases the level of nesting. That often makes code harder to understand.

The company also had commenting standards so the code might look something like the following. GOAL means "something we want to do". CLAIM means "something that is true at this point in the code".
my $name = param("name"); # GOAL : ensure input values are valid if ($name eq "") { print qq(Sorry, name can't be empty); exit; } # CLAIM : all input values are valid # GOAL : process input values print "Thank you, $name. Your submission is complete.";

Replies are listed 'Best First'.
Re^2: Coding styles using if/else
by roboticus (Chancellor) on Apr 06, 2007 at 12:57 UTC
    superfrink:

    And that blank area between the CLAIM and GOAL is the perfect location for:

    # CLAIM: all input values are valid assert($name ne ""); # GOAL: process input values
    (Yes, the exit above moots the assert in this case, but not all functions check all arguments.)

    ...roboticus

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-26 03:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found