3 ms·
Unfortunately I don't think this is particularly complete advice, because it totally ignores the question of scope. Yes, variable names serve to disambiguate.
by crazygringo 7y ago
Unfortunately I don't think this is particularly complete advice, because it totally ignores the question of scope.
Yes, variable names serve to disambiguate. But at what level -- function? File? Component? Business process? There's no clear rule here. And are they disambiguating merely between other variables that have already been defined, or from the wider scope of potential variables that a programmer might likely otherwise accidentally confuse them with?
If I have 20 different functions that all deal internally with only a single "price" variable, it may be legitimately helpful in preventing bugs to explicitly name these "priceWithTax" and "priceWithoutTax", or "priceLocalCurrency" and "priceForeignCurrency", or "priceList" and "priceSold". Or even use "priceWithTax" even if I never once use or define a "priceWithoutTax", because I know from experience that people are going to make a mistake otherwise.
In the end we're all trying to balance brevity with descriptiveness that prevents someone from accidentally misinterpreting and therefore misusing the variable when calling or modifying later on.
Sometimes I agree that yes variable names seem too long... but then I realize it may well be worth the peace of mind knowing that it's reducing chances of mistakes and bugs.
- TheRealPomax 7y agoThe article is explicitly about object oriented code, which pretty much tells you exactly how scoping will work, and in which contexts you drop parts of a name because they're contextually obvious. If you have 20 different functions that all internally deal with `price`, then either you're not writing OO code, or each of those functions is in its own object type, providing a clear context on what your code is talking about.
- saurik 7y agoReaders of your code are not reading only a single class's code; when I hear "20 different functions that all internally deal with price" that doesn't mean the state is shared, but the linguistic context of the project is still shared. Just because in the real world we have conversations among small groups ignored people within rooms that encapsulate sound and information doesn't mean that within a large organization we don't find ourselves confused when someone says "price" without indicating what might or might not be included in that price. If you are using terminology inconsistently within the implementations of separate classes--or as the person you are responding to notes even if you aren't but the mistake is common--you could at least provide a glossary (if not use a longer name).
- crazygringo 7y ago> The article is explicitly about object oriented code You're wrong, it's absolutely not. It never once mentions being about objects, and the first half of examples don't involve objects at all. I don't know where you're getting that from. And even if it were, it has nothing to do with my point. Different objects could use different standards for a price variable. You claim that objects would provice a "clear context", but if it's not through variable names, then how? Just through comments around the variable names?
- mangamadaiyan 7y agoFrom TFA: "Obviously it’s an object. Everything is an object. That’s kind of what “object-oriented” means". So ... TFA is perhaps not explicitly, but _implicitly_ about object oriented code?
- xnzakg 7y agoWell, I would say that goes under the second point made in the article. It doesn't mention scopes directly though.