![]() |
|
Syntactic Confectionery Delight | |
PerlMonks |
to strict or not to strictby castaway (Parson) |
on Oct 16, 2003 at 20:49 UTC ( #299878=perlmeditation: print w/replies, xml ) | Need Help?? |
YASD (Yet Another Strict Discussion)
... But this one has already been had, I thought I'd share it with the rest of you.. Enjoy (or not :)
2003-10-16 09:58:26 <castaway> demerphq, thats all your fault.. now theres a 'use strict' in the patch..
2003-10-16 10:01:21 <demerphq> Heh. Thatll learn ya! 2003-10-16 10:01:24 <broquaint> castaway: the 'use strict' shouldn't affect your code though, (lexically scoped pragmas)++ # no need for shotguns 2003-10-16 10:01:43 demerphq high-fives theorbtwo 2003-10-16 10:02:33 <castaway> what'd he do? 2003-10-16 10:03:29 bart thinks basically that theorbtwo is a strict fan 2003-10-16 10:03:34 <demerphq> I was kidding... 2003-10-16 10:05:04 <demerphq> I have to say that I find it marginally shocking that a woman of your intellignece doesnt use strictures castaway. You should post a meditiation on it. :-) 2003-10-16 10:06:29 <castaway> Im pretty sure I have at least one script/module with it in (more or less by accident) - I just find it more annoying than helpful.. *shrug* (and there are already some 'dont use strict' nodes :) 2003-10-16 10:07:05 castaway does a grep -r strict * .. 2003-10-16 10:07:12 <bart> I've not been using strict myself for years. Occasionally I stll do. "use strict 'refs';" was the first thing I did with it. 2003-10-16 10:07:54 <demerphq> feel free to point me at one of them. 2003-10-16 10:08:07 <demerphq> not using strict is IMO insane. Sorry. :-) 2003-10-16 10:08:24 <castaway> hmm, 1 by me, several that other people seem to have sneaked in.. 2003-10-16 10:09:02 <bart> The latter is useful, castaway. It prevent symbolic references and things like $foo{bar} = 'baz'; $foo{bar}{bleep}++; 2003-10-16 10:09:45 <liz> demerphq: I agree, not using strict is not very wise.. 2003-10-16 10:10:09 <demerphq> the amount of time that they save you in debugging far outweighs the time it takes to type it in. 2003-10-16 10:10:09 castaway religously used {'text'} and not {text} in hashes, and never uses symbolic thingummys anyways.. *shrug* 2003-10-16 10:10:37 castaway thought Ovid wrote one, trying to find it.. 2003-10-16 10:10:42 <castaway> wanna bet? :) 2003-10-16 10:10:51 <bart> castaway you seem to have overlooked the culprit. :]. 2003-10-16 10:11:14 <bart> $foo{'bar'} = 'baz'; $foo{'bar'}{'bleep'}++; still has it. 2003-10-16 10:11:24 virtualsue routinely uses strict for scripts longer than a few lines, but doesn't worry much about it 2003-10-16 10:11:50 <castaway> you're changing a scalar hash val to a hashref.. and? 2003-10-16 10:12:20 <bart> It's using "baz" as a symbolic reference. 2003-10-16 10:12:52 <demerphq> yeah, where he talks about TheDamian and whatshisface that wrote CGI. Have you looked at the code in CGI? Its not one to set any metrics. And thedamian is, well thedamian :-) 2003-10-16 10:13:01 ybiC wonders if the Webmistress who doesn't hesistate to use strict; will weigh in on this discussion. 2003-10-16 10:13:57 <bart> It sets %baz to ( 'bleep' => 1 ) 2003-10-16 10:14:16 castaway peers at bart 2003-10-16 10:14:18 castaway does a test.. 2003-10-16 10:15:59 <bart> Adding use strict 'refs' gives a runtime complaint: "Can't use string ("baz") as a HASH ref while "strict refs" in use at ..." 2003-10-16 10:16:24 <castaway> setting use strict in my code appears to just get me a heap of 'Global symbol needs explicit package name' .. nothin else 2003-10-16 10:17:05 <bart> Well... yes... but that's just use strict 'vars'; 2003-10-16 10:17:45 <demerphq> And thats a good thing castaway it catches typos and forces you to _think_ about the globals you are using. 2003-10-16 10:17:56 <bart> If you work with complex, variable depth data structures, like my example, stict 'refs' is extremeley useful. 2003-10-16 10:18:07 <castaway> yeah, but my code complies else, so why use it? :) 2003-10-16 10:18:40 castaway does think, doesnt need some pragma to do that for her ,) 2003-10-16 10:18:59 <bart> So my example wasn't convincing enough? Even though Perl found more errors than you did? 2003-10-16 10:19:36 <castaway> nope 2003-10-16 10:19:44 <demerphq> castaway I just said to broquaint elsewhere: "Im Lazy. I prefer the compiler to find my stupid errors for me. Ive got enough on my hands dealing with the _difficult_ errors. :-)" 2003-10-16 10:20:16 demerphq wonders when a competent perl programmer doesnt work with complex data structures. 2003-10-16 10:20:53 <liz> castaway: I find that by using strict, I don't have to think about misspelt variable names anymore, freeing my mind for other, more fun stuff 2003-10-16 10:21:13 <demerphq> liz++ 2003-10-16 10:21:14 <castaway> lazy programmers, the lot of ya ,) 2003-10-16 10:21:28 <castaway> liz thats to me a good reason *not* to use it 2003-10-16 10:21:57 <Yendor> Lazy programmers are the best kind. 2003-10-16 10:21:58 <demerphq> castaway that comment makes no sense. 2003-10-16 10:22:19 <tilly> castaway: I didn't think that I needed strict.pm for many years as well. It annoyed me more than it helped. Then I had one too many programs crash from a typo in a variable in an if branch I hadn't tested, and saw the light. 2003-10-16 10:22:30 <castaway> in fact Im getting convinced that not using it forces me to code+debug better.. 2003-10-16 10:22:30 <bart> castaway If I hadn't said what my code snippet did, how long would it have taken you to figure it out by yourself? 2003-10-16 10:22:53 hardburn finds careful use of no strict 'refs' to be very useful 2003-10-16 10:23:02 <virtualsue> you can drive better when you drink, too, because you need to be more careful ;) 2003-10-16 10:23:17 <castaway> bart I dunno, gimme a code piece that misuses it, and I'll tell you.. 2003-10-16 10:23:47 tilly recognizes castaway making the same arguments that he used to... 2003-10-16 10:24:16 <demerphq> certainly hardburn I do the same. but turning it off for carefully considered localized blocks is entirely different than no using strict at all. 2003-10-16 10:24:39 <castaway> they're arguing.. *shrug* ,9 2003-10-16 10:24:48 liz agrees with hardburn as it puts a marker as to where strange things might happen 2003-10-16 10:25:44 liz also thinks you can bring a horse to a drinking pool, but you can't force it to drink... 2003-10-16 10:26:21 <liz> or should that have been "camel" instead of "horse" ;-) 2003-10-16 10:26:49 bart points castaway to the source of Exporter 2003-10-16 10:26:53 <castaway> Camels are even more obstinate :) 2003-10-16 10:27:24 <demerphq> tilly: the use of past tense suggests you have changed your position on the subject? 2003-10-16 10:27:44 <castaway> anyfish, I seem to program to strict without actually using it.. think whatever you like about that.. 2003-10-16 10:27:56 castaway hands demerphq a cbhistory 2003-10-16 10:28:34 bart points demerphq to cbhistory, while he's at it. 2003-10-16 10:29:13 <bart> castaway if that were true, you wouldn't object to it so much. 2003-10-16 10:30:07 <bart> If there's anything I hate about it, it is how syntax errors, like mismatched braces, produce a lot of complaints from strict. 2003-10-16 10:30:08 <demerphq> oh indeed. I did overlook that. 2003-10-16 10:30:34 <castaway> bart thats your opinion 2003-10-16 10:30:54 <castaway> 'more or less' to, then.. 2003-10-16 10:31:39 <virtualsue> ! 2003-10-16 10:31:50 <castaway> yummy, 3 pages of 'Global XYZ' in cbgateway, nothing else 2003-10-16 10:32:58 <demerphq> All i know is every time ive turned strict on in my code when ive allowed a one liner without it to grow without adding it, ive found errors. and same for other peoples code without it. 2003-10-16 10:33:10 <bart> IMO strict shoudl hold its complaints until a script compiles properly otherwise. 2003-10-16 10:33:22 <castaway> (and who said I objected, I just dont use it ,) 2003-10-16 10:33:33 <demerphq> If you look at strict as a sort of moral discouragement of globals its worthy on that level alone. 2003-10-16 10:34:03 <castaway> I like globals, they have their uses 2003-10-16 10:34:27 <liz> not to mention all of the errors found by Test::Harness forcing warnings on test-suites... ;-) 2003-10-16 10:34:43 <bart> That I disagree on. Globals are not bad. Especially not in modules, where you want to allow people to access it from their scripts. 2003-10-16 10:36:17 <castaway> depends on your definition of errors, demerphq .. :) 2003-10-16 10:36:30 <castaway> I wouldnt say strict just catches errors.. 2003-10-16 10:36:36 <demerphq> Globals are evil except when you can explain why they arent. Requireing them to be declared is like a little mental reminder:"do I really need a global for this?" 2003-10-16 10:36:36 <liz> bart: I disagree with that: globals need to be accessed with class methods in my book... 2003-10-16 10:37:37 <demerphq> liz class method interfaces dont allow localization easily. 2003-10-16 10:37:38 <bart> For example, those conversion hashes in HTML::Entities. Even though most code doesn't use it, you don't want to force people to rewrite the same code if they ever need direct access to them. 2003-10-16 10:37:41 <liz> so that you have the option of changing the underlying interface in the future 2003-10-16 10:38:25 <bart> liz that's OO only. procedural programming doesn't work that way. 2003-10-16 10:38:51 castaway thinks she'll filter this lot outta the log and post it as a node ,) (unless anyone objects..) 2003-10-16 10:39:16 <liz> demerphq: I assume you mean {local $Other::Module;:var; } automatically restoring? 2003-10-16 10:39:27 <demerphq> there is a time and a place for globals and dynamics. but if your design and code is clean they shouldnt come up often. 2003-10-16 10:39:32 broquaint expects royalties 2003-10-16 10:40:20 <bart> A module shouldn't expect to do everything for everybody. Sometimes its own building blocks are useful for other stuff as well. 2003-10-16 10:40:26 <tilly> demerphq: In many cases you can roll localization of anything with ReleaseAction. :-P 2003-10-16 10:41:05 <liz> bart: I guess I only do OO with modules then... that's true... 2003-10-16 10:41:08 <bart> Otherwise, you'd just be adding bloat for the common cases. 2003-10-16 10:41:27 <diotalevi> I'll echo bart. I wrote Stealing lexicals - best practice suggestions just because the module tried to keep something private it shouldn't have. I dunno - module's data might be always better off being global unless it can be explained why it should be private. 2003-10-16 10:41:31 <bart> (code bloat)-- 2003-10-16 10:42:07 tilly wonders if bart is familiar with the concept of "loose coupling"... 2003-10-16 10:44:30 <bart> "loose coupling"? In software? Eh.. no... I don't think so. 2003-10-16 10:46:49 <tilly> Loose coupling is the idea that different sections of code should not have too strong a web of interdependencies because those dependencies is what breaks. 2003-10-16 10:47:17 <castaway> there, now we're running strict (+ no 'vars') - happier? ,) 2003-10-16 10:48:11 <tilly> Going after someone else's global is an easy way to solve your immediate problem. But it erodes the overall maintainability of your code because you now have a hidden dependency. Over time that turns you into fragile spaghetti. 2003-10-16 10:48:12 <bart> I thought that was what you're referring to, tilly. However, some data structures should be considered so basic that you never evr need to change their basic structure. The hashes in HTML::Entites is a good example. 2003-10-16 10:48:28 <castaway> 'defined interfaces', tilly (between subs as well as modules..)' 2003-10-16 10:48:32 <demerphq> tilly that looks pretty cool. ill have to keep it mind next time I need something like that. I like it. imagine its use in a 3arg for loop. :-) 2003-10-16 10:49:35 <tilly> Let me read the source to HTML::Entities... 2003-10-16 10:50:32 <bart> That module optionally exports those hashes. So that goes even one step further. 2003-10-16 10:51:17 <tilly> That one is a pretty good example. But only because it is a published RFC, implemented in many different browsers as well as in HTML::Entities. 2003-10-16 10:52:08 <tilly> But examples which are that clear-cut are darned rare. 2003-10-16 10:52:20 <bart> Exactly. That's why I still prefer to use %HTML::Entities::char2entity 2003-10-16 10:53:06 <demerphq> tilly I borrow Data::Dumper quote() quite often. :-) 2003-10-16 10:54:07 <castaway> (hmm, no-one happier? shall I take 'em back out? :) ..... Make of it what you will :) - I'm still not convinced, anyone else? C.
Back to
Meditations
|
|