Archive for Programming

How to transfer the ownership of a code?

The agile practices are not still being embraced by many organizations or they are unaware of them and old practices(any common old practice exists in fact?) are used. Since every body does not have the chance of finding a chance to work with an agile team everywhere, we sometimes work on companies where green field development is not done. Teams are supposed to work as a whole but in reality each member of the team has his own usual way of doing something so there is no common behavior across the team. If you work in such an organization and the ownership of codes is not handled by the whole team, there are always times when you have to transfer the ownership of a project. Things that I do while transferring the ownership to another colleague are as follows:

  • Don’t give the source of your project through network sharing or email as a zip file. Share it through a version control system(VCS), so if a case occurs in future, there is always a way we can track back.
  • Prepare a small manual, that will help the new owner how to set up his machine to run the project in dev/test/prod envrimonts. It may include, VCS info of project, used database/telnet/ftp urls, commands to run, PATHS….In short, write every detail to make the code run without a problem on a new machine.
  • Have scripts to make the project build->deploy->run automatically, instead of doing things manually.
  • Be asure that your code works and don’t transfer it in a unfinished, middle stage. If your code is not working, new guy will suffer a lot to understand your work and this is is really difficult at the early stages. So fix everything first and then leave it.
  • After the installment is done, run the existing code and check if the results are as expected together.
  • Always write your own tests and show the idea of testing and how to run it so that test writing practice may be also continued after you:)

Leave a Comment

assert_equal is better than eyes for testing

Once again I understood why testing properly is important when I came up with an interesting problem yesterday night. I am writing a ruby program to download flash videos from sites like Youtube, Blip.tv, Metacafe… to watch them while I am offline. I have a text file. On its first line I wrote the path to the directory to store them and URLs of videos on the next lines. The content of it looks like:

c:/temp/download

http://www.youtube.com/watch?v=QZnyrp__CNU

http://www.youtube.com/watch?v=_x0YtFUYSvY

http://www.youtube.com/watch?v=_sM9vcAIYyc

…….

So, here is my plan: read the lines from file into an array, create the download directory and grab the videos to that folder. To read the lines and create the folder, I did:

lines = IO.readlines(‘c:/temp/config.txt’)

dir = lines[0]

FileUtils.make_p(dir)

When I ran that, it produced:

  1) Error:
test_initialize(TestYoutubexDownloader):
Errno::EINVAL: Invalid argument – c:/temp/download

    …..
    C:/ruby/lib/ruby/1.8/fileutils.rb:201:in `each’
    C:/ruby/lib/ruby/1.8/fileutils.rb:201:in `mkdir_p’
    C:/temp/Youtubex/test/TestYoutubexDownloader.rb:23:in `test_initialize

1 tests, 0 assertions, 0 failures, 1 errors

Very simple code, but FileUtils didn’t behave as expected. So I changed the code to:

lines = IO.readlines(‘c:/temp/config.txt’)

dir = lines[0]

# FileUtils.make_p(dir)

FileUtils.make_p(“c:/temp/download”)

This time it worked and folder is created. So I thought IO.readlines is not getting lines from file and dir is not correctly initiated. To see it ,I did:

lines = IO.readlines(‘c:/temp/config.txt’)

dir = lines[0]

puts “directory=” + dir

FileUtils.make_p(dir)

# FileUtils.make_p(“c:/temp/download”)

So when I executed the code, output is as follows:

Started
directory=c:/temp/download
E
Finished in 0.0 seconds.

  1) Error:
test_initialize(TestYoutubexDownloader):
Errno::EINVAL: Invalid argument – c:/temp/download

     …….
    C:/ruby/lib/ruby/1.8/fileutils.rb:201:in `each’
    C:/ruby/lib/ruby/1.8/fileutils.rb:201:in `mkdir_p’
    C:/temp/Youtubex/test/TestYoutubexDownloader.rb:25:in `test_initialize’

The directory path is correct since I see it on the debug output of my computer screen but still FileUtils.make_p is not doing the right thing. I passed the string manually it worked but when passed through a parameter with the correct value, the error occurs. So what maybe the problem??? :) Then I decided to make a check with assert_equal instead of puts statement. I knew that it will act same, since I saw the output on screen but did it:

lines = IO.readlines(‘c:/temp/config.txt’)

dir = lines[0]

assert_equal(“c:/temp/download”, dir)

And it produced:

  1) Failure:
test_initialize(TestYoutubexDownloader) [C:/temp/Youtubex/test/TestYoutubexDownloader.rb:24]:
<”c:/temp/download”> expected but was
<”c:/temp/download\n”>.

The trick is when the line is fetched by IO.readlines, newline characters at the end are also preserved. Eyes doesn’t detect newline characters so this type of testing doesn’t help. So they must be stripped if we want to clear them:

lines = IO.readlines(‘c:/temp/config.txt’).collect!{|line| line.strip}

dir = lines[0]

FileUtils.make_p(dir)

At last it worked :) I lost time by making guesses for the problem but the solution was simple, do the assertion test at the first time instead of making guesses or fighting with the debug output strings! The results of the puts, writeln, printf.. statements may lead us to a wrong way during testing and they must be avoided as becoming the first choice to use when tension is high in projects. Furthermore; not only for Ruby but xUnit testing tools are available for most of the other languages and testing with them is much more fun than the old debug methods!

Leave a Comment

Trying Subversion

I finally setup Subversion to a windows server in our company. Apache web server is used for http-based access to source control system. I had a problem during Apache on win32, Subversion integration which says that the module mod_dav_svn can not be found when the mod_dav_svn.so file was in \Apache\modules. It was a crazy and dummy mistake. Solution was moving libdb43.dll file to \Apache\modules directory, problem has no relation with mod_dav_svn.so file. Apache error message is just misdirecting!

My Java IDE, Intellij IDEA, has tools to access Subversion, no need to search for SVN plugins. SVN is easy to set up and use. The usage of TRUNK-BRANCH-TAG pattern for release management is very easy to configure and use. From now on I can access the sources from anywhere independent of location and whether the local connection has a firewall since I have HTTP-based connection to SVN through Apache. I like open-source ideas which makes my life easier:)

Leave a Comment

When to write code comments? Comments must be written?

I saw many developers who are writing comments/copyright information while writing their codes. These innocent commments seem harmless at the first hand. But I believe that no comments must be written during development phase. Comments must be deferred until the last minute when everything is ready for production. Because:

  • Comments decrease the readibility of the source code during development phase. Developer does not need a comment in order to understand his own code. If he needs comments, that means the code is not clean and simple enough. So refactoring and cleaning must be the first action, instead of writing comments. Uncle Bob Martin wrote a good article about clean code.
  • During development phase, requirements may change. When requirement changes, the code handling the requirement also changes. If there is a comment for the code, it must also change. As a result, the chained dependency brings extra cost to project development time.

So in general, I believe clean code is more important than adding comments. If a necessity really forces for writing comment, for instance for an API, it may be written. But before adding it, everything must be tried in order to remove the comment from code.

So why most developers write comments to his code?

I believe one of the reason for this is that the developer is aware of the fact that there is something complex in the code and assumes that comments will make everything fine. So comments are mostly written as an excuse to the outcomer for the ugly code.

Another reason is the study resources. Usually developers learn new techs by going over the samples distributed with the technical writings. Most of these writings include comments in every piece of code. So a new java student learning from a book sees comments with every getter/setter method which is not necessary. As a result, he uses the same technique in his own developments and everywhere is covered with comments.

Leave a Comment

Google Techtalk Videos

Do not worry if you can’t attend the seminars of famous names from software industry. Google is sharing the videos of the tech seminars given inside the company.

Leave a Comment

BuildMonitor-Watch CruiseControl from your Firefox status bar

We use CruiseControl at work for managing the build operations. Whenever a build fails, an email is sent to the project owners by CruiseControl for the notification of problem. Since this email is sent to the relevant parties, others, monitoring CruiseControl for their projects is mostly unaware of these build problems.

While I was surfing, I come up with an extension to my Firefox browser, BuildMonitor, which stays on your browser’s status bar and gives information about the overall status of CruiseControl’s continuous build process results. My browser is mostly open and with BuildMonitor I have the updated info about all CruiseControl builds. The only pitfall is it only works on Gecko-based browsers and doesn’t support IE.

Comments (1)

JMS testing

At work, I am currently building a small framework which behaves like a bridge that carries the requests and their responses between the client and our system. The client sends his requests through a web service call. This request is sent to our bridge framework named, Response Handler,  over RMI. Response Handler takes this request and puts into a JMS queue for processing by the application listening this queue. After the request is processed, the response is put into another JMS queue which Response Handler is polling periodically. Response Handler gets the response from this queue and sends back to web service. The following diagram shows the flow of work:

In order to test Response Handler, I wrote a tiny client which sends requests over RMI similar to the ones that the web service will send in the real environment and also a transaction schedular simulator, that fetches the requests from JMS queue and puts their response back. The web service-Response Handler integration over RMI can be done in the same VM with out any problem. Also since the Weblogic was always up at work, there also no problem with JMS integration. However, if I go to home, my tests for Response Handler was not running since, it requires a running Weblogic server. I didn’t want to be bound to Weblogic, so I installed OpenJMS to my laptop. Everything was fine and my tests were running without any problem on my laptop. When I deployed my application to the CruiseControl, tests were getting failed. There were no OpenJMS instance on Unix ! So I started to look for an alternative. I checked out Mockrunner. I need some integration tests which sends a requests to a JMS queue and listens a queue for a response. I discovered that Mockrunner does not simulate that behavior and directly returns something when it has to wait for a message in a queue. So I looked for an alternative and come up with Somnifugi. It works inside a single JVM and JMS Messages are send between Threads. So, in order to make my tests pass on any machine, all I did was to run each component(web service client, Response Handler, Transaction Scheduler, JMS-Somnifugi) in a seperate thread in the same JVM. I really liked it so my dependcy to Weblogic JMS server is broken and my tests can now work on any machine without requiring a JMS server for run.

I like in memory server solutions, like HSQLDB. Whenever you have a tight coupling to a heavy resource, they come up for rescue to make your tests run without any problem. The only lacking point I would like to state about Somnifugi is its scarce documentation. I come up accidentally how to use it from my application while checking Javadocs. It would be better if the author prepare a  seperate how-to doc for it. 

Leave a Comment

Extracting to a method

I see similar code repetitions at work in projects. One of the things which I come up frequently nowadays is while using log4j logger:

void insertEmployee(Employee employee) {
// code …

if (logger.isDebugEnabled()) {
logger.debug(“Some log message”);
}

// code …

}

These lines are written to many places in code which makes the reading of it harder for the outcomer. Extract Method refactoring fits very good for such cases. The above lines can be moved to a method:

void writeLog(String logMessage) {
if (logger.isDebugEnabled()) {
logger.debug(logMessage);
}
}

and whenever a log meesage is needed, our new method can be called which will help the readers understand the code more easily and also keep it clean.

void insertEmployee(Employee employee) {
// code …

writeLog(“Some log message”);

// code …

}

In future if a need rise to change the log level, it can be easily done by updating our new method instead of searching and replacing every piece of code doing a logging.

Comments (3)