Archive for the ‘How to code properly’ Category

How to code properly: A revision control tip

Tuesday, July 31st, 2007

One of a developers best tools is his revision control system. Be it the crufty old standby CVS or Subversion, the darling of the hipster Rails community, or a wacky fringe “I hope you paid attention in school. And majored in math. And got an advanced degree in it” system like Darcs, it can be your best friend, saving you time and again from almost certain inconvenience. Getting the most out of your revision control system can be difficult, but it’s a skill worth mastering. Today I’m going to share something out of my personal bag of tricks that I think developers everywhere can benefit from.

The idea is relatively simple. If you have code that you aren’t using any more, delete it. If it happens that you need it again, get it out of the revision control system. Simple as that.

Compare the following sample bits of code:

 public void foo( /* String filename */ )  
 // throws CannotMakeItWorkException
 {
   // try {
   //   if(null != filename) {
   //     FileWriter fw = new FileWriter(filename);
   //     String buffer = "Hello World";
   //     fw.write(buffer, 0, buffer.length());
   //   } else {
   System.out.println("Hello World\n");
   //   }
   // } catch(IOException ioe) {
   //   throw new CannotMakeItWorkException("*shrug*", ioe);
   // }
 }

and

 public void foo() {
   System.out.println("Hello World");
 }

Indeed, both functions do the same thing. One is relatively straight forward. The other could stand some pruning. Is it worth knowing that foo() once took the name of an output file as an argument and wrote “Hello World” to that file when the argument was non-null? Maybe. Does keeping that information around in the soruce make foo() an absolute pain to read? Without question.

The grand thing about revision control systems is that if it turns out that you in fact do need foo()’s advanced file writing capabilities, you could without too much difficulty pull an old version out and use its code.

So remember that if you’re using a revision control system — and you had better be — that you can go ahead and delete dead code. Your coworkers will thank you for it.

Happy Coding!


This is a free Wordpress template provided by Mathew Browne | Web Design | SEO