I don’t know if you ever had the opportunity to attend those old time geek fests where after the days conferences and presentations, the programmers would gather for drinks and a little after hours friendly competitions, but they were fun, and sometimes really scary. One of my favorite was to attend a contest where C++ programmers would be given a programming task and the team that produced the lowest total sum of number of semi-colons plus number of programming structures won.
In case you’re not sure what that means exactly, it means who could do the most stuff on the least lines of code. Get as much processing into a single line of code. And in C++ you canREALLY do a lot of work in a single line of code if you know what you were about; Mind you the line could end up being 400 characters in length, and completely impossible to figure out what it was doing in retrospect, but damn it was concise!
Needless to say BASIC (and it’s variant Visual Basic) definitely DIDN’T have that capability. I come from a VB background, and while you can get ridiculous in C#, my coding habits from my VB days means I generally don’t go for obtuse line coding. In fact one of my mantras is “One line of code, should only do One thing.”
But then along comes Linq, which just BEGS you to do many things in one line, and before you know it you start figuring out how to combine. And there is this urge to just “combine a little bit more” and before you know it, a C++ programmer would be willing to buy you a beer for your ability to product obtuse, obscure, confusing code.
_Events = Claim.Events
.OfType<Wizard>( )
.Where(
w => w.StatusType == ( int )Enums.EventWizardStatusType.Open || w.StatusType == ( int )Enums.EventWizardStatusType.InProgress )
.Cast<Event>( )
.Union( Claim.Events .OfType<Task>( )
.Where( w => w.StatusType == ( int )Enums.TaskStatusType.Open || w.StatusType == ( int )Enums.TaskStatusType.InProgress )
.Where( w => w.Tasks.Count == 0 )
.Cast<Event>( ) ).OrderBy( e => e.DueDate ).ToList( );
Ok quick can you tell at a glance what that does? No, neither could I, after I had finished compressing four statements into one. It was really cool what I had achieved, but when the day comes that I need to modify that code, and trust me the day will come, I would curse myself. So even though it means I have to hand in my Obfuscated Code Black Belt, I decided to go with instead the following (which is functionally equivalent to the preceding).
var wizards = Claim.Events
.OfType<Wizard>( )
.Where( w => w.StatusType == ( int )Enums.EventWizardStatusType.Open || w.StatusType == ( int )Enums.EventWizardStatusType.InProgress )
.Cast<Event>( );
var tasks = Claim.Events
.OfType<Task>( )
.Where( w => w.StatusType == ( int )Enums.TaskStatusType.Open || w.StatusType == ( int )Enums.TaskStatusType.InProgress ) .Where( w => w.Tasks.Count == 0 )
.Cast<Event>( );
var events = tasks.Union( wizards );
_Events = events.OrderBy( e => e.DueDate ).ToList( );
Posted on
11 Jun 10 22:31
by
matthew.hintzen |
Bookmark this post with:
E-mail |
Comments(0) |
Comment RSS