Python makes me think about C#

I love going to the Michipug meetings. I don’t use python on a day to day basis, but I find that talking to those who do really helps me to think about things differently.

Thursday, even before going into the Michipug meeting, I was reading a python blog or something in anticipation of the meeting that night. Something I read triggered me to think about python’s filter, map, and reduce built-in functions. I wondered what .NET has to correspond to these. Well, List.FindAll and List.ConvertAll and their Array counterparts do the work of filter and map. That left reduce.

I couldn’t find reduce, so I whipped up an implementation. I started with just an integer implementation. Again, Func<T,T,T> is a delegate definition straight out of System.Query in the LINQ preview, or Rhino.Commons or wherever.


public int Reduce( Func<int,int,int> function, List<int> list )
{
IEnumerator<int> enumerator = list.GetEnumerator();
enumerator.MoveNext();
T result = enumerator.Current;
while(enumerator.MoveNext())
{
T item = enumerator.Current;
result = function( result, item );
}
return result;
}

Once I had this running, I just made it generic.


public T Reduce<T>( Func<T,T,T> function, IEnumerable<T> list )
{
IEnumerator<T> enumerator = list.GetEnumerator();
enumerator.MoveNext();
T result = enumerator.Current;
while(enumerator.MoveNext())
{
T item = enumerator.Current;
result = function( result, item );
}
return result;
}

I also changed the List<T> to IEnumerable<T> so that it would work with Arrays, Lists, or anything Enumberable.

So what is the point? Well, I don’t have one, other than “its cool!”

Its nice to write factorial as simply as this:


var res = Reduce( ( x, y )=> x * y, list );

or sum a list like this:


var res = Reduce( ( x, y )=> x + y, list );

Its very strange when string.Join(“, “…) doesn’t use Join


Reduce( (x,y) => string.Format("{0}, {1}",x ,y) , message)

And even stranger when you can reverse the join order


Reduce( (x,y) => string.Format("{1}, {0}",x ,y) , message)

What else can you do with reduce?

Well, I don’t know. Give a google for python reduce and you will see that Guido is even getting rid of filter, map, and reduce from Python in the future. So .NET just got filter and map in the form of Array and List member methods which take delegates as arguments, and python is removing filter and map in favor of its list comprehensions and generator expressions.

So what? Well, I don’t really think it means much. In the same article Guido talks about adding two functions, Any and All. .NET already has these in the form of Exists and TrueForAll as Array and List members.


// python: any(x > 42 for x in S) # True if any elements of S are > 42

S.Exists( x => x > 42 );

// python: all(x != 0 for x in S) # True if all elements if S are nonzero

S.TrueForAll( x => x != 0 );
I don’t see much of a difference in readability between the Python and the C#. They are merely different.

One of the very strange things about the .NET implementation is that Exists and TrueForAll aren’t members of the Array class, rather they are static members. So the invocation of the same thing on an Array is slightly different.


Array.Exists( S, x => x > 42 );
Array.TrueForAll( S, x => x != 0 );

C# 3.0 extension methods don’t help. Array inherits from IEnumerable. Nothing about its type is known for use by the compiler at compile time. You could try to implement Exists and TrueForAll, but you end up having to invoke like this:


S.Exists<int>( x => x > 42 );
S.TrueForAll<int>( x => x != 0 );

All this because I think about python. I should just write more boo, but alas, boo still doesn’t support Generics. Boo is great, but not for interoperating with Generic types from other .NET libraries.

It is still about CodeMash

Richard Hale Shaw at the Ann Arbor Computer Society meeting on Wednesday gave a good, detailed overview of the C# 2.0 features that are getting used to build interesting things in C# 3.0.

Everything came back to CodeMash. The topic reminded me of Bill Wagner’s CodeMash talk, and the talk he gave at Ann Arbor Dot Net Developers back in December. I had just converted my application from log4net to NLog and ran into a case where I didn’t want to execute some code unless I was logging it. NLog and log4net recommend putting your logger statement in an if block like

if (logger.IsLogErrorEnabled)
log.Error( "oh no!"+ SomeLongRunningFunctionWhichReturnsString() );

I prefer to have my log statements on one line, I find it clutters my source code less. Afterall, logging is NOT the primary concern of my code. I want to focus on the task and hand and obey single responsibility as much as I can.

It turns out C# 2.0 anonymous delegates provide a nice solution (and of course C# 3.0 lambdas make it more readable).

Lets assume our logger has an override with a signature of

public void Error( Func<string> function );

Where Func is straight out of System.Query or Rhino.Commons or wherever.

public delegate TR Func<TR>();

Now the overriden Error method on the Logger class accepts a method as input. If we turn logging off, this method never needs to be executed. Using it is not too ugly.


logger.Error(delegate() {return "oh no! " + SomeLongRunningFunctionWhichReturnsString(); } );

C# 3.0 brings lambdas to the syntax of the language and makes this a whole lot easier to write.


logger.Error( () => "oh no!" + SomeLongRunningFunctionWhichReturnsString() );

And that was only WEDNESDAY!!! WOW.

Thursday followed up strongly with a Michipug meeting where we talked python templating libraries. The format was awesome. We broke into little groups and each group explored the template library. I took a look at twiddler and it is very simple and elegant. I really feel like I grasp the whole thing and I only looked at it for a very short period of time. I don’t use enough python to really comment on any of the libraries we looked at. After looking at them, each group presented an overview of the template library. I think everyone enjoyed the format and learned a lot.

At both of these meetings everyone talked about CodeMash. All of the comments were positive. What a great event. I mentioned that my favorite thing was the presentations on Selenium and Niel Ford’s Productive Programmer presentation. These topics really transcend the CodeMash language barriers. The concepts being The Productive Programmer are really things that EVERY programmer should know. It doesn’t matter if you do Java, Ruby, C, C++, Python, Perl, C#, PHP, whatever. I found it so valuable because at any other language or technology specific conference you wouldn’t get panels like that. It simply wouldn’t fit. It fit perfectly at CodeMash.

LINQ to NHibernate

Best Quote from Oren on DNRtv:

Oren: “This is HQL, Hibernate Query Language. It is similar to the SQL look but it has more OO notation. …”

Carl: “Starting to look like LINQ a little bit.”

Oren: “Yes, it looks like LINQ, but this is string. This is string but what will happen is once LINQ passes beta status me or someone else from NHibernate community will write a LINQ adapter for NHibernate. I don’t know the exact name. It actually isn’t very hard. I talked with at TechEd isreal last year and it doesn’t look very hard at all to do this. The concept is very much the same.”

So, learn and use NHibernate (or Castle ActiveRecord) now. Don’t worry or fret about LINQ, DLINQ, LINQ to SQL, LINQ to Entities, LINQ to DataSets. The beauty of LINQ will be available for NHiberate. LINQ is not the death of NHibernate.

NHibernate is a vast library with many extensiblity points. NHibernate exists today, it has proven itself for years. Its intricacies are well understood by the community and there is excellent documentation surrounding it. Now I’m sure that LINQ to SQL will have reams of docoumentation as most Microsoft products eventually do. But, LINQ to SQL is for Microsoft SQL Server only. NHibernate has mappings to MSSQL, PostgreSQL, Firebird, MySQL, SQLite, even Access, and probably many others. By leaning on NHibernate you are assured a high level of compatibility with any RDBMS. Some organizations value this. I value this.

Test a web app – I must try Selenium in addition to WatiN

WOW!

Dan Bunea has an awesome post on Test First Web Applications

I felt a little dirty after CodeMash because Selenium was presented there and it really looked at felt like a more powerful web testing system than Watir or Watin. I had just adopted Watin for some basic tests and I had to admit I really didn’t like the fact that it was just driving an IE engine. Especially since I don’t really care about IE, Firefox is my standard.

I didn’t know about Selenium RC. Apparently I can use .NET to write my tests and they will get translated to something Selenium will use. This is what I was looking for when I opted to use Watin. I’m not sure if this was mentioned in the CodeMash presentation. I had duct out trying to see both the Selenium talk and the “What Makes Rails Work” talk.

Thanks to Dan for the awesome post.

Oh and the post is also an awesome example of building a MonoRail application.

David Redding on Abstract Factory Pattern

At first I thought “that isn’t a factory!” but that is because the entire post wasn’t there in RSS.

My buddy David Redding has what could be the funniest explaination of the Abstract Factory Pattern that I’ve ever read.

He also mentions the Ann Arbor DotNET Developers meeting coming up on January 10th. It will be awesome to have an expert on Windows Workflow in town. If you are in the Ann Arbor area, stop by at 6pm and see Matt Winkler.

On : Downcasting is a Code Smell

I almost always tend to agree with Jeremy D. Miller over at CodeBetter.Com. This time, I still agree with him. I agree so much that I am wondering if he is joking or was having a bad day and posted this in frustration.

Jeremy, of course Downcasting is a code smell. It does all the bad things that you said it does. I thought that this was a given. I see code like this and I immediately assume that the person who wrote it either 1. was in too much of a hurry to take pride in their work, or 2. had no idea how to program using good OO concepts.

When I run into code like this I usually get VERY frustrated. It is never fun to run into ugly code. It is more frustrating to run into code that could be so much better with just slightly more than trivial understanding of the underlying libraries. I guess this is exactly what I wrote on with respect to System.Data over a year ago. Not the downcasting part, but rather just good use of the supplied interfaces instead of always using the concrete class.

It was downcasting that inspired me to write those posts over a year ago. I was working with a project which was using the DAAB from EntLib 1.0. DAAB returns IDbConnection and it was being Downcast to SqlConnection everywhere it was using in this project. I cursed a bit. Then I realized that all of the code was in my hands, and I went about cleaning it up. It was not difficult. That was something I COULD take pride in. Stupid, yes. Maybe a waste of time, but now I know that the next time I go into that module I won’t be disgusted by what I see.

Visual Studio 2003 web projects

If you use subversion for version control, you are probably aware of the .svn issues.

I feel like Studio 2003 is Developing Old School. I’ve fully embraced 2005. Now I can hear you developers that are ten and twenty years older than me talking about programming with buttons and switches and the graduation to teletype and finally dumb terminals. While I haven’t experienced that first hand, I can say that I can still develop with notepad or vi with the best of them.

Still, stepping back to Visual Studio 2003 after a full year of 2005 just feels old.

OK, the point is, that I am doing this. I’ve run into the well known .svn issue, and after some searching I think I’ve come up with a better fix than I’ve seen suggested yet.

My fix:
1. Close the project file.
2. Open in a text editor such a gvim or notepad2.
3. Change the value of the ProjectType attribute to “Local” from “Web” under the /VisualStudioProject/VisualBasic node.
4. Open the new project file in visual studio.

It turns out that the web project is just a class library project with a remote target. Why MS feels compelled to continue to abstract this for developers is beyond me. Developers should have a deep understanding of filesystems, both local and remote. If they do not, then purhaps they need additional training, or should consider a different career.

I hate frameworks too

Y E S !!!!!

As usual I couldn’t agree with Oren any more. Of course I read the Joel article when it was released 15 months ago. It is just as true now as it was then. Honestly, I have zero use for the Enterprise Library as a whole. What does it do for me? It doesn’t help me with good design (and architecture, but I hate the “arch” word) like Castle’s Windsor helps me. It doesn’t help me with Database abstraction like NHibernate or Castle’s ActiveRecord. I already have logging with log4net. So what does Enterprise Library do for me? Caching, Configuration, Instrumentation? mmm… Maybe. I’m not sure. I’d rather just enable an NHibernate Cache. For Configuration, Windsor does a lot of this for me. Instrumentation. OK. I’ll investigate the Instrumentation block. I think the Enterprise Library team could learn a lot from Castle.