Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: [Study]: Searching for square roots

by blazar (Canon)
on Nov 14, 2006 at 16:57 UTC ( [id://584008]=note: print w/replies, xml ) Need Help??


in reply to [Study]: Searching for square roots

A few side comments:
sub middle ($$) { my ( $a, $b ) = ( shift, shift ); return ($a + $b) / 2 }

Don't pass parameters like that, it's confusing: which shift gets executed first? I'd either use it if only one is involved at a time, and possibly if I have to use the rest of @_ as a whole, or stick with:

my ($n,$m)=@_;

(I tend not to use $a and $b as general purpose variables even when they wouldn't be error prone because... their use is potentially error prone.)

It's a commonly recommended style to put a semicolon on the last line too.

Oh, and there's no need to use prototypes. Actually many people deprecate them. I find them useful for code and autorefs, but other than that they don't buy you much.

print "SQRT 16:\n", &sqrt( 16 ), $/, $/;

&-form of sub call is now obsolete and most likely not to do what you want. Read more about this in perldoc perlsub.

Replies are listed 'Best First'.
Re^2: [Study]: Searching for square roots
by duff (Parson) on Nov 14, 2006 at 17:51 UTC

    If I were writing the middle subroutine, I wouldn't copy the values into other variables before use:

    sub middle { return ($_[0] + $_[1]) / 2; }

    But that's probably just me.

      Indeed: YAWTDI!

Re^2: [Study]: Searching for square roots
by revdiablo (Prior) on Nov 14, 2006 at 23:48 UTC
    Oh, and there's no need to use prototypes. Actually many people deprecate them.

    The OP's code even does what you've said "many people" do. Using the & prefix to call a sub disables prototype checking. The combination of both &sub and prototypes should be a red flag (in addition to each being a red flag in and of themselves).

      The OP's code even does what you've said "many people" do. Using the & prefix to call a sub disables prototype checking. The combination of both &sub and prototypes should be a red flag (in addition to each being a red flag in and of themselves).

      I second that thoroughly. And it was very good of you to underline this sort of "synergy", because I didn't. Actually since there are prototypes, this may be one situation in which &sub may have sense, to overcome them. But it does not have sense to overcome them, so just as in the vast majority of cases, it's not needed and better avoided, period.

Re^2: [Study]: Searching for square roots
by GrandFather (Saint) on Nov 14, 2006 at 17:41 UTC

    If I needed to pull more than one value off the front of @_ and leave the rest I'd possibly use splice:

    my ($arg1, $arg2) = splice 0, 2, @_;

    but more likely I'd pull the tail elements out into an array:

    my ($arg1, $arg2, @tail) = @_;

    DWIM is Perl's answer to Gödel

      Well, I do use splice occasionally, but not for argument passing. I'd either do

      my $arg1=shift; my $arg2=shift;

      or as in your second alternative, the point still being that multiple shift's on one line can be confusing albeit potentially correct, and IMHO clearly neither particularly concise nor expressive in terms of readability. Anyway what I choose depends on the emphasis I want to put on each argument. Fortunately in a not (any more) so remote future we will avoid all these parameter passing acrobatics...

Re^2: [Study]: Searching for square roots
by pemungkah (Priest) on Nov 15, 2006 at 21:19 UTC
    And remember that $a and $b are the special comparison variables for sort(). It's a bad habit to use meaningless variable names anyway - let's move on past FORTRAN!

    I'd recommend maybe

    sub middle { my($low, $high) = @_; return ($low + $high) / 2; }
    because it's now much more evident that the "middle" is the value between low and high.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-16 07:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found