- 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
The funniest thing about this to me is that not only does it reinvent the wheel, it reinvents it as an oval.
I've had to write code to do this in a language that didn't have this feaure (or at least I didn't know what it was) and I would just add "000000" to the front and then substring the last 6 characters.
Admin
Cripes.
// not far from...
public int intValue(int i) {
return i;
}
Admin
Three words: W T F???
In Perl (non-production code!) I've used inelegant formatting constructs like this:
index="000000$i"
rtindex=$((${#index}-5))
nrtindex=${index:$rtindex}
But switch/case? Ack!
(Note I am not a particularly expert Perl programmer, and there's almost certainly a more elegant way to do that.)
Admin
Why not use an XSLT stylesheet to format this output?
Admin
You could also do...
while(strWhatever.GetLength()<6)
strWhatever.Insert('0');
Though your version might use fewer CPU cycles :)
Admin
Well, they sure did that. Adapted the hell out of it, I would say.
Admin
erm... that's not perl. You've got $s missing off the variables to the left of the equalss, no semicolons, $(...) is invalid, and I'm not even going to try to guess what ${index:$rtindex} is supposed to do.
Not trying to troll, that's just too alarmingly non-perl to not comment. But in terms of elegant solutions,
$str = sprintf '%05d', $i;
and
$str = '0' x (5 - length "$i") . $i;
both spring to my mind. And (take note all perl-is-ugly fundies) neithier of these are particularly ugly (unlike stevekj's non-perl there ;] ).
Admin
So the first one would be from CSC Australia, then?
http://au.country.csc.com/en/mcs/mcs43/index.shtml contains the incriminating quote, though thankfully no actual code.
Admin
I think it's P#.
Admin
inputEmployeeNo.PadLeft(6, "0"c) seems the easiest way to do it to me...
Admin
uh, because like the original post says, the .NET Framework has extensive built-in support for number formatting?
Admin
adapts leading-edge technology, best practices, and experience to help customers realise their business objectives
Looks awfully lot like what can be found on this page...
http://au.country.csc.com/en/mcs/mcs43/index.shtml
Admin
Any VB.NET people know if you can use that method? If so, that's a one-line replacement for WTF #1.
Admin
I would just leave the emp no as an int value and do
intValue.ToString("000000")
It may be that the conversion to string was only done for the length-check/padding anyway.
Admin
It's actually bash (UNIX shell), not Perl. And it does work, though it's still not a particularly good solution (bash has a printf command just like the C function, you know...)
Admin
[:S] Wow. That was depressing. WTF!
Admin
Actually there are 10 kinds of people in the world. Those who count binary and thouse who don't.
Admin
But if only you and dead people can read hex, how many people can read hex? [:D]
Admin
Not sure about CPU cycles but if String is immutable in the language in question, this creates a bunch of junk Objects. Never more than 6 but still.
Admin
Well, that's 1 (you) plus DEAD which is 57005 is decimal, so one can reasonably assume that about 57006 can read hex.
Admin
A better question would be...
If only you, me and dead people can read hex, how many people can read hex?
Admin
Those who understand binary and those who don't. ( 10 - 3 == 1 )
There are 11 kinds of people, those who understand string theory and those who don't.
Admin
This is the line I really like:
In what way is "inputEmployeeNo.Length" evaluating a userid (whatever that may be)? It's evaluating the length of the ID in digits, not the ID itself. Insane.
Incidentally: the Perl idiom I would use is sprintf("%04d",$n) rather than any other silliness. Why? Because it's good enough, it's fast enough, and it's obvious to anyone who's familiar with the C [sf]printf format, which is a large majority of programmers.
Oh, and regarding the kinds of people: my favourite version is:
But the original poster who brought it up was misquoting a subtly different (and considerably wittier) original:
No, it's not a typo; it's wit.
Admin
Just in case you were wondering, Example 2 is C#. sob
Admin
Check out this page http://au.country.csc.com/en/mcs/mcs46/index.shtml in Firefox (and perhaps other Non-IE browsers) to see how much on the leading edge they are!
Admin
Read The F_ing Manual!
Read The F_ing MSDN!
Read the F_ing Intellisense!
WTF, just read!!!
Admin
At least in the second example you get an empty string if you input the wrong month. Reminds me of something with 13 months in a fiscal year? Month 13? Oh, thats "" [:O]
Drak
Admin
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemstringclasspadlefttopic.asp
Admin
Sigh:
Or: -for those that really want an "if" statement:Admin
In the mid to late 80s I worked as an analyst programmer at a company developing applications for the shipping industry, specifically to be used on board ships.
One of the other developers was clue impaired and routinely did...
if this = that then do something else i = i
he insisted that strange things happened if he excluded an else clause and this was his way of doing a no-op.
The same guy wrote a screen clearing routine like this (it was basic)
print
print
print
... (20 more) ...
print
when asked about loop optimisation and if that was what he was doing, he didnt know what I was talking about.
so, you see, some things don't change.
Admin
In that month number format, forgetting for the moment the general stupidity, would it not be better to throw an exception on the default case?
Admin
"when asked about loop optimisation and if that was what he was doing, he didnt know what I was talking about."
Loop optimisation, hell. What about CLS?
Admin
Well yes, but we where developing on both
HP-150b (using MS CBASIC on MSDOS) and IBM PCs, the HPs did not support things like CLS as the
system was a NONIBMPC MSDOS system that was effectively a graphic terminal with a PC shoved in the bottom of the case, so the interface between the "PC" and the terminal was done by printing sets of ANSI command strings. :-(
But, even so, there WAS an ANSI sequence for clear screen. so, no excuses I spose.
Admin
10 print "{insert inverted heart symbol here}";
Usually the first line of any program I ever wrote on my VIC 20
Drak
Admin
Or perhaps more normal: PRINT"§" , where § is a reversed heart
Admin
argh, drak beat me to it! :)
Admin
For when you absolutely have to have your data your way!
Admin
I imagine he once got caught by the construct:
IF A=B THEN
IF C=D THEN
do something
ELSE
IF E=D THEN
do something else
Admin
Heh, I didn't notice it at first, for those wondering what the WTF is, just scroll down the page.
Admin
How about a bogosort version? That would be cool.
Admin
Hire a bunch of monkeys to smash the keyboard until the proper number of zeroes are padded to the string?
This looks like a job for Primate Programming Inc.
Admin
I made a comment a couple days back that we shouldn't blame noobs for not knowing the framework, but are there still people who program without google groups or msdn in .NET? I can't possibly disagree with that quote...
About that website though - any company with 4,000 employees is going to have tonnes of horrible programmers (let's include whoever made that website). Most of the ones that have been procedural programming for the last 10 years and just switching into oo now are too stubborn to learn anything new properly. Before you slag me for that you should know I worked for a company with 46,000 IT staff and half the programmers did daily WTF's - mostly VB6 and cobol programmers stepping over to .NET.
Admin
I know where you can find a stupider solution. On our HR website, you login with your employee id. They just have a blurb that says: "<label for="par0">Employee ID # use zero(s) BEFORE your ID# if it's under 100,000</label>:"
If you guess wrong on the number of digits they're thinking about, you get this: "Employee ID# not found. Please read directions carefully - check your Employee ID# entry; be sure to enter a total of 6 digits, using zero(s) as needed."
Why code it, when you can just force the user to fix it?
Admin
On your last day, hack in to the database and set the identity seed to 999999 - they won't be able to hire anyone else ;)
Admin
In terms of speed and memory consumption, both WTFs are probably better than the usual straight-forward solution.
The first example does the most simple possible action - append the string to a constant length string.
Probably there will be only one intermediate object, a StringBuilder used by the compiler, which cannot be avoided.
The second example always returns a constant string, so it doesn't even have to create a new object.
This are examples where ugly WTF-solutions probably perform better than anything else. Although I doubt anyone will ever notice the difference
Admin
Yes, this works in VB.NET as well as C# I would imagine. Its a method in the string class, so it should work in any managed language.
The real syntax would of course be
inputEmployeeNo = inputEmployeeNo.PadLeft(6, "0"c)
Admin
They have their site map / site outline at the bottom of every page .. WTF ???
Admin
Exactly. The second example is good code IMHO, its totally clear how it behaves, its not bug prone, its a few lines longer than obvious alternative, but so what, its much faster than the alternatives and although this wouldnt make a difference unless it was called very frequently, why the hell not just write a fast function in the first place.
Admin
I can read some hex too, that would mean deaf people can read hex
Admin
Well assuming they had the profiler numbers to back it up, there is probably a performance advantage here over the default implementations, especially the latter case. Particularly against the Java alternatives, which probably carried over to C#--they are probably an order of magnitude or two slower.
An array lookup would have been cleaner still, and we can probably assume there was no such excuse, but heck I just have to play devil's advocate sometimes...