Taswar Bhatti
The synonyms of software simplicity
Learning

sylar
So there is this character (antagonist) in this TV series “Heroes”, and his name is Sylar

People might also know him from the new Star Trek movie, where he plays the young Spock. Anyways, back to the Sylar character, in the show he plays a serial killer who kills people with ability (e.g flying, shooting fire out of hands, invisible, shape shifter etc etc) and takes on their ability/power after killing them.

That sounds awful as a character but some may say that does sound pretty cool, if you could take like Beethoven ability to play the piano or Jimi Hendrix guitar playing ability. So what does this have to do with software development you may ask?

The ability that I speak of could very well be from one of your fellow developer. S/he may have some ability/knowledge in software programming that you may not have learned yet. If you wish to take the ability try and set up some time with them so that you can learn from them. Try pair programming with them or have them show you some stuff, the next time you are near their cubicle or office. There is a lot you can learn from others, you can watch them use their IDE or Resharper short cut keys, or just their design principles they may use in their application development. Trust me most developer’s don’t mind showing off to their coworker its part of their job to act smart 🙂

I think the quote from a TED talk by John Wooden a basketball coach sums up my entry pretty well:

“Never try to be better than someone else, always learn from others, and never cease trying to be the best, since that is under your control”

design patterns

Today, I thought I would mention the Null Object design pattern, its an object that provides intelligent do nothing behavior, hiding the details from its collaborators.

So what does this mean??? Before I tell you what it means, lets see an example of how often you might be seeing code that is checking for null.

Let say we have a Person object, that is implementing an abstract or interface IPerson and there is a repository somewhere that finds the person for us.

What happens when we don’t find the person, since the Find method above returns null, we end up with NullReferenceException thrown, because we are calling person.Firstname in the PrintPerson method.

So in order to fix the code we write if statements around it like so

And we keep writing code with the if statement over and over, every time we want to find we need to check the if(person != null). That is just too painful in my opinion.

So how do we fix it. We use the NullObject, and here is how it looks like in UML.

Null Object

Null Object

Basically we create another class that implements the interface or abstract class and all its methods and properties implementations does nothing, an example below

As you can see the NullPerson has no implementation code in the DoSomething method and it returns 0 for CalculateSalary.
Back in the repository code we will have to return NullPerson rather than null when we can’t find the person.

From here onwards you can eliminate most of your person != null code. Since whatever method or property you call on it, it would never fail and never return you a NullReferenceException.

You could do something like

Our code would not be littered, and we could start writing code that is more elegant that does not contain those nasty if (x == null) statements all over the place. Do remember we might have to implement more NullObjects for other classes that we will be using rather than mishmash of null and NullObject, which would make it ugly.

This pattern helps us realize that we sometimes don’t really want to know if the Get or Find method returns null or not we just want to do something like

One may also say that there might be cases where you want to an error message, then we can use this way to solve the issue

The NullObject is a very simple idea and once you start using it, you would truly get hooked on it and see the power of something so simple. We then start not caring about those if statements and an added benefit is that our code would be much cleaner and easier to read for others.

design patterns

As promised here is the version of observer pattern with Event & Delegate. Personally I am not a big fan of this solution, although it does remove quite a bit of code and show how to use a push model with the observer pattern. Next time around I will show a more elegant solution by using Event Aggregator.

But for now lets see how I implemented Observer Pattern with Event/Delegate.

First I tackled the Subject of our class, the thing you will see different is there is an instance object called TextChanged and is an EventHandler, the second change is it Notify method. I also removed the ISubject interface just because there isn’t really a need for it in this example and to make things simple.

 

From the above code you will see that there is an object called TextLangChangedEventArgs, that is basically our data object that would be pushed to the observer. Here is how it looks like, the important part is that it extends/inherits from EventArgs

 

Finally back to our WPFApp, we again have 2 labels the Turkish and English one, but they both don’t know anything about the Subject which is a good thing, so that it is loosely coupled. In order for the subject to hook into them they provide a signature where the event will call it.

 

Turkish UserControl does the translation of English to Turkish

 

Last but not least here is the WPFApp

 

I have not posted the XAML code but one can download the source and solution.

For next time I would like to show a more elegant solution called Event Aggregator which is a pretty cool pattern introduced by Martin Flower that allows one to write extensible code.

design patterns

Learn the Observer Pattern:

I am starting to write a series of blog post which would be named “Learn {tagline}…..”
In these series I would post things about design pattern, programming methodologies, skills etc etc

For today, I will start with my favorite Design Pattern. The Observer Pattern.

I would first state out what the GoF book, states as the intent of this pattern.

Intent

  • Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Applicability of this pattern

  • When a change to one object requires changing others, and you don’t know how many objects need to be changed
  • When an object should be able to notify other objects without making assumptions about who these objects are. (Loosely coupled objects)
  • When an abstraction has two aspects, one dependent on the other.

UML Structure

observerpattern

Sample Code

I am going to write a sample application using WPF to show how to use the pattern. One may say its an overkill but it does show loose coupling.

First our pattern, our class diagram would look like this

classdiagram1

Our Interface ISubject and our Concrete Subject implementation

 

Next the interface for IObserver

 

Now that we got the pattern in place, let start with creating two UserControl in WPF, we will create an English and Turkish Label Control that implements the IObserver interface, as shown in the class diagram below
classdiagram12
English and Turkish Label Control implementation

 

Simple nothing fancy about these 2 controls other than the Turkish one, which has English -> Turkish hard coded translation.

Now lets see how the WPF app looks like and how it hooks it all up.

 

Application Screen shot

observerapp

Conclusion
Our main WPF app hooks the EnglishControl and TurkishControl with the Concrete Subject by using poor man’s Property Injection in the Default Constructor of the App. When a button is clicked on the App it sets the Subject State to a random English word from the list. This in turns calls the Notify on the Subject and it will then notify all its IObservers.

Additionally I would like to mention that we have used the pull method. Where we request to get the state from the subject, we could also use the push method where the subject will sends details information about the change to the observer. The push method makes the observer less reusable, because Subject class makes assumptions about the Observer classes that might not always be true. I suggest one to use the pull method whenever possible.

Once you understand the Observer Pattern concept, it is very easy to implement your own Events, the basic idea of Events is the Observer Pattern. In my next post I will show you how to use Events and Delegates to implement Observer Pattern in C#

You can also download the entire source code.

Hope you enjoy the first series of the Learn topics, will be posting more in the future.

mysql

Here is another post moved over from old blog about mysql installation on FreeBSD.

Installing the mysql ports

 

I will be using mysql50-server from the ports section of FreeBSD 7.0

The above command will build and store the database in /var/db/mysql

One will also need to add to rc.conf file to start mysql

The scripts of where the database to start is stored at /usr/local/etc/rc.d/mysql-server

But before we start it, we will create a my.cnf file and store it in /etc/my.cnf

By default the install provides a list of my.cnf files stored in /usr/local/share/mysql

I will be using the my-large.cnf since I know I will be using large data tables, but choose one that you find fits your needs.

Now lets start the database (assuming you are logged in as root)
Now that the database has started, lets set the root password for mysql

To setup root password for first time, use mysqladmin command at shell prompt as follows:

However if you want to change (or update) a root password, then you need to use following command:

 

Backup of Mysql Database

 

For backup of the database I will be using mysqlblasy.pl

On can download mysqlblasy.pl from http://pol.spurious.biz/projects/scripting/mysqlblasy.php#download

I will be using mysqlblasy verison 0.73

In order for mysqlblasy to run one needs to have the configuration file ready for it

Here is a sample configuration file that I placed in /etc/mysqlblasy.conf

This will allow root user to use mysqldump with password provided above and it will rotate the logs 7 days, compress it and store it at /database/mysql-backup

Lets try to run the command now (I got an error since I have not got some of the Perl modules in)

So lets fix it and install the Archive/Zip perl module

Now when we run

We should have a mysqldump stored in /database/mysql-backup

Lets now add it to the crontab at the end of the file.

This will backup mysql everyday at 11:00 pm

FreeBSD

This is a repost of my old blog wanted to move the post over, since I used to use a lot of FreeBSD in my previous position, and still love the OS wanted to continue posting stuff about it.

In this little tutorial I will teach how to upgrade ports in FreeBSD

  1. Install and use cvsup to sync the ports collection
  2. Create / Copy the supfile and use portsdb to update the database INDEX.db
  3. Use portversion to find which all ports need upgrading
  4. Use portupgrade to upgrade those ports
  5. Use portaudit to find vulnerabilities
  6. Script and add to crontab to auto do things for you

 

1. First lets install cvsup-without-gui

2. Now that cvsup is installed we need to create/copy the supfiles

Now we need to edit the ports-supfile to get only the ports

Edit the lines

Since my ports are installed on /usr/ports therefore I change base to /usr

For host one can change it to the one that is nearest you. Here is a list of cvsup server http://www.freebsd.org/doc/en/books/handbook/cvsup.html#HANDBOOK-MIRRORS-CHAPTER-SGML-CENTRAL-CVSUP

Remember to check if ports-all is listed since you wish to update all the ports

Next use this command to update the ports tree

Now that the port tree is updated, lets update the ports database (the following commands assume you have installed portupgrade form /usr/ports/ports-mgmt/portupgrade)

This creates an INDEX.db btree file on your server

3. Lets try to find out which ports needs upgrading

This will list out which ports need to be upgraded where the < sign means a new port exist.

Might show you something like

4. If we wish to upgrade php5 we would simply type

5. One may also like to install portaudit to check installed packages for known vulnerabilities.

This will list out all the ports that is installed and has vulnerabilities.

e.g

So you might want to upgrade that package.

Now that we have done all of this maybe it would be a good idea to actually script all of this into one script and put it in a cron job maybe even email yourself the result everyday (email left for yourself to script )

6. Here is a quick script that we can use.

Add it to crontab

Working Effectively with Legacy Code

This is part 2 of Extract and Override refactoring technique. You can find part 1 in my previous post.

I am going to show another technique by using Factory Method to break dependency in your code.

*WARNING* *WARNING*

Before I begin I would like to say all these techniques are not the best design but they will help you to break dependency and it might make some people flinch for the method but the intention is to get people to start looking at breaking dependency. Hopefully it will lead them to learn better design and make their code more modular in the process of it.

For anyone who doesn’t know Factory Method, you can read the GoF book or Head First Design Patterns. Which I would suggest you to buy if you don’t already own them.

Let’s say you have to send a letter to someone

Now we see from the above code that it is actually very hard for us to test this Letter class dependency since LetterSendingService is not quite accessible and created in the Construtor of Letter class. So how do we extract and override it then?

Lets give it another shot.

Now with this in place we can create a stub Letter class and override its dependency like this

With this in place we can then override the dependency and test the Letter class with a fake dependency.

The downsides
This method maybe great since its easy to work with but it lacks the interaction.

  • What if you wanted to check or verify some return values from ILetterSendingService?
  • What if SendLetter method returns a boolean value, true if letter was sent and false otherwise
  • What if ILetterSendingService interface has 15 methods in them, do we override all of them but we only care about 1 of them, which is SendLetter

A Mocking Framework like Rhino Mock or Moq would do a much better job at this, although you probably can override all the ILetterSendingService method but that is just too much work by hand writing stub etc.

Hopefully this post gave you some ways to think about breaking dependency in the future I will be writing about modular code and using some kind of IoC container to dependency inject and mocking framework to test your code 🙂

appengine_java

Just a link post today since google started supporting Java for their app engine rather than just Python, this will very likely bring lots of developers on board by having Java for Google App Engine

http://code.google.com/appengine/ Google AppEngine
http://code.google.com/eclipse/ Eclipse Plugin
http://blog.springsource.com/2009/04/07/write-your-google-app-engine-applications-in-groovy/ App Engine in Groovy

Time to start writing some apps in it 🙂

demeter

The Law of Demeter (LoD), or Principle of Least Knowledge, is a design guideline for developing software, particularly object-oriented programs. (wikipedia)

While everyday working on code that was written by lazy coders or junior coders I see a lot of violation of this law. I think every programmer should know the Law of Demeter by heart, as it states.

Any method of an object should call only methods belonging to:
1. Itself
2. Any parameters that were passed in to the method
3. Any objects it created
4. any directly held component objects

Quite simple one may think, in fact it is simple and here is an example of it

Now if only people would follow these principles, but the sad part is this is how I see most people just going about and violating the principle.

So how do we fix this, so that it doesn’t violate the principle.

By having phoneBook return us the withAreaCode, we reduce the number of class we are dependent on, and if anything changes in PhoneNumber this class/method would not be affected, only one place to change.

Next time if you see someone using code that violates the principle (wack them in the head first and then refer them here), remember to use an analogy I used with my co-worker.

  • I told him, I know you and your cell number, but would you think it would be okay for me to go through your contacts and get your girlfriend’s number?
  • Don’t you think that there is something wrong with it that?
  • I should only be able to get her number if I request it from you. (if he is willing to give me that is)
  • Not going through your contact list, what if your contact list changes? (Now he stores it in Outlook but what if he changes it to using pen and paper)
  • Does that mean when I call outlook contact list I am looking at your ex-girlfriend’s number? Since you moved to pen and paper?
  • Why am I dependent on your contact list? I only know you, shouldn’t I be able to just ask you for it and wherever you store it, is not my problem.
  • All you have to do is give me the number or return me nothing and life goes on.

 

Last but not lease always remember to question and review code, find out, why is this part dependent on that part? Why is there a call to something that feels like couple of level’s down, (i.e ObjectA.ObjectB.ObjectC.methodZ ) once you see that you know there is something fishy there, and own it to yourself to refactor and change it.

artofunittesting

Resources

  • Manning – Where one can buy the MEAP early release of the book
  • Amazon – Sometimes cheaper than Manning for print books
  • Art of Unit Testing Wiki Site -The wiki site for the book
  • The book will be in print I think April 2009 as Roy mentioned in his blog if you wish to wait for a hard copy

 

Additional Information

  • Paperback: 225 pages
  • Publisher: Manning Publications (May 1 2009)
  • Language: English
  • ISBN-10: 1933988274
  • ISBN-13: 978-1933988276

 

Audience and Content

The intended audience for this book are people who are beginning/intermediate learning unit testing or in the process of implementing unit testing at their shops. The contents is for developers to start writing better unit test. I wouldn’t say it is a book target at expert unit testers out there, people who may already be using SpecUnit or BDD, since the book doesn’t cover BDD or Spec. But nevertheless it still has valuable information for everyone.

What is it all about?

Part I Getting started

The first two chapters 1 and 2, goes through the basic of what unit testing is, (definitions, intro to nunit) and writing your first unit test using nunit. The author touches a bit on TDD but not going into details of it, since I think TDD is another mind set and the author just didn’t want to overwhelm the reader (if they were beginners). In the book he also uses the example of a Logger which he builds upon to explain the techniques of unit testing.

Part II Core techniques

The second part of the book chapters 3, 4 and 5, details the core techniques of how to use stub, what mocks are and using mocking framework Rhino Mocks to do unit testing. These three chapters I believe are quite important for a beginner to go through, since in chapter 3, the author goes through the techniques of Extract and Override, using Factories and Dependency Injection through constructor/properties. The author also talks about when should one use these techniques, the pros and cons of them.

In Chapter 5 he introduces Rhino Mocks, (also mentions other frameworks, Typemock, NMock2) although he uses the “using record/playback” way of Rhino Mocks (i.e. not using AAA style or the fluent way) of coding the examples, not that there is anything wrong with it, personally I prefer the AAA style and using lamda expressions. If you are looking at technical information or the meat of things, these three chapters are definitely the core of it.

Part III The test code

In part three of the book chapter 6 and 7, the author explains the important of using build automation, and continuous integration. How to separate out test so that they are in different folders/projects for integration and unit (i.e. slow and fast test). Also some techniques of refactoring your code to use base class/template method/generics to reuse code, although warning that base class etc might lead to less readability of the code.

In chapter 7, the author goes into the explaining trustworthy test by using coding standards (naming of test), some basic principle of DRY (don’t repeat yourself), code coverage, testing only one thing, using only one assert statement and finally using attributes to test different input cases of the test, rather than writing test for each case. I think this might be the chapter that some might considered it controversial, since they might see this chapter as in telling readers (laying down the law) of how to create trustworthy unit test. I would only say to that, give it a read and nobody is pointing a gun and saying this is the way to do it, I don’t seem to remember reading a book and following it the exact way of coding or using the exact libraries the book uses. Yes, things that the author list out might be valuable but at the end of the day whatever fits your boat or shop is what matters.

Part IV Design and process

The last two chapters 8 and 9 talks about integrating unit testing and working with legacy code. Chapter 8, was the chapter that had the most importance for me personally, on bring change and unit testing into an organization. Although there was an error on Table 8.1 which had the amount of bug found in production for teams doing test and not doing test, they were in reverse, I guess a typo from the author. The last chapter 9 talks about where should one start introducing test in your code, the hard part or the easy part, where does it pay off and some tools to use for unit testing. The author also uses the 4 quadrants to map out easy/hard test with dependencies, so that one can pick what would be easy and hard to test.

And lastly in Appendix I and II, the author talks about using design for testability and some tools/framework. In Appendix I, the author lays out a table of design guidelines and benefit of using them. What I found lacking was the cons of using the design, I think a column that also included some cons of using these techniques might be a better and be well rounded for readers to choose which design to use.

Conclusion

Last but not least, would I recommend this book to anyone? The answer is definitely “YES”!!! I would give this book a 4.2 out of 5 stars, I only wished the MEAP version didn’t have so many typos but hopefully when the final release comes out it would be all corrected.

UA-4524639-2