4 ms·
I disagree strongly with the "no foo variables" rule. If you put meaning into a variable name that you use twice, then you are repeating yourself, which is ver
by enriquto 7y ago
I disagree strongly with the "no foo variables" rule. If you put meaning into a variable name that you use twice, then you are repeating yourself, which is very bad. Thus, variable names should NEVER be meaningful (except maybe when they are used only once). It is better to name your variable "x" with a clear comment of its meaning upon its declaration.
Moreover, mixed-case symbols suck big time.
- MobileVet 7y agoAre you familiar with the concept of ‘coding your documentation?’ Code often changes without the comments being updated so making the code as informative as possible has lasting benefits. It is much easier to use descriptive variables than keep the comments up to date. Additionally, descriptive variables allow anyone to read and more quickly understand the code.
- enriquto 7y ago> Are you familiar with the concept of ‘coding your documentation?’ Code often changes without the comments being updated so making the code as informative as possible has lasting benefits Yes, and I agree 100% with it. It is much better to write self-documenting code than comments. I just contend that descriptive variable names are not the way to self-documenting code. Of course, I'm talking about local variables whose scope should never go beyond a single screen of code. Globally accessible identifiers (e.g. functions) should of course have very long and descriptive names. > Additionally, descriptive variables allow anyone to read and more quickly understand the code. This does not ring true to me, but maybe it depends a lot on the context. At least in numeric mathematics, you always want to name your numbers, vectors and matrices using single letters (as they appear in the corresponding paper). Using "cute" descriptive names hinders a lot the readability of formulas.
- MobileVet 7y agoAh, I think the context of your programming and mine are quite different. I don’t do any mathematical formulations beyond basic manipulations. I can see where someone writing complex mathematical formulas in C would definitely benefit from single character variable names
- rswail 7y agoThe "x" in mathematics is completely different to an "x" in some business logic. Business logic tends to be concrete, not abstract. Mathematics tends to be abstract, not concrete. Yes, using x,y,a,b,c as names in the quadratic formula makes sense. But so does "isOverCreditLimit" and "MonthlyInsurancePremium" in business (leaving aside arguments about camelCase etc etc). Business logic needs to reflect the intent of the business. There is rarely the case for an "x" to "solve for". Deconstructing a mobile phone plan into code is not the same as deconstructing a math paper. Quantitative mathematics is a very different computing environment to business logic.
- zapzupnz 7y agoThis sounds fine if you're the only one using the code. In business, the practice you're describing is needlessly fragile. My opinion is that you shouldn't have a home style and a work style — you should just have correct style, something that works equally well in both scenarios. > If you put meaning into a variable name that you use twice, then you are repeating yourself, which is very bad. Thus, variable names should NEVER be meaningful (except maybe when they are used only once) I don't understand this sentence at all. My initial interpretation was that one should only use a variable no more than once which seems completely arbitrary. If a variable is only being used once, that's not a variable, that's a constant — but why are constants more deserving of a meaningful name than a variable, something that could (and should) be used multiple times? After all, if you're declaring a variable but not modifying it or only modifying it once, you should use a constant — but that still doesn't explain why constants are special enough to merit meaningful names. Further, you haven't narrowed down to when your assertion applies. What about variables in OOP as members of objects? Should mutating members always have nonsense names?
- rswail 7y agoI think this conversation is at cross purposes. In algebra, the statement: x = x + 1 is meaningless. Algebraic variables are not the same as what are effectively names for value storage locations, which is the common meaning of a "variable" in most imperative languages. Of course, with FP, the concept of a mutating variable is frowned upon in general. Mutation is effectively a performance hack.
- zapzupnz 7y agoI suspect you're right that GP was likely talking about a different context but I think that person failed to make that clear, therefore making it seem like their mantra for variable names was universally applicable. Clearly, it isn't.