From: Martin Gregorie on 13 Mar 2010 17:20 On Sat, 13 Mar 2010 08:37:09 -0800, david.karr wrote: > should instead be: > > String foo = StringConstants.EMPTY_STRING; > Inventing something like a StringConstants class causes problems on just about every level. A constant is only meaningful within some context, so should be defined in a superclass or class that is logically part of the context and is named appropriately. The constant name should reflect is meaning within the context. > Assuming you conclude, as I do, that this is going too far, what > rational and logical arguments would you present to convince someone > that doing this is a mistake? > The person proposing this idea is probably an MBA or related organism and so won't understand logic or rational arguments. -- martin@ | Martin Gregorie gregorie. | Essex, UK org |
From: Arne Vajhøj on 13 Mar 2010 18:41 On 13-03-2010 11:37, david.karr wrote: > So let's say you're working with a developer who has accepted the > wisdom of defining constants for oft-used hardcoded strings. They > then decide that lines like this: > > String foo = ""; > > should instead be: > > String foo = StringConstants.EMPTY_STRING; > > Assuming you conclude, as I do, that this is going too far, what > rational and logical arguments would you present to convince someone > that doing this is a mistake? It does not provide any value. "" is readable and will never change value. This is just code clutter. One of the most important aspects of good rules is to know when they apply and when they do not apply. Note that: String foo = ""; is better than: String foo = StringConstants.EMPTY_STRING; but in some contexts it would be even better to use: String foo = FOO_DEFAULT; because "" wil never change value, but the default value of foo could change. Arne
From: Arne Vajhøj on 13 Mar 2010 18:44 On 13-03-2010 11:50, Mike Schilling wrote: > david.karr wrote: >> So let's say you're working with a developer who has accepted the >> wisdom of defining constants for oft-used hardcoded strings. They >> then decide that lines like this: >> >> String foo = ""; >> >> should instead be: >> >> String foo = StringConstants.EMPTY_STRING; >> >> Assuming you conclude, as I do, that this is going too far, what >> rational and logical arguments would you present to convince someone >> that doing this is a mistake? > > The point of defining a string constant is to > > 1. Give the string a meaingful name > 2. Make life easier for developers, who can refer to the constant rather > than type the string (especially useful with an IDE) > 3. Ensure the string doesn't get mistyped. > 4. Allow the string value, if it needs to change, to be changed in one > place. > > Inventing a constant that simply means "the empty string" accomplishes none > of these. > > 1. We already know that "" is the empty string. The name EMPTY_STRING adds > no new information > 2. "" is easier to type than the constant > 3. There's a very small change of mistyping "". > 4. here's no other possible value for StringConstants.EMPTY_STRING Very well explained. > However, if an empty string is being used for some specific purpose, e.g. to > mean "this value is unknown", it would make perfect sense to define > > public static final String UNKNOWN_VALUE = ""; > > 1. Anyone reading the program knows that the value is unknown as opposed to > being known to be empty > 2. Developers don't have to recall whether unknwn values are empty strings > or nulls > 3. More or less the same as 2 in this case > 4. If you later decide that "" is a valid value, UNKNOWN_VALUE can be > changed to, say, "*unknown*". Even though I completely agree with the point, then I am not sure that I like the example. Valid values having special semantics can create a lot of problems. Arne
From: Lew on 13 Mar 2010 19:09 Stefan Ram wrote: >> Actually, it holds /less/ information. We do not know its text, >> it could be defined as >> >> EMPTY_STRING := "EMPTY_STRING" >> >> or >> >> EMPTY_STRING := "empty" >> >> or so. Lothar Kimmeringer wrote: > That would be a new entry for the dailywtf. Best until now was > enum Bool = {True, False, FileNotFound} "This page intentionally left blank." -- Lew This brain intentionally left blank.
From: Andreas Leitgeb on 13 Mar 2010 21:32
Mike Schilling <mscottschilling(a)hotmail.com> wrote: > 3. Ensure the string doesn't get mistyped. I think to vaguely remember the existence of mistyped (w.r.t orthography of the corresponding natural language word) constants' names in Java's standard library. :-) It was mentioned in this group, some while back. |