Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Should We Have A "Red Flags" Area?

by Cody Pendant (Prior)
on Feb 16, 2003 at 22:39 UTC ( #235818=monkdiscuss: print w/ replies, xml ) Need Help??

I was meditating on Red Flags the other day.

M J D's Red flags and program repair material on perl.com has been very useful to me not just in coding, but in thinking about problems.

The most obvious red flags are
"don't do '($var1,$var2,$var3) = split', do '@vars = split"
and
"don't do "$user_name = $x; $user_rank = $y; $user_serial_number = $z', do '$user{'name'}', etc."

But there must be quite a few other ones that are key issues of good programming, plus there must be lots in specialised areas, like I see people getting very agitated about the use of placeholders in database contexts, which I don't care much about myself, but seems, just from skimming posts here, to be an important factor in good DB code.

Regexes also spring to mind as an example. Use of dot-star is always a bit worrisome, and do people know about the memory implications of using $& if you don't have to?

I was thinking of Red Flags as an area itself, or something to go in the Q & A area?

What do we think?
--

“Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
M-J D

Comment on Should We Have A "Red Flags" Area?
Re: Should We Have A "Red Flags" Area?
by Abigail-II (Bishop) on Feb 16, 2003 at 23:56 UTC
    No, I don't think we should. What is "good" coding and what isn't is very subjective. And that's also for what is a "red flag". Take for instance the first example. I do not agree that ($var1,$var2,$var3) = split is a red flag, let alone an obvious one.

    Perlmonks is already too dogmatic IMO (for instance, all the chanting of "you didn't use 'strict' or 'warnings'" even for the smallest code fragments that wouldn't benefit at all from enabling strictness). I don't think a red flags area on Perlmonks would be a good thing.

    Abigail, being kind fond of $& and friends.

      Couldn't agree more. I'm not too familiar with Perl internals and perldelta but couldn't differences between implementations and releases also cause problems? For example what might be a bad practice in perl 5.8 on wintel might be the best way for perl 4 on solaris?

      Also, as for strict vars, why isn't this the specified behaviour for all perl programs? It's scoped that way in many other languages, why is Perl different? It seems to just be inviting trouble.

        Perl is different so you can write useful programs as one-liners.

        No, I don't think we should. What is "good" coding and what isn't is very subjective.

      I'd like to see a Red Flags area — or at least a series of Meditations along those lines — for exactly that reason. I think it'd be quite useful for us to declare our biases and preferences wrt. coding style and "dangerous practices", and refute/bitch about others' biases and preferences. If nothing else, having to defend our beliefs would make us think about why we like what we do, and would give us at least a bit of insight into coding practices.

      In my opinion, that's a win.

      --
      F o x t r o t U n i f o r m
      Found a typo in this node? /msg me
      The hell with paco, vote for Erudil!

        Thing is, "Red Flags" sounds too canonical. It sounds like "these are thing that may be empirically shown to be dangerous or wrong much of the time". If we want to write about our own coding styles, our biases and preferences, the way maybe we love to have dozens of variables like $user_address_streetaddress1 around, or the way we love to have pairs of parallel arrays instead of hashes... well, we've got Meditations, home nodes and scratchpads, Obfuscation, the CB, and other places depending on the specific bent of the rant.

        It seems to me wasteful and counter-productive to create a new section dedicated to "this is the way I like to do stuff, and you ought to as well." Already that list of links at the top of the page is kind of expansive; I wouldn't think a listing of "Red Flags" would be of enough benefit to warrant the creation of an entire section for it.

        LAI
        :eof

      I hope that all chants of strict and warnings are directed towards those that don't know why they should use them, rather than those that do know why they shouldn't use them. This is not always the case of course ... but, i like to think of our chants as a means of undoing all the bad habits instilled in the late 1990's by Matt Wright's Scripts. ;)

      I agree with you in that i don't think we need a "Red Flag" area. Besides, merlyn does a pretty darned good job of flagging "Red Flag" code without such an area ... /duck

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
        All too often "use strict" and "use warnings" is chanted without addressing the problem the OP has. Or the problem is addressed, but whether "use strict" or "use warnings" was used is irrelevant.

        Wearing your seatbelts is a good thing, but giving that advice to someone who has problems getting anywhere because he's out of gas is completely irrelevant, and counterproductive.

        Abigail

Re: Should We Have A "Red Flags" Area?
by tachyon (Chancellor) on Feb 17, 2003 at 00:24 UTC
    # clear and self documenting ( undef, undef, $user, $pass, $domain ) = split; if ( $user eq 'foo' and $pass eq 'bar' ) { } # obfuscated, requires looking at data to understand @fields = split; if ( $field[2] eq 'foo' and $field[3] eq 'bar' ) { }

    For every thing TURN TURN there is a season LEARN LEARN and a time to every purpose under the heaven. A time to be born, and a time to die; a time to plant, and a time to pluck up that which is planted; a time to kill, and a time to heal; a time to break down, and a time to build up; a time to weep, and a time to laugh; a time to mourn, and a time to dance; a time to cast away stones, and a time to gather stones together; a time to embrace, and a time to refrain from embracing; a time to get, and a time to lose; a time to keep, and a time to cast away; a time to rend, and a time to sew; a time to keep silence, and a time to speak; a time to love, and a time to hate; a time of war, and a time of peace.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Let's be clear about this. The Red Flag isn't for:\
      ($user, $fred, $hair, $thing) = split ...

      The red flag is on code like:

      ($user1, $user2, $user3, $user4) = split ...

      The "Red Flag" in both cases is not putting a numbered list of like items into an array. Putting a mix of different items into an array is very much a decision that needs to be made on a case by case basis. 99% of the time, the varX..varY case should be a list. It is a Red Flag that the person doesn't yet comprehend the array abstraction and is very much the same mistake as the hash abstraction error.

      The example wasn't very clear thanks to you all abstracting "var" to mean "any word here" rather than the same word over and over with a numeral attached. (And yes, there are occasionally good reasons to do that but they are the 1%, not the 99.

      Still, I don't see any need to point those Red Flags up. The people who don't get that far into Perl generally aren't going to read a Red Flags section of this site or any other site. :)

      --
      $you = new YOU;
      honk() if $you->love(perl)

Re: Should We Have A "Red Flags" Area?
by castaway (Parson) on Feb 17, 2003 at 08:08 UTC
    I'll have to agree with Abigail, I don't think theres a need for a whole section on it, and programming style is a matter of taste. There are probably many reasons to use both sides of the examples you mention.
    (And shouting 'use strict' all the time gets on my nerves too, I've yet to see the benefit apart from making Perl more VB like.. ;)

    There's no reason not to write meditations/tutorials on programming style though, as long as the pros and cons are explained.

    C.

Re: Should We Have A "Red Flags" Area?
by Cody Pendant (Prior) on Feb 17, 2003 at 20:25 UTC
    Thank you all for your input. I hope it's been useful.

    Thank you especially to extremely for pointing out the specific case that is a red flag, the $varN thing.

    Also, what's a red flag anyway? A lot of you seemed to interpret it as "if you do this, you're a bad programmer" which is certainly not how I saw it at all.

    To me a red flag is more like "that way works, sure, but did you know there's a much easier way?". Or in fact, it's maybe "that works, but you don't have to write code that way in Perl -- who told you you did?".

    How about this example, to stimulate further discussion.

    When I first started, the only way I knew how to iterate through an array was

    for ($x=0;$<@array;$x++){ # .. do something with $array[$x] }
    but I would never do that any more. It looks horrible to me, plus, extra variables, more syntax to go wrong, scary for beginners and so on.

    Is that a red flag? It shows either that the person's a refugee from another, less accomodating language, or it shows that they've learned from sources that don't understand how easy iterating through an array is in Perl.

    Maybe it's just red flags for beginners that I'm interested in, er, flagging..?
    --

    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
    M-J D
      Frankly, I don't see the for loop as a red flag. So what if people have experience in other languages. I started coding long before Larry started thinking about a new language. Besides, I do use the for loop every now and then. Why? Because it's more flexible. It requires just minimal changes if you want every other element instead of each element. Or if halfway during the pass, you decide you have to skip the next few elements. And sometimes, it's just very convenient to have the index number.

      To me a red flag is more like "that way works, sure, but did you know there's a much easier way?". Or in fact, it's maybe "that works, but you don't have to write code that way in Perl -- who told you you did?".
      For me, such an attitude doesn't differ much from
      A lot of you seemed to interpret it as "if you do this, you're a bad programmer"
      For me, a red flag is nothing more than "hmmm, why is (s)he using this? Bad coding, or is there something else?".

      Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: monkdiscuss [id://235818]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2014-12-25 01:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls