Bad coding style is a little vague..

You could be meaning syntax- which is more about formatting- or maybe symbol names (variable names, etc) - or your lack of use strict, or your use of foreach instead of for.. etc etc.

Here's how I would naturally code this, myself..

my @a = qw/1 2 3 # 6 7 # 9 10/; for my $val ( @a ){ $val eq '#' ? do_something() : $val eq '1' ? do_where_var_is_1() : do_something_else(); }

Why am I naming a variable $val instead of simply using $_ ? Because it's more intuitive when I look at it- to mean that I am checking that value, if I were using the value, I may name it $arg.

Why use a for instead of foreach? I use foreach with hashes, for with arrays, something personal.

Why use the ternary operator instead of if and else? For brevity and because it rocks.

Why do I use eq for both 1 and # ? Because it makes the code make more sense for human eyes. ( Besides you don't need to differentiate between numbers and strings here- they are all strings, right? That is, it appears the sample data you may have is by default all string- and it may be a character number or something else. You can't have all numbers and one character and still have all numbers, but you can have all c... anyways.. )

That's what *I* would do naturally. But everyone has a style. What's important is that is work, and it's also important that another coder can look at it and easily tell what you are doing.


In reply to Re: Is this bad coding style? by leocharre
in thread Is this bad coding style? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.