uncategorized

Shit samples == shit coders

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Helper method: This method establishes a connection to a database
public static DB2Connection ConnectDb(String[] argv)
{
String server = "";
String alias = "";
String userId = "";
String password = "";
Int32 portNumber = -1;
String connectString;

if( argv.Length > 5 ||
( argv.Length == 1 &&
( String.Compare(argv[0],"?") == 0 ||
String.Compare(argv[0],"-?") == 0 ||
String.Compare(argv[0],"/?") == 0 ||
String.Compare(argv[0],"-h",true) == 0 ||
String.Compare(argv[0],"/h",true) == 0 ||
String.Compare(argv[0],"-help",true) == 0 ||
String.Compare(argv[0],"/help",true) == 0 ) ) )
{
throw new Exception(
"Usage: prog_name [dbAlias] [userId passwd] \n" +
" prog_name [dbAlias] server portNum userId passwd");
}

switch (argv.Length)
{
case 0: // Use all defaults
alias = "sample";
userId = "";
password = "";
break;
case 1: // dbAlias specified
alias = argv[0];
userId = "";
password = "";
break;
case 2: // userId & passwd specified
alias = "sample";
userId = argv[0];
password = argv[1];
break;
case 3: // dbAlias, userId & passwd specified
alias = argv[0];
userId = argv[1];
password = argv[2];
break;
case 4: // use default dbAlias
alias = "sample";
server = argv[0];
portNumber = Convert.ToInt32(argv[1]);
userId = argv[2];
password = argv[3];
break;
case 5: // everything specified
alias = argv[0];
server = argv[1];
portNumber = Convert.ToInt32(argv[2]);
userId = argv[3];
password = argv[4];
break;
}

if(portNumber==-1)
{
connectString = "Database=" + alias;
}
else
{
connectString = "Server=" + server + ":" + portNumber +
";Database=" + alias;
}

if(userId != "")
{
connectString += ";UID=" + userId + ";PWD=" + password;
}

DB2Connection conn = new DB2Connection(connectString);
conn.Open();
Console.WriteLine(" Connected to the " + alias + " database");
return conn;

} // ConnectDb

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.