I tend to use it whenever I am working on a prototype script, and:
- Use a somewhat common variable (such as
fileCount), and - Have a large method (20+ lines), and
- Do not use classes or namespaces yet.
In this situation, in order to avoid potential variable clash, I delete the bugger as soon as I am done with it. I know, in a production code I should avoid 1., 2., and 3., but going from a prototype that works to a completely polished class is time consuming. Sometimes I might want to settle for a sub-optimal, quick refactoring job. In that case I find keeping the del statements handy. Am I developing an unnecessary, bad habit? Is del totally avoidable? When would it be a good thing?
I don’t think that
delby itself is a code smell.Reusing a variable name in the same namespace is definitely a code smell as is not using classes and other namespaces where appropriate. So using
delto facilitate that sort of thing is a code smell.The only really appropriate use of
delthat I can think of off the top of my head is breaking cyclic references which are often a code smell as well (and often times, this isn’t even necessary). Remember, alldeldoes is delete the reference to the object and not the object itself. That will be taken care of by either reference counting or garbage collecting.You can see that the list is kept alive after the
delstatement becausebstill holds a reference to it.So, while
delisn’t really a code smell, it can be associated with things that are.