6 ms·
return x >= ‘A’; Would be better than return x >= ASCII_A; surely. ASCII_A could be set incorrectly, or have a dumb type, and is more verbose anyway. By usin
by robotresearcher 9y ago
return x >= ‘A’;
Would be better than
return x >= ASCII_A;
surely. ASCII_A could be set incorrectly, or have a dumb type, and is more verbose anyway. By using the character directly, the code speaks its purpose.
- stevenwoo 9y agoThere's the theory that any hardcoded constant directly in code is bad idea. It may be used more than once, or used only once now, but in the future used more than once, or in the future the value may be changed and if it's used more than once, this is a source of issues.
- robotresearcher 9y agoI get that in general. It depends if the code is meant to inspect the character x on this machine right now, or really the ASCII character x. As an aside, if someone changes the constant value of ‘A’ now, the world will be broken for a while. (But my code would recompile correctly unchanged with the new standard header.)
- yongjik 9y agoI get that using hard-coded constant is a bad idea, but using ASCII_A instead of 'A' is about as sensible as using SIXTY_FOUR instead of 64. If A signifies something else, use that name; otherwise just use plain 'A': it already gives us as much information as needed, and has one less place where the programmer can screw up.
- vlovich123 9y agoIn this strawman example, perhaps. However, code is usually surrounded by other code. So you could have the 'A' in multiple places. By using an explicit identifier you are protecting yourself against typos (depending on the language, it could be a compile-time error or at worst a very clear runtime error instead of a logic error). The other benefit of ASCII_A is that you are signalling that you are doing ASCII comparisons as opposed to using 'A' as a placeholder for a special value of 65 & thus be confusing the reader (e.g. some spec says 65 is some kind of magic value). Finally, by having an ASCII_A it provides you with the opportunity to add documentation explaining why this constant is the way it is (why not 'B'). The benefits scale with the number of instances (e.g. if that specific 'A' appears multiple times in a file, you wouldn't be able to document it in 1 spot). Of course, all of this is likely overkill for your specific example. If I'm writing a to_hex routine, I'm not going to extract those constants as the context & commonplaceness of the algorithm makes it redundant. For the same reason that one might write i++ in a for loop instead of i += ONE. However, extracting inline constants to named variables is frequently something I look out for in code review, especially the more frequently the same constant appears in multiple places, the more difficulty a reader might have trying to understand why that value is the way it is (or if there's any discussion at all), or if it's a value that will potentially change over time. The negative drawbacks of extracting constants is typically minimal & with modern-day refactoring it's a very small ask of the contributor.
- robotresearcher 9y agoSure, I understand. The surrounding code would include the type of x, which, if char, would help understanding even more. But you’re channeling some crazy madness suggesting that someone would use ‘A’ to mean 65. Shudder. I guess we’ve all seen some horrors over the years.
- coldtea 9y ago>But you’re channeling some crazy madness suggesting that someone would use ‘A’ to mean 65. Or just an encoding scheme.
- robotresearcher 9y agoWhere 65 means ‘A’? Madness.
- vageli 9y agoA is 65 in ASCII. http://www.asciitable.com http://www.asciitable.com
- robotresearcher 9y agoI know. It was a joke.
- vlovich123 9y agoYou could have a binary file format with a header of ABBA. You could choose to check the signature by doing an integer comparison of 65666665, 0x41424241 or "ABBA". Like I said, ASCII_A is a bit silly, but the maintenance value of extracting constant literals to constant variables with an explicit name & documentation explaining where the constant comes from is pretty solid, at least in my experience.
- sheepmullet 9y ago> The negative drawbacks of extracting constants is typically minimal > ASCII_A It comes down to naming and purpose. The example, ASCII_A, is terrible because it doesn't describe the purpose with its name. What will end up happening in any large codebase is ASCII_A will get reused in dozens of different places for dozens of different reasons. If it was named minValidLetterForAlgorithmX it would convey intent and its more likely to be used correctly.
- coldtea 9y ago>ASCII_A could be set incorrectly, or have a dumb type, and is more verbose anyway. By using the character directly, the code speaks its purpose. I disagree. ASCII_A speaks it's purpose (we purposefully want an ASCII A stored here). And one can check the constant's definition, and immediately tell if it's correct. E.g. const ASCII_A = 'A' // correct const ASCII_A = 'E' // wrong So: return x >= ASCII_A tell us the intention of the code's author. Whereas: return x >= ‘A’; only tells us what the code does, which might nor might not be correct (and we have no way of knowing, without some other documentation). So, by those two lines: const ASCII_A = 'E'; (...) return x >= ASCII_A; We know what the code is meant to do, AND that it does it wrongly (and thus, we know what to fix). These line, on the other hand: return x >= ‘A’; tells us nothing. Should it be 'A'? Should it be something else? We don't know.
- phkahler 9y agoreturn x >= "A"; // ascii A Gets the whole message across in one line, as does using 65 with the comment.
- mannykannot 9y agoBe careful with your quotes (depending on which pseudo-language this is.)
- yesenadam 9y agoComments aren't so good - now you have 2 things to change when the program changes. In the real world, often the comment won't be updated and will become actively misleading/wrong/bad.
- robotresearcher 9y ago(Ignoring the typo "A" != 'A') return x >= 'A'; already and only means ascii A. Is there a C compiler anywhere where or likely in future where 'A' in C is NOT ascii A? The comment is redundant if correct, and could be wrong after an edit, so it has no value.
- 9y ago
- buckminster 9y agoHe says that in the article: > ASCII_A (usually spelled just 'A') Of course, they are not the same thing. In the last 6 months I've worked on a very old system that uses not-quite-ASCII. 'A' was 65 but '#' wasn't 35.
- pkamb 9y agohttps://stackoverflow.com/questions/3202629/where-can-i-find-a-list-of-mac-virtual-key-codes/16125341#16125341 https://stackoverflow.com/questions/3202629/where-can-i-find...
- deleted 9y ago[deleted]
- deleted 9y ago[deleted]