Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^5: Warnings on unused variables?

by Corion (Patriarch)
on Sep 27, 2008 at 21:23 UTC ( [id://714062]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Warnings on unused variables?
in thread Warnings on unused variables?

I just tested that #1 in fact does use $foo++, and warnings::unused handles it properly

You forgot about tied variables. $foo++ can be tied to code that prints out "Hello World" as a side effect.

I guess the short version of this is, I don't see any situation where using a declare-once-and-forget variable is ever clearer or substantially more efficient than an alternative, so I don't see any of these situations as warranting exceptions from warnings, even if they are in common use.

You haven't programmed much with resource acquisition/release systems then. C has lots of problems with that, Windows COM has lots of problems with that and Perls reference counting also has these problems (although many of them have been fixed in all instances). With Reference Counting as the instance of resource acquisition and release, you have exactly the problem that the points of acquisition (increase of refcount) and release (decrement of refcount) are not in the same place of the code and hence, quite unpleasantly, not every acquisition is matched with a release, because it's hard to see that they match up because they do not happen in the same place. Which is why a "execute this code at scope exit" functionality is wanted.

Replies are listed 'Best First'.
Re^6: Warnings on unused variables?
by AZed (Monk) on Sep 27, 2008 at 22:20 UTC

    That's the point of resource acquisition/release, sure. Why is it clearer to allow it to silently happen at the closing brace than to explicitly release on the line before the closing brace? The issue isn't whether or not you've used a destructor with a special ability — it's whether you've done so in a way that isn't clear, and thus should trigger a warning.

    Also, does a tie matter? Would a tie have caused $foo++ cause warnings::unused to throw a warning?

    (I posted this comment earlier, but it seems to have disappeared. Apologies if this makes it show up twice.)

      You are conflating the linear layout of your code with the flow of your program, which is not necessarily linear. If you can specify what should happen at scope exit, you don't need to specify that at the end of the scope, especially if the scope can be left through various exit points, like die for example. And it's even less a matter of where such things are specified and more a matter of that they're specified and executed at all, for all exit conditions. And hence, it's easier and IMO even clearer to specify what should happen at cleanup time (whenever that is) in the same place where the resource is acquired.

        I'm deliberately conflating linear layout of code with the flow of the program, because that's what makes it easier to read. Exit points other than } are typically easier to notice, and can conceptually link "special thing happens here that I should watch for" with that point in a programmer's head.

        I want a warning at the } if you have created an object that is going to do something special at that point and nothing has been done to make it visible. Call your destructor explicitly as a reminder, unless doing so would be even worse (i.e. you built a dozen of these objects in the same scope), in which case, put a single-line comment at the } and another at the line that turns off the warnings.

        But as I said before, this is a stylistic thing, and I imagine it's not really worth getting into a big scuffle over it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-20 06:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found