• Latest
Get Out Early With Perl Statement Modifiers

Get Out Early With Perl Statement Modifiers

January 20, 2022
S.T.A.L.K.E.R. 2 Development Has Reportedly Resumed Following Its Pause Due to Ukraine Invasion

S.T.A.L.K.E.R. 2 Development Has Reportedly Resumed Following Its Pause Due to Ukraine Invasion

May 23, 2022
Soulvars Fatal Error, Bokura, and the 13th Month Announced for Mobile – TouchArcade

Soulvars Fatal Error, Bokura, and the 13th Month Announced for Mobile – TouchArcade

May 23, 2022
Prehistoric Planet: How to watch the new dinosaur docuseries

Prehistoric Planet: How to watch the new dinosaur docuseries

May 23, 2022
Harry Potter: Magic Awakened and Everything Else Featured at NetEase Connect 2022

Harry Potter: Magic Awakened and Everything Else Featured at NetEase Connect 2022

May 23, 2022
Magic: The Gathering Returns to Dungeons & Dragons – See a New Card!

Magic: The Gathering Returns to Dungeons & Dragons – See a New Card!

May 23, 2022
Comparing Distributed Databases – DZone Database

Comparing Distributed Databases – DZone Database

May 23, 2022
Data Security Considerations in CDWs

Data Security Considerations in CDWs

May 23, 2022
Xiaomi officially announces partnership with Leica, first jointly developed phone arrives in July

Xiaomi officially announces partnership with Leica, first jointly developed phone arrives in July

May 23, 2022
Basic Convolutional Neural Network Architectures

Basic Convolutional Neural Network Architectures

May 23, 2022
Configuring a Shopify MuleSoft Connector

Configuring a Shopify MuleSoft Connector

May 23, 2022
Stunning aerial shots of the Dolomites in a post-sunset glow

Stunning aerial shots of the Dolomites in a post-sunset glow

May 23, 2022
Klonoa Collection Frame Rate, Resolution And File Size For Switch Revealed

Klonoa Collection Frame Rate, Resolution And File Size For Switch Revealed

May 23, 2022
Advertise with us
Monday, May 23, 2022
Bookmarks
  • Login
  • Register
GetUpdated
  • Home
  • Game Updates
    • Mobile Gaming
    • Playstation News
    • Xbox News
    • Switch News
    • MMORPG
    • Game News
    • IGN
    • Retro Gaming
  • Tech News
    • Apple Updates
    • Jailbreak News
    • Mobile News
  • Software Development
  • Photography
  • Contact
    • Advertise With Us
    • About
No Result
View All Result
GetUpdated
No Result
View All Result
GetUpdated
No Result
View All Result
ADVERTISEMENT

Get Out Early With Perl Statement Modifiers

January 20, 2022
in Software Development
Reading Time:5 mins read
0 0
0
Share on FacebookShare on WhatsAppShare on Twitter


When I first started writing Perl in my early 20’s, I tended to follow a lot of the structured programming conventions I had learned in school through Pascal, especially the notion that every function has a single point of exit. For example:

sub double_even_number {
    # not using signatures, this is mid-1990's code
    my $number = shift;

    if (not $number % 2) {
        $number *= 2;
    }

    return $number; 
}

This could get pretty convoluted, especially if I was doing something like validating multiple arguments. And at the time I didn’t yet grok how to handle exceptions with eval and die, so I’d end up with code like:

sub print_postal_address {
    # too many arguments, I know
    my ($name, $street1, $street2, $city, $state, $zip) = @_;
    # also this notion of addresses is naive and US-centric

    my $error;

    if (!$name) {
        $error="no name";
    }
    else {
        print "$namen";

        if (!$street1) {
            $error="no street";
        }
        else {
            print "$street1n";

            if ($street2) {
                print "$street2n";
            }

            if (!$city) {
                $error="no city";
            }
            else {
                print "$city, ";

                if (!$state) {
                    $error="no state";
                }
                else {
                    print "$state ";

                    if (!$zip) {
                        $error="no ZIP code";
                    }
                    else {
                        print "$zipn";
                    }
                }
            }
        }
    }

    return $error;
}

What a mess. Want to count all those braces to make sure they’re balanced? This is sometimes called the arrow anti-pattern, with the arrowhead(s) being the most nested statement. The default ProhibitDeepNests perlcritic policy is meant to keep you from doing that.

The way out (literally) is guard clauses: checking early if something is valid and bailing out quickly if not. The above example could be written:

sub print_postal_address {
    my ($name, $street1, $street2, $city, $state, $zip) = @_;

    if (!$name) {
        return 'no name';
    }
    if (!$street1) {
        return 'no street1';
    }
    if (!$city) {
        return 'no city';
    }
    if (!$state) {
        return 'no state';
    }
    if (!$zip) {
        return 'no zip';
    }

    print join "n",
      $name,
      $street1,
      $street2 ? $street2 : (),
      "$city, $state $zipn";

    return;
}

With Perl’s statement modifiers (sometimes called postfix controls) we can do even better:

    ...

    return 'no name'    if !$name;
    return 'no street1' if !$street1;
    return 'no city'    if !$city;
    return 'no state'   if !$state;
    return 'no zip'     if !$zip;

    ...

(Why if instead of unless? Because the latter can be confusing with double-negatives.)

Guard clauses aren’t limited to the beginnings of functions or even exiting functions entirely. Often you’ll want to skip or even exit early conditions in a loop, like this example that processes files from standard input or the command line:

while (<>) {
    next if /^SKIP THIS LINE: /;
    last if /^END THINGS HERE$/;

    ...
}

Of course, if you are validating function arguments, you should consider using actual subroutine signatures if you have a Perl newer than v5.20 (released in 2014), or one of the other type validation solutions if not. Today I would write that postal function like this, using Type::Params for validation and named arguments:

use feature qw(say state); 
use Types::Standard 'Str';
use Type::Params 'compile_named';

sub print_postal_address {
    state $check = compile_named(
        name    => Str,
        street1 => Str,
        street2 => Str, {optional => 1},
        city    => Str,
        state   => Str,
        zip     => Str,
    );
    my $arg = $check->(@_);

    say join "n",
      $arg->{name},
      $arg->{street1},
      $arg->{street2} ? $arg->{street2} : (),
      "$arg->{city}, $arg->{state} $arg->{zip}";

    return;
}

print_postal_address(
    name    => 'J. Random Hacker',
    street1 => '123 Any Street',
    city    => 'Somewhereville',
    state   => 'TX',
    zip     => 12345,
);

Note that was this part of a larger program, I’d wrap that print_postal_address call in a try block and catch exceptions such as those thrown by the code reference $check generated by compile_named. This highlights one concern of guard clauses and other “return early” patterns: depending on how much has already occurred in your program, you may have to perform some resource cleanup either in a catch block or something like Syntax::Keyword::Try’s finally block if you need to tidy up after both success and failure.



Source link

ShareSendTweet
Previous Post

Understanding composition, with the rule of thirds

Next Post

The Activision/Xbox Deal by the Numbers

Related Posts

Comparing Distributed Databases – DZone Database

May 23, 2022
0
0
Comparing Distributed Databases – DZone Database
Software Development

The past decade saw the rise of fully distributed databases. Not just local clustering to enable basic load balancing and...

Read more

Data Security Considerations in CDWs

May 23, 2022
0
0
Data Security Considerations in CDWs
Software Development

This is an article from DZone's 2022 Data Pipelines Trend Report.For more: Read the Report Cloud data warehouses (CDWs) are...

Read more
Next Post
The Activision/Xbox Deal by the Numbers

The Activision/Xbox Deal by the Numbers

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

© 2021 GetUpdated – MW.

  • About
  • Advertise
  • Privacy & Policy
  • Terms & Conditions
  • Contact

No Result
View All Result
  • Home
  • Game Updates
    • Mobile Gaming
    • Playstation News
    • Xbox News
    • Switch News
    • MMORPG
    • Game News
    • IGN
    • Retro Gaming
  • Tech News
    • Apple Updates
    • Jailbreak News
    • Mobile News
  • Software Development
  • Photography
  • Contact
    • Advertise With Us
    • About

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?