I read The Daily WTF. I read Jeff Atwood’s Coding Horror. Tonight I read IBM’s sample document for How to use a DB2DataAdapter with the DB2 .NET Data Provider. All three are essentially the same thing. Repositories for coding cluster-fucks. The difference between IBM and the other two is that the code examples at the IBM site are presented as samples of how to write code.
I found this sample for C# code while I was poking around. I never expect sample code to be extremely tight, but there is this one class that I have to show you.
// Helper method: This method establishes a connection to a database
So the first thing that I was drawn to was the use of a string array to pass in the values used to build the connection string. Now I’m no expert, but isn’t the use of overloaded functions the better way to do this? If that won’t work for you, it’s also possible to use a function with defaulted parameters. Every one of the cases in the switch statement defaults the values exactly the same if they aren’t provided which would make either of those three options viable.
You could argue that the example is using the .NET 2.0 parameter array feature, but the .NET Native Provider, as of the time this was written, does not support .NET 2.0 so why would they show it. If that is what they are trying to do then they should at least make a statement to that fact in the sample code. Heck the code doesn’t even say what version of the .NET Framework it is written to.
This type of shit pisses me off. Some straight out of school newbie who, through no fault of their own, reads this sample on the web and now they think that this is the way the world should work. Then they show up on my project and I have to unlearn them, which usually happens only after you find code like this in a code review, and then point them down the road know to be straight and narrow.
I know that some other blogger has hammered a company (Macromedia if I remember correctly) for similar sample code best practices infractions. If this is what reputed companies are putting out as sample code, it’s no wonder that we have some much crap code out there keeping the sites by Jeff and Alex active.