2004-12-03

State Machine

I have been working with the State Pattern for several months now.  It is a great way to control an application in a Kung Fu sort of way.  It has a very hands off director feel to it.

I am planning on writing up a full doc on my take on the pattern and an implimentation of it, based off of the MyXaml MxStateMachine.  I like the looks of MyXaml but it seems too declarative for me.  I plan to impliment a code generation process to build the State Machine.

2004-11-19

World of Warcraft

Blizzard Entertainment - World of Warcraft I have been waiting for over three years for WOW to come out. Is it worth the wait? Am I going to rush and play it? Should I? After all these years are online dungeons even relevant?

UPDATE:  My wife bought me the game for Christmas, but she said I can't open it until Christmas!  But we are going to Flint, MI for Christmas so I won't be able to play it until New Years Eve, when we get back.  Oh the humanity of it all!

2004-11-12

Closer

Scott Peterson is found guilty.  Finally, maybe this will become a non-issue now.  I'm sick of hearing about this case, Who Cares!  There are far worse attrocities going on in the world, who cares about this cheap ass punk?

Arafat is dead.  Finally!  He did nothing but cause problems.  Maybe now the Palestinians can get some solid leadership.

The election is a solid win.

What ever are the reporters going to harp on now?  I'm sure they will find something, the bastards.

2004-09-24

Death

There is one primary motivating factor in my life:  Death.  One day, I will die.

I imagine what it is like, that momement when I realize that this is it, game over.  I expect I will chuckle and think "well, this is it" and try to be philosophical.  If I can see I will concentrate on my final vision.  I hope I will think of my wife and son, if I am not in too much pain.

I see it happening in one of two ways, either a heartattach or an auto accident.  I fear that it will be in the middle of the night.  I'll wake up and go, "crap" and that will be that.  I'll probably shit my pants then.

This is hard for me to write because it is one my deepest emotions, something I have felt for a long long time.  At least since I was a teenager.  I don't know the exact moment I felt it but at that point I knew my mortality and felt real fear.  The fear of the end but more so the fear of not having accomplished anything.

I have accomplished one good thing.  Thomas Edward.  My son.  I pray that God watches over him and protects him.

2004-09-09

The Election

If you hadn't noticed the US Presidential Election is in full swing. I find all this about as exciting as watching a train wreck, I'm too enthralled to look away.

I see lots of mud slinging on both sides. Crap about both candidates Vietnam War anticts. "He's so liberal he will let the UN run us." "He's so conservative he will let all of his rich buddies go tax free." Its the same old crap that both sides have been saying for years, but as much as we bemone the mud slinging we, the fickel mob, enjoy it. We thrive on it.

There are issues being talked about. Yep, the same issues that got mentioned in the previous half centuries elections: War and Peace, Taxes, The Economy, Education, Entitlement Programs (welfare/medicade/social securty/healthcare, take your pick), and of course each candidate will do what is right for THE CHILDREN.

There are two big issues though that are not getting talked about, and they are issues that affect not only the IT industry as a whole but every American consumer as well as the future of technology growth in this country. These issues of course are Copyright and Patent laws.

The Geek Community has been screaming about these issues since Napster was crushed by the RIAA. The fair use rights of consumers are being threatened by big conglomerates who wish to control how we partake of the entertianment they produce. Their money making process is being threatend by technology that eliminates the prime motherloads: Commercials and Price Fixing.

At what point do we, the consumer, get fed up with their schenanigans and rebel?

Software patents are stifling inovation in the IT industry. The between SCO suing everyone and Microsoft filing for patents like it is the end of time we the developers of code will one day have to include law degrees on our resume. I personally want to code, not fanagal with some smuck over the definition of IS.

The candidates need to address these issues. We the People need to ask them their policies on these issues. Pretty soon, if we don't, then we will wind up paying just to watch each episode of Survivor.

Oh, the humanity.

2004-07-28

Hey Man, so long as it works that is all that matters...

So says my bossman at least.  And such is the attiutude of a lot of programmers.

But it is not All That Matters.  It is a side affect of doing things right and an accident when you do things wrong.  There is more to building software, lots more, and it is unfortunate that some people will never see beyond the short sided goals of "Making it Work."

It kills me to here people I work with say that.  Sometimes I feel that I am the only person innovating, or even just trying to improve my work practices.  I have to fight to make them use source control much less follow any kind of standard development procedure.

I try to lead by example but that does not work.  I give up.

2004-02-27

Secure in Capatilism

I know that no socialistic form of government will ever last very long.  I know this because, no matter how good the intentions some one will always take advantage of the system.  There will always be some one to take advantage of some one elses generosity. 

How do I know this?  Because of the Coffee Leaches!

We have a pretty nice coffee bar in our office.  We have fresh beans, good quaility beans, and a grinder.  One of the people I work with could be considered a Coffee Snob, but he is a super nice guy.  He has no problem going out of his way to help out others.  And he loves coffee.  He is the one behind the coffee bar and the reason it is so top notch.

But we also have Coffee Leaches.  The ones who always leave just enough coffee in the bottom of the pot so they can, in their little minds, justify not making the next pot.  They do not contribute to the effort to make the coffee or in any other way support it.

Their efforts have proven to me that the Leaches will always create the class system.  The doers and the slugs.  Because of this I rest easy.

2004-02-13

Learning the Three R's

Respectful - Of your parents

Resourceful - Think for yourself

Responsable - Be a man, take the blame

2004-02-12

Chasing the Technology

A big problem I have with people who consider themselves programmers is that they spend most of their time chasing the technology.  They spend more time whack-a-mole code hacking and no time trying to learn the basics, like source control, methodologies or algorithms.

I personally lack in these areas because I do not come from a Computer Science background, but I strive to overcome this.  My favorite computer books deal with ways of thinking about and doing programming, not just code cookbooks.  I observe my programming habits and change them to be better.  I make the effort that I don't see these others making.

I see the chase in people I work with and friends of mine.  I see it in people that think they are doing a great job just because the project they are working on compiles, runs and the client likes the pretty interface.  Unfortunately they have taken so many short cuts in building their monolithic bag-o-crap that the slightest requirements change pops the poop bag leaving a big nasty mess to clean up.

Or they make a change in the code then forget why they changed it and what they changed now the program breaks.  If only they had used soruce control they could roll back to a working copy.

I love the blank stares I get from these chasers when I say that, instead of learning xyz programming language, they should read a programming book, like The Pragmatic Programmer or The Mythical Man Month.  They look at these books and ask "But where is the CODE?"

This is when I realize that I have real job security.

2004-01-30

Period Matirx Twisting

The Period Matrix table, Pd_Matrix, is used in calculating accounting period formulas. These formulas include current month, quarter or years activity or balance. These formulas can also be created using standard SQL functions but not all functions are common between SQL Server and MS Access. The Period Matrix has the advantage in that it can be used in any DBMS and it also can be used in table pivoting and folding.

The table listed below is for use with the Activity table and not the Balance table. Either a separate matrix table will have to be created or an additional field would have to be added for calculating account formulas with the Balance table.

Pd_TypePd_IDPd_Namepd_1pd_2pd_3pd_4pd_5pd_6pd_7pd_8pd_9pd_10pd_11pd_12
annual 1 YR 1 1 1 1 1 1 1 1 1 1 1 1
month 1 JAN 1 0 0 0 0 0 0 0 0 0 0 0
month 2 FEB 0 1 0 0 0 0 0 0 0 0 0 0
month 3 MAR 0 0 1 0 0 0 0 0 0 0 0 0
month 4 APR 0 0 0 1 0 0 0 0 0 0 0 0
month 5 MAY 0 0 0 0 1 0 0 0 0 0 0 0
month 6 JUN 0 0 0 0 0 1 0 0 0 0 0 0
month 7 JUL 0 0 0 0 0 0 1 0 0 0 0 0
month 8 AUG 0 0 0 0 0 0 0 1 0 0 0 0
month 9 SEP 0 0 0 0 0 0 0 0 1 0 0 0
month 10 OCT 0 0 0 0 0 0 0 0 0 1 0 0
month 11 NOV 0 0 0 0 0 0 0 0 0 0 1 0
month 12 DEC 0 0 0 0 0 0 0 0 0 0 0 1
quarter 1 QTR1 1 1 1 0 0 0 0 0 0 0 0 0
quarter 2 QTR2 0 0 0 1 1 1 0 0 0 0 0 0
quarter 3 QTR3 0 0 0 0 0 0 1 1 1 0 0 0
quarter 4 QTR4 0 0 0 0 0 0 0 0 0 1 1 1
semiannual 1 SA1 1 1 1 1 1 1 0 0 0 0 0 0
semiannual 2 SA2 0 0 0 0 0 0 1 1 1 1 1 1

Pd_Type

The Period Type is used to indicate the type of formula being used. The types listed are:

  • Month: The given months activity

  • Quarter: The given quarters activity

  • Semiannual: The given half-year activity

  • Annual: The entire years activity

  • Other types that could be created include a months or quarter ending balance.

Pd_ID and PD_Name

The Period ID and Name are used to identify a given time frame:

  • Month: The month number (1 thru 12) and name (January thru December)
  • Quarter: The quarter number (1 thru 4) and name (QTR1 thru QTR4)
  • Semiannual: The semiannual number (1 or2) and name (SA1 and SA2)
  • Annual: The year number (1) or name (YR)

pd_1 thru pd_12

The values for period 1 through 12 can only be 1 or 0. These fields are multiplied with the Activity table month fields to create the necessary formulas.

Creating Formulas

To create a formula using the Period Matrix and the Activity table you would create a select statement where in the period fields in the Activity table were multiplied to the period fields in the Matrix table then add them all together:

( Activity.Jan * Pd_Matrix.pd_1 + Activity.Feb * Pd_Matrix.pd_2
Activity.Mar * Pd_Matrix.pd_3 + Activity.Apr * Pd_Matrix.pd_4
Activity.May * Pd_Matrix.pd_5 + Activity.Jun * Pd_Matrix.pd_6
Activity.Jul * Pd_Matrix.pd_7 + Activity.Aug * Pd_Matrix.pd_8
Activity.Sep * Pd_Matrix.pd_9 + Activity.Oct * Pd_Matrix.pd_10
Activity.Nov * Pd_Matrix.pd_11 + Activity.Dec * Pd_Matrix.pd_12)

In the WHERE clause of the select statement you would set the Period Type (Pd_Type) and Period Name (Pd_Name) of the Matrix table to the name of the formula. For example, if you wanted to show the monthly activity for January you would create the following select statement:

SELECT
      Activity.AcctID
     , Activity.DeptID
     , Activity.Class
     , Pd_Matrix.Pd_Name
     , Activity.Year
     , ( Activity.Jan * Pd_Matrix.pd_1 + Activity.Feb * Pd_Matrix.pd_2
         + Activity.Mar * Pd_Matrix.pd_3 + Activity.Apr * Pd_Matrix.pd_4
         + Activity.May * Pd_Matrix.pd_5 + Activity.Jun * Pd_Matrix.pd_6
         + Activity.Jul * Pd_Matrix.pd_7 + Activity.Aug * Pd_Matrix.pd_8
         + Activity.Sep * Pd_Matrix.pd_9 + Activity.Oct * Pd_Matrix.pd_10
         + Activity.Nov * Pd_Matrix.pd_11 + Activity.Dec * Pd_Matrix.pd_12
         ) As Amount

FROM Activity, Pd_Matrix

WHERE
         (Pd_Type = 'month')
     And (Pd_Matrix.Pd_Name = 'JAN')

This SQL statement could be changed to a parameterized stored procedure with variables for Pd_Type and Pd_Name. This would allow you to return a recordset for any of the given formulas.

Table Folding

The Period Matrix can also be used in table folding, where the columns of the original table are converted to rows. This is accomplished by creating a Cartesian product between the original table, in our case the Activity table, and the Period Matrix table.

A Cartesian product result set, also known as a Cross Join, is familiar to people that have created SQL statements with bad or missing joins between tables. They are recognizable because the result set will have the same row multiple times or the total row count far exceeds the total number of expected rows. In most cases this is unwanted but it comes in very handy for table folding.

The following SQL select statement will fold the Activity table's period activity columns into rows:

SELECT

       Activity.AcctID
     , Activity.DeptID
     , Activity.Class
     , Pd_Matrix.Pd_Name
     , Activity.Year
     ,    (Activity.Jan * Pd_Matrix.pd_1 + Activity.Feb * Pd_Matrix.pd_2
         + Activity.Mar * Pd_Matrix.pd_3 + Activity.Apr * Pd_Matrix.pd_4
         + Activity.May * Pd_Matrix.pd_5 + Activity.Jun * Pd_Matrix.pd_6
         + Activity.Jul * Pd_Matrix.pd_7 + Activity.Aug * Pd_Matrix.pd_8
         + Activity.Sep * Pd_Matrix.pd_9 + Activity.Oct * Pd_Matrix.pd_10
         + Activity.Nov * Pd_Matrix.pd_11 + Activity.Dec * Pd_Matrix.pd_12
         ) As Amount

FROM Activity, Pd_Matrix
 
WHERE Pd_Type = 'month'

Note that this is identical to the SQL used to show the activity for just one month used previously, with the only exception being in the WHERE clause. Instead of indicating which month (Pd_Name) to show, the SQL was allowed to return all the available months.

This statement works for the same reasons the previous SQL statement worked. When the Pd_Name is JAN then pd_1 equals one while the rest of the pd_2 through pd_12 equals zero. When the multiplications are carried out all the results are all zero except for January's, which is equal to the January activity. When the addition is carried out therefore only the January amount is returned.

Also, since there is no join between the two tables the result set will have twelve times the number of rows that the Activity table actually has. For each row in the Activity table there will be twelve rows generated in the result because there are twelve possible rows for the Pd_Type of 'month'. If instead of choosing 'month' we had chosen 'quarter' for the Period Type then the results set would have four times the number of rows in the Activity table.

This method of table folding is fast and very simple to create and maintain. The most common way table folding is done though is by manually creating the final table then creating an append or update query for each column to fold. In the case of our example we would have had to create and execute thirteen queries in order to obtain the same results.

Table Folding and Pivoting Using Formulas

Where as table folding is deals with turning columns into rows, table pivoting is turning rows into columns.

Taking the example from the table folding section lets add the requirement that our resultset show the Actual and Budget amounts in separate columns. The periods will still be shown as rows.

The SQL needed in Access would be:

SELECT

       Activity.AcctID
     , Activity.DeptID
     , Pd_Matrix.Pd_Name
     , Activity.Year
     ,Sum( IIF(Activity.Class = "Actual",
         (Activity.Jan * Pd_Matrix.pd_1 + Activity.Feb * Pd_Matrix.pd_2
         + Activity.Mar * Pd_Matrix.pd_3 + Activity.Apr * Pd_Matrix.pd_4
         + Activity.May * Pd_Matrix.pd_5 + Activity.Jun * Pd_Matrix.pd_6
         + Activity.Jul * Pd_Matrix.pd_7 + Activity.Aug * Pd_Matrix.pd_8
         + Activity.Sep * Pd_Matrix.pd_9 + Activity.Oct * Pd_Matrix.pd_10
         + Activity.Nov * Pd_Matrix.pd_11 + Activity.Dec * Pd_Matrix.pd_12
         ), 0)) As Actual

     ,Sum( IIF(Activity.Class = "Budget",
         (Activity.Jan * Pd_Matrix.pd_1 + Activity.Feb * Pd_Matrix.pd_2
         + Activity.Mar * Pd_Matrix.pd_3 + Activity.Apr * Pd_Matrix.pd_4
         + Activity.May * Pd_Matrix.pd_5 + Activity.Jun * Pd_Matrix.pd_6
         + Activity.Jul * Pd_Matrix.pd_7 + Activity.Aug * Pd_Matrix.pd_8
         + Activity.Sep * Pd_Matrix.pd_9 + Activity.Oct * Pd_Matrix.pd_10
         + Activity.Nov * Pd_Matrix.pd_11 + Activity.Dec * Pd_Matrix.pd_12
         ), 0)) As Budget

FROM Activity, Pd_Matrix

WHERE Pd_Type = 'month'

GROUP BY
       Activity.AcctID
     , Activity.DeptID
     , Pd_Matrix.Pd_Name
     , Activity.Year

In SQL Server the CASE statement would replace the IIF( ) function:

SELECT

       Activity.AcctID
     , Activity.DeptID
     , Pd_Matrix.Pd_Name
     , Activity.Year
    
     ,Sum( CASE Activity.Class
         WHEN "Actual"THEN
         (Activity.Jan * Pd_Matrix.pd_1 + Activity.Feb * Pd_Matrix.pd_2
         + Activity.Mar * Pd_Matrix.pd_3 + Activity.Apr * Pd_Matrix.pd_4
         + Activity.May * Pd_Matrix.pd_5 + Activity.Jun * Pd_Matrix.pd_6
         + Activity.Jul * Pd_Matrix.pd_7 + Activity.Aug * Pd_Matrix.pd_8
         + Activity.Sep * Pd_Matrix.pd_9 + Activity.Oct * Pd_Matrix.pd_10
         + Activity.Nov * Pd_Matrix.pd_11 + Activity.Dec * Pd_Matrix.pd_12
         ) ELSE 0 END) Actual

     ,Sum( CASE Activity.Class
         WHEN "Budget"THEN
         (Activity.Jan * Pd_Matrix.pd_1 + Activity.Feb * Pd_Matrix.pd_2
         + Activity.Mar * Pd_Matrix.pd_3 + Activity.Apr * Pd_Matrix.pd_4
         + Activity.May * Pd_Matrix.pd_5 + Activity.Jun * Pd_Matrix.pd_6
         + Activity.Jul * Pd_Matrix.pd_7 + Activity.Aug * Pd_Matrix.pd_8
         + Activity.Sep * Pd_Matrix.pd_9 + Activity.Oct * Pd_Matrix.pd_10
         + Activity.Nov * Pd_Matrix.pd_11 + Activity.Dec * Pd_Matrix.pd_12
         ) ELSE 0 END) Budget

FROM Activity, Pd_Matrix

WHERE Pd_Type = 'month'
 
GROUP BY
       Activity.AcctID
     , Activity.DeptID
 
     , Pd_Matrix.Pd_Name
     , Activity.Year
 

Summary

The Period Matrix table is a versatile tool for SQL development. It can be used in all DBMSs with little modification to the SQL statements. It can be used to replace several separate SQL statements needed to create the different formulas and it is useful in table folding and pivoting.