- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
"While looking through some old code, I found this snippet that creats a 'human friendly' date to display to the user," Adam Smith writes, "perhaps this is more efficient than date('d/m/Y')."
Admin
I wonder what those might be. Doesn't compile in C or C++ (unless there's obscene preprocessor abuse going on, in which case it could do anything), in C# and Java it creates a string array of length four. It needs C-ish syntax, so we can exclude Python, Haskell, O'Caml, Lisp, VB, Brainfuck, Malbolge, Befunge, ... I'm not sure whether Perl or PHP would accept it, I think they wouldn't. Javascript perhaps, but if it does, it would behave more like C# and Java.
Not in any commonly used. The language in the article seems to be Java, so it creates a String array of length four, as one would expect.Admin
Actually, something like:
has a valid use case. Specifically, when fixing problems in the field that are causing problems for one customer, the *worst* thing you can do is to introduce problems for all your other customers.So what you do is to add a configuration option to use the new behaviour. Anyone who doesn't care or who, for some reason, has come to rely on the "errant" behaviour, remains untouched. All those who complain (including the customer you're fixing the problem for) are told to add that particular configuration option.
Ideally, it's cleaned up in the next major release, but this almost never happens :-)
Admin
Admin
Funny... NoScript didn't like the URL of this article. Thought it was some kind of XSS attack. I have verified that, yes, it really is the URL that's the problem, since I created a dummy file on my own webserver with the same filename, no JS or anything at all in it, and it reported the same thing.
Admin
You have a strange definition of "efficient". Remind me not to hire you.
Admin
May I also remind you to read the rest of the thread? All is explained.
Admin
"While looking through some old code, I found this snippet that creats a 'human friendly' date to display to the user," Adam Smith writes, "perhaps this is more efficient than date('d/m/Y')."
“Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.” - Jamie Zawinski
Admin
I think the only problem here is, that it is not written as a recursive function...
Admin
I humbly beg to disagree.
Sure, instead of writing:
We could write
But why is (b) better than (a)?
In simple human psychology, once you define a constant, it's a lot easier to get programmers to use it, then to get them to add a comment. They have to write SOME value to get the code to work, and it's no more effort to type in the name of the constant than to type in the value. But writing a comment is always extra effort.
Also, using named constants enforces consistency. Suppose we find one comment that refers to "top speed" and another that refers to "maximum velocity". Are these the same thing or two different things? With a named constant, the programmer would have to use the exact same name. (I suppose he could create two named constants with the same value, but that would require extra effort and thus be less likely.)
I'm all for writing good documentation, and I don't buy the argument that "good code is self-documenting". But I don't see any reason to go out of our way to make the code LESS self-documenting and then make up for it with more comments or external documentation.
Admin
Hmm, considering that the value of pi was discovered in this universe, it's hard to see how any new knowledge about the nature of the universe could change the value of pi. Perhaps we could learn that there are other possible universes with a different value for pi, but that's not what your friend is saying at all.
Admin
Admin
Efficient!?!?? One If statement would use 3 comparisons, whereas this uses 394 comparisons....
Admin
Amazes me how downright stupid people are with renaming things. Unless you are coding in Notepad, modern IDEs have a "Refactor" feature that will rename all references to a particular variable/method/file/class. There should NEVER be a reason to keep a misspelled or incorrectly-named piece of code around other than developer ignorance.
Admin
Well, considering todays smartphones and stuff, you might rewrite that bit to something like this (warning: assuming stuff about preprocessors, don't know exactly what they do):
#ifdef IPHONE_3 #define NUMBER_OF_NUMBERS_THAT_A_WORD_IS_WORTH 3000 #elseif IPHONE_4 #define NUMBER_OF_NUMBERS_THAT_A_WORD_IS_WORTH 8000 ... #endif
.. or something similar (numbers are aproximate too, I'm to lazy to google the exact numbers of megapixels in the cameras).
Admin
Perhaps it's just me, but I cannot see the "real error" quarter code above.
To the best of my understanding, that would actually generate the following array:
Ofc. it depends on the language used
Admin