Archive for the ‘Uncategorized’ Category

Legos and Optimization

Sunday, 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

Friday, 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.

White Screen of Death

Wednesday, July 18th, 2007

I bought an Acer laptop last year. It seemed like a pretty good deal at the time. It was 1000$ cheaper than other. Though, I seemed to have got what I paid for. It promptly died in December, slightly before Christmas. I sent it back ($20) to them to get fixed and they said they replaced the mother board.

Since then, the screen turns white. It just fizzles out and turns completely white. This isn’t a software problem because it happens anytime, from the BIOS start screen, to inside XP. This was only a few weeks after I had got it back from being repaired. Sigh… So I sent it back again (another $20) to be fixed. Each time I sent it, its at least 2 weeks before I got it back.

Its been pretty good since then, until this week. It started turning white again. I only got it back a little over a month ago. Not again. So rather then send it back again (For another $20 and 2 weeks) I looked at what I could do.

Looking “White screen of death” up on the internet, showed that it could be problems with the LCD boards. I was lucky enough to find a repair manual for the laptop though. The manual showed some LCD parts above the keyboard. This looked like a good place to start. Since my screen was not almost permanently white, I figured it wouldn’t hurt to poke around.

After removing a cover, I had access to the connectors. After poking and wiggling a few of them, the screen came back. It seems the LCD cable is loose. Its not connected to the motherboard completely, and comes partially loose. Hmm.. seems someone didn’t put my computer back together quite that well after replacing the motherboard. As far as I can see though, the connection is tight and all the way in. It just isn’t staying in. Bad design?

Anywho, instead replacing a pricey inverter or back light or controller card for the LCD, it was just a partially loose connection. And to think, I could have sent it back to Acer to be fixed for another $20 and 2 weeks.

(Edit Dec 22 2007)

I notice a lot of people finding this page when searching for ‘acer white screen’.  From my findings, a white screen means that your LCD cable has partially loose.   Either take it into a shop to get fixed, or do it yourself.  To do it yourself, you probably need to open the top of your laptop above the keyboard.  Unplug the LCD connection and plug it back it, and secure it in place somehow.

Combination Generation

Saturday, December 23rd, 2006

I ran across a wonderful article a while ago about a Combination / Permutation class that generated them as a stream.  It’s a great idea.  It makes unit testing pretty easy, as you can have the class make sure you hit all your test cases.  Sadly I wasn’t able to find the article again, at least I don’t think i was able to find it.

Anyway, I went a made my own combination generator.  For those wondering what a Combination is, its arranging K items from a set of N items, order doesn’t matter.  So given 5 items, and only taking 3 at a time you would get

0,1,2
0,1,3
0,1,4
0,2,3
0,2,4
0,3,4
1,2,3
1,2,4
1,3,4
2,3,4
The way this works is quite simple.  Each column has a min and max value that it can hold.  The first column goes from 0-2 (or 0 to (N-K) ), the last column goes from N-K to N-1.  You can easily generalize this into column C can hold a value from C to N-K+C.  The value of N-K can be precomputed.  Armed with this knowledge you can now make all your combinations.  First start with the identity (0,1,2,…,n).  To get the next combination, start at the right most item, and move left until you hit the first item that can be incremented, in other words, the value is between C and N-K+C.  Increment this value, then move back right and set each of these columns equal to 1 more than the item at its left (ie   c[i] = c[i-1]+1).

So given 0,3,4,  we start at the right (4).  4 is the max value for this column, so we move left.  3 is also the max value for this column, so we move left again.  0 is between 0 and 2, so we can still increment it.  We increment it to 1.  Now the array is 1,3,4.  Next we go back right and add set each column to one more than the column at its right, so we change the 3 into 1+1, which is 2.  We change the 4 into 2+1, which is 3.  This leaves us with the next combination of 1,2,3.

The last combination has the pattern of the first item equal to N-K (or 2 in our case).  When we reach this case we stop.

Ok, thats my little walk through on combinations. I’ll put something up about permutations next

Earthquake!!!!

Saturday, October 21st, 2006

Wait what…?  Yes thats right, there was an earthquake here on Sunday.  Quite the moving experience.  I was sitting in front on my computer at the time.  It felt like some big machine was driving infront of the house.  Everything vibrarted for a little bit, slowly getting stronger.  Then the actaul shaking started.  My bed started shaking back and forth, just enough that I could feel it.  It got a little strong than that and then stoped.  There were a few aftershocks, where were felt as more shaking back and forth.  

Being on Oaha some could say I got it easy.  The Big Island (Hawaii) got the full force of the 6.6 so buidlings and walls fell.  But here, we were out of power for most of the day.  Some places didn’t even get power untill 1 in the morning.  People were flocking to the stores to buy food, water, and bateries.  Because the stores didn’t want to let people inside when the power was out, the stores setup a booth out front and would bring items out one customer at a time.  This made for really long lines in front of stores, but people were still able to buy things.  The most interesting thing about the power outage to me, is that most people had wireless phones in their house.  When the power went out, their phones didn’t work anymore, even though the phone line was fine.  The following day all the stores on the island were out of phones that didn’t require power.  Home Depo had generators on 4 month back order.  Alot of places were out of food on monday, because it had spoiled durning the blackout.