Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

The first cargo cults

by jepri (Parson)
on Jan 11, 2002 at 18:23 UTC ( [id://138003]=perlmeditation: print w/replies, xml ) Need Help??

One of our illustrious members once had the habit of crying "cargo cult!" whenever he saw someone cut and paste a code snippet into a program. In particular, he was decrying the habit of cutting and pasting bad code. But even cutting and pasting good code is not necessarily a good thing. Here's the origin of the term "cargo cult", and why it's such a bad idea:

This quote is from a book called "Surely you're joking Mr. Feynman", by Richard Feynman. He was one of the few scientists in the world who could pack a lecture hall when he gave public lectures. Worked on the bomb, revolutionised physics and was funny as well.

"I think the educational and psychological studies I mentioned are examples of what I would like to call cargo cult science. In the South Seas there is a cargo cult of people. During the war they saw airplanes land with lots of good materials, and they want the same thing to happen now. So they've arranged to make things like runways, to put fires along the sides of the runways, to make a wooden hut for a man to sit in, with two wooden pieces on his head like headphones and bars of bamboo sticking out like antennas - he's the controller - and they wait for the airplanes to land. They're doing everything right. The form is perfect. It looks exactly the way it looked before. But it doesn't work. No airplanes land. So I call these things cargo cult science, because they follow all the apparent precepts and forms of scientific investigation, but they're missing something essential, because the planes don't land.... "

So now you know why cargo cult programming is bad. Because it's like waving some bamboo sticks to try and summon a cargo plane to fulfill your wishes. Whenever you cut and paste code without knowing what it does, you are like the poor guy on the runway, waving bamboo sticks to try and make the magic happen. You've got the look (you've got the code), but you're lacking something essential.

It probably wouldn't be right to finish without a tip on how to get that essential something - experiment! Feyman was a scientist, and he goes on to explain how science succeeds by constantly testing it's knowledge. So if you have some code that does what you want, without knowing how or why it does what you want, tinker with it.

Cut it out and paste it into a little program called 'test.pl'. Make it print out something different. Change the colours. Add mag wheels. Break it and make it work again. Ask about it in the CB.

Don't just paste it into your own program, because when the people who originally wrote it read your code, they see a little man waving bamboo leaves...

____________________
Jeremy
I didn't believe in evil until I dated it.

Replies are listed 'Best First'.
Re (tilly) 1: The first cargo cults
by tilly (Archbishop) on Jan 11, 2002 at 19:00 UTC
    Those who want to read the speech the above Feynman quote is from can find it online as well.

    It isn't as simple as just saying to experiment. It is a lot (as Feynman says in the longer speech) about maintaining a conceptual integrity from which you learn through experience what to pay attention to, what is essential, and what is probably not. Programming is a little different from science in this. In science we have no good preconceptions available of how things should be. In programming there is likely to be documentation on how things were intended to be (they might not have wound up that way, but that is another story).

    But still, without a solid mental model of why we do the actions that we do, we are likely to repeat actions by rote which make no sense out of their original context, and we will miss obvious ways of doing things simply because we have no way to even understand that they make sense. So try to understand what you are working with. Don't wave bamboo because it looks like what you saw. Understand what you saw and then act according to your best understanding.

      Ack! It does look like I said that Feynman's remedy was to experiment. Like you say, Feyman was really getting into integrity and experimental methodology. Although I would point out a large part of that speech was about how people don't learn from experience what to pay attention to.

      I appreciate how correct you are in focussing on building a compact mental model that helps you understand and perform better. I was trying to focus more on the first step, which is letting go of a very beguiling mental model, the 'magic wand' model of doing something.

      I suggested the experiment idea because I have never failed to learn something by taking stuff apart - whether programs, televisions or cars. I feel that conceptual integrity only becomes an issue after someone stops believing in magic and starts forming their own concepts.

      ____________________
      Jeremy
      I didn't believe in evil until I dated it.

        What I think is that we all start by believing in some form of "magic". How we learn to distinguish our magical misconceptions from reality is through trying to create conceptual integrity. Without having at least some of that we lack the basic tools to try to form our own concepts. (For more on this, a lot more on this, I suggest picking up Why People Believe Weird Things.)

        As for the speech, I agree absolutely that a lot of it was about how people don't learn from experience. More than that, it was what you need to do to have a chance of learning from experience. And at the top of that list is that you have to be willing to discover that you are wrong. If at all possible, you want to encourage this. Keep in mind that that discovering that you are wrong is the first step to improving and having a chance of being right.

        Incidentally while this is one of the first principles of how successful science works, it is not sufficient for progress. All of the willingness to learn won't help you one bit if what you are trying to learn about isn't amenable to simplification and analysis with your current toolset. Which is one of the reasons that the social sciences have been unable to establish solid foundations for themselves. It is not that no good scientists have tried to do so. It is that the subject is not tractable enough for us to do a good job with it.

Re: The first cargo cults
by talexb (Chancellor) on Jan 11, 2002 at 19:19 UTC
    That's a fascinating peek at history. Where was all this great stuff in high school?

    To go left-brained for a minute, copying and pasting can be a good thing if it means you're not "re-inventing the wheel" to quote a popular phrase from one of the monks. I would hasten to add that the utility of cutting and pasting varies indirectly with the size of the piece of code involved.

    If it's ten lines that you want to re-use, but the second use is different enough that it doesn't make sense to build a common subroutine, go ahead. If it's 80 lines from someone else's script, but you're not sure how it works (and what're all these modules!), that's cargo cult.

    The corollary to that is, if you're doing a lot of copying and pasting while writing a script or a module, you should really stop yourself and ask why. Couldn't that be put into a subroutine? Are you sure? Really, really sure?

    --t. alex

    "Excellent. Release the hounds." -- Monty Burns.

      IMNSHO, it's not the size of the code you copy, or the code's generality, that distinguishes between sloppy, dangerous cargo-cult programming and legitimate re-use, but how well you understand the code. (Sure, it's almost always better to generalize the code you want to copy and modify, make it a subroutine, and solve a whole class of problems -- I refer you to Perl's genesis for prior art -- but that's not so much the point here.)

      In The Pragmatic Programmer, Hunt and Thomas talk about code "wizards" -- programs which take a rough specification and generate an application framework. This is classic cargo-cult programming, if taken the wrong way: you have a complete MDI interface before you, but you have no idea of how, or why, it works. You just fill in the templates and hope you don't clobber anything important. Hunt and Thomas suggest that the way to make wizard use healthy is to understand the code that gets produced.

      So it's understanding that makes the difference between laudable re-use and cargo-cult programming. If I understand the code that I'm copying in, I'm fine. If I don't understand the how, but the code's in a library (or a module) and I grok the interface, then I'm fine using the module. (If I open it up and copy a likely looking function out of it, then I'm probably hosed.) If I don't understand even one line of it, but I use it anyways, and try to make the rest of that line's context look like the last place I saw it, then I've fallen into cargo-cult programming, and I deserve everything I get.

      --
      :wq
        FoxtrotUniform++++++++++++ (if I could, which is why I'm posting instead). There is a definite tendency towards wizards and blackboxes everywhere and I hate it.
      That's a fascinating peek at history. Where was all this great stuff in high school?

      Almost by definition, the people who teach science are the people who (didn't want to go it|couldn't cut it) in research, thus making them ill equipped to teach the important things about research. Many of the good researchers I have talked to resented having to give lectures because lectures take away from research time (you may only see one hour of class, but it takes three or four hours to prepare, not counting marking homework). Feyman was a good researcher who was different enough to enjoy teaching too.

      Of course I should shut up now, since I'm not yet doing research, but I've researched doing research, if that makes sense. I have however noticed that the quality of a teacher can be enough to drive an entire class of people away from a subject, or to pack a lecture hall for a subject - even among third-year students.

      If it's ten lines that you want to re-use, but the second use is different enough that it doesn't make sense to build a common subroutine, go ahead.

      I can't think of a situation where this would happen. I suspect you mean that the in the second use the code is likely to diverge from the first over time, so factoring the ten lines in a subroutine would end up with lots of conditionals in the routing to deal with changing circumstances. In that case, definately don't factor.

      It has been pointed out that typeing use CGI is as much cargo cult as rolling your own, but that's not quite true. Using a module is like buying a manufactured product, like a plane. Rolling a quickie is like trying to make one from scratch is inherently dodgy, like trying to make a plane of of bamboo and get it flying. It worked for Daedelus, kinda, but it's not a good way to do it for production code.

      ____________________
      Jeremy
      I didn't believe in evil until I dated it.

        Sorry, but you just hit a pet peeve.

        I likewise have talked to many good researchers who hated to teach. And when I have sat in their classes I have found that their students quite rightly disliked being taught by them as much as they disliked teaching.

        Teaching well and doing quality research are different skills. Setting good researchers in front of classes does not result in good teaching. Making researchers teach classes without quality feedback while rewarding them almost entirely for their research results in dreadful teaching.

        One of my favorite books is about exactly this. It focusses on math education, and explains why the state of math education at the college level is so dreadful. (The problems there tend to trickle down, but that is another story.) And it isn't just a rant, it is accurate, and the more you know the more you can recognize that accuracy. I have seen mathematicians say, in all seriousness, "That book is my biography."

        And, as I just found out from Google, this book is available online! Why the Professor Can't Teach by Morris Kline. The next time you see someone suggesting that ability at research is a good qualification for teaching, I suggest handing them this link.

        (An incidental note. The author, Morris Kline, was both respected as a researcher and an educator.)

        jepri,

        You wrote:

        Almost by definition, the people who teach science are the people who (didn't want to go it|couldn't cut it) in research

        That is probably true as a generalization, but I can certainly think of exceptions. I, for instance, think of myself as a good researcher, and very much enjoy teaching. I would happily teach at the high school or college level, but the sad truth is I can't afford it. I have a family and gigantic student loans to pay back, so I can't give up my good job in industry (which I also enjoy) to take a huge pay cut to teach. :-/ It says something very sad about our education system that I am in this situation.

        Scott

      I use modules on CPAN, some I have a had a peek in the see how they work. But some I didn't so I don't know how they work...

      So follolwing your explantion I'm doing cargo cult?

      --
      My opinions may have changed,
      but not the fact that I am right

Re: The first cargo cults
by danger (Priest) on Jan 12, 2002 at 04:11 UTC
    On a tangent, but not entirely without Perl content ...

    I would just like to point out that most of todays usage of 'cargo cult' is based on an impoverished view of some of the practices found in the South Seas islands. That there were/are such cults isn't at issue --- but to think that they sprung up as a post-war belief system is to miss a great deal. Even as early as 1830, missionaries were subverting beliefs and rituals involving ancestors returning in a great ship bringing wealth and prosperity (a new age) --- ie, that converting to christianity would bring peace and vessels carrying abundances of goods (Gilson R.P.(1970) "Samoa 1830-1900" Univ Oxford Press). The fact that the missionary supply ships, as well as other ships of exploration, indeed had a wealth of goods from the West was almost certainly not lost on the people in question.

    There are other factors as well --- more elaborate cults (the kind caricatured in the modern metaphor) have been seen to be part and parcel of political unrest, anti-colonial movements and protest, etc. But I only wanted to bring forth the idea that the quicky version that Feynman uses (to good effect I might add, but see below) --- ie, that there was a sudden ritualistic copying of airstrips based entirely on a lack of understanding --- is not a version that tells us much of anything about the people or the cargo cults of the South Seas, nor in the many other places of the world where similar (but not specifically geared to wartime cargo planes and western "cargo") beliefs and movements also took place. It is easy to look with incredulity upon a practice such as "cargo cults" when they are stripped of their historical, religious, polical, and cultural contexts, and to pronounce such beliefs as simply "mistaken" or "misguided" or involving a fundamental lack of understanding.

    Why did I bring all this up? Well, it isn't like it isn't important to at least imagine that there is more to the story than what we've been told. But it continues. The cry of 'cargo cultism' is definitely a negative or derogatory term, and, using only Feynman's account, rightly so if justly applied. One example of where we hear this cry today, specifically relating to Perl (see, I'm not entirely off topic), can be found right here (regarding ref($proto) in new() methods.

    To be sure merlyn makes a case for avoiding this particular construct --- he also admits that other prominent members of the Perl community disagree. Indeed, Tom Christiansen would be one of them, and the standard documentation that comes with perl contains the perltoot manpage where he suggests using the very technique merlyn advises us against, and explains that it provides more flexibility to the constructor. I have no problem that such a disagreement exists --- but I do think the cry of "cargo cult" (in Feynman's sense) is quite misplaced and unfairly presupposes a great deal about those who might choose one way or the other. Sure, some might actually be cargo-culting it in one sense (that's how Tom C. does it, so that's how I'll do it even though I have no idea what it means or really does). Others, however, may perfectly well understand what it means and how it works but may not have considered merlyn's argument about why not to use it --- is that cargo culting? No. Others still may know both what it does and understand merlyn's disagreement, but choose to use it anyway. Perhaps merlyn was influenced by programming Smalltalk in 1980, but using that experience as a defense of his position regarding 'ref($proto)' may also be called 'cargo culting' in the same vacuous sense as his denouncement of 'ref($proto)' as cargo cult (after all, this is neither Smalltalk nor 1980, and we've been provided zero information about why *that* matters in the current debate) --- it ignores more information than it provides and tells us nothing about the people involved or their reasons for doing things in a given way.

    I am not saying that merlyn did not give reasons (beyond the Smalltalk reference), he did in the post referenced above, and TomC gives flexibility as a reason in perltoot. What I am saying is that using the technique may or may not be cargo-culting in exactly the same way that avoiding the technique (or using a clone() method) based on merlyn's post may or may not be cargo-culting --- it all depends on the understanding of the programmer in question, not the technique.

    To go a step further, there is a fundamental misalignment in how the term is used in programming circles --- a telling factor in Feynman's story is that the planes *failed to land*, yet the cultists continued the practice. In programming, the real problem isn't when I copy your technique (without understanding it) but can't get it to work --- though that certainly accounts for a number of "why won't this work" posts on various programming forums. No, the real problem in programming is when I copy it (without understanding it) and it *does* work (or appears to work), and thus it propogates not only in my code, but perhaps in other's code as well ... setting myself up for failures down the road that I won't be able to understand because I didn't understand the technique and why it (seemed to) work in the first place. That is, the real problem is the propogation of bad practices because they appear to work --- and in Feynman's cargo-cult science the problem is exactly the same ... the poor science propogates because it *appears* to offer explanatory power where it does not

    Thus, the term "cargo cult" itself may be said to have been 'cargo culted' as a meme that is semantically misaligned with the context from which it was taken. At any rate, I really wanted to simply bring home the point that with the current meme, an accusation of 'cargo cult' is not saying something negative about the code in question, but rather about the coder in question. And yet, we still often see "code" labelled as cargo culted --- and, sadly, there appears to be a tendency for the discourse to be ruder when thusly labelled code is exposed than when a simple lack of understanding is exposed through code that does not have such a label attached. What does this say of us I wonder?

      Excellent post. Just a couple of points.

      First of all for a longer discussion of the (de)merits of the ref($proto)||$proto construct, I would point to Re (tilly) 2: Paradigm Shift - Dual Use Constructors. (I particularly like chip's comments about cargo culting being appropriate at a larval stage of development.)

      Secondly I think the fact that the phrase fails to capture either the original usage or the historical situation it came out of is a natural part of how language evolves. Phrases come into use, and seldom do people remember the original circumstances, and seldom does a widely used phrase remain static in meaning. Indeed after time its original meaning becomes far less relevant than the agreed upon, shared meaning.

      For a random instance, we all know what it means to welsh on a bet. I have seen the phrase used, you will find it in the dictionary, etc. And almost never is it used with the intention of furthering a racial stereotype...because that isn't the meaning we continue to share.

        First off, good link to further discussion about the particular example I brought up.

        With regards to languages changing: Oh indeed, languages evolve and the meanings of phrases and terms change over time, often losing their connection to original meaning in the process. "Cargo cult" seems slightly different (perhaps only because it is so recent a term) in that it is expressly still used with direct reference to its origins (ie, when people ask what you mean by that term) --- its just that the meaning has been slightly skewed right from the get go.

        I don't really have a problem with the meaning we programmers give it (and that Feynman gives it), I understand what is trying to be said --- but I do think the slight skew does bring to light something interesting. For example, if I copy some poor form-processing code X, I have a "real" copy that works (or doesn't) exactly like the original. I am not missing some fundamental property of X that prevents my copy from working as the original. I may, most certainly, be lacking a full understanding of X and therefore not know why X is not a good way to do it or why it fails in some situations, but unlike the 'cargo cult', I have a working airstrip and the planes are landing (even if some mysteriously crash). Thus, I don't think we have a 'cargo cult' problem.

        The better model of the problem seems to me to be an evolutionary* one: X is a 'meme' that confers (some modicum of) success** on those that possess it. It is more like a local optima in the fitness landscape of 'things that do X', and thus it propogates. Unfortunately, it seems that many who arrive on the scene of this particular fitness landscape (forms processing in the present example) find this local optima before discovering better memes (XX for example), and this very fact helps to broaden this optima into an ever larger plateau (making it even more likely to be encountered first).

        Hopefully, things such as davorg's NMS archive will begin to erode early paths that lead to X with paths leading to XX --- that won't happen by virtue of XX being a better optima, it needs also to create paths that get positioned where intitial explorers of the landscape are likely to find them first (it can't simply be a bridge from X to XX because that won't stop the propogation of X), and those paths can't be too steep lest the explorers turn to easier pathways (we can't cop out and say that those who take the low road get what they deserve, because that only helps propogate the low road). On the other hand, I simply don't have a map of what the landscape looks like for many who just jump right in today. If I didn't know how to swim, I'd be looking for swimming lessons before venturing into the deep end ... but many today jump in first and then start looking for floatation devices. I admit, I don't understand that particular mindset, nor do I have a solution.

        * there isn't any "real" current scientific theory of 'memetic' evolution --- I am merely appealing to an intuitive sense of what such a theory might be like.

        ** ie: using Matt's FormMail Script numerous times just fine...
Re: The first cargo cults
by MungeMeister (Scribe) on Jan 11, 2002 at 21:30 UTC
    If you think about it, this relates to Perl and other programming languages perfectly. Why did the "cargo cult" think that making a runway with lights (fires) and sticks to direct there airplanes would make the airplanes arrive? Because they didn't have all of the information, and it wasn't easily available! The missing link was the radio to call the airplanes, which would have been locked and off-limits. C/C++/Java are very similar. You can get some really cool module/libraries, but you can't learn how they work because you don't get the source code.

    But Perl is different...

    Because of great communities like PerlMonks, not only are there repositories of code, but also excellent descriptions of how they work. And with the open nature of Perl, if you want to know how a module like CGI.pm works, take a look. The source code is there for you to read. Almost nothing is hidden! Try that with C, C++, or Java.

    One of the best things, IMHO, about perl is that it allows you to easily experiment. I use test.pl all the time now. I'm always testing snippets of code to make sure I understand what is going to happen before I stick it in a big script. It makes it easy to try new techniques and commands. This really reduces the amount of time I spend not only writing the overall script, but also debugging time.

    All of this combines to make Perl easy to pick up and start using, even if you don't understand all of the "why". Then, as you use it more, its easy to pick pieces of the language to learn more about and understand how things work. My own Perl experience has followed that course.

    In other words, the door to the radio room is open, and everyone is encouraged to talk...

    Brian
Re: The first cargo cults
by archen (Pilgrim) on Jan 11, 2002 at 23:53 UTC
    Well I'd venture to say that it's not ALWAYS a bad thing to cut and paste code (certainly not if you understand it and wrote it yourself).

    Okay, a confession: I cut and pasted a lot of code when I first started Perl (that includes typing what I found in a book). My first project (and the reason I learned Perl) was to make a guestbook MY way. My reference was the Black Book of Perl - informative, but horribly organized. The only code I used for reference was the Ultimate Bulletin Board (UBB), yes THAT one.

    About half of the code was my own, and about half was some sort of copy job which I vaguely understood. The guestbook worked, which was good enough for me.

    Months pass, and I tend to hack Perl on a daily basis. I decided to go back to the guestbook to add a feature. I was somewhat horrified what I found in there, but needless to say it was a mess. I endeavored to fix it, and in the end it's far better then the cut and paste patch job. It's also a good feeling when you surpass the things that taught you.

    So in a nutshell, sometimes 'cut and paste' makes a good crutch. Just keep in mind that in the long run, it will eventually hold you back - and it's certainly no substitute for learning the language.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-03-19 10:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found