Friday, November 18, 2011

'use strict' and cargo cult programming

I've just read  mjd's confession "Why I Hate strict", it is from 2003 so he might have changed his views now, but there is nothing that would indicate that on this web page.  Anyway, his main argument that the usual advice to use strict is automatic and mindless  and that it often does not really prevent the problems that people think it does.  In other words it is a cargo cult programming to which he contrast programming with thinking and deep analysis of everything you do.

I used to program without use strict; use warnings but after exposure to the usual propaganda I switched and I found that the cost of mindlessly adding it is negligible, the cases where I need no strict are very rare, and there are many benefits of doing it, especially when working with old code.  This cult is rather effective in luring the cargo planes to land in my atoll.  On the other hand I am all for deep analysis and checking your assumptions from time to time.  There are many valid points in Marc Lehman common::sense and I would like to see them discussed.  While we are on the road to have use strict by default we might also try to make it better.


Anonymous said...

I'd rather see people blindly "use strict" than blindly leave it out.

Of course it would be better if everyone using Perl would understand strict and a lot of other things, but we cannot expect that, can we? Most people just want to get some job done.

Nevertheless I think it is important that people understand what strict does and what kind of pitfalls it helps avoid.
That's why I started to explain that. so far 2 of the 3 parts were covered:
Symbolic references in Perl and Barewords in Perl

Chas. Owens said...

I think a better metaphor would be the natives seeing the army doctors washing their hands before treating the sick and thinking it was some form of ritual and adopting it. Sure it doesn't do what they think, but it is still good hygiene.

It does worry me how few of the people I interview know what strict actually does. The best I have to hope for is some sort of confused statement that it forces them to declare their variables (which is not true, you can fully qualify them and strict won't error).

Michael Campbell said...

Both comments above are fantastic.

While it would be nice to be able to do the same things mjd does, for the same reasons, one must remember "you are not mjd" (for various values of "you").

Anonymous said...

Michael: true, but one can strive to be more mjd-like :-) On which subject, I found his talk on how to progress (in the same set) quite inspiring.

In this case, he's quite right that it's bad practice to do something without at least attempting to understand the reason why, and it's even worse practice to pass on advice that you don't understand. I'm ashamed to confess that I didn't really know what "use strict" did before; now I do, and I'll continue to use it mainly because of strict vars.

Anonymous said...

use Modern::Perl; # instead :)