Project Euler

Posted by Aaron Feng Tue, 25 Dec 2007 04:54:00 GMT

A while back my friend James Horsley told me about Project Euler. I just pushed it onto my stack of things to look into. Recently, I was reminded of it again from Steve Eichert at work, so decided to give a try.

Project Euler contains a collection of mathematical problems ranging in difficulties. A problem can be solved using pencil and paper or using a computer program. The only requirement using a computer program is that it should run under one minute. It's an honor system because all you need to submit is the answer.

My choice of weapon is Ruby. I have been spectating Ruby for the past 5 years (not entirely true, but mostly). I figured it's time to roll up my sleeves. I'll be posting my solutions in batches for those who are interested. In addition to posting the solutions, I'll also post the amount of time each solution took. However, I will not post the answers. You can run the code on your own machine if you wish to see the answers. This way I will not ruin it for people who are interested in solving the problems themselves. All the code has been run on my MacBook on an Intel Core 2 Duo 2.0 Ghz with Ruby 1.8.

Weird characters by Visual Studio 2008 2

Posted by Aaron Feng Fri, 21 Dec 2007 03:57:00 GMT

This week we upgraded to VS 2008 RTM at work. Our solution contains database scripts for both SQL Server and Oracle. I noticed when I ran the script on an Oracle database via sqlplus an error appeared on the first line of the script. After a closer examination with a Hex editor, VS 2008 apparently inserts 3 non-printable characters in the beginning of a new file. Check out the image below:

vs2008

I'm not sure why, or what those characters mean, but they seem to be ignored by most Windows programs except for sqlplus.

Hardcore Erlang

Posted by Aaron Feng Thu, 29 Nov 2007 04:01:00 GMT

There is definitely a lot of momentum behind Erlang recently and more is about to come. A few months ago Joe Armstrong released Programming in Erlang which set off the initial Erlang awareness for many people including myself. Earlier this month, Channel 9 posted two videos with Armstrong on Erlang (part1 and part2).

Now another Erlang book is in progress: Hardcore Erlang by Joel Reymont.
It is also another Pragmatic Programmers book. The inital project for the book was a poker server, but now the focus is on a stock exchange program. A quote from Reymont:

"So lets build a stock exchange! Not just any stock exchange but one running on the biggest Erlang cluster in the world. This cluster does not exist yet but can be put together on a moments notice, using Amazon EC2."

This book might just keep Erlang on the hotness list for the year of 2008.

Subversion is the most pointless project 4

Posted by Aaron Feng Fri, 26 Oct 2007 05:04:00 GMT

The title is a direct quote from Linus Torvald's talk at Google. Torvald is the creator of the Linux kernel and Git. Git is a distributed source control system that is super high performance and reliable.

Like many developers, Torvald was tired of the complexity of CVS (branching), and how it is often cumbersome (merging) to use. Since the CVS repository is centralized, it requires a network connection for operations like commit which made it not truly offline. The fact that all the source code lives in one central location also made Torvald uneasy. The reason he said Subversion is the most pointless project is because Subversion is basically "CVS done right". Torvald didn't feel Subversion added any value since CVS was never done right to begin with. That's why he took two weeks off from Linux kernel project to write Git.

I had never used Git until I started to play with Rubinius. Git has been around for a couple of years now, and the most known usuage is with the Linux kernel project. Git is not like anything else I have seen before. It is partially inspired by BitKeeper[bit]. It's very different and radical compared to the typical source control system. After understanding why it is designed that way, it actually makes a lot of sense.

Git has no central repository. In fact, everyone has their own copy of the repository. Since there is no central repository, all operations are local, even commits. Local repository also removes any need for permission. The next logical question is how to propagate out the changes? It's easy, when someone is ready, he/she will contact you and say "Hey, I just finished feature X, please pull from me." If you trust this person, you'll "pull" directly from his/her repository. If you are having trouble merging the changes when you are pulling, you can ask the other person to pull from you instead. Once the changes have been merged you can pull again from that person. This is how thousands of people can work together in the same code base concurrently without interfering with each other. Lots of people can do the work, but you will only "pull" from people you trust. Distributed repository also makes backing up unnecessary.

There is a lot more to Git, but this is the 2 second overview of it. For more information on Git, here is a great Git tutorial.

Ocean's first language 1

Posted by Aaron Feng Mon, 15 Oct 2007 03:13:00 GMT

ocean

Private method creep 1

Posted by Aaron Feng Tue, 09 Oct 2007 10:53:00 GMT

Refactoring has been a buzz around the software industry for quite some time now. No matter what Editor you use, there are some kind of refactoring tools that will do all the dirty work for you.

The most common refactoring is probably the extract method. With a couple of clicks, a shiny new private method is generated with all the extracted code inside of it. This is where the problem creeps in. There's nothing wrong with private methods, but when you have a hand full of them it usually means that your class is carrying too much responsibilities. It should be split out into multiple classes.

Michael Feather recently had a post titled The Deep Synergy Between Testability and Good Design. In the post, Michael mentioned that the need to test private methods should prompt a redesign by breaking out the private methods into a new class. With the ease of the refactoring tools, redesign doesn't always come to mind since it does everything for you. Michael recognized that private methods are actually public interface of another class. As Michael said in his post, by removing private methods you are improving your design and increasing the testability of your code. Isn't that what we are all after?

Another reason for Rhino Mocks Generic Constraint 2

Posted by Aaron Feng Mon, 08 Oct 2007 12:22:00 GMT

Jeffrey Palermo recently had a post about Generic Constraints for Rhino Mocks - make unit tests more readable. I would like to touch on an alternative reason why you might want to use Generic Constraint. I'll reiterate Jeffrey's example before I start, with minor modification, and offer possible alternative ways the tests can be written.

Jeffrey implemented a GenericConstraint class to capture the parameter of the method call on a mock object. Below resembles his original example:

[Test]
public void ShouldSaveObjectWithAllInformation() {
    string firstName = "Aaron";
    string lastName = "Feng";

    MockRepository mocks = new MockRepository();
    IPersonRepository personRepository = mocks.CreateMock<IPersonRepository>();

    personRepository.SavePerson(null);
    GenericConstraint<Person> personConstraint = new GenericConstraint<Person>();
    LastCall.On(personRepository).Constraints(personConstraint);

    mocks.ReplayAll();

    PersonController controller = new PersonController(personRepository);
    controller.PersonFirstName = "Aaron";
    controller.PersonLastName = "Feng";
    controller.Save();

    mocks.VerifyAll();

    Person person = personConstraint.GetParameterObject();
    Assert.AreEqual(person.FirstName, firstName);
    Assert.AreEqual(person.LastName, lastName);
}

Once the PersonConstraint captured the Person that is being saved, he asserted that the values are as expected. This makes the test look more like a typical unit test.

Jeffrey's goal was to avoid the following code:

public delegate void Proc<P>(P p);

[Test]
public void ShouldSaveObjectWithAllInformationUsingBuildInConstraint() {
    string firstName = "Aaron";
    string lastName = "Feng";

    MockRepository mocks = new MockRepository();
    IPersonRepository personRepository = mocks.CreateMock<IPersonRepository>();

    personRepository.SavePerson(null);
    LastCall.On(personRepository).IgnoreArguments().Do(
        new Proc<Person>(delegate(Person person) {
            Assert.AreEqual(person.FirstName, firstName);
            Assert.AreEqual(person.LastName, lastName);
        })
    );

    mocks.ReplayAll();

    PersonController controller = new PersonController(personRepository);
    controller.PersonFirstName = "Aaron";
    controller.PersonLastName = "Feng";
    controller.Save();

    mocks.VerifyAll();
    // Notice no asserts
}

An astute reader might say: "Hey you don't have to do that, just implement the Equals method on the Person class." Which would look like the following:

[Test]
public void ShouldSaveObjectWithAllInformationUsingEquals() {
    MockRepository mocks = new MockRepository();
    IPersonRepository personRepository = mocks.CreateMock<IPersonRepository>();

    // Have to implement Equals on Person
    personRepository.SavePerson(new Person("Aaron", "Feng"));

    mocks.ReplayAll();

    PersonController controller = new PersonController(personRepository);
    controller.PersonFirstName = "Aaron";
    controller.PersonLastName = "Feng";
    controller.Save();

    mocks.VerifyAll();
    // Notice no asserts again
}

The last example by implementing an Equals on the Person object which made the test look too clean. The asserts are invisible. On top of that, you have to implement an Equals method on an Object which you might not ever call the Equals in the real system. I believe this is the real power behind Jeffrey's Generic constraint approach. One should avoid writing any code that is not utilized by the real system just to satify the test.

List comprehension kicks ass 4

Posted by Aaron Feng Sat, 29 Sep 2007 03:50:00 GMT

Recently I'm on an Erlang high, so I have tried to play around with it as much as I can. It's very common for any application to create a new list based on an existing list. For example in C# you would do something like the following:

public List<string> QualifiedUserNames(List<User> users) {
  List<string> names = new List<string>();
  foreach(User user in users) {
    if(user.Age >= 30) {
      names.Add(user.Name);
    }
  }
  return names;
}

Equivalent code in Erlang:

QualifiedUserNames(Users) -> [Name || {user,{name,Name},{age, Age}} <- Users, Age >= 30]

The Erlang function uses list comprehension to do all the dirty work. It loops through every item in the Users list, and extracts only user "type" which matches the pattern {user,{name,Name},{age,Age}}. This is done because Erlang is a dynamic language, and the list doesn't have to contain heterogeneous items. Age >= 30 is a predicate that checks if the user should be added to the newly created list and if so, the Name is added.

Pretty cool, right? I think so. This capability is one of the many reasons why Erlang program is usually shorter than programs written in other languages. Well back to programming in Erlang some more.

Is Site5 Crash5?

Posted by Aaron Feng Thu, 27 Sep 2007 15:16:00 GMT

Earlier this year I move my blog hosting to Site5. In less than a year, there were two hardware failures (that I know of) which caused my blog to go offline for a day or two. I'm bring this up because it just happened again last week. Site5 customer service has been very reponsive and helpful, however, I wish their servers wouldn't crash so often. Search for "Service Disruption:" on Site5's forum to see all the server crashes.

Welcome to our world baby girl 4

Posted by Aaron Feng Sun, 23 Sep 2007 18:02:00 GMT

ocean

Ocean Soleil Feng was born at 12:03 on 9/20/07. She weighted 7 lbs and 5 ozs and is 20 1/2 ins long. Seeing her coming into the world was the most incredible experience ever. This picture was taken 3 hours after her birth.

Older posts: 1 2 3 4 5 ... 7