1

I've gotten this idea in my head that i shouldn't use a libraries variable, if the variable name begins with '_', as it is standard to write private variables that way.

But now i'm thinking i'm taking that too literally. If my choices are to use "_var" that points to a HTML element or add a unique attribute to said element, so that i can query the DOM to find it, should i just use the variable?

Do other people avoid private variables and if so, would you say this is too much effort?

2
  • 1
    A habit is a recurrent, often unconscious pattern of behavior that is acquired through frequent repetition -- so in order to be a bad habit, you have to do it with frequent repetition. So, if you find yourself using other's private variables as matter of course and without conscious thought, then perhaps that is indeed a bad habit.
    – Erik Eidt
    Jul 25, 2022 at 16:05
  • 3
    The wording "bad habit" expresses a wrong assumption, that this might only be a cosmetic issue. I think a better wording would be to ask if this will introduce a serious drawback.
    – Doc Brown
    Jul 26, 2022 at 8:15

2 Answers 2

10

When I stick an _ at the start of a variable name I'm saying this is not part of the public API. I reserve the right to change this later.

If you're ok with me breaking your stuff with an update then fine. Go ahead. If you want stability then make sure the public API doesn't support what you're trying to do before you sneak in the back door.

And, as Thomas Junk points out, if the API doesn't support your needs, feel free to make some noise about it. You might be doing something I simply didn't think of.

3
  • 3
    Additionally - in case functionality is missing: Write a pull-request to enhance said API and let others profit too from the result. Jul 25, 2022 at 15:34
  • 4
    In case you didn't know: ECMAScript has "real" private members as of the ECMAScript 2022 release 6 weeks ago, and they have been supported in every browser and runtime except IE for significantly longer than that. These are private members deserving of their name whose encapsulation cannot be broken. In fact, unlike Java, I believe they are even safe from reflection. Jul 25, 2022 at 17:06
  • @JörgWMittag I'm not 100% convinced being 'safe from reflection' isn't a bug. The point is to show people where the front door is. Not to lock every window. Which is probably why people still use _. Jul 25, 2022 at 17:36
0

Personally I'd consider it a bad habit for two reasons: one, that it's good to be deliberate and verbose about what you need access to, so you're adding ids / attributes to elements when and where you need them, and two, libraries generally have a specific public API intended for users, whereas "private" variables ( not just "real" private variables ) are intended to be under-the-hood, subject-to-change without breaking the way the API is meant to behave.

Document.getElementById is so fast that I can't imagine ever reversing my above advice for performance reasons, even if you're writing JS for a toaster, although assigning the result to a variable is just good DRY if you're going to be doing much with the element after you've grabbed it.

TL;DR: Yes. :D

Not the answer you're looking for? Browse other questions tagged or ask your own question.