- Recent
- Popular
- Tags (10)
- Subscribers (79)
- CodeSOD: The Long Way toUpperYesterday
-
Have you ever found yourself writing a function to do something that seems pretty simple, then months or years later you find out there's a built-in function to accomplish exactly what you were doing? Maybe you didn't know where to look or the built-in function's name was confusing. I'd argue that Java's toUpperCase() does not fall in this category.
Somehow I imagine that if you were to reverse engineer the built-in method, it wouldn't look like this implementation (submitted anonymously):
private static String upperCaseIt(String Account) { String result = ""; int i = 0; while (Account.length() > i) { if (Account.substring(i,i+1).equals("a")) { result = result + "A";} else { if (Account.substring(i,i+1).equals("b")) { result = result + "B";} else { if (Account.substring(i,i+1).equals("c")) { result = result + "C";} else { if (Account.substring(i,i+1).equals("d")) { result = result + "D";} else { if (Account.substring(i,i+1).equals("e")) { result = result + "E";} else { if (Account.substring(i,i+1).equals("f")) { result = result + "F";} else { /* I'm sure you get the idea. */ } } } } } } i++; } return result; }
Brought to you by the Non-WTF Job Board - CodeSOD: nice_num, mean_programmerNovember 28
-
When Timothy stumbled across a function that had a vague name, no comments, and variables, he took a few minutes to try to break it down in his head.
static char *nice_num(long n) { int neg = 0, d = 3; char *buffer = prtbuf; int bufsize = 20; if (n < 0) { neg = 1; n = -n; } buffer += bufsize; *--buffer = '\0'; do { *--buffer = '0' + (n % 10); n /= 10; if (--d == 0) { d = 3; *--buffer = ','; } } while (n); if (*buffer == ',') ++buffer; if (neg) *--buffer = '-'; return buffer; }Got that? Yeah, it takes a few minutes to figure out. The function that this, er, function is trying to accomplish is to add commas at the thousands, millions, etc. places.
It works backwards, starting with the string terminator, then setting the one, ten, hundred, etc. digits using goofy modulus math. The function divides by ten with each iteration (rather than 1,000 for some reason), stopping at every third to stick a comma in. And if it was negative to begin with, it mashes a minus sign back on to the string before returning it.
Personally, I would've called the function "nice_num,horrible_implementation", but I'm pretty sure commas aren't legal in C++ function names.
Brought to you by the Non-WTF Job Board:
- I Can't See What You're SayingNovember 26
-
"Why won't this stupid thing just... just... graagh!" The salesman clutched the edges of his massive keyboard tightly, his knucles white. While he looked angry, he wasn't actually angry; rather he was frustrated approaching angry.The year was 1984, and the PC was finally moving out of the "early adopter" range and companies started sending them out into the field. Ricky happened to be working at a help desk during this exciting time, aiding in the rollout of the shiny new PCs to various teams in the company. Unfortunately for him, one of the first teams that needed the PCs most desperately was also one of the departments least able to understand and use them – sales.
It's not that the sales team was stupid; it was just that they were now having to deal with a major change to the way they'd measure performance, record sales, and run reports. One salesman in particular, Jeff, struggled with it more than his other colleagues in the department. He became well known by much of the helpdesk staff – Ricky, in particular.
Jeff didn't really have much of a temper, but it didn't take much to frustrate him. When he encountered a problem, it was as though he'd encountered quicksand. He could save himself if he took a moment to assess the situation when he was just ankle deep, but instead would struggle and thrash around, burying hims
- Representative Line: Why IndeedNovember 26
-
Having just inherited a mammoth, ASP-based ecommerce application created in a developmestuction by a handful of different consultants over several years, Ryan Davis found himself asking one question, over and over: why?
Why didn't they use some off-the-shelf ecommerce site? Why aren't there any comments, anywhere?? Why did anyone let the original developer near a keyboard, let alone allow him to program!? Why did I even bother coming in today!?!?
After nearly reaching his very last nerve, Ryan noticed a file that came up in his search for "contact_id". It was simply named why.txt and, although it was a mere 114 bytes, he hoped it would contain the answer to all the questions he had asking. Upon opening the file, however, it left him with one more question...
/cart/checkout.asp:15 if not contact_id <> 0 then
Why indeed.
Brought to you by the Non-WTF Job Board:
- Tales from the Interview: The Mandatory Three, The Easy Road to Success, and Relevant InexperienceNovember 25
-
The Mandatory Three (from Jim)
After the dot com bust, I spent a lot of time interviewing. It was mostly dead ends or companies that were only willing to hire one person to do the job of four (specifically, the four that they had just laid off). A friend of mine who worked at a school, told me about an IT position there. Being out of work for so long, I was very eager to get in for an interview, and figured I might have an "in" since he was working there already.When I arrived at the interview, there were two other candidates waiting. I was to go in first, and I was informed that all three of the interviews would be done in "rapid fire" succession, and each would take just 30 minutes. I went into their conference room, and was a bit startled by the fifteen people sitting around the table. This was known as "The Gauntlet." This group of people only asked a few, fairly non-technical questions. Most seemed bored, and some occasionally looked at their watches and yawned! Now, I am not the most exciting person in the world, but I am certainly not that boring. I finished the interview and left feeling quite confused. What the hell just happened?
I got a call from my mom later in the day, and I told her about the interview. Being a teacher, she was familiar with what I had gone through and she expl
