2007-06-21

Quantum bugs

I've just been driven mad by a "Quantum bug". What is a quantum bug? Well, it's a bug that doesn't exist unless you look at it :-)

I kept getting a NullReferenceException in my code. It happened whenever I changed a property of a class. I assumed it was something in the framework, but it was in fact in a library of my own. When the property changed it would trigger an observer I had attached, the purpose of this observer is to check if the form that owns my component is the active form, if it is then it records the object that changed; the purpose is to know which objects were modified by which form.

Anyway, to check if the form is active I do this

Form activeForm = System.Windows.Forms.Form.ActiveForm;
Form activeMdiChildForm = activeForm.ActiveMdiChild;
if (activeForm == this.Form || activeMdiChildForm == this.Form)
.....

Now the quantum!

If you are debugging your app Form.ActiveForm will always return null! How useful is that eh? :-) So, when debugging my app I get a NullReferenceException, when I let it run through I don't.

There was me being a good boy, stepping through the code I had just added to check it does what it is supposed to, and Bill punishes me for it!

Damn :-)

2007-06-16

Cocoa, falling at the first hurdle?

I was REALLY looking forward to programming some apps on the Mac using Cocoa and objective-C.

I am reading "Cocoa programming for MAC OS X" by Aaron Hillegas. I had read as far as page 30 when I saw the following:

"Objective-C is a very simple language. It has no visibility specifiers: All methods are public; and all instance variables are protected. (Actually, there are instance specifiers for instance variables, but they are rarely used. The default is protected, and that works nicely.)"


WHAT?

Works nicely? I disagree! It is actually possible to make a class's method private by not including it in the interface declaration (myclass.h) and just adding the implementation file instead, but what about protected methods?

public
I expose as little as needed to ensure the class provides the service it was designed to. The signatures/names of members in my public area change as little as possible so as not to break other people's code. Public members are therefore inflexible.

protected
I am happy to expose a few more methods here. Typically virtual methods so that anyone inheriting from my class can hook in their own code and modify the behaviour of my class. Protected members are a little more flexible to change as fewer lines of code depend on them, but I still try to change them as little as possible.

internal / friend
These are vital to me. They allow me (and only me) access to certain members of other classes. I find this invaluable when I need to have some kind of collaboration between classes in a framework that nobody else should have access to, including descendants of my own classes that are not in the same assembly/module.

private
An absolute necessity. Access to some members would allow a consumer to manipulate members of your class without obeying "the rules". This would include adding items to a private array member without going through the relevant method which might perform other tasks such as


  1. Enforcing read-only.

  2. Checking security permissions.

  3. Executing other methods to enforce proper business procedure.



As you can see, I feel very strongly about encapsulation. It provides flexibility to change the way you provide your service and enforces correct behaviour. When you order new computer from a manufacturer you don't walk along the conveyor belt interfering with the way each element is added do you? You interface with the manufacturer using the approved process (public PlaceOrder) and then leave them to get on with the 100 or so private procedures they go through to fulfill the order.

Never before have I read so little about a language before deciding it is a waste of time proceeding! Maybe encapsulation will be implemented along with the new garbage collection features (I wont go into reference counting) that is coming in the next version of Mac OS X. I'll have to wait and see!

Enough rails for me

That's it, I've had enough of Ruby on Rails!

I like the rails part, it's a very clever approach, but I really dislike the Ruby part!

The final straw occurred yesterday. It's very common in OOP to have the constructor set default values for your object just in case the consumer of your class does not set them. In C# I would do something like this.....

public class Post : MyBaseClass
{
private bool isPost = true;
}

In Ruby I was trying to achieve this simple behaviour...

01: IsPost is set to true
02: A form is displayed with the default value
03: User changes the value
04: My Post instance is updated with the values from the form

I tried to override Initialize() only.

def Initialize
super
is_post = true
end

Now Post.new(params[:post]) is not reachable for some reason.


So in my Post class I did this

def Initialize(* params)
super(params)
is_post = true
end

The problem here is when I try to initialize the values from the form in my controller class.

post = Post.new(params[:post])

This means that the super(params) is called (which sets is_post to the value in the form) and then sets is_post = true. So my value is always true regardless of what the user enters.

So I tried putting "is_post = true" before the call to super, but that throws a null reference exception.

Finally someone told me to merge hashes like this

{:is_post => true}.merge(params);
super(params)

I don't recall the error, but that didn't work either.


I'm sorry, but ANY language that gives me this much hassle just to set a default is not worth it.

C# on rails, now *that* would be nice!

2007-06-01

Installing Rails on a Mac

What a lot of grief!

After trying more simple steps on another site (and failing) I eventually managed to get the following steps to work:

http://hivelogic.com/narrative/articles/ruby-rails-mongrel-mysql-osx


There were however some changes:

01: Make sure you download TextMate. I kept reading it as TextEdit for some reason, so just be aware of that when "mate ~/.bash_login" wont work.

02: One of the steps tells you to type "make ~/.bash_login". I found that adding the suggested text to that file made no difference when you log out/in or close the Terminal window. This was because a file named "~/.bash_profile" existed. If this file exists then you should modify it instead of the one mentioned in the article.

03: When trying to do "rake db:migrate" I would experience the following error:

otherwise you will experience the following error:
dyld: NSLinkModule() error
dyld: Library not loaded: /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib
Referenced from: /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.bundle
Reason: image not found
Trace/BPT trap


This is because the latest version of MySql has changed a path from lib/mysql to just lib/. To fix this problem you need to type the following (it's all one line):

sudo install_name_tool -change /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib /usr/local/mysql/lib/libmysqlclient.15.dylib /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7/lib/mysql.bundle

Right then. Now that those Hellish few days are over it's time to get annoyed with trying to write a website I suppose :-)

2007-05-16

Convert absolute path to relative path

Today I needed to convert an absolute path to a relative path based on a specified base path. E.g.

c:\a\b\c -> c:\a\b\c\d\file.txt = d\file.txt
c:\a\b\c -> c:\a\file.txt = ..\..\file.txt
c:\a\b\c -> c:\a\x\file.txt = ..\..\x\file.txt

I am surprised there is nothing in the .NET framework so I had a hunt around and converted the code from the following URL (http://www.vergentsoftware.com/blogs/ckinsman/default.aspx?date=2006-08-07) into C#....


private string RelativePath(string absolutePath, string relativeTo)
{
string[] absoluteDirectories = absolutePath.Split('\\');
string[] relativeDirectories = relativeTo.Split('\\');

//Get the shortest of the two paths
int length = absoluteDirectories.Length < relativeDirectories.Length ? absoluteDirectories.Length : relativeDirectories.Length;

//Use to determine where in the loop we exited
int lastCommonRoot = -1;
int index;

//Find common root
for (index = 0; index < length; index++)
if (absoluteDirectories[index] == relativeDirectories[index])
lastCommonRoot = index;
else
break;

//If we didn't find a common prefix then throw
if (lastCommonRoot == -1)
throw new ArgumentException("Paths do not have a common base");

//Build up the relative path
StringBuilder relativePath = new StringBuilder();

//Add on the ..
for (index = lastCommonRoot + 1; index < absoluteDirectories.Length; index++)
if (absoluteDirectories[index].Length > 0)
relativePath.Append("..\\");

//Add on the folders
for (index = lastCommonRoot + 1; index < relativeDirectories.Length - 1; index++)
relativePath.Append(relativeDirectories[index] + "\\");
relativePath.Append(relativeDirectories[relativeDirectories.Length - 1]);

return relativePath.ToString();
}

2007-05-08

Acer Skoda - I mean Ferrari

I have owned an Acer Ferrari now for approximately 13 months. Previously I had a problem with it freezing randomly. I spent some time trying to reproduce the problem and after a month or two was finally able to reproduce it 100% of the time. I sent my laptop back to Acer armed with exact steps and as a result my laptop was returned to me after only a couple of days.

For the past 2-3 months I have been seeing very rare, random resets. Obviously I blamed Windows, as you do :-) Some software I had to convert AVI to MPEG would always reset the laptop, but I just put that down to dodgy software. More recently I noticed that 7-zip would also reset my laptop *only* if I used ULTRA compression on a file larger than 500MB.

So I tried Winzip, same problem. I installed Vista and tried both Winzip and 7-zip in there, same problem. With steps to reproduce + proof that it was not the OS I felt I could finally send the laptop back for repair without running the risk of it coming back "No fault found".

The problem lies in my opening sentence "I have owned an Acer Ferrari now for approximately 13 months". The problem is that in my quest to find 100% reproducible steps my warranty has expired by about 30 days.

I explained all of this to the guy on the phone at Acer. "If only more customers were like you!" he said, and then continued to tell me that I had to pay for the repair. After talking over me for a few minutes I finally lost my temper and swore at him, something I have never done before.

So, now I am really annoyed! I have an old £400 Hewlett Packard laptop that has been working (slowly) without problems for years now, yet I pay thousands for a laptop with a quality brand such as "Ferrari" and it breaks twice in 13 months! I'd expect the quality to be better, wouldn't you?

What *really* annoys me though is that Acer didn't send me a card telling me my warranty was nearly up and asking if I would like to extend it (which I would have done), and ofcourse I had no idea it was coming to an end because the receipt is.......surprise surprise it is with my accountant who is preparing last year's accounts!

Ferrari I am disappointed. You have put your name to a machine that has the body of an F1 and the inner workings of a cheap old banger!

Sprites

Someone sent me this on Skype this morning. I think it's really cool, it reminds me of the hardware sprites on the old Commodore 64!

01: Go to any site with lots of images (image search on google is a good one)
02: Once the images appear copy/paste this text into your address bar.

javascript:R=0; x1=.1; y1=.05; x2=.25; y2=.24; x3=1.6; y3=.24; x4=300; y4=200; x5=300; y5=200; DI=document.images; DIL=DI.length; function A(){for(i=0; i<DIL; i++){DIS=DI[ i ].style; DIS.position='absolute'; DIS.left=Math.sin(R*x1+i*x2+x3)*x4+x5; DIS.top=Math.cos(R*y1+i*y2+y3)*y4+y5}R++}setInterval('A()',5 ); void(0);