There is more than one way to linq that

Eric Gunnerson has a post in which he responds to Justin Etheredge regarding “Is Programming A Generic Skill?

I’ll just say that I agree with them both. There are differences between ability, proficiency and mastery.

A Java programmer can jump into C# and be an able C# programmer immediately. It will take time to become proficient and even more time to develop mastery.

I am proof of this with python. I’ve been an able python programmer for over 10 years now(actually closer to 15), but I never spent enough time in python to call myself proficient and I’m certainly far from a master.

But the thing that Eric said which triggered me to write this is not the generic skill discussion with Justin, it is instead a remark that Eric said about Perl, “… because of TMTOWTDI”.

C# is very much becoming afflicted with There is More Than One Way To Do It. It certainly is both the language and library.

Consider LINQ. LINQ to Objects is a new way to do something which everyone has done differently in the past. When new programmers are exposed to linq, I often hear the question “When do I use linq?” The answer I give is typically “whenever you would use a for loop or foreach loop and if you can’t, figure out how you can".

If we throw away the ‘idiomatic code” to which Eric refers, then a giant bucket of TMTOWTDI is thrown in our face. Do we use properties or fields, events or delegate fields?

For public interfaces, FxCop helps a great deal with guidelines for these things and helping to learn the idioms of the language and framework, but for other things, you are left to yourself. That is why reading others source code is so important. (see the weekly course code)

Expand into libraries and TMTOWTDI explodes. From MSFT alone there is Remoting, Web Services (asmx), WSE, WCF, RIA Services, ASHX, ASP.NET Data Services, all of which have some overlap. Then for data access there is ADO.NET, Linq2Sql, Entity Framework. Even within ADO.NET alone, TMTOWTDI. Do you use a reader? Table Adapter with DataSets? Strongly Typed DataSets?

Open to non MSFT and TMTOWTDI explodes again, to the point where I won’t bother listing anything. There is too much.

I think that library TMTOWTDI will always exist. Even Python has SqlObject, SqlAlchemy and ORM as parts of other frameworks like Zope and Django. But language TMTOWTDI is reasonably well mitigated in Python. This is stated in a single line in PEP20 – The Zen of Python

There should be one—preferably only one –obvious way to do it.

C#, perhaps because it was not a goal and because of its C/Java/Delphi roots, has never had that. There has always been TMTOWTDI in C#.

Using the MVPS.ORG Hosts file with ISC Bind

I run my own DNS in our home. You may think this is crazy, but test after test has shown that nearly all ISPs provide substandard DNS to their customers. Even the finest DNS servers are only responsive 95% of the time. With the number of times you use DNS, you could be losing seconds or minutes per day while you wait for timeouts and rerequests.

Mvps.org maintains a list of “known bad domains”. While it is certainly not a replacement for other security measures, its another line of defense. It is another tool in the bag. For more reasons, read their site.

http://www.mvps.org/winhelp2002/hosts.htm

I don’t want to maintain host files on all of my home systems and all of the VMs too, I’d like to just tell my DNS server about these hosts and have it do the right thing.

By combining the downloaded hosts file and using this little boo script to map it into bind config, I have done just that. I use include files to bind. I’ve added a line like this to my /etc/bind/named.conf.local on my ubuntu server

include "/etc/bind/named.conf.mvps";

Then, I’ve added the output of this boo script to the /etc/bind/named.conf.mvps file. Reload bind and everything is done.

import System.IO
for line as string in [line for line in  @/\n|\r\n/.Split( File.OpenText("HOSTS").ReadToEnd() ) if (not line.StartsWith("#") and line!=string.Empty and not line.Contains("localhost"))]:
    fields = @/ +/.Split(line)
    if (fields.Length > 1):
      host = fields[1]
      print "zone \"${host}\" { type master; file \"/etc/bind/db.local\"; };"

Before hand, host resolution looked like this:

$ host ad.a8.net
ad.a8.net has address 203.190.224.60

After reloading bind, it looks like this:

$ host ad.a8.net
ad.a8.net has address 127.0.0.1
ad.a8.net has IPv6 address ::1

Browsing NHibernate Source Can Be Beautiful

NHibernate is awesome, but when Oren’s blog doesn’t have for what you are looking, and your google-fu is coming up short, it can be challenging to find what you want.

http://nhforge.org – is excellent, but almost never comes up in google searches.

http://nhibernate.sf.net – the sourceforge page redirects to http://nhibernate.org which in turn redirects to the http://hibernate.org nhibernate area.

I often want to browse the source. I just need to quickly see what they have done, and I don’t want to open the nhibernate sln on my system.

http://nhibernate.svn.sourceforge.net/viewvc/nhibernate/

I had to hunt around for more than a minute, to find the above url, but rest assured, it is viewvc pointed to nhibernate svn hosted by sourceforge.

If you can’t tell, I love viewvc.

ASP.NET via Mono and Apache

In my last post I showed how to build Mono trunk on the latest Ubuntu. I pointed out that I am using it on a “server” install of Ubuntu. I don’t have GTK+ or GTK# installed. So what good is Mono? Its good for hosting ASP.NET web applications.

  1. Configure an apache development environment.
    sudo apt-get install apache2-prefork-dev
  2. Grab my mono-dev-update script and tell it to only do mod_mono
    curl http://jrwren.wrenfam.com/scripts/mono-dev-update
    ./mono-dev-update –m mod_mono
  3. Install the apache module
    pushd /opt/mono/src/mod_mono ; sudo make install ; popd
  4. Enable the apache module
    pushd /etc/apache2/mods-enabled;ln –s ../mod_mono.conf . ; popd
  5. Restart Apache
    sudo /etc/init.d/apache2 restart
  6. Test it
    echo ’<%=DateTime.Now%>’ |sudo tee /var/www/index.aspx
    curl localhost/index.aspx

You should see the current date and time displayed. If you don’t, you have done something wrong.  Note that quotes paste poorly out of wordpress, so you may need to type out some of these commands.

Mono trunk on Ubuntu Jaunty

With the release of Ubuntu 9.04 aka Jaunty later this month, I thought I’d share how to get a current version of Mono on the latest Ubuntu release.

For a number of reasons, Ubuntu always seems to be just a little behind current with its Mono packages. The largest reason IMO is the difficulty in properly packaging Mono for Debian/Ubuntu. I’ve tried and it is not easy.

  1. Make our install a tiny developer environment.
    sudo apt-get install build-essential subversion autoconf libtool bison gettext pkg-config libglib2.0-dev
  2. Install the old Mono 2.0 C# compiler from jaunty so that we can bootstrap the trunk compiler.
    sudo apt-get install mono-mcs
    ln –s /usr/bin/mcs1 /usr/bin/mcs
    This installs enough mono to let the C# compiler run. We will remove this and all its dependencies later.
  3. Fetch the source from SVN. I use a mono-dev-update script to checkout the first time and keep me updated. The script pulls and installs mono, mcs, xsp to /opt/mono on a default ubuntu server install once step 1 above has been performed. To build more parts of mono such as GTK# and MonoDevelop you will need more gtk+ library dependencies.
    sudo mkdir –p /opt/mono/src
    sudo chown $USER /opt/mono /opt/mono/src
    ./mono-dev-update
  4. Remove the bootstrap mcs
    sudo apt-get remove binfmt-support cli-common libmono-corlib1.0-cil libmono-corlib2.0-cil libmono-i18n1.0-cil libmono-i18n2.0-cil libmono-security2.0-cil libmono-system1.0-cil libmono-system2.0-cil libmono0 mono-2.0-gac mono-2.0-runtime mono-common mono-gac mono-jit mono-mcs mono-runtime

The real work is done by the mono-dev-update script. Its a mess of bash script which has served me well for a couple of years now.

I’ll paste the current version of it here, but I’ll also try to keep it up to date for download here.

#!/bin/bash
#mono-dev-update
#Jay R. Wren <jrwren@xmtp.net>
#usage:
#   -s      skip svn operations. don't try to pull updates.
#   -f      force autogen.sh or configure to run.
#   -l n    set build level to value of 'n'. This controls optional package 
#           builds. Not used much
#   -n      no auto retry. if make fails, do not try to ./configure and 
#           make again.

MONO_PREFIX=/opt/mono
#GNOME_PREFIX=/opt/gnome
GNOME_PREFIX=/usr
export LD_LIBRARY_PATH=$MONO_PREFIX/lib:$LD_LIBRARY_PATH
export C_INCLUDE_PATH=$MONO_PREFIX/include:$GNOME_PREFIX/include
export ACLOCAL_PATH=$MONO_PREFIX/share/aclocal
export PKG_CONFIG_PATH=$MONO_PREFIX/lib/pkgconfig:$GNOME_PREFIX/lib/pkgconfig
PATH=$MONO_PREFIX/bin:$PATH
export MANPATH=$MANPATH:$MONO_PREFIX/share/man
if [[ ! -d $MONO_PREFIX/src ]];then mkdir $MONO_PREFIX/src; fi
pushd $MONO_PREFIX/src

SVNDIRS="mcs mono xsp monodevelop gtk-sharp gtkmozembed-sharp type-reflector debugger banshee-sample-plugin olive mono-tools" 
CVSDIRS="banshee"
MAKEDIRS="mono xsp debugger mono-tools monodevelop gtkmozembed-sharp "
BANSCHEEPLUGINS="banshee-sample-plugin" 

level=0
skipsvn=0

optCount=0;
while getopts :xvfsnd:t:cl:m: OPTS ;do
	if [[ $OPTS == "x" ]]; then outputStyle=xml ; optCount=$((optCount+1)) ; fi
	if [[ $OPTS == "v" ]]; then action=view ; optCount=$((optCount+1)) ; fi
	if [[ $OPTS == "f" ]]; then forceAGen="true" ; optCount=$((optCount+1)) ; fi
	if [[ $OPTS == "s" ]]; then skipsvn=1 ; optCount=$((optCount+1)) ; fi
	if [[ $OPTS == "n" ]]; then noautoretry="true" ; optCount=$((optCount+1)) ; fi
	if [[ $OPTS == "d" ]]; then DEBUG="$OPTARG" ; optCount=$((optCount+2)) ; fi
	if [[ $OPTS == "t" ]]; then transform="$OPTARG" ; optCount=$((optCount+2)) ; fi
	if [[ $OPTS == "c" ]]; then for i in $MAKEDIRS;do pushd $i ; make clean ; popd ; done ;exit ; optCount=$((optCount+1)) ; fi
	if [[ $OPTS == "l" ]]; then level="$OPTARG" ; optCount=$((optCount+2)) ; fi
	if [[ $OPTS == "m" ]]; then MAKEDIRS="$OPTARG" ; optCount=$((optCount+2));fi
done
if [[ $optCount != $((OPTIND-1)) ]] ; then echo "optcount($optCount) not equal to OPTIND($OPTIND)";fi

echo "using makedirs $MAKEDIRS"

echo "using prefix $MONO_PREFIX"

if [[ 1 > $skipsvn ]]; then
for i in $SVNDIRS
do
	echo -e "\e[1;31m pushd $i \e[m"
	if [[ -d $i ]];then
		pushd $i 
        if [[ ! -d .svn ]];then pushd .. ; svn co http://anonsvn.mono-project.com/source/trunk/$i ;popd; fi
		echo -e "\e[1;31m svn info"
		svn info
		echo 'svn log -r `svn info|grep Revision|cut -f2 -d' '`:HEAD'
		echo -e "\e[m"
		svn log -r `svn info|grep Revision|cut -f2 -d' '`:HEAD
		echo svn up
		nice -n 20 svn up 
		if [ $? != 0 ]; then echo "ERROR: $?" ;popd ; break; fi
		pwd
		popd
	fi
done
else
	echo "-s detected, skipping svn update"
fi 


#mono, xsp, MD, GTK# etc
function autogenAndMake () {
	echo "running for $*"
	for i in $*
	do
		if [[ -d $i ]]; then
			echo -e "\e[1;31m pushd $i \e[m"
			pushd $i 
			if [[ -f "autogen.sh" ]];then
				PROG=./autogen.sh
			else
				PROG=./configure
			fi
			if [[ "true" != $forceAGen ]]; then
				nice -n 20 make && nice -n 20 make install  
			fi

			if [[ "true" != $noautoretry && $? != 0 || "true" == $forceAGen ]]; then 
				echo -e "\e[1;31m "
				echo 'make clean ; $PROG --prefix=$MONO_PREFIX --enable-aspnet --enable-aspnetedit --with-preview --with-moonlight && nice -n 20 make && nice -n 20 make install'
				echo -e "\e[m"
				if [[ -f Makefile ]]; then make clean; fi
				$PROG --prefix=$MONO_PREFIX --enable-aspnet --enable-aspnetedit --with-preview --with-moonlight && nice -n 20 make && nice -n 20 make install || break
			fi
			popd 
		fi
	done 
}

#-l not  speced or even means build all - odd means build only banshee & olive
if [[ $((level % 2)) == 0 ]]; then 
autogenAndMake $MAKEDIRS
fi

if [[ $level > 1 ]]; then
	i=gtk-sharp
			echo -e "\e[1;31m pushd $i \e[m"
			pushd $i
			echo -e "\e[1;31m "
			echo 'make clean ; ./bootstrap-2.12 --prefix=$MONO_PREFIX  && nice -n 20 make && nice -n 20 make install'
			echo -e "\e[m"
			if [[ -f Makefile ]]; then make clean ; fi
			./bootstrap-2.12 --prefix=$MONO_PREFIX  && nice -n 20 make && nice -n 20 make install || break
	popd
fi

if [[ $level > 1 ]];then
	#olive
	for i in olive 
	do 
		if [[ -d $i ]] ; then
			echo -e "\e[1;31m pushd $i \e[m"
			pushd $i
			echo -e "\e[1;31m "
			echo 'make clean ; ./configure  --prefix=$MONO_PREFIX  && nice -n 20 make && nice -n 20 make install'
			echo -e "\e[m"
			if [[ -f Makefile ]]; then make clean ; fi
			./configure  --prefix=$MONO_PREFIX  && nice -n 20 make && nice -n 20 make install || break
			popd
		fi
	done
fi

if [[ $level > 1 ]];then
	#banshee
	if [[ -d banshee ]];then
	pushd banshee
		cvs up
		./configure --prefix=$MONO_PREFIX --disable-helix && nice -n 20 make && nice -n 20 make install || break
	popd
	autogenAndMake $BANSCHEEPLUGINS
	fi
fi

popd

Excellence in Hiring and Practice

There was a Off Topic discussion on a mailing list recently which left me with these thoughts.

There is nothing wrong with expecting and even demanding excellence. It is these things which make society a better place. People do have a tendency of at least trying to live up to expectations. People want to succeed. The sad part is that there are so many employee positions which do not encourage or expect excellence. I feel it is not unreasonable that you have high expectations out of your interview candidates. I feel that it is important that we all recognize this problem in our community and recognize that it impacts the world as a whole.

While I’m not directly in the hiring position like some of the people ?venting? on the list, I will say that I’ve also been very impressed with a few of our straight out of college hires.(actually all, I credit good interviewing by the awesome people with which I work)

Extending this beyond just interviewing candidates, I was recently reminded of the importance of seeking excellence as we work. Sometimes as consultants we feel like doing things for our clients is a balancing act and that sometimes pursuing the best design isn’t the best thing for the client. Maybe sometimes that is true. But I think I realized that I was leaning toward the side of “settling” for non-excellent design. I shall not continue that. I shall provide a better balance of excellence.

Don’t mix ANY and x86 build and forget about it

you will get a very stupid error like this:

System.BadImageFormatException : Could not load file or assembly or one of its dependencies. An attempt was made to load a program with an incorrect format.

In my case, I was getting it in a TestFixtureSetUp method.

DUH. I had set the class library project to force to x86, because I’m on x64, but I wanted to use edit and continue. I never changed the Test project, so it was still running x64. It failed with a “BadImageFormatException” when it loaded the references x86 project.

Don’t be stupid like me.

Eric Sink doesn’t understand DVCS

Sorry Eric, but that is what I read when you trash Git’s index.

Eric says “One of the best practices I suggest in Source Control HOWTO is to never use a version control feature which encourages you to checkin code which you have never been allowed to compile and test.”

I agree 100% with that “best practice” when using a centralized version control system. But Git, Bazaar and friends are not centralized version control systems. They are decentralized.

With a decentralized version control system, your commits are decoupled from your compiles and tests. Your commits are decoupled from your build server. Your commits are decentralized from any team process or build process system. Remember that “check in” or “commit” in a distributed version control system just means “hey, I might want to come back to this at some point in time”. They key word there being “I”. Its about you as a single developer. Its about being selfish. Its about being able to work without barriers. Its about being able to make large sweeping changes and then rollback all but some of them and then merge only those changes which you want with your teams branch.

Ok, so Eric ends his post saying “That doesn’t mean Git or its index are bad.  I’ll agree that "git add –p" is a very powerful feature that has its place.  But in this respect, Git is a bit like C.”

Fair enough Eric. Eric knows that this is a powerful tool to be used carefully by a skilled craftsperson. That is what we are as programmers.

If C’s killer feature is casting an int to a pointer, then Ruby’s killer feature is open classes. Sure, it can be a confusing tool to the uninitiated, but ask a well versed programmer to not use that tool and I would encourage them to call you crazy.

Don’t fear your tools. Master them. They will server you longer than any fear will.

I’m very glad that Eric says that “no book on this topic can be truly credible these days without covering distributed version control tools”. He says these tools aren’t mainstream yet, but with ruby forge, linux and github using git and with Ubuntu, mysql, Mainman and squid using Bazaar, DVCS is mainstream.

Using Decentralized Version Control for Your Presentation Demos

Occasionally, I present some slides and demo code at a local user group or event. Sometimes these demos include some code that I write throughout the presentation. By the end of the presentation, there is a bunch of code there that I did not start with.

Problem: next time I give the presentation, invariably, I have forgotten to go back and reset things so that my code files don’t have the code to be written in them.

Solution: use a decentralized version control system for presentation demo code.

It doesn’t matter if it is Bazaar (the best), Git or Mercurial (the others). Use one of them!

Use looks like this:

  1. Prep your demo project with the files to be handled when you start your presentation
  2. bzr init ; bzr add ; bzr commit –m ‘initial check in’
  3. Give your presentation. When you are done, run bzr revert. You need not even commit the changes.

Sure, its pretty much the same as making a backup, but when you invariably forget to remove the post-presentation written code, you can simply type bzr revert. Typing one command that is not prone to error is a lot easier than doing the “remove folder, rename folder” dance.

For longer presentations like 1/2 day or day long tutorials or multi day classroom training, you can break your code into stages. Just commit at every stage. Then you can bzr revert –r 2 to get to your 2nd stage code, bzr revert –r 4 to get to the tail end of the day, etc.

You can even use loggerhead to show your class participants differences between revisions, or even just bzr diff output. You will be able to show people a different view of the evolution of your project, which hopefully will give them new understanding and more clarity. This is why we use these tools, right?

Read the Fine Diffs

aka RTFD.

I was very happy to see this blog post this morning(I read it Feb 02, but didn’t finish writing this post until now). It reminded me that I find great value in doing this, even though I rarely do it. I shall be doing this more often as of now.

Do you work as part of a software team?  Here’s a piece of advice for you:

Read the diffs.

Every morning before you start your own coding tasks, use your favorite diff tool to look at all the changes that everybody else checked in the day before.

in SVN, it is as easy as svn log –r –2; svn diff –r –2, assuming there is just one commit that you missed.

The same command works for bzr: bzr log –r –2; bzr diff –r –2.

Assuming you work in the real world where there is more than one commit since you last read the log, you will need to read since your last revision. In this work flow, you really should read the commit log and diffs every type you pull an update. I use a script which looks like this:

$svn log -r `svn info|grep Revision|cut -f2 -d’ ‘`:HEAD

with bzr it looks like this (a little more complex since we aren’t used to getting this type of information remotely)

$bzr log -r `bzr log -r -1 | grep revno | awk -F: ‘{print $2}’`.. `bzr info | grep "checkout of branch" | awk ‘{print $4}’`

I’m pretty bad at this, so I highly recommend some web UI. ViewVC or Loggerhead are both great UI for this. Both Loggerhead and ViewVC offer RSS feeds for the logs of your SVN or BZR repository respectively.

If you are running subversion, please consider installing ViewVC. If you are running bazaar, please consider installing Loggerhead.