Timeline for Are single-character constants better than literals?
Current License: CC BY-SA 3.0
38 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Aug 15, 2016 at 0:04 | review | Close votes | |||
| Aug 19, 2016 at 3:01 | |||||
| Jul 16, 2016 at 5:24 | history | tweeted | twitter.com/StackProgrammer/status/754184979656085504 | ||
| S Jul 13, 2016 at 9:02 | history | suggested | Bilesh Ganguly | CC BY-SA 3.0 |
improved formatting
|
| Jul 13, 2016 at 6:26 | review | Suggested edits | |||
| S Jul 13, 2016 at 9:02 | |||||
| Jul 12, 2016 at 14:50 | answer | added | Peter Turner | timeline score: 0 | |
| Jul 11, 2016 at 14:17 | answer | added | candied_orange | timeline score: -1 | |
| Jul 11, 2016 at 13:27 | history | protected | gnat | ||
| Jul 11, 2016 at 12:21 | comment | added | Lyubomyr Shaydariv |
Would you probably have a special separator/joiner for city + CharacterClass.COMMA + state that's designed for the domain scope, say CITY_STATE_JOINER or similar? CharacterClass.COMMA=',' is nothing else than, let's say, .red { color: red; } in CSS -- bound to representation directly, and not to semantics.
|
|
| Jul 11, 2016 at 10:07 | answer | added | Paul G | timeline score: -4 | |
| Jul 10, 2016 at 22:40 | answer | added | Michał Kosmulski | timeline score: 0 | |
| Jul 9, 2016 at 17:27 | comment | added | Thorbjørn Ravn Andersen |
It may be easier to avoid overlooking mixing similar characters. . and , are very visually very similar as : and ; are. If it is very important to get this right, then spelling out the character may make it easier for future readers to be certain that the right characters are used.
|
|
| Jul 9, 2016 at 7:58 | comment | added | sara |
extracting an inlined literal to a constant provides and ABSTRACTION. you remove the explicit coupling to the inlined literal to an abstract field, whose value can change without you caring. now, this CAN be useful. but if you abstract away ',' to COMMA, then you haven't really abstracted away anything, because a comma is a comma is a comma. it's not an abstraction, it's just itself. you CAN gain value from this if what the code needs isn't a comma per se, but rather some delimiter for creating a CSV (which can use semicolons etc too), but even then it's shady. abstractions have a price.
|
|
| Jul 8, 2016 at 15:03 | comment | added | user7519 |
for those that argue readibility and explicit intent, with these single letter constants, the idiomatic way in every language would be their supported string formatting facility. for example in Java you would write String.format("(%s,%s)",lastName,firstName) and a static import makes it all that more succinct.
|
|
| Jul 7, 2016 at 19:07 | comment | added | meriton |
I still have nightmares about some code where all String literals were replaced by constants. He actually wrote stuff like StringConstants.LEFT_PARENTHESIS + lastName + StringConstants.COMMA + StringConstants.SPACE + firstName + StringConstants.RIGHT_PARENTHESIS rather than "(" + lastName + ", " + firstName + ")". I have never been as glad about the inline constant field refactoring as I was back then :-)
|
|
| Jul 7, 2016 at 17:18 | answer | added | user541686 | timeline score: 2 | |
| Jul 7, 2016 at 15:26 | comment | added | user22815 |
That article you linked is very terrible. Constants should never be thrown into a bucket like that. Put them on the classes where they have context: in essence, the class with the constant provides the context in which the constant is used. For example, Java's File.separator. The class tells you the type of separator. Having a class named Consts or Constants provides no context and makes constants harder to use correctly.
|
|
| Jul 7, 2016 at 11:23 | comment | added | the_lotus | In your case, it's like calling a variable "number". Your constant should've been called DELIMITER. Or it should be CITY_STATE = "{0}, {1}" | |
| Jul 7, 2016 at 9:56 | answer | added | Peter | timeline score: 22 | |
| Jul 7, 2016 at 6:00 | answer | added | Cort Ammon | timeline score: 2 | |
| Jul 7, 2016 at 1:26 | answer | added | Erik Eidt | timeline score: 4 | |
| Jul 6, 2016 at 22:28 | answer | added | Adrian McCarthy | timeline score: 29 | |
| Jul 6, 2016 at 21:57 | comment | added | Steve Cox |
I can't imagine that this would be the reason why, but you might want to define a constant of a literal character to manage the type, or be able to hand it to a function as a pointer (why you would want that i have no idea). In particular c and c++ define character constants differently, in c they are signed ints and in c++ they are signed chars. You might want to define unsigned char COMMA = ','; which is actually implicitly converting the comma constant to the type you want.
|
|
| S Jul 6, 2016 at 21:18 | history | suggested | Aaron Hall | CC BY-SA 3.0 |
remove tha thanks
|
| Jul 6, 2016 at 21:16 | comment | added | Martin Maat | It could make sense to do this if the type of the constants matters. Suppose your application produces ASCII files and you want it to do that also after you upgrade to a compiler that does unicode by default for text, this could be the way to go. Something similar could be the case for integer values, using 8, 16 or 32 bits. | |
| Jul 6, 2016 at 20:06 | answer | added | corsiKa | timeline score: 17 | |
| Jul 6, 2016 at 19:57 | comment | added | Aaron Hall | There are lots of different variants on commas: en.wikipedia.org/wiki/Comma#Comma_variants - perhaps this is not such a dumb idea, especially if your source code can be encoded as utf-8. | |
| Jul 6, 2016 at 19:50 | review | Suggested edits | |||
| S Jul 6, 2016 at 21:18 | |||||
| Jul 6, 2016 at 19:39 | answer | added | Eric Lippert | timeline score: 150 | |
| Jul 6, 2016 at 19:33 | answer | added | Pascal | timeline score: 3 | |
| Jul 6, 2016 at 19:28 | comment | added | Justin Time - Reinstate Monica |
Hmm... it might be useful for different locales, maybe? For example, some languages use guillements (angle quotes, « and ») as quotation marks instead of English's standard " (or nicer-looking “ and ”). Apart from that, it just sounds like a set of magic characters. Assuming two instances of CharacterClass called englishChars and frenchChars, it's possible that englishChars.LEFT_QUOTE might be “, while frenchChars.LEFT_QUOTE might be «.
|
|
| Jul 6, 2016 at 18:29 | vote | accept | Austin Day | ||
| Jul 6, 2016 at 17:53 | answer | added | Justin Cave | timeline score: 0 | |
| Jul 6, 2016 at 17:53 | answer | added | user7519 | timeline score: 181 | |
| Jul 6, 2016 at 17:48 | comment | added | Blrfl | Very related: programmers.stackexchange.com/questions/221034/… | |
| Jul 6, 2016 at 17:36 | review | Close votes | |||
| Jul 16, 2016 at 3:04 | |||||
| Jul 6, 2016 at 17:25 | answer | added | Telastyn | timeline score: 62 | |
| Jul 6, 2016 at 17:18 | review | First posts | |||
| Jul 12, 2016 at 13:52 | |||||
| Jul 6, 2016 at 17:16 | history | asked | Austin Day | CC BY-SA 3.0 |