Where have I been?

I’ve been extremely busy for the past five months and just this week that I have time to breathe. So what have I been doing?

New venture in Thailand real estate

Launched a real estate venture in Thailand. Check it out here http://www.tk-property.com . (Guess where the name comes from!).

TinyMassive

Three months ago I joined Lightmaker as contract software developer. Right off the bat I was assigned to work on TinyMassive.com. Ironically, there’s nothing tiny about this project. The ultimate idea behind this website is to combine Ebay, Amazon, FaceBook, and Shopping.com into one e-commerce platform. The sprint for this project is crazily aggressive, there are a number of 90 hours weeks.

RESAAS.com

After TinyMassive, I continued to work with LightMaker on their next project, RESAAS.Com, the software-as-a-service real estate/MLS lisitng web engine. This time my role is developer consultant. It was a nice break from TinyMassive. My task was to fix problems and acted as a SWAT team when things go bumpy.

BODYGLIDE.com

The most recent project was BodyGlide.com. This is my first full time project with Lightmaker and my role is the lead developer. The project itself was a great success (the customer was extremely happy with the result). The turnaround time was record breaking (2 weeks and 2 days). But the thing that surprised me the most was that nobody on my team has ever done anything with flash before 2 weeks ago. It was exactly 2 weeks and 1 days ago that I cranked out the first HelloWorld in ActionScript and started architecting a flash application. It’s amazing what human can do when there’s enough pressure and motivation.

What’s next?

At the end of BodyGlide project, I accepted Lightmaker’s full-time offer and it’s a new beginning for my career. So after my Christmas break, I’ll be going full steam ahead on the next project. :) For now, I’m just having fun with ASP.NET MVC and StructureMap.

No Comments »

Teera on November 18th 2008 in Personal

LINQ to SQL and the Microsoft Way

On this blog post, ADO.NET team announced the dropping of the support and further development on LINQ to SQL. This is sad. Seems like MS’s done it again (My heart still aches over MS Site Server!) with the abandonment of its own product/technology in “Upgrade or Die” style.


Although I’m not a big LINQ to SQL user, I managed to ship two small website projects with it. I know a few who had invested far more time and resources on learning the technology. Personally I think it’s a great lightweight framework suitable for small to medium size project (according to Jeff Atwood, StackOverflow.com is built on LINQ to SQL).


In .NET Framework 3.5 we released several LINQ providers, including LINQ to SQL which set the bar for a great programming model with LINQ over relational databases.  In .NET 3.5 SP1, we followed up that investment with the Entity Framework enabling developers to build more advanced scenarios and to use LINQ against any database including SQL Server, Oracle, DB2, MySQL, etc.


So, it seems that Microsoft is suggesting developers to move on to Entity Framework. As they discontinued LINQ to SQL in .NET 3.5 SP1.  IMHO, the current version of EF stinks. The designer sucks and the modeling XML is bloated. It’s still far from matching NHibernate for medium to large websites, and overly complicate for small sites compare to LINQ-to-SQL. Maybe it’s because my first impression with EF didn’t go so well. As a general rule of thumb, if I follow the official tutorial and still can’t get a basic ‘HelloWorld’ running in 15 minutes, that framework is not usable. EF still have a lot to catch up if it’s going to become mainstream ORM.


The feedback from the community have been quite negative, see what people say on Ayende and David Hayden’s blogs.Â


We are listening to customers regarding LINQ to SQL and will continue to evolve the product based on feedback we receive from the community as well.


Listening to customers? They must either be deaf or doesn’t really mean it.

No Comments »

Teera on November 14th 2008 in Software Development, .NET, Personal

StructureMap 2.5

For the past week, I’ve been trying out StructureMap, a dependency injection framework for .NET. I’ve tried a few DI/IoC containers before including Spring and Castle Windsor and I feel right at home with StructureMap.Usually when I try out a new framework, I usually look at how much time it takes and how much code I have to write to get some basic (HelloWorld) program running. Comparing to Spring, I found StructureMap API and configuration more intuitive and efficient. Here are the example

 
public interface IHelloWorld
{
  void SayHello();
}
 
public class JapaneseHelloWorld: IHelloWorld
{
   public string Name { get; set; }
   public void SayHello()
   {
     Console.WriteLine("Konijiwa! " + Name);
   }
}
 
public class SpanishHelloWorld: IHelloWorld
{
  public string Name { get; set; }
  public void SayHello()
  {
     Console.WriteLine("Ohla! " + Name);
  }
}

Here I have a hello world interface with 2 speaker classes, Japanese and Chinese. In my client project, I add a new file called StructureMap.config with the following configuration. StructureMap Config file In my client program, I run the following code

IList<IHelloWorld> helloWorlders = ObjectFactory.GetAllInstances<IHelloWorld>(); //return list of 2 
IHelloWorld jap = ObjectFactory.GetNamedInstance<IHelloworld>("Japanese"); 
jap.SayHello();

The output is “Konijiwa! Koizumi-san”. As you can see, the amount of code I have to write and configuration files are quite minimum. Less code is always a good code. I also found StructureMap API to be well thought out and straight to the point of getting things done. Definitely worth your time checking it out.

No Comments »

Teera on November 14th 2008 in Software Development, .NET

System.String and string in C#

Confusing about which one to use? Don’t be, they are the same! “string” is C# alias of type System.String. Both string and String are compiled down to System.String in IL, very much like int for System.Int32. There’s no performance implication in any way. I recalled a forum answer mentioned something about String is kept on heap and string is on stack, but that is not true. I find most organic C# developers use “string”, but ones with background in Java go for “String”. Which one to use is only a matter of style :).

No Comments »

Teera on October 18th 2008 in Software Development, .NET

ForEach and RemoveAll methods of List

ForEach(Action<T>) and RemoveAll(Predicate<T>) methods of List<T> were ones of a few underused features in C# 2. In short, ForEach method allows you to apply an Action<T> delegate to every elements in a list. The result of using ForEach method is essentially the same as explicitly use foreach loop and apply the delegate to each of the item in the list. Similarly, the RemoveAll method apply bool Predicate to list items, and remove items that return true when the Predicate is applied.

At a glance, it may seem that the only benefit yielded from using ForEach and RemoveAll instead of traditional foreach loop is reducing lines of code. The more compelling scenario for using both methods will be in complex logic and nested loop. Another important use of RemoveAll is that it takes care of the Halloween problem for you as well.

Examine the code sample below, what we try to accomplish is to iterate through all customers, examine each of their order and remove that customer if one of his order has no item.

List<customer> customersToRemove = new List<customer>();
 
//customers is List
foreach(Customer customer in customers)
{
   Console.WriteLine("Customer: " + customer.Name); //print all customer name
   foreach(Order order in customer.Orders)
   {
      if(order.Items.Count < 1)
      {
         customersToRemove.Add(customer); //determine customer to remove from order
      }
   }
}
 
foreach(Customer customerToRemove in customersToRemove)
{
   customers.Remove(customerToRemove); //remove customer with invalid order
}

This code works thought it doesn’t indicate the intention of of what we try to accomplish here. We can rewrite it as

 
        customers.ForEach(               //print all customer name
            delegate(Customer customer)
            {
                Console.WriteLine("Customer: " + customer.Name);
            }
        );
        customers.RemoveAll(            //remove customers containing order with no item
            delegate(Customer customer)
            {
                foreach(Order order in customer.Orders)
                {
                    if(order.Items.Count < 1)
                        return true;
                }
            }
        )

A point to note here is that the main benefit of syntax sugar is not to reduce lines of code but make the code more readable and explicitly indicate the intent of what it’s trying to accomplish

No Comments »

Teera on October 13th 2008 in Software Development, .NET

Three things I learned about Software….

I read Scott Hanselman and Dare Obasanjo’s old blog posts a while ago about things they learned about software while and while not in college. Now that I’ve got my degree and started working full time for a while it’s time to reflect upon what my three things. Though I’m counting the freelance and internship experience as my college experience.

Three things I learned about software IN COLLEGE

  1. Programming is only a small subset of software development. People often think of software development as writing code, but in a software project, the amount time spent on writing code is small. Most of the time is usually spent on deciphering other’s code, fix bugs and get your words across to other developers.
  2. Developing software, like designing a building architecture, is as much an art as it is a science.
  3. You usually get 20% of a project done with an entire semeter time. The rest 80% usually get done at 3AM the day you  hand it in.

Three things I learned about software WHILE NOT in college

  1. You can neither judge a developer by how much he knows nor how much code he can churn out in a limited time. Knowledge can be acquired given time and effort. A good developer is the one who can come up with an optimal solution that performs best against its objectives under a given context and constraints.
  2. 95% or more of the projects you’ll be working on in your career are going to be brown-field. Being in a project from the get-go is a privelege not many can afford.
  3. You have to update yourself more often than your computer to stay relevant.

:) So that’s my three things, what’s yours?

1 Comment »

Teera on September 26th 2008 in Software Development, Personal

Take the TDD pill, it’s good for you.

I’ve been a strong advocate for TDD for a bit over a year now. It’s the thing that I would suggest to anyone who cares about improving their software development and ask me about it. Now, the benefits of TDD are well-known, yet I find many people are still skeptic about taking the TDD pill. Here is my take based on my own experience with the technique, no fluff.

Testing discipline

We, developers, enjoy writing code but not so much of testing them. Now we all know that testing is good and we shouldn’t leave it all to the QA, but it’s another story to keep the discipline to do it. I find that if a developer leaves testing to later in development, he’ll end up either forget or not having enough time to do it. Since I started doing TDD, the minimum test coverage of my code’s gone up quite a lot (about 70% on a good day).

Enforce better design

With TDD, I have to think about how to test the code before thinking about the code itself. And to be able to unit test my code, I need to have a modular design. I also need interface based design. So I have to follow a good coding practice in order to effectively design my test. Many TDD followers would say that you’re already done designing your code when you are working on your test. I can’t agree more.

Peace of mind

Any line of code is guilty until proven otherwise“. How confident are you on your code? Are you sure that your code does what it’s supposed to do? I find myself more confident in my code and it really helps me sleep much better at night.

“Whack-a-mole” proof

Whack-a-mole scenario describes the butterfly effect in software breakage. A break or a change in any line of code can trickles and bring down the entire system. When this happens, developers usually go after and do a clean-up op on the breakage as it is. But it can get worse when the act of fixing the breakage changes the behaviors of the code and introduces more breakages elsewhere in the system.

To prevent this disaster, you need a complete code testing coverage. This testing coverage will serve as guideline. When someone changes a line of code or check in new code, you can run this entire test suite again. If all the test cases pass, you can be sure that it’s still functioning as it should. More on this in Continuous Integration

Keeping up the morale

Developing test system, one test at a time give quick win goals and direction to work on. TDD serves as a self checking system that I’m on the right path of developing working component.  There’s also a sense of getting something done by the end of the day as well.

These points are based on my own experience. So, believe me, take the TDD pill it’s good for you. :)

No Comments »

Teera on August 12th 2008 in Software Development, .NET, Personal

Populating XML elements tree with Linq

I find Linq to comes in handy from time-to-time, especially when I refactor my code and try to shorten it. Consider this example, let’s say I have a Book data that I want to dump into XML document. Now I have a Book class to represent the object.

    class Book
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public string Author { get; set; }
        public double Price { get; set; }
        public Book(int id, string title, string author, double price)
        {
            ID = id;
            Title = title;
            Author = author;
            Price = price;
        }
    }

To dump a collection of Book to XML, the old fashion way of doing it is creating foreach loop over Book collection and create XML object. With Linq it can become a one-liner.

            List<book> bookList = new List<book>();
 
            //Create book collection
            bookList.Add(new Book
                            (787, @"The Back of the Napkin: "+
                             @"Solving Problems and Selling "+
                             @"Ideas with Pictures", "Dan Roam", 16.47));
            bookList.Add(new Book
                (89, @"Presentation Zen: Simple Ideas "+
                 @"on Presentation Design and Delivery"
                 , "Garr Reynolds", 19.79));
            bookList.Add(new Book
                (897, @"C# in Depth: What you need to master C# 2 and 3",
                "Jon Skeet", 26.29));
 
            //Generate XML tree from Book collection data
            XElement xmlElement =
                        new XElement("Books",
                                bookList.Select(
                                    book => new XElement("Book",
                                                new XAttribute("ID", book.ID.ToString()),
                                                new XElement("Title", book.Title),
                                                new XElement("Author", book.Author),
                                                new XElement("Price", book.Price)
                                                        )
                                               )
                                    );
            Console.WriteLine("{0}", xmlElement.ToString());</book></book>

This query yields the XML output. No loop or messy enumeration required, the code is also more readable the Linq query.

No Comments »

Teera on August 4th 2008 in Software Development, Web Dev, .NET

Module and Mixin revisited

From the feedback I got from ruby on rails course, there seem to be many students who find many core concept of ruby difficult to understand. So, the course mailing thread aside, I’ll try my best to explain some of these concept in depth here. This post will be about the concept of Mixin and ruby Module.

Mixin and Module

Wikipedia offers one of the best description of what mixin is.

In OO languages, a mixin is a  class that provides a certain functionality to be inherited by a subclass, but is not meant to stand alone. Inheriting from a mixin is not a form of specialization but is rather a means to collect functionality. A class may inherit most or all of its functionality by inheriting from one or more mixins through multiple inheritance.” - Wikipedia

Ruby’s flavor of mixin comes in the form of Module. Module is a collection of methods and constant. The most commonly use of module is to have its members (methods, features) “mixed” into a class. The most evident example is the Kernel methods that are mixed-in to Object class. Because of this, methods from Kernels are universally available to all ruby objects.

Basic example

In the last post, I showed an example of ‘puts’ method. ‘puts’ is available to all Object in Ruby. ‘puts’ exists in Kernel module, and Kernel is mixed into Object class. Now since everything in ruby is an Object, they also automatically have access to ‘puts’ in Kernel module.

puts "string".class.ancestors
# [String,Enumerable,Comparable,Object,Kernel]

puts Object.class.ancestors
# [Class,Module,Object,Kernel]

puts Object.private_methods.include?('puts')
# true => Object has 'puts' method as member

puts Object.class.private_methods(false).include?('puts')
# false => 'puts' is inherited member

puts Kernel.class.private_methods.include?('puts')
# true => 'puts' is a member of Kernel module

Another example is the use of Math module. you can access Math’s constant value without having to include Math module

puts Math::PI
# 3.141592653589793

puts Math.sqrt(256)
# 16

Matz has called the mix-in feature of module “single inheritance with implementation sharing“. It’s a way of getting benefit of multiple inheritance without the mess and difficulties.

Module instance method

With “include” keyword, the modules methods become available  as object’s instance methods. You can think of including Ruby module to a class is similar to appending functions of that module to an object.

module MyModule
   def instance_method
      puts 'this is MyModule instance_method'
   end
end
class MyClass
   include MyModule
end
my_class = MyClass.new
my_class.instance_method #this is MyModule instance_method

As you can see, with ‘include‘ keyword, MyModule’s method becomes available to all instance of MyClass object.

Module class method with append_features

It’s also possible to make module methods available to class. What we’re going to use is ‘append_features‘. By overriding module’s ‘append_features' method with class as parameter, nested method under append_features will be accessible to that class.

module MyModule
   def MyModule.append_features(childClass)
      def childClass.classMethod
         puts 'MyModule classMethod'
      end
      super #HAVE to call super here
   end
 
   def instance_method
      puts 'this is MyModule instance_method'
   end
end
my_class = MyClass.new
my_class.classMethod     # illegal
MyClass.classMethod      # print MyModule classMethod

A few things to note here. First,’append_features‘ actually do the work equivalent to include operation. Therefore, we need to call super. Otherwise, the rest of the code in the module wouldn’t be included at all. Another thing, the nested method can only be singleton/static method.

Module class method with inheritance

Alternative to using append_features, you can mix in module’s instance methods as class methods with inheritance

class MyOtherClass
   class << self
      include MyModule
   end
end
MyOtherClass.instance_method
# print this is MyModule instance_method
# instance_method becomes class method

As you can see, the ruby language itself is very powerful. But by nature, it still follows the Object Oriented principle. On the future post, I’ll review the concept of blocks and lambda expression.

2 Comments »

Teera on July 29th 2008 in Software Development, Ruby

Everything is an object (in ruby).

Less than a week since it began, many interesting discussions have been floating around ruby on rails course mailing thread. A great one in particular is the discussion on the OO-ness of ruby taken from the conversation between Victoria Pocladova and Raul Parolari. Consider this:

“Ruby is an object oriented language and an object has a well-defined interface that specifies the behavior of the object in a manner that is independent of its implementation. This interface defines the collection of services that can be invoked by other objects.” - http://c2.com/cgi/wiki?ObjectOriented

Main.rb and Object

The next questions become how would you describe the behavior “main.rb”? You can have blocks of code within the main class, and no method containing this code. How is this code exactly invoked by another objects, or by the compiler itself when the main class is executed? And since I don’t have any methods in the main class, where does the “puts” command come from? Does main class get special treatment like void main() method in Java?

It does seem a bit contradiction to the earlier paragraph describing the OO concept of ruby. Raul Parolari, my course adviser colleague, offers a great explanation of this.

First, let’s try creating a main.rb file and execute this line of code.

puts self #main
puts self.class #Object

‘main’ is like the backstop of Ruby program. It is the farthest back you can fall; but it is also the current object as soon as your programs starts up. By the way, ‘puts’ is in Object? no, it is a method of module Kernel. But wait a moment; didn’t we say that ’self’ class was Object? how can we call ‘puts’ on an Object instance? Because… every object’s search path includes the Kernel module; and how is that?

Kernel module

That’s because the class Object mixes in Kernel, and every object’s Class has Object as an ancestor. Now try this

puts self.class.ancestors #[Object, Kernel]
puts 'string'.class.ancestors # [String, Enumerable, Comparable, Object, Kernel]

Again, we see Kernel lurking at the back.

puts

The next thing we can investigate is if ‘puts’ is actually a member of Object.

puts self.class.private_methods.include?('puts') #true
puts Object.private_methods.include?('puts') #true

Oh wait? Didn’t I just say that ‘puts’ is a method of module Kernel? If you trace EVERY class in ruby, you’ll see Object and Kernel at the top of inheritance hierarchy. Now let’s try to interogate Object class again and keep out inherited members.

puts Object.private_methods(false).include?('puts') # false
puts Kernel.class.private_methods.include?('puts') #true

Gotcha! As you can see, ‘puts’ is actually a member of Kernel module that Object benefits from. And since every class in ruby is an Object, they all benefit from the work of Kernel as well.

As the lead adviser for this program, I’m astound to see this kind of thing in the discussion thread myself. The understanding of core concept highlights beauty and benefits of the language.

1 Comment »

Teera on July 25th 2008 in Software Development, Ruby