I'm confused
After reflection, this feels like it doesn’t carry enough idea mass to deserve a blog essay, but it isn’t brief enough to qualify as a twitter-like micro-post.
I just stumbled across a parenthetical remark about
Why are local variables bad?, an article wherein I started to talk about why it is important to really understand the objective of refactoring code, not just the mechanical process. I then got to the title’s point, that
mutable local variables are bad, and why I feel that way.
Ocean
wrote:
Local variables aren’t bad they’re just confusing. A lot of times people use the word ‘bad’ when they really mean ‘confusing.’
This is interesting. When we are talking about computer programs, I suggest that confusing code is
always bad. Perhaps there are “degrees of bad,” and local variables are less bad than, say, dangling pointers. But overall, isn’t it bad to confuse?
I guess my personal mantra is that
programs must be written for people to read, and only incidentally for machines to execute (and here’s the entire text
online).
Code that is confusing is difficult to understand. I pointed out that mutable local variables make code difficult to refactor. But hark! The problem is not that it’s difficult to refactor: that’s a
symptom. The problem is that the logic is entangled and tightly coupled, such that it is difficult to decompose it into smaller blocks, to move pieces around, or to share common code.
Now there’s a kind of confusion that comes from unfamiliarity. If you grew up with Haskell, perhaps mutation will make your hair stand on end when you first see a BASIC program in all of its glory. But honestly, I have used mutable state since 1974, and it is not getting any easier.
There’s something intrinsically confusing about mutable local variables, and I suggest that this—in and of itself—is bad to the bone.