in reply to Re^2: On comments
in thread On comments
Sorry, but bad comments do not make up for bad code.
All of this:
The following variables are altered by C<DB::eval()> during its execut +ion. They are "stacked" via C<local()>, enabling recursive calls to C<DB::eval() +>. =over 4 =item C<@res> - used to capture output from actual C<eval>. =item C<$otrace> - saved value of C<$trace>. =item C<$osingle> - saved value of C<$single>. =item C<$od> - saved value of C<$^D>. =item C<$saved[0]> - saved value of C<$@>. =item $\ - for output of C<$@> if there is an evaluation error. =back =head3 The problem of lexicals The context of C<DB::eval()> presents us with some problems. Obviously +, we want to be 'sandboxed' away from the debugger's internals when we d +o the eval, but we need some way to control how punctuation variables an +d debugger globals are used. We can't use local, because the code inside C<DB::eval> can see locali +zed variables; and we can't use C<my> either for the same reason. The code in this routine compromises and uses C<my>. After this routine is over, we don't have user code executing in the d +ebugger's context, so we can use C<my> freely. =cut ############################################## Begin lexical danger zo +ne # 'my' variables used here could leak into (that is, be visible in) # the context that the code being evaluated is executing in. This mean +s that # the code could modify the debugger's variables. # # Fiddling with the debugger's context could be Bad. We insulate thing +s as # much as we can. sub eval { # 'my' would make it visible from user code # but so does local! --tchrist # Remember: this localizes @DB::res, not @main::res. local @res; { # Try to keep the user code from messing with us. Save these +so that # even if the eval'ed code changes them, we can put them back +again. # Needed because the user could refer directly to the debugger +'s # package globals (and any 'my' variables in this containing s +cope) # inside the eval(), and we want to try to stay safe. local $otrace = $trace; local $osingle = $single; local $od = $^D; # Untaint the incoming eval() argument. { ($evalarg) = $evalarg =~ /(.*)/s; } # $usercontext built in DB::DB near the comment # "set up the context for DB::eval ..." # Evaluate and save any results. @res = eval "$usercontext $evalarg;\n"; # '\n' for nice recur +sive debug # Restore those old values. $trace = $otrace; $single = $osingle; $^D = $od; }
Can be replaced by:
{ local( $trace, $single, $^D ); ($evalarg) = $evalarg =~ /(.*)/s; # Untaint # $usercontext built in DB::DB @res = eval "$usercontext $evalarg;\n"; # '\n' for nice recur +sive debug }
Not only is it shorter and clearer, it is safer. As tchrist points out in the above code:
# 'my' would make it visible from user code # but so does local! --tchrist
You've gone to great verbosity to explain how the variables: $otrace, $osingle, $od, are used to prevent the user code from messing with the debugger's internal state. Completely missing the fact that by storing copies in those localised globals, it is just as likely that the user code will mess with those variables as the originals. And if they do, the code will be restoring the messed with values over the top of the untouched original values.
Whereas if you simply localise the original globals, they'll be restored when the block exits regardless of what the user does. And the need for all the code-concealing and confusing comments just goes away.
I can see from what you've done to perl5db.pl that we'll never agree. That's fine. Some people like marmite, some don't.
But, the last four paras of your post above show me that you've either not read Programming *is* much more than "just writing code"., or you have and still think you can justify your position by concluding that those with the opposed viewpoint are either too lazy or too arrogant to comment. And that is just plain not the case.
I rarely comment because I've found over the years that the vast majority of comments tell me nothing more than the code does. But worse, as above, the comments attempt to persuade me stuff that is just flat out wrong.
Even your point that "Not every programmer is going to be as brilliant as every other." totally misreads that reasoning. For example, any programmer unfamiliar with local--which is a larger portion of modern Perl programmers--will, from reading your comments above, get entirely the wrong idea about what it does and how it works. And will go away with entirely the wrong impression because of those bad comments.
If however, there were no comments, they would have had to have gone to the documentation, and read up about the local keyword and learnt what it really does.
Like I said. Bad comment are worse than no comments. And most comments are bad. Even those by people who think they write good ones.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: On comments
by pemungkah (Priest) on Dec 29, 2010 at 04:20 UTC | |
by BrowserUk (Patriarch) on Dec 29, 2010 at 13:12 UTC |