in reply to Is this bad coding style?
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Is this bad coding style?
by Anonymous Monk on Jan 08, 2009 at 17:30 UTC | |
by pemungkah (Priest) on Jan 10, 2009 at 00:58 UTC | |
by leocharre (Priest) on Jan 30, 2009 at 20:11 UTC |