Attention Perl programmers!

The following, while valid, is not “good code”.

Yes, you can fit it all in one SLOC, and you've got ternary operators and magic variables and conditional execution all jammed in there at the same time, well done; but it's bad code. Do not do it.

    defined $uri or $uri = !$prefix || $prefix eq $self->envprefix
    # still in doubts what should namespace be in this case
    # but will keep it like this for now and be compatible with our server
        ? ( $method_is_data
            && $^W
            && warn("URI is not provided as an attribute for method ($method)\n"),
            ''
            )
        : die "Can't find namespace for method ($prefix:$method)\n";

Note the following code sample. Yes, it has many more lines of code, and yes there is more typing, but note too how it's easier for a human maintainer to scan, parse, and grok?

    if (!defined $uri)
    {
        if (!$prefix || $prefix eq $self->envprefix)
        {
            # still in doubts what should namespace be in this case
            # but will keep it like this for now and be compatible with our server
            if ($method_is_data)
            {
                $^W && warn("URI is not provided as an attribute for method ($method)\n");
            }
            $uri = '';
        }
        else
        {
            die "Can't find namespace for method ($prefix:$method)\n";
        }
    }

I know that maintainability is a swear-word in Perl circles, but honestly people, I think it's time for a paradigm shift.


Matthew Kerwin

Published
Modified
License
CC BY-SA 4.0
Tags
development, perl, software

Comments powered by Disqus