Comment On SingleQautedString(), RemoveMeFunction(), Human-Friendly Dates, and More

"Following the code-as-a-commodity principle," writes Jan, "our management team not only sends large projects overseas, but goes with the lowest bidder." [expand full text]
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Re: SingleQautedString(), RemoveMeFunction(), Human-Friendly Dates, and More

2011-09-27 19:43 • by Cpt. Repeatable (unregistered)
"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')."

// Get today's date, and also format it in a more human-friendly way
$date = date("Y-m-d");
$nicedate = preg_replace("/(....).(..).(..)/","$3/$2/$1",$date);

Re: SingleQautedString(), RemoveMeFunction(), Human-Friendly Dates, and More

2011-09-27 21:01 • by I. G. E. (unregistered)
361462 in reply to 361452
qbolec:
As to:

String[] quarters = new String[QUARTERS_IN_A_YEAR];

I think I know languages in which this creates :
a) a 3 character long string with trailing \0 character, making the length equal to 3
b) a 4 pseudo random characters which happen to lay in the uninitialized memory, making the length anyhting between 0 and .. Segfault
c) a 4 bytes array initialized with all zeros, making the length equal to 0


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.

P.S. in what language does it define a string of length 4?

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.

Re: SingleQautedString(), RemoveMeFunction(), Human-Friendly Dates, and More

2011-09-27 22:29 • by JustSomeGuy (unregistered)
Actually, something like:
if (GlobalConfig["UseSpellingError"])

pKey = pKey.replace("gb_custom", "gb_costum");
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 :-)

Re: SingleQautedString(), RemoveMeFunction(), Human-Friendly Dates, and More

2011-09-28 02:35 • by Eternal Density
361471 in reply to 361304
silent d:
dgvid:
D-Coder:
... Words are more explanatory than numbers in almost all cases.

A word is worth one-thousand numbers.


FTFY


#define NUMBER_OF_NUMBERS_THAT_A_WORD_IS_WORTH 1000
So the picture that's worth a thousand words must be a megapixel since each word is worth a thousand numbers.

Re: SingleQautedString(), RemoveMeFunction(), Human-Friendly Dates, and More

2011-09-28 04:45 • by Kef Schecter (unregistered)
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.

Re: SingleQautedString(), RemoveMeFunction(), Human-Friendly Dates, and More

2011-09-28 05:11 • by realmerlyn
361474 in reply to 361221
eVil:
A sinlge if could probably do the job, but surely you'd have to write some pretty inglorious code to achieve that.

As it stands, the code is readable and efficient, so I dont see what you'd gain by refactoring it?


You have a strange definition of "efficient". Remind me not to hire you.

Re: SingleQautedString(), RemoveMeFunction(), Human-Friendly Dates, and More

2011-09-28 09:10 • by eVil (unregistered)
361483 in reply to 361474
realmerlyn:
eVil:
A sinlge if could probably do the job, but surely you'd have to write some pretty inglorious code to achieve that.

As it stands, the code is readable and efficient, so I dont see what you'd gain by refactoring it?


You have a strange definition of "efficient". Remind me not to hire you.


May I also remind you to read the rest of the thread? All is explained.

Re: SingleQautedString(), RemoveMeFunction(), Human-Friendly Dates, and More

2011-09-28 14:08 • by NotJamieZawinski (unregistered)
"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')."

// Get today's date, and also format it in a more human-friendly way
$date = date("Y-m-d");
$nicedate = preg_replace("/(....).(..).(..)/","$3/$2/$1",$date);


“Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.” - Jamie Zawinski

Re: SingleQautedString(), RemoveMeFunction(), Human-Friendly Dates, and More

2011-09-29 04:39 • by gilgamash (unregistered)
I think the only problem here is, that it is not written as a recursive function...


for i=3000 to 3299
if cint(val)=cint(i) then
bolMatch=true
exit for
end if
next

for i=3351 to 3441
if cint(val)=cint(i) then
bolMatch=true
exit for
end if
next

for i=5964 to 5969
if cint(val)=cint(i) then
bolMatch=true
exit for
end if
next

Re: SingleQautedString(), RemoveMeFunction(), Human-Friendly Dates, and More

2011-09-29 13:25 • by Jay (unregistered)
361699 in reply to 361335
Mwah:
Constants to replace numbers for clarity mean the code is poorly commented. ... Things that are constant in the world (number of quarters in a whole, for example) are usually expressed more efficiently as numbers, with a clarifying comment as necessary....


I humbly beg to disagree.

Sure, instead of writing:


final static float PI=3.1415926;
...
if (angle>PI/4) ...


We could write


if (angle>0.78539815) // value is pi/4


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.

Re: SingleQautedString(), RemoveMeFunction(), Human-Friendly Dates, and More

2011-09-29 13:30 • by Jay (unregistered)
361701 in reply to 361319
Matt Westwood:

I shared an office with someone who suggested that the value of pi will change if they discover the universe isn't flat. I never was able to persuade him he was talking bollocks.


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.

Re: SingleQautedString(), RemoveMeFunction(), Human-Friendly Dates, and More

2011-10-01 19:36 • by Randy Snicker (unregistered)
361891 in reply to 361413
the beholder:
gallier2:
the beholder:
<snip>


Yeah, well you're a big poopyhead too.
That's so childish! Besides, I am rubber, you are glue... ;)

Oh, it's all right. I'm sure that we can handle this situation maturely, just like the responsible adults that we are. Isn't that right, Mr... Poopy Pants?

Re: SingleQautedString(), RemoveMeFunction(), Human-Friendly Dates, and More

2011-10-02 18:01 • by Paul (unregistered)
361901 in reply to 361221
Efficient!?!?? One If statement would use 3 comparisons, whereas this uses 394 comparisons....

Re: SingleQautedString(), RemoveMeFunction(), Human-Friendly Dates, and More

2011-10-04 16:52 • by ObiWayneKenobi
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.

Re: SingleQautedString(), RemoveMeFunction(), Human-Friendly Dates, and More

2012-03-02 06:09 • by photo shopper (unregistered)
375935 in reply to 361471
Eternal Density:
silent d:
dgvid:
D-Coder:
... Words are more explanatory than numbers in almost all cases.

A word is worth one-thousand numbers.


FTFY


#define NUMBER_OF_NUMBERS_THAT_A_WORD_IS_WORTH 1000
So the picture that's worth a thousand words must be a megapixel since each word is worth a thousand numbers.


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).

Re: SingleQautedString(), RemoveMeFunction(), Human-Friendly Dates, and More

2012-04-18 10:24 • by Rasmus (unregistered)
Perhaps it's just me, but I cannot see the "real error" quarter code above.


const QUARTERS_IN_A_YEAR = 4;

... several codefiles later...

private String[] getQuarters()
{
String[] quarters = new String[QUARTERS_IN_A_YEAR];
for (int k = 0; k < quarters.length; k++) {
quarters[k] = new Integer(k+1).toString();
}
return quarters;
}

To the best of my understanding, that would actually generate the following array:

quarters[0] = "1"
quarters[1] = "2"
quarters[2] = "3"
quarters[3] = "4"

Ofc. it depends on the language used

Re: SingleQautedString(), RemoveMeFunction(), Human-Friendly Dates, and More

2014-01-29 09:45 • by Axel (unregistered)
425943 in reply to 361335
Mwah:


int _timeout = getTimeout(configFile);
/* We have a range of errors that are returned as negative numbers, but we don't care here about them much, to be honest */
if(_timeout < 0) _timeout = DEAFULT_TIMEOUT;

This reminds me of http://www.ibiblio.org/harris/500milemail.html
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Add Comment