I actually disagree - while sizeof(char) might always be 1, code isn't just to communicate to a compiler, it's to communicate to another developer . When I see "sizeof(char) * strlen(foo)", I know that they want to allocate 'n' characters and that this is a string buffer: even though as a dev I certainly could've figured out, making code read like you think makes it far faster for others to interpret the intent of th…
It bears repeating: "[P]rograms must be written for people to read, and only incidentally for machines to execute." -- Abelson & Sussman, Structure and Interpretation of Computer Programs [1], strangely misquoted by Paul Graham [2] [1] http://mitpress.mit.edu/sicp/full-text/sicp/book/node3.html [2] http://www.paulgraham.com/hp.html
Sizeof(char) is 1
41–50 of 55 posts
Re: Sizeof(char) is 1
#42Earlier quoted context omitted.
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 + Co…
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 URL…
url = "http://" + ....
:-)Re: Sizeof(char) is 1
#43Earlier quoted context omitted.
Which is actually pretty useful and readable, imho (if formatted correctly): return a ? b : c ? d : e ? f : g; Side note: this does not work in PHP, as the operator is left-associative instead of right-associative there. The above in PHP would be evaluated as: return ((a ? b : c) ? d : e) ? f : g; Which is almost never what you want. I can't even format this properly to convey intent.
The only sane thing is to use if statements, imho. No ninja cowboy engineer nonsense.
I would say it's a case-by-case statement. Well, okay, more specifically, I would say that the use of a single ternary if is a case-by-case statement.
Re: Sizeof(char) is 1
#44Earlier quoted context omitted.
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 + Co…
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 URL…
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
#45Earlier quoted context omitted.
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 URL…
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 * SECOND…
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
#46Earlier quoted context omitted.
It's also the main reason to use constants, even when magic numbers would do (e.g., use HOURS_IN_DAY instead of 24. It's not likely you'll ever have to change the number, but HOURS_IN_DAY is clearer for humans to parse). This also applies to the bit in the article about coercing return values, which I disagree with. Funnily enough, he mentions that K&R also recommend coercion, even though they have a small note that…
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 + Co…
Re: Sizeof(char) is 1
#47Earlier quoted context omitted.
Which is actually pretty useful and readable, imho (if formatted correctly): return a ? b : c ? d : e ? f : g; Side note: this does not work in PHP, as the operator is left-associative instead of right-associative there. The above in PHP would be evaluated as: return ((a ? b : c) ? d : e) ? f : g; Which is almost never what you want. I can't even format this properly to convey intent.
The only sane thing is to use if statements, imho. No ninja cowboy engineer nonsense.
Re: Sizeof(char) is 1
#48Earlier quoted context omitted.
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 + Co…
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
#49Re: Sizeof(char) is 1
#50Earlier quoted context omitted.
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 + Co…
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.