News for hackers. Stuff that matters.
Re:Sizeof(char) is 1 by arethuza on Monday September 06, 2010 @04:14AM (#1666228)
As with all rules like that they can be taken to DailyWTF levels of silliness.

I remember working on a Java codebase years ago where every string literal was defined in a Constants class - fair enough I guess. However, it had entries that looked like this:

   public static String HTTP = "http";

   public static String COLON = ":";

   public static String SLASH = "/";
leading to code that looked like:

   url = Constants.HTTP + Constants.COLON + Constants.SLASH + Constants.SLASH +
Re:Sizeof(char) is 1 by edanm on Monday September 06, 2010 @06:16AM (#1666385)
Yep, I've seen similar monstrosities often.

There's actually two problems with such code: one is that it doesn't actually make the code easier to read, but harder.

The other is more subtle: using constants like this is working at the wrong level of abstraction. The correct way to write this kind of code is to use a wrapper function which knows how to put together URLs, since there are lots of special rules to how URLs are put together (encoding, etc.) But a lot of people don't bother, and instead simply concatenate strings with a SLASH between them, and think that because they define the slash in some constant that makes the code correct.

Re:Sizeof(char) is 1 by tetha on Monday September 06, 2010 @07:12AM (#1666434)
I'd go even further with the issue. These constants do not offer _any_ abstraction or readability, and precisely this is the issue.

Naturally, I like constants like POPCORN_PRICE = 12 or whatever. They add meaning to meaningless numbers.

I am on the fence for HOUR_PER_DAY and MINUTE_PER_HOUR, because they never change, but they at least add a certain security if you chain them together. bar = lengthInSeconds * SECONDS_IN_MINUTE * MINUTES_IN_HOUR * DAYS_IN_YEAR. In this case you can simply match. Seconds - Seconds, fits. Minute - Minutes, fits. Hour - days... wait, doesn't fit.

However, if you look at PROTOCOL + COLON + SLASH + SLASH versus "http://, there is just nothing added.

Re:Sizeof(char) is 1 by masklinn on Monday September 06, 2010 @07:26AM (#1666448)
> I am on the fence for HOUR_PER_DAY and MINUTE_PER_HOUR, because they never change, but they at least add a certain security if you chain them together. bar = lengthInSeconds * SECONDS_IN_MINUTE * MINUTES_IN_HOUR * DAYS_IN_YEAR.

Ideally, especially if you're using a strongly typed language, you should just use time-unit types (or at least macros or inlinable functions) for that: `fromSeconds(lengthInSeconds).toYears()`, lengthInSeconds.seconds.to_year, `SECONDS_TO_YEARS(lengthInSeconds)`, new TimeSpan(seconds=lengthInSeconds).toYears(), etc...)

Re:Sizeof(char) is 1 by arethuza on Monday September 06, 2010 @06:38AM (#1666407)
I have to admit that I would just make that line:

   url = "http://" + ....
:-)
Re:Sizeof(char) is 1 by fauigerzigerk on Monday September 06, 2010 @07:39AM (#1666457)
The only reason to do that is if you want the compiler to check for typos: HTPP is caught, "htpp" isn't. It makes no sense for single letter constants of course.
Re:Sizeof(char) is 1 by riffraff on Monday September 06, 2010 @08:32AM (#1666518)
and to avoid silly warnings from some static analysis software (e.g. findbugs telling you you are repeating a hardcoded string N times and you could use a constant)
Re:Sizeof(char) is 1 by arethuza on Monday September 06, 2010 @08:05AM (#1666489)
Isn't that what unit testing is for?
Re:Sizeof(char) is 1 by loewenskind on Monday September 06, 2010 @09:02AM (#1666557)
Do all your projects get 100% branch coverage? If not you'll probably want the compiler to help you where ever it can.