Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Paradigm Shift - Don't use strict

by Ovid (Cardinal)
on Nov 12, 2001 at 22:47 UTC ( #124879=perlmeditation: print w/ replies, xml ) Need Help??

Imagine the following: you're facing a serious programming problem and a coworker tells you not to reinvent the wheel. There's a module that will do just what you need. Rather than just blindly use this module, you decide to give it a code review. Here's what you find:

No strict.

Most variables are global. In fact, much of the module's configuration is controlled by global variables.

Hand-rolled version of Exporter. The author was concerned about performance, so he rolled his own.

To make his code as desireable as possible, the author decided to give users the ability to use the code as both functions or object methods, thus, in your view, unnecessarily complicating the code. Maintenance is rather tricky (just try inheriting from it).

Plenty of symbolic references, such as the following:

*{"${callpack}::$sym"} = \&{"$def\:\:$sym"};

The following line can be found in the contructor:

bless $self,ref $class || $class || $DefaultClass;

In fact, merlyn refers to the above style as Cargo Cult programming. Is it a class method? Is it an instance method? Why is that there?

At least one function has the same name as a standard keyword, forcing the author to use CORE:: to call the original function. Other functions had to be renamed from their originals in prior versions due to collissions with Perl's built-ins.

Needless to say, after discovering all of this, you're absolutely horrified and tell your co-worker that under no circumstances will you use


The above information is valid for $CGI::VERSION='2.752'. I went and downloaded VERSION 2.0 and most, if not all of these points were still valid.

Please note that I am not advocating that anyone abandon this module. I just wanted to take the time to point out that some good programmers can violate the rules and get away with this. In this case, many of the issues above stem from the fact that this is legacy code that has been constantly upgraded.



Comment on Paradigm Shift - Don't use strict
Select or Download Code
Re: Paradigm Shift - Don't use strict
by suaveant (Parson) on Nov 12, 2001 at 23:06 UTC



                    - Ant
                    - Some of my best work - (1 2 3)

Re: Paradigm Shift - Don't use strict
by Asim (Hermit) on Nov 12, 2001 at 23:15 UTC

    Perhaps version 3.0 addresses some of the issues, although he still doesn't use strict; according to a quick glance of the code.

    ----Asim, known to some as Woodrow.

Re: Paradigm Shift - Don't use strict
by blakem (Monsignor) on Nov 13, 2001 at 00:17 UTC
    I agree with most of your points, but handrolling your own exporter really isn't all that bad1. If you look through, you'll find that most of it is just syntactic sugar around the following bit of code2 that looks remarkably similiar to what you noted:
    *{"${callpkg}::$sym"} = \&{"${pkg}::$sym"}
    There is also a nearly identical call for $vars and @arrays and %hashes, etc. is mainly just a nicer interface for this admitedly scary chunk of code.

    1Assuming you're Lincoln Stein
    2From 5.005_03 since I once disected its for fun


Re: Paradigm Shift - Don't use strict
by Elgon (Curate) on Nov 13, 2001 at 06:16 UTC

    Confucius say,

    Do what is right: Do not adhere to dogma.



Re: Paradigm Shift - Don't use strict
by chip (Curate) on Nov 13, 2001 at 10:09 UTC
    Salzenberg's Rules of Programming, Number Zero: "Anything that works is better than anything that doesn't."

    And does, in fact, work.

    That said: is atrocious in its style and horrifying in its design sense. It should be distributed in the directory examples/bad. I would welcome a well-written alternative. Atually, I'd welcome several well-written alternatives, one for each of the current module's several problem domains.

    Sigh.... So many kludges, so little time.

        -- Chip Salzenberg, Free-Floating Agent of Chaos

        Thank ghod I don't have to do it. Nice work, and nice documentation of work, too. Now for the hard part: Politics.

            -- Chip Salzenberg, Free-Floating Agent of Chaos

Re: Paradigm Shift - Don't use strict
by dmmiller2k (Chaplain) on Nov 13, 2001 at 19:50 UTC

    I feel you, man, er, as it were.



    Why rush the inevitable?



Re: Paradigm Shift - Dual Use Constructors
by demerphq (Chancellor) on Nov 13, 2001 at 21:20 UTC

    With all due respect to merlyn, I have encountered this post in the past and frankly was quite disappointed, neigh, shocked at what he had written. At the time I decided that as it was an old post and given that I was a newbie here it was inappropriate to criticise such a senior monk and icon of the perl community. However since then I have read various discussions about newbies not being afraid of commenting when they think they should and the fact that you bring it up here has made me change my mind:

    Lets consider merlyns rational for not having a dual use new()


    So it would appear that merlyn doesn't read the documentation of the modules he uses. The documentation should specify the exact semantics of a method, including whether it can be called in object or class context, and what it should do when it is.

    Now I agree if the documentation does not explicitly state that the new() is both a class/object method it probably should only be a class method. But this is perl, which as we all know if phenomenally flexible language, where part of that flexibility allows for exactly such behaviour. In fact one could almost argue that providing such behaviour goes along with the perl philosophy. After all subroutines are allowed to know if they have been called in list or scalar context, and change their behaviour accordingly, so whats the problem if constructors do the same?


    And now we see what is quite frankly one of my pet peeves: The ascribing of protocols and conventions from one language to perl. Frankly I couldnt give a toss what smalltalk users did in the 80's or what they are doing now. Nor do I care how Java does things or VB or C or C++. (Except when I use them :-) So lets provide a modern equivelent of merlyns misguided words:

      Yes there are prominent members of the perl community who do not think it is appropriate to use hungarian notation in their code, but ask them if they programmed Visual Basic in the 90's

    And just cause it annoys me so much how about a perlish one:
      Yes there are prominent members of the perl community who think it is appropriate to utilize wantarray and have their subs/methods be context aware, but ask them if they programmed C in the 70's

    Update:changed 60's to 70's as per tillys email

    Which IMO is as absurd as merlyn saying Perls constructors should conform to the conventions of Smalltalk. (And yes, I know that Smalltalk was the groundbreaking OO language. Consider that ALGOL/FORTRAN/COBOL were also a groundbreaking languages from the past, have you ever heard anyone arguing that we should comply with the conventions from them?


    To be quite frank I lost a lot of respect for merlyn when I read his post, but this line was the whammy. (Oh and its ok that I lost a lot of respect for him over this, theres still lots of respect left :-) Anybody that says 'any use of x is cargo cult programming' without giving damn good justification for the comment, is advocating cargo cult programming. And IMO the reasons that merlyn has given in his post for not using this construct are weak, verging on ridiculous.

    If I was in a class where I lost marks for doing such a thing in perl because my teacher thought that I should write my perl to reflect the conventions of another language (one that I may or may not be familiar with) I would not rate the teacher very highly, in fact I would probably demand my money back. I mean imagine the scenario, there you are using closures and a hash to dynamically generate accessors for an class, and the teacher marks you down because you haven't set them up as they do in Java! Bah! I signed on for a Perl course mate, not how to do Smalltalk/Java in Perl!


    My issue here is with merlyns justification and his knee-jerk approach to the issue. It should be understood that I am not necessarily advocating the use of dual use new()s.

    I usually provide dual use constructors in my objects, primarily for eas of use and brevity reasons (I dislike sprinkling classnames through my code, especially long ones :-) but I recently discovered what I consider to be a good reason to follow merlyns advice:

    C++ programmers like to use the indirect syntax for creating objects, instead of using the more perlish direct syntax. But sometimes they use both:

    my $obj=new CLASS($arg1,$arg2); my $obj=CLASS->new($arg1,$arg2); my $mistake=new CLASS->new($arg1,$arg2);
    When they make this mistake with an object that doesnt provide for dual use constructors they will get an error, when it does they will get an object. But depending on the semantics of the constructor they may get a very different object than they thought. For instance if the new() is a plain vanilla constructor in object or class, the arguments wouldn't be passed to the second call of new() resulting in a different object. Of course the story would be different if the new() in object context was a clone method.

    So the bottom line is that yes there are some minor issues with dual use constructors, but no they definately are not outright wrong, much less cargo cult programming.

    Yves / DeMerphq
    Have you registered your Name Space?
      I disagree that this is just a protocol or convention from another language. There is a lot more to this issue.

      The heart of this question is what your model of OO programming is. The ref trick makes Perl halfway towards being prototype based, but not really. Avoiding it allows you to have a model closer to Smalltalk's, which works much better in Perl. Given that I prefer Smalltalk's model, I am inclined towards merlyn's position. But given that the ref trick can't make the prototype approach work, I think that he is objectively right.

      (Those who don't know what I am talking about when I talk about prototypes should read the explanation I gave at A Cat's eye view of OO.)

      If you want to take a prototype approach in Perl, you can. Do it with BikeNomad's Class::Prototyped and have it work right. Don't do it with a half-way hack that allows you to maintain a bad mental model of your code that won't really work if you push it.

      Furthermore merlyn's accusation that it is typically cargo-cult programming is definitely on target. Take 10 random people who regularly use the ref meme in their constructors. Of them at least 8 have never given any serious thought to the question of why they would want to write code that way. They have no clear thoughts on it at all. They have just seen the construct, use it because they have seen it, but they have no strong thoughts on what the reason is. That is exactly what cargo-cult programming is. The blind repetition of programming patterns that you have not thought about and do not understand.

      And about the two other examples you gave. Steve McConnell was circumspect about saying how bad Hungarian Notation is in his classic Code Complete, but what he said there about why it is bad is exactly correct. And it was correct coming from someone who had (among other things) done VB programming for Microsoft in the 90's. Secondly nobody programmed in C in the 1960's. It wasn't invented until the 70's. Even if it were, the use of context in Perl is something a C programmer should have no opinion on since the concept doesn't exist in C. By contrast the use of objects in Perl is something which a user of another OO language reasonably can have opinions on in Perl because it exists both places. Their opinions might be wrong for Perl, but they at least have an experience base which is somewhat relevant.


        I know what it does, I know when I would use it, but I honestly can't remember when I've actually utilized a prototype, yet I continue to put the 'ref($proto) || $proto' stuff in my code (I think I may have actually used it once but I'm not sure). In a code review with a 'perl master' (not merlyn) at the conference I pointed out that I'm not sure if its all that useful but he assured me that it was fine. I figure that it has become such an idiomatic perl thing that if you don't do it that way, you take the chance of disappointing someone down the line who might want to utilize it (yeah, it's a weak excuse).


        Hah! beat ya to it. Though I continue to use it, I mark myself off first (and this is what I had in the 'code review'):

        sub new { # Do the cargo cult OO construction thing my $proto = shift; my $class = ref($proto) || $proto; ...

        For further information, see any human child.

            -- Chip Salzenberg, Free-Floating Agent of Chaos

        Tilly perhaps ive missed your point, but you seem to be doing the same thing as merlyn. You are trying to apply OO models from various languages to Perl. OO is a simple idea: the strong association or binding of procedures with the data that they manipulate. Everything else in OO is an extension of this simple concept. Now many languages embraced this idea and as mathematicians (think i)and computer science types often do extended the idea to its logical limits, or beyond in some cases, to see what the implications, pros cons etc of such systems.

        But these ideas (prototypes, inheritance, polymorhism, overloading....) are just additional concepts that have been added (or not depending on the language) because of the interests, predilictions and whathaveyou of the various authors and users of the various languages. The fact that they are often useful or desirable features of a language does not mean that they are necessary, much less right.

        Im happy that you prefer smalltalks OO model, and even happier that perl provides what is necessary to emulate it. But not because I like the smalltalk model but because I like the perl model, where I can do all kinds of things, only a few of which are considered to be standard OO techniques.

        Now as I said, I have come to agree with merlyns point, but not for any of the reasons he provided (which were, to be blunt, crap). You said: The blind repetition of programming patterns that you have not thought about and do not understand. Well Im sorry but to me that is exaclty what merlyns statement about marking people down whenever he saw the construct is. The blind application of a rule of thumb without trying to understand why it was done. For instance I have written a number of GA implementations in perl (unpublished sofar). Now I have a class GA::Entity in my model. This class is used to represent a solution in the GA. When I call GA::Entity->new() it returns a randomly generated solution. When I call $ga_entity->new() it produces a randomly mutated version of $ga_entity, and when I call $ga_entity->new($other_ga_entity) it returns a cross of the two. I thought long and hard if I wanted this type of behaviour and experimented with a number of alternatives before deciding I was happy with this approach. But of course merlyn would mark me down bigtime without considering why I had done it, and what my reasons were. And that my friend is plain and simple Cargo Cult Programming.

        Now, Ill grant that if merlyn had said something like "Well, generally I dont think this is a good idea. If I saw this in a code review I would want to see good justification for doing it, and a healthy amount of documentation to explain what is going on" or something to that effect then my attitude would be very different. Also I will grant that if the users of this construct are as you allege unaware of the issues related to it, then indeed it is cargo cult programming as well, but this doesnt change the fact that merlyns comments are too.

        I think you might want to reread my sarcastic comment about hungarian. I was trying to come up with the equivelent of a perl guru advocating the use of hungarian in perl.

        As for the C/Context point, I dont agree. To me there is little or no difference between someone who has experienced a (different) concept or not experienced the concept at all in a given langauge demanding that a second language behave the same way. That quite possibly is why they are different languages.

        Your last sentence is to me the key here, Their opinions might be wrong for Perl, but they at least have an experience base which is somewhat relevant, yes their opinions are relevant but not necessarily correct. And that is exactly what I think of merlyns post. Relevent and wrong.

        Yves / DeMerphq
        Have you registered your Name Space?





        -- Frag.

Re: Paradigm Shift - Don't use strict
by TheDamian (Priest) on Nov 16, 2001 at 16:05 UTC




    For example(s):

    • I rarely use strict in my modules.
    • My code brims over with symbolic references and typeglobbing.
    • I almost never write comments.
    • I often provide multiple interfaces to a module.








    As the Jargon File illustrates in its Zen-like appendix:



    Knight turned the machine off and on.

    The machine worked.

    Or, to misquote from The Fifth Element:
      Just a few comments.

      First of all I agree with you on the importance of making your own mind up. I have made similar points in the past, and on several occasions managed to irritate people by refusing to accept the received wisdom. But I think I learn better for it.

      That said, there are a few points about your own behaviour that I would like to point out.

      • I use strict fairly religiously. While you may not make the mistakes it catches, it catches a good fraction of my typos. Also I note that while you don't use strict, you get most of the benefits (and more!) from the test suites you write. Contrary to appearances, you do attempt sanity and caution.
      • Roll your own import or not, decisions, decisions. I have written my own imports before, and most of the time I don't. The rule of thumb that I use is, "If what I want is achievable from the basic Exporter, I use that just so that my semantics will match that. If they are totally different, then I won't. Consistency is good. If I am going to be inconsistent, I have to have an opposing good reason." Generally speaking the more "interesting" the code is, the more likely it is to need custom semantics. The vast majority of code does not.
      • I doubt that many knowledgable people would disagree with the uses of typeglobs and symbolic references that I have seen you use. However when you write more routine programs, I suspect you use fewer typeglobs and symbolic references.
      • My commenting rules are at Re (tilly) 4 (disagree): Another commenting question,. If you count documentation as commenting (which I am inclined to), then you do indeed have verbose comments.
      • I think the key-word about goto is well-placed. Few people know how to place them, and I would prefer to tell people never to use them than to have them try to figure out how to use them well. (An incidental note. Your goto in Switch also uncovered interesting bugs in the development release of Perl...)
      • Polymorphism is the one issue I disagree with you on. Sure, 5 people may find 20 ways of thinking about the same problem. But if they are going to work closely together, it is important that they manage to agree on one or two ways of cooperating. Furthermore many of what I consider the worst design mistakes in good modules are attempts to overload functions with every possible behaviour. Take, for instance, CGI's param method. As I pointed out at Re (tilly) 2: form parsing, the overloading is extreme enough that even good programmers will make mistakes and omissions due to forgetting parts of the interface and therefore passing through data that may be misinterpreted as metadata.

        Yes, people find different things intuitive. Yes, people have a native capacity to handle ambiguity. Yes, as long as you stay within the limits of what people can handle, using that native ability is good. Perl has done this very well. But people's intuition is generally flexible enough to learn how your code works, and people ability to handle ambiguity does seem to have a hard upper limit. OK, possibly you don't have an upper limit. But I do, and you manage to exceed it on a fairly regular basis...

      Which brings me to my biggest single comment. The average business has a huge concern known as, "What do we do if ___ gets hit by a bus tomorrow?" This is irritating for any decent programmer, we don't want to feel replacable. (Though a tip for bystanders, make yourself replacable and they are freer to put you into new, more interesting, roles.) Well you are not currently coding for a company that can ask that question, but for the sake of Perl, I hope you look both ways before you cross the street...

        tilly wrote:


        Just out of curiosity, what do you consider well-placed? The only time I have ever used goto is to subvert the call stack. And I did that once.

        You've mentioned before that goto has uses. Other than this Perl-specific reason, what else would you use it for?



        Contrary to appearances, you do attempt sanity and caution.



        Polymorphism is the one issue I disagree with you on.






      They're actually the opposite of a cargo cultist, even if the visible symptoms are identical. ... It isn't what you's why you do it. ... "What" is not important. Only "why" is important.

      I'll take the antithesis to that. If I'm given a body of code to incorporate into something else, or to extend because the requirements changed, or otherwise to maintain, and I see a "mess" that is fragile and hard to change, than the original author was either very smart or had a large monkey multiplier factor. Do I care which? NO! I only see the result, that the code is hard to maintain.

      In anything other than your own throw-away program, the "visible symptoms" is the only part that matters. Everything else is just cold dead history.

      (still making you challange your assumptions)

        You missed the point.

        The "symptoms" that TheDamian talked about are checklists of specific programming practices people are told to avoid. If you know what you are doing and why, you can use most of them but avoid the problems which make it simpler to tell people not to use them. The resulting code can be both good and maintainable, but will cause people who only know the checklists to freak out because it violates their checklists. TheDamian is infamous for writing code like that.

        It is like swimming. If you have had any swimming lessons, one of the first things they drill into you is to not get anywhere near a drowning person in the water because you will die. The reason is that a drowning person is several times stronger than normal, and their only desire is to climb on top of you and stand on your head. You simply are not a good enough swimmer to handle that treatment. Nobody is.

        Yet lifeguards go after drowning people in the water all the time. Isn't that stupid? No, because lifeguards know the danger perfectly well and understand how to avoid it. (Always approach from behind, stay in place holding the person's head up until they are calm, the instant you get into trouble - dive!)

        Yes, it is dangerous. Yes, it is what everyone is told from day one not to do. But if you know what you are doing, it sometimes is exactly what needs to happen.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://124879]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
As of 2015-04-25 00:20 GMT
Find Nodes?
    Voting Booth?

    Who makes your decisions?

    Results (474 votes), past polls