Monday 28 June 2021

Do as I say, not as I do


One of the more attentive people on the CFML Slack channel observed I had some unVARed variables in the last couple of my articles. And quite possibly in other ones I've written recently.

This was unintentional. I'm being caught out by Lucee's setting to not need to var local variables, which I have switched on in my dev environment. Also I'm still in the throes of porting my brain back from PHP which doesn't have such dumb-arsed notions about needing to be explicit about these things.

To be clear, my position on variable-scoping is as follows:

  • always constrain one's variables to the "nearest" scope. This would mean one ought always use function-local variables in one's functions.
  • If using any other scope: actively scope it. So if a function needs to set something in the variables scope, don't simply rely on not VARing it; explicitly refer to it as variables.myVar.
  • Only use explicit scoping on variables when it would otherwise be unclear which scope is being referenced. If you still to small simple functions, data encapsulation, and clean code, this generally means there's no need to scope stuff. It's just clutter.
  • If you see some code I've written that doesn't do this, presume it's by accident. It won't be the important thing about the code you're looking at, unless the topic under discussion is "let's look at variables scoping" or some such.

That last point is not meant to dismiss the observation our colleague made of my code: I mean I hastily went and fixed it! But just I'm gonna sometimes forget to dot my Ts and cross my Is&hellips; it's safe to assume that's just me being inattentive. For my day job, I take this stuff very seriously.

Please do point out when I mess stuff up! I'll fix it.

Cheers to all who actually read this shite, and pay that amount of attention to it. I really appreciate it :-)