Archive for March, 2007

Why Contractors Charge More

Mar 27

I posted about this a while back along the fiscal side of it but there were a lot of things that I did not mention.

-We get zero benefits. That means if we are sick for a week, month, year we don’t make any money yet still have to pay our bills somehow. If you go to the dentist shell out (unless you have purchased your own coverage of course)

-We do not qualify for EI. Being self employed means that you don’t have to pay into EI (w00 h00) but it also means that if a contract ends we can not collect.

-Training costs double. First off you have to pay for the course (and travel to it) itself. Second you are not being able to bill for that time so it costs you money to go.

-Vacations are the same way in that you just don’t make any money for that period. (Maybe this is why a lot of contractors seem to always be working)

I recently had a friend of mine have a heart attack and has to take two months of work. Thankfully his company is paying him a salary while he is off. I was thinking how that would affect contractors. If you could not bill for 2+ months could you afford that?

Filed Under: General

Database Tricks – Effective Records

Mar 23

 One common place where people replicate data is in situations where you need data at a specific point in time. A perfect example I saw today was this situation:

You can see that the price column is duplicated in both the products and orderDetails table. The rationale for this is that so that when the price of the product changes that it does not affect historical orders.

This is a fairly viable solution but has a few drawbacks. The main one is data duplication. Most of the time the data will be the same (unless product pricing changes frequently). The second one is that if the product name changes it will affect historical data (to overcome that we could add another column to orderDetails called productName but that is leading to more duplication). The final issue is that if a product is to be remove from the catalog then the order detail would have to be removed as well (or some other ugly alternative).

The simple fix for this is to use the effective record approach. With this approach whenever you change a row you expire the original record and create a new record with the new changes in it. In this example we will change the products table to have an effectiveEndDate column on it and remove price from the orderDetails table:

And here is a sample of changing the products table data:

ProductId     Name         Price    EffectiveEndDate
1                 Oranges     1.99     null

so lets say we update Oranges to be $2.35 we would first expire the original and then create a new record with the updated information:

ProductId     Name         Price    EffectiveEndDate
1                 Oranges     1.99     3/21/2007 12:00AM
2                 Oranges     2.35     null

So now people who purchased oranges before 3/21/2007 will show the price of 1.99 while purchases made after will be linked to the $2.35 price.

Things to Consider
1. Anything that queries a table must factor in wether or not to return expired records.
2. When updating a record you now need to expire the old record and create a new record which is more work and should be done in a transactional context incase of failure.
3. To delete a record simply change the record so that it’s EffectiveEndDate is current.
4. Using nulls in the effectiveEndDate column. This can be tricky as it is not a valid date but the best way to describe an indeterminate expiry. Some people use a high date value which makes writing queries easier for this data but is really a magic number which makes me cringe. One technique I have used is to create a function called MaxDateTime() that returns my magic maxdatetime and then have a query like this:
    select    *
    from     product
    where   isnull(EffectiveEndDate, MaxDateTime()) > getDate()
so if the EffectiveEndDate is null convert it to our magic number (that is only in one place due to our use of a function) and ensure that the expiry is greater than today.

You don’t have to use dates either but I like that I can see when a record was taken out of commission. I bit field called IsExpired could be used if you would like.

Another thing that I have implemented before is an audit trail on records that shows which record was changed. I do this by adding a ParentRecord column to the table e.g.:

ProductId     Name         Price    EffectiveEndDate          ParentRecord
1                 Oranges     1.99     3/21/2007 12:00AM      null
2                 Oranges     2.35     4/21/2007 12:00AM      1
3                 Tangerines 2.35     null                             2

Another Use
Another use for this that I have done is time sensitive pricing. For this I had to effective columns. The first is the start date and the second is the end date. This allows to have a record be active for a period of time:

ProductId     Name         Price    EffectiveStartDate      EffectiveEndDate         
1                 Oranges     1.99     3/21/2007 12:00AM    4/21/2007 12:00AM
2                 Oranges     2.35     4/21/2007 12:00AM    5/21/2007 12:00AM
3                 Oranges     2.95     5/21/2007 12:00AM    null

In this case the price is effective for a month. The select query (if properly written) will automatically return the proper price for the point in time which the query is run.

Filed Under: Sql

2007 – The Year Of The Developer

Mar 17

I know I missed Chinese new year but it is not going to stop me from Declaring 2007 as THE year of the coder. Things just seem to be coming together.

Edmug is growing and keeps on having good presentations. Its great to see the membership increasing and also keep seeing new faces. I am disappointed that there are a lot of faces that I don’t see anymore but that’s not my problem. I am starting to work on some ideas for a presentation but its so hard to come up with something that is the caliber of the previous presentations.

I don’t know if its just spring talking but this city seems to be starting down the path of good development. New usergroups are popping up like OWASP and the EAMUG which is great to see more of a community forming. I see a lot of developers working towards improving themselves which is great. I really hope that things keep rolling forward here.

I have also decided to start taking off early on fridays to learn or practice something new. Already I have found this to be really good and I would recommend it to anyone that can do it. I am to much of a procrastinator so its nice to have 4-5 hours of time dedicated to learning / improvement / beer (ok its usually beer).

I might try to get out of town a bit and do some speaking too once I have some presentation ideas and a place to talk at.

I am stoked for this year and I hope it brings good fortune (or contracts) to everyone!

Filed Under: Uncategorized

Agile Users Group First Meeting

Mar 15

 I went out the agile user groups first meeting last night. Vlad (who started the group) did the first presentation which was a good overview of Agile and then Scrum and XP. For me I found that very valuable as I have never worked on a project using agile techniques.  I got a lot of good information from his presentation

The meeting was quite open and informal with people asking questions and contributing experiences of their use with Agile techniques. I found that to be good as well showing some of the tradeoffs and risks of using these techniques. The big issue seemed to be people turning a technique into a process and getting stuck trying to follow “rules” when agile is meant to be open and adaptive.

I look forward to more meetings and more talks around this. It seems that lots of people have things to contribute and lots of things to debate so it should be a good group if not only for fights to the death. Insert fight music:

Filed Under: Uncategorized

Vista

Mar 5

So I got a vista computer and I am pretty disappointed. A big issue is the UAC introduced. Great idea (stolen from mac of course) but poorly implemented. Check out the latest Mac vs. PC commercial that explains it way better than I can.

Filed Under: Uncategorized

Course Followup

Mar 5

It has been a week since JPs developer bootcamp ended and I thought I would post on some of the things I am noticing:

1. Subversion = life. I had a subversion server here for a while but now I use it for everything. Every project I have done or started on is now in there.

2. TDD is hard. Its really hard to switch my brain to work this way but it is starting to get easier and easier.

3. Single Responsibility. Every class I am making is now doing only one task. It is amazing to look at a class and say “that’s doing to much”, refactor, and boom! some nice clean testable code.

4. Along the same lines as TDD is using an xUnit framework. The last thing I want to build is a UI so its great to develop with that mindset that the application must be consumable (and testable) from the xUnit tool. Keeps me from putting logic into the UI

5. My brain hurts more. I am really trying to look at things to figure out a better way to do something. It’s a good change from just throwing code at the project until it works.

6. I have not touched nant / cc.net at all because I really don’t want to tackle that learning curve this week.

Filed Under: Uncategorized

Rethrowing Exceptions

Mar 2

One mistake I see developers constantly doing is how they handle and rethrow an exception. A lot of developers catch an exception and rethrow it as shown here (note the throw ex):

 

        private void button1_Click(object sender, EventArgs e)
        {
            try {
                Three();
            } catch (Exception ex)
            {
                throw ex;
            }
        }

        private void Three()
        {
             try {
            Two();
            } catch (Exception ex)
            {
                throw ex;
            }
        }

        private void Two()
        {
            try {
            One();
            } catch (Exception ex)
            {
                throw ex;
            }
        }

        private void One()
        {
            throw new NotImplementedException(“one is not implemented yet”);
        }

The issue with using throw ex is that it wipes out the stack trace so the exception will look like it actually happened in button1_Click not in the One method.

System.NotImplementedException was unhandled
  Message=”one is not implemented yet”
  Source=”ThrowAway.ExceptionExamples”
  StackTrace:
       at ThrowAway.ExceptionExamples.Form1.button1_Click(Object sender, EventArgs e) in ..\Form1.cs:line 26
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)

If you need to catch and rethrow an exception (and this sample certainly does not but is done for illustration purposes). Just call throw (without the ex on the end):

  private void button1_Click(object sender, EventArgs e)
        {
            try {
                Three();
            } catch (Exception ex)
            {
                throw;
            }
        }

        private void Three()
        {
            try {
            Two();
            } catch (Exception ex)
            {
                throw;
            }
        }

        private void Two()
        {
            try {
            One();
            } catch (Exception ex)
            {
                throw;
            }
        }

        private void One()
        {
            throw new NotImplementedException(“one is not implemented yet”);
        }

The stack trace will then show the actuall method that caused the exception:

System.NotImplementedException was unhandled
  Message=”one is not implemented yet”
  Source=”ThrowAway.ExceptionExamples”
  StackTrace:
       at ThrowAway.ExceptionExamples.Form1.One() in ..Form1.cs:line 56
       at ThrowAway.ExceptionExamples.Form1.Two() in ..Form1.cs:line 50
       at ThrowAway.ExceptionExamples.Form1.Three() in ..Form1.cs:line 38
       at ThrowAway.ExceptionExamples.Form1.button1_Click(Object sender, EventArgs e) in ..Form1.cs:line 26

 Granted you should never have to catch and rethrow exceptions like this in your app anyways unless you need to change the type of the exception, or if you want to capture and log the exception.

Filed Under: Uncategorized

Two Frameworks and SQL State Server

Mar 2

<plagiarize>
There are also three different ways where session state can be stored:

  1. On the web server itself (“In process”)
  2. On another machine in memory (“state server”)
  3. In SQL Server

It is recommended by many people that if you want session state to be usable in a load balanced environment then you need to choose option 2 or 3. If you want this session state to persist in the event of a server hiccup then you need to use option 3, SQL Server. This has some benefits (persistence of data, usable in a load balanced environment) but some distinct disadvantages (slower than the other two options and it requires objects to be serializable).
</plagiarize>

What surprised me is that the same session state database that was used for .NET 1.1 cannot be used for session state in .NET 2.0. So, in order to use session state with .NET 2.0 you will need to separate state databases.

<plagiarize>
Theoretically .NET 1.0 and 1.1 web apps can use the .NET 2.0 database for storing state, but this has not yet been tested.
</plagiarize>

*NOTE: plagiarism comes from Donald Jessop of Alberta Education fame

Filed Under: Uncategorized