3 ms·
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 har
by edanm 16y ago
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.
- arethuza 16y agoI have to admit that I would just make that line: url = "http://" + .... :-)
- tetha 16y agoI'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:// http://, there is just nothing added.
- masklinn 16y ago> 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...)