C# blues
April 16, 2008 | 6:24 pmI have this hate-hate relationship with C# - I can’t go out and proclaim that I hate C#, ‘coz people mistake me for a Linux Zealot then. So, I usually list C# when people ask me what languages I know, but "never" use it out of choice. I always prefer VB, and with VB9, it’s got some kickass XML features that eat C#’s lunch, Dinner, and next day’s breakfast as well
One thing that does drive me crazy is people accusing VB of being "verbose", while C# is "clean". Here’s some code I’m writing:
PID = dataReader["PID"].ToString() ;
UID = dataReader["UID"].ToString() ;
Title = dataReader["TITLE"].ToString() ;
Description = dataReader["DESCRIPTION"].ToString() ;
Views = int.Parse(dataReader["VIEWS"].ToString()) ;
Favs = int.Parse(dataReader["FAVS"].ToString());
Notes = int.Parse(dataReader["NOTES"].ToString());
Comments = int.Parse(dataReader["COMMENTS"].ToString());
SmallURL = dataReader["SMALLURL"].ToString() ;
MediumURL = dataReader["MEDIUMURL"].ToString() ;
LargeURL = dataReader["LARGEURL"].ToString() ;
PhotoURL = dataReader["PHOTOURL"].ToString() ;
TakenOn = new DateTime(int.Parse(dataReader["TAKENON"].ToString()));
PostedOn = new DateTime(int.Parse(dataReader["POSTEDON"].ToString()));
MadeExploreOn = new DateTime(int.Parse(dataReader["MADEEXPLOREON"].ToString()));
CanDownload = bool.Parse(CanDownload.ToString());
This is C# 2.0, ‘coz I’m writing this to run on Mono, and I don’t want to depend on unsupported features, and their VB compiler ain’t exactly world class right now. If this mess is called "clean", then thank-you-very-much-I’ll-have-my-verbose-VB9-and-eat-your-lunch
Maybe some people prefer this. Maybe I’m doing something wrong here. I still prefer the VB (and Ruby (and Python)) approach of writing for people rather than for computers.







Perhaps this will only make your blues worse, but still:
punky | April 17, 2008 | 9:01 amPerhaps this will only make your blues worse, but still: refactor away all that repetition and keep it DRY, and maybe it won’t seem so dirty anymore?
Dunno if this is compilable C# (I’m in Javaland at the moment), but you should be able to make whatever minor adjustments to make the compiler happy.
class Wrapper
{
private DataReader dataReader;
public Wrapper(DataReader dataReader) {
this.dataReader = dataReader;
}
public string GetString(string name) {
return dataReader[name].ToString();
}
public int GetInt(string name) {
return int.Parse(GetString(name));
}
pubic DateTime GetDateTime(string name) {
return new DateTime(GetInt(name));
}
}
Then your code would read:
Wrapper w = new Wrapper(dataReader);
PID = w.GetString(”PID”);
UID = w.GetString(”UID”);
Title = w.GetString(”TITLE”);
Description = w.GetString(”DESCRIPTION”) ;
Views = w.GetInt(”VIEWS”) ;
Favs = w.GetInt(”FAVS”);
Notes = w.GetInt(”NOTES”);
Comments = w.GetInt(”COMMENTS”);
SmallURL = w.GetString(”SMALLURL”) ;
MediumURL = w.GetString(”MEDIUMURL”) ;
LargeURL = w.GetString(”LARGEURL”) ;
PhotoURL = w.GetString(”PHOTOURL”) ;
TakenOn = w.GetDateTime(”TAKENON”);
PostedOn = w.GetDateTime(”POSTEDON”);
MadeExploreOn = w.GetDateTime(”MADEEXPLOREON”);
Which seems reasonably clean to me. Of course, I also prefer to use Python over Java if and when I can
(I am sorry I mistyped 'public' in the previous comment,
punky | April 17, 2008 | 9:02 am(I am sorry I mistyped ‘public’ in the previous comment, turning it into a dirty word)
@punky: Is writing an entire wrapper class just to satisfy
yuvipanda | April 17, 2008 | 1:20 pm@punky: Is writing an entire wrapper class just to satisfy a “compiler” clean?
Just as you prefer Python, I prefer VB when I can
TypeInfering/DuckTyped languages ftw!
Heh, I guess we're talking degrees of cleanness/dirtiness here. Certainly
punky | April 17, 2008 | 1:57 pmHeh, I guess we’re talking degrees of cleanness/dirtiness here. Certainly I think it’s cleanER than the original, because it gets rid of the duplication. But yeah, one of the problems with Java/C#-type languages is that we do seem to spend a lot of time throwing structures at the compiler. Incidental complexity, anyone?
I do think that statically typed languages are getting too much a flak these days, though. Certainly there is room for both statically and dynamically typed languages. But I hear you on the type inference bit, of course. Explicit typing is just such a noise generator. Genuine Java example from punky’s current project:
final Map<Integer, Set<Pair<String, List>>> result = new HashMap<Integer, Set<Pair<String, List>>>();
Having to type that type declaration once is bad enough… I definitely need to take a closer look at Scala soon!
I don’t have any experience with VB.NET, so I’ll refrain from badmouthing it. It’s better to speak well of the things I like. Like list comprehensions, tuples and first-class functions in Python. Oh-wee.
Hm. For some very, very strange reason, the type of
punky | April 17, 2008 | 2:07 pmHm. For some very, very strange reason, the type of the List inside the Pair inside the Set inside the Map got swallowed by your blog software (It’s present in the original that I cut and pasted into this text box). Perhaps four levels of nested angle brackets are just too much?
Testing, testing: <<<>>>
Yep, the innermost brackets disappeared. Funny.
punky | April 17, 2008 | 2:08 pmYep, the innermost brackets disappeared. Funny.
i suppose this is where LINQ an anonymous types come
sven | May 4, 2008 | 9:08 pmi suppose this is where LINQ an anonymous types come in, huh? too bad that stuff won’t make it to mono for another, ooooh, 2 years? (boy, i wish i was smart enough to help in the MONO project!)
@sven: Yep, kinda, but only for *this specific problem*. I
yuvipanda | May 5, 2008 | 5:52 am@sven: Yep, kinda, but only for *this specific problem*. I had to use datareader since I was reading from SQLite, which doesn’t have a LINQ provider atm. The bigger issue at hand is us servicing the ccompiler when it should be the other way ’bout.
take a look at this: http://blogs.msdn.com/mattwar/archive/2007/07/30/linq-building-an-iqueryable-provider-part-i.aspx and this: http://blogs.msdn.com/mattwar/archive/2008/05/04/mocks-nix-an-extensible-linq-to-sql-datacontext.aspx i used these to make
sven | May 7, 2008 | 12:58 amtake a look at this:
http://blogs.msdn.com/mattwar/archive/2007/07/30/linq-building-an-iqueryable-provider-part-i.aspx
and this:
http://blogs.msdn.com/mattwar/archive/2008/05/04/mocks-nix-an-extensible-linq-to-sql-datacontext.aspx
i used these to make a linq provider for FoxPro
life insurance policy questions... inverts deductions:timeshare!payoff,boroughs pocketbook!...
life insurance policy questions | October 6, 2008 | 8:59 pmlife insurance policy questions…
inverts deductions:timeshare!payoff,boroughs pocketbook!…