Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Confession of a hard headed Monk

by Lady_Aleena (Curate)
on May 06, 2017 at 03:43 UTC ( #1189640=perlmeditation: print w/replies, xml ) Need Help??

Over the last few days, I've been doing a project that is forcing me take a new harder look at everything I've written: code and markup. One of the items which came up in my audit was a little subroutine which takes a number (usually scalar(@list)) and returns how many columns the list would be in according to exponential calculations. The default behavior of this subroutine is to return a word number not an integer. When I looked at it again, a voice in the back of my head told me the subroutine should return the integer by default because integers are more useful. I also had the sinking feeling someone here probably pointed that out to me, and I ignored it. I wanted what I wanted, and no one was going to make me change my mind at the time. I had to change my own mind and pay the price for it later.

So, this is my confession. I am a doofus. I am hard headed and obstinate when it comes to attaining my short term goals. I crawled into my shell to avoid any good advice about coding for the future.

So here is my old sub...

use Lingua::EN::Inflect qw(NUMWORDS); sub get_columns { my ($max_cols, $amount, $number) = @_; if ($amount <= $max_cols ** 2) { for my $num (1..$max_cols) { if ($amount >= $num ** 2 && $amount < ($num + 1) ** 2) { return $number && $number =~ /^[yt1]/ ? $num : NUMWORDS($num); } } } else { return $number && $number =~ /^[yt1]/ ? $max_cols : NUMWORDS($max_ +cols); } }

And the new more useful sub...

use Lingua::EN::Inflect qw(NUMWORDS); sub get_columns { my ($max_cols, $amount, $word) = @_; if ($amount <= $max_cols ** 2) { for my $num (1..$max_cols) { if ($amount >= $num ** 2 && $amount < ($num + 1) ** 2) { return $word && $word =~ /^[yt1]/ ? NUMWORDS($num) : $num; ############################################# Delete to + get rid of Lingua::EN::Inflect } } } else { return $word && $word =~ /^[yt1]/ ? $NUMWORDS($max_cols) : $max_co +ls; ################################################### Delete +to get rid of Lingua::EN::Inflect } }

I feel bad for only now realizing returning a integer by default would be more useful than returning a number word. I think I spoke aloud when I called myself a doofus for returning a number word by default.

I've been doing a lot of rethinks lately. Some of my "utility" scripts seem useless in a broader sense.

The great advice I got from Monks long ago is finally settling into my head. I hope I am better with future advice, but that is to be seen.

No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
Lady Aleena

Replies are listed 'Best First'.
Re: Confession of a hard headed Monk
by Anonymous Monk on May 22, 2017 at 14:22 UTC

    /me nods ...

    You certainly made a good case for it with your explanation.   If you are returning “a count of things,” yes, it should be truncated or rounded ... as the case may be.

Re: Confession of a hard headed Monk
by sundialsvc4 (Abbot) on May 12, 2017 at 00:04 UTC
    The default behavior of this subroutine is to return a word number not an integer.

    So why, exactly, are you now arguing against yourself ... even, “calling yourself ‘a doofus™”” ... with regards to a software solution that apparently works?

      Just because something works does not mean it is useful, good, or right. There are very few people would find a number as a word useful, but an integer is very useful. So making the return of an integer the default might lead to a few people using this little subroutine possibly. I've given an example below of what it does, and to show why it is more useful to return an integer by default.

      use Util::Columns; my @list = <DATA>; chomp @list; my $columns = get_columns(7, \@list); # why take another step to get a +n integer? # If you don't add 1 to the result of the division below, # you will get an additional column. my $splicer = (scalar(@list) / $columns) + 1; my $row; push @$row, join("\n", splice(@list, 0, $splicer)) while @list; # then print the row however you like

      This is what I got from a list of html elements. If the list had fewer than 49 items, the there would have been 6 columns. (I have been working a lot with html elements lately, so they are on my mind.)

      <a> <code> <fieldset> <img> <object> <sect +ion> <tfoot> <abbr> <col> <figcaption> <input> <ol> <sele +ct> <th> <address> <colgroup> <figure> <ins> <optgroup> <shad +ow> <thead> <area> <content> <footer> <kbd> <option> <slot +> <time> <article> <data> <form> <label> <output> <smal +l> <title> <aside> <datalist> <h1> <legend> <p> <sour +ce> <tr> <audio> <dd> <h2> <li> <param> <span +> <track> <b> <del> <h3> <link> <pre> <stro +ng> <u> <base> <details> <h4> <main> <progress> <styl +e> <ul> <bdi> <dfn> <h5> <map> <q> <sub> + <var> <bdo> <dialog> <h6> <mark> <rp> <summ +ary> <video> <blockquote> <div> <head> <menu> <rt> <sup> + <wbr> <br> <dl> <header> <menuitem> <rtc> <tabl +e> <button> <dt> <hgroup> <meta> <ruby> <tbod +y> <canvas> <element> <hr> <meter> <s> <td> <caption> <em> <html> <nav> <samp> <temp +late> <cite> <embed> <i> <noscript> <script> <text +area>
      No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
      Lady Aleena

        Just and FYI, you're replying to someone with a track record of terrible advice and frankly nonsense postings.

        Bad, bad, bad logic in a part of the usage code I provided above...

        # If you don't add 1 to the result of the division below, # you will get an additional column. my $splicer = (scalar(@list) / $columns) + 1;

        It should be...

        # Add 1 to the result of the division below # if list is not evenly divisible by $columns. my $splicer = int(scalar(@list) / $columns); $splicer += scalar(@list) % $columns > 0 ? 1 : 0;

        Sorry for not thoroughly testing that part. 8(

        No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
        Lady Aleena
      Because "works" is merely the beginning. You also need to address the following:
      1. User experience
      2. Availability
      3. Performance
      4. Scalability
      5. Adaptability
      6. Security
      7. Economy
      I applaud any effort to improve the skills to fulfill these requirements.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://1189640]
Approved by Athanasius
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2017-06-26 17:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (585 votes). Check out past polls.