Wednesday, April 19, 2006

Who needs road rules?

There is a theory that if you take away the road rules everyone would have to driver slower and safer to make sure they don't have accidents. There is evidence to back this up. Think about your local street. Imagine kids were frequently playing ball in the road. How fast would you drive? If there was an accident the kid would get hurt, but not killed. Parents are scared their kids will get hurt, so people move off the streets to the front lawn.This gives the cars a chance to go faster, this makes the roads less safe, so the kids are told to only play in the back garden. This means the cars can go even faster. Now if there is an accident it will be fatal. Take a look at this footage from an intersection in india. The cars all keep moving. The speeds are quite slow though. Everyone expects someone to get in their way. No accidents.

There has to be a simpler way!

Tuesday, April 11, 2006

Comments are for 'why?'

I have blogged on this before, but I keep running across it, so here goes again...

Here is a snippet from some VB code I am having to clean up at the moment.

    Public Property PasswordKeyboard() As Boolean
'Retrieves PasswordKeyboard
Get
Return boolPasswordKeyboard
End Get
'Assigns to PasswordKeyboard
Set(ByVal Value As Boolean)
boolPasswordKeyboard = Value
End Set
End Property


Check out the comments! Your code should clearly explain to the reader your understanding of the problem domain, and your solution to the problems involved. Your code should answer a few questions: What is it doing? How is it doing it? and Why is it being done that way?
'What' should be covered by the method and class names.
'How' is covered by the method body (which should be made easy to read)
'Why' is something you can't easly enbed in the code. This is where comments are invaluable. This is what comments are for.

The only reason I can see for having the comments in the above code is to explain why you put the following lines of code in the file. This only make sence if you are trying to document/teach the VB
language. Maybe this is why this problem is so prolific. When someone learns to program they come across this sort of commenting all the time in tutorials. Students duplicate what they are fed, so they add the same comments to their own code.

Do me a favor... if you see this style of commenting anywhere in code you have your hands on, just delete it. The code will be MORE readable, and smaller. Your brain will thank you.


Friday, February 10, 2006

Casting from object[] in C#

Can anyone explane this?
ArrayList list = new ArrayList();
list.Add(new Banana());
object[] areTheseBananas = list.ToArray();
Banana[] bananas = (Banana[]) areTheseBananas; // causes runtime casting exception!
Why doesn't the language allow this?

I know you can do...
ArrayList list = new ArrayList();
list.Add(new Banana());
Banana[] bananas = (Banana[]) list.ToArray(typeof(Banana));
...but in the code I am working with I don't know the type of the array at the point that ToArray is being called.

Thursday, February 09, 2006

Missing method? What missing method?

I just spend the best part of a day tracing down why my unit tests were throwing a 'MissingMethodException' with a description of exactly the menthod that was there. I recompiled, I rebooted, I did everything I could think of. It still didn't find the method. I wrote a simpler test. No good. I ended up with a test that created an instance of the class and called a one line method that just returned a constant int. Still no good.

I explored the assembly that was being created when I compiled with Reflector. Everything was there. All in place. So the assembly was created ok.
So..the hunt was on. What assembly is NUnit picking up?

Eventually.. through the use of fuslogvw.exe and this helpful advice I eventually found it was loading from 'C:\Documents and Settings\nigel\Local Settings\Application Data\assembly\...someplace'. I tried to delete this directory, and couldn't because devenv.exe was using it. So it turns out Dev Studio was helpfully holding onto an old version of an assembly I was working on in a place that .Net checks BEFORE it checks the directory the test assembly was in. How rediculious!

I killed closed dev studio. I killed the devenv process that just kept hanging around even though devstudio was no longer running (nice) and then deleted the '...Application Data\assembly' directory.

This time I run the tests and Ping! lovely green lights.

I mention this so maybe it will save you the 1/2 day it cost me. :)



Monday, February 06, 2006

online calendar with RSS

One of the many projects I have on the back burner (waiting for enthusiasm) is an online calendar/agrigator.

http://www.spongecell.com is a good calendar (with lots of ajax goodness) and a nice simple plain text way of adding appointments.


GitHub Projects