Legos and Optimization

August 16th, 2009

Everyone wants faster, simpler code.  “Optimization” doesn’t mean the same thing to everyone though.  For example a FSM.  This usually start out as a big mass of very simple states.  You then “optimize” redundant and nop states away, and combine states.  For example a simple FSM for most console games

Logos -> Loading screen -> Main menu -> ….

After main menu, you could go to any number of states such as playing the game, loading a save, configuration, etc…  The first three states though, will always follow the same path and can be combined down into one state.  This is great on paper and pencil because now you have less things to draw.  When translating this into the software world each “state” can be represented as its own object or method.  So we’ve also cut down on the typing we had to do in software.  Such an “optimization” was probably not a good thing though.   If the code has to change later we’ll have to break apart the state to put in a new state.

It’s best to leave the states as separate states (objects,methods) in the software.  This allows the code to easily be changed later if and when it does (because it will always change).  By having objects/methods do TOO much, it has a level of complexity that does not and should not be there.  Break things up into smaller, simpler parts.  Maybe you have already heard of Cyclomatic complexity (Firefox hasn’t though, as it comes up as a spelling error.)  There are a number of tools on the web that will measure the this on each method/class in program.

If the three initial states were merged we’d end up with one state that was 3x more complex than the individual states.  In terms of maintainability, this isn’t a good thing.  Merging the states also did not make anything faster, and might have made things slower.  Instead of the state machine deciding which of the three states we are in, the new ‘optimized’ state much decide which of the three states it is in, so there are two decision points instead of one.

Having lots of small methods, classes,objects in a program is not a bad thing.  Merging them down into larger objects is not a good thing.  Keep the code broken up as possible, or it will become unmaintainable mess of large objects with very specific (and sometimes redundant) functions .   Think of code like building blocks.  As long as the objects are small you can mix and match them anywhere to build things.  The larger pieces can only be used for a few things and are limited in their use.  Look at some of the Lego sets you can buy.  Some are made with very generic parts that can be used for almost anything.  Others have very specific (often large) pieces that serve specific functions.  Such as a cockpit on the spaceship, or a fancy roof for a building.

Python like .NET

April 17th, 2009

Python allows you to assign almost everything.  There doesn’t seem to be a difference between variables and functions.  You can have a class Foo with attributes bar and baz. These can be methods or plain varaibles, and you can re assign them at run time.   So Foo.bar = 10  and Foo.bar = { return a+1 }  are both valid (though not valid python syntax).   This allows you to reassign methods/functions at run time.

.NET has a simular feature called Delegates.  These are basicly pointers to function.  Most people will use these for callbacks and events.  Something interesting you can do is use them in properties.

class Foo
{
  delegate void Fn(string s);
  Fn bar;
  Fn baz
  public Fn Bar{
    get{ return bar; }
    set{ bar = value; }
  }
  public Fn Baz{
    get{ return baz; }
    set{ baz = value; }
  }
}

Using this class you can now assign the class methods at runtime.

Calling the method:

f.Baz("something")

and reassign the function to print out a line

f.Baz = f => Console.WriteLine(f);

The class looks just like it has normal methods, but they are not assignable.

Fun with timezones

April 9th, 2009

Using dates and times in a program is not always as straight forward as you might think.  The issue is with timezones and daylight savings.  Most people forget to use a timezone when they store dates.  This might not seem like an issue if you think your program will never be used in another timezone, but it still is.

Timezones tell you how to convert your datetime from one timezone to the other.  10am in NYC is not the same 10am in Tokyo.  What makes this all the more fun is the wonder idea of daylight savings time.  Time will go backwards and forwards or seeming random days.  Suddenly there are 2 different 11:59pm.

When you look at a file in windows explort, did you ever think about the missing timezone on the dates?  You probably didn’t give it a second thought.  But you should.  You probably assume that it is showing the times in your current time zone.   Sadly you are more correct then you think.  I found out about this after I read this link.  It seems windows uses your CURRENT timezone to display the datetime.  Whats the problem with this?  If a file was created at 3/1 10:00pm and you look at the file in April, windows shows the file date as 11:00pm, even though the correct time should be 10:00pm.

Windows is smart enough to store the date in UTC/GMT/ZULU time, but when converting it back to your local timezone, it just uses your CURRENT timezone offset, instead of figuring out what it was on that date with DST factored in.  I wonder if someone was trying to optimize out a few CPU cycles.

So next time you are looking at the access times on your files, remember that they could be up to an hour off from what the actual time was.

Swap with out the tmp

January 27th, 2009

When swapping variables most people use

tmp = a
a = b
b = tmp

This requires using an extra variable tmp to store A while it is overwritten with b.  Wouldn’t it be great if you could swap the two without using an extra variable?  The XOR swap algorithm is just what you need

a^=b^=a^=b

This ends up with A and B swapped.  Still 3 assignments but with out the extra variable.  If you want a good explaination of how that works read the wikipedia article.

A word of warning though, the above code only works in C/C++.   It will not work in an VM language like Java or .NET.    This has to do with how the languages are implemented.  Java and .NET are stack based VMs where as C/C++ is register based (and not a VM).  This means that values are pushed onto the stack and then poped off to calculate values.

For example

int a = 1;
int b = 1;
int c1 = (a++) + a;
int c2 = b + (b++);

c1 will be set to 3 and c2 will be set to 2.   Written in C/C++ both will give the answer of 2.  The operator a++ will increment a by 1 but return its original value.  With a stack base, the 1st part of the equation, (a++), is calculated and the answer ( 1 ) push onto the stack.   The next part of the equation (a) is calculated and the answer (2) and push onto the static.   Why is the 2nd part of the equation 2?  When the add operation is run, it retrieves the actual values of the variable one of which is the returned ‘original value’ of a and the other is the current value of A.

An alternate way of writing the XOR swap that works on stack based (and non stack based) VMs is as follows.

a^=b
b^=a
a^=b

Note:  This does not work if A and B are the same object.  You shouldn’t be swapping in the case any ways.

The short moral of the story is dont use shortcut code to put a bunch of commands on the same line.  It may not do what you think it will.

Launch Wrapper

September 2nd, 2007

Here’s a small utility i wrote after having to keep adding the same command lines arguments to a java. This program is a wrapper around the one you want to launch. You can specify a prefix and a postfix to the arguments. You can also alter them through a regular expression. This is all done through the .config file. The basic idea is to rename your executable, point the wrapper to the renamed file, and rename wrapper.exe to the original name of your executable.

wrapper.zip
Edit:  Updated exe to C++.    C#/.NET doesn’t allow exec like processes