Not Valid Enough
by in CodeSOD on 2008-02-26Sandy Barnabas received a rather odd message when submitting a registration form:
Code Snippet Of the Day (CodeSOD) features interesting and usually incorrect code snippets taken from actual production code in a commercial and/or open source software projects.
Sandy Barnabas received a rather odd message when submitting a registration form:
I'm always amazed at the lengths some people will go to 'enterprise up' there Java apps," Ben writes, "especially those daft Yes/No enums that we've seen here lately."
"Now, I've always been proud that I am a practical common-sense programmer, so imagine my surprise when I found myself writing this joy of over-engineering ...
At David’s company, management wasn’t a big fan of the whole check-in/re-compile/re-deploy process. They felt that it had too many steps and took far too long to complete. Their organization needed to adapt to software changes - especially bug fixes - much faster. So, to address this problem, they thoroughly analyzed the development process and carefully tweaked it for efficiency. I’m just kidding. Actually, their solution was to pioneer a new methodology called “SQL Sentences”.
Management figured that, since a large portion of their application dealt with saving and retrieving data from the database, then a lot of the bugs must be a result of bad SQL queries. Likely, it was things like a LEFT JOIN instead of an INNER JOIN, or a > instead of a >=. And, since their application already used a database, they figured - you know where this is going - why not store all of the SQL queries in the database?
"In C," writes Keith Lucas, "literal floating point number are interpreted as double precision floating point numbers. For example, if you have '0.0' in your code, the compiler will determine the type of '0.0' to be 'double'. In some compilers, such as the Visual C++ 6 compiler that we use, if a literal 'double' is used for a function with a 'float' parameter, a compiler warning is issued because the literal could be less accurate."
Keith continued, "My former coworker thought of an ingenious way to fix this problem. He defined a constant with a manual cast for double he intended to use..."
In the world of .NET, the Right Way to include newlines in a string is with the Environment.NewLine constant. Of course, given how easy "\n" is, few developers actually follow this rule.
// The Right Way myString = "Line1" + Environment.NewLine + "Line2" + Environment.NewLine + "Line3"; // The Way Everyone Does It myString = "Line1\nLine2\nLine3";
"When I went to dispute my credit information," Raj K wrote, "I noticed that the company's online form required a complex password. The developer in me was curious as to how the validation was implemented, so I viewed the page source. Shortly thereafter, my palm quickly met my forehead."
Following is a summary of the 700-line function used to validate the form's three different fields...
"You'd think that the C++ Boolean would be a welcome addition to the language," writes Jake E. "Not so much for our outsourcing company. This is what's now in our constants.h file."
#define TRUE 1 #define FALSE 0 ...snip... #define SUCCESS 0 #define FAILURE 1 ...snip... #define SUCCESSFUL 0 #define FAILURE 1 ...snip... #define OPER_SUCCESSFUL "SUCSESS"
"Where I work we keep a lot of data stored in XML files," Ben writes. "They're not your average XML files, though — they're special." His colleague invented the following technique (recommended for senior level XML programmers only).
<rootNode> <numberOfAddresses>110</numberOfAddresses> <address_1>442 Fake St.</address_1> <address_2>61 Main St.</address_2> ... <address_110>3881 N 4th Ave. #5D</address_110> </rootNode>
For some tasks, you're presented an opportunity to do things the easy way or the hard way. When your friend is proudly showing off his work at replacing a light fixture, the switch, and all the wiring, you might wonder why not just replace the dead bulb?
"I was just fixing a bug in our software, and I came across this function," Johnny A. writes. "It works perfectly — does exactly what it says on the tin. I can't help thinking it could have been done with a few less lines of code, however. One, for example."
"At manufacturing companies," Brent Railey wrote, "all you ever hear about is SAFETY, SAFETY, SAFETY. In fact, that was exactly the type of application I was tasked with reviewing: an 'Activity Based Safety Program' application for the corporate offices. It was a Classic ASP application that was 'slightly customized' from another application."
"As I was testing out the application, I noticed a little AJAXy behavior. When I selected one category, it would populate another section with the list of items in that category. Not bad for ASP, I figured. That is, until I looked at the code."