Day 5: Write a Unit Test Before Coding

Hi, this is the fifth part of  a series of posts on 30 tips to becoming a better developer. If you would like to keep up to date with the topics that I am covering, just check the main post.


From all trends of software development in the last years, few are so important and useful as test-driven programming. Testing in general and unit testing in particular are an essential part of any software system and cannot be left to the last moments.

In the past, testing was considered to be almost an afterthought. The idea was: to have a bug free system, build, then test. That was in the early days, when people believed in the waterfall model and in Santa Claus.

Thankfully, experience has shown that nothing could be further from the truth. If testing is not added to the day-to-day of building the system it is really hard to make sure that final testing will work at all. Another way of viewing this issue is to understand that testing isn’t important only at a single moment in time. Instead, it is necessary during the whole process of creating software, the same way compiling is necessary at each iteration.

Unit Testing as “Extra Compilation”

keyboardThe workings of a compiler provide a good analogy for unit testing. Think of unit testing as an extension of compilation where you are testing for dynamic properties of your code, instead of static properties. For example, the compiler will easily check syntax errors and identify areas where you are using the wrong data types. Unit testing, on the other hand, will identify potential errors in the way you are implementing algorithms. The best thing of unit testing is that you are free to perform any test that can be possibly implemented in your programming language. Thus, you are not limited to checking for static properties, as your compiler is, but you can extend your checking to any dynamic property whatsoever.

What is the Catch?

Well, the catch, if you look at it in this way, is that you need to plan for testing. In reality, the main reason why it is difficult to test existing code is that it was not designed to be easy to test. Examples abound, like the common case where an object has concrete dependencies on many other objects, which creates a nightmare for someone who wants to test a class in isolation. There are several cases that can be considered when writing testable code, but the easiest way to solve these issues is to ask yourself before starting to code: how can I write a test for this functionality? Once you ask and answer this question, you will have everything you need to make unit tests that work and that are appropriate for the feature you want to implement.

Write a Test Before Coding

This can be summarized as saying that you should write a test for features you want to implement before writing the code. The reason is that if you are able to create a test for a feature, you will consequently implement it in a way that can be easily verified. And this happens just by the way you implemented the feature. Most people fail in this simple step and compromise all the time spent in coding. If you have to rework your code to test it, you might just as well be adding bugs that were not there before. So, adding unit tests after the case can, at least initially, damage something that was working fine.

Conclusion

Testing is good, and nowadays more and more people are aware of it. However, the catch is that you need to code with testing in mind, or you risk losing your time, instead of reaping more of the benefits. Writing tests before you code is a smart way of avoiding problems that might arise when creating unit tests.

Further Reading

  • Pragmatic Unit Testing, along with its sibling for C#, cover practically everything you need to understand about testing, including coupling issues, mocking objects, and more.
  • Test Driven Development teaches you how to develop based on tests. It is a great book with lots of examples for you to get used to this methodology.

Go to the next post of the series:

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • HackerNews
  • Reddit
  • StumbleUpon
  • Twitter

Day 4: Learn a New Programming Language

Hi, this is the forth part of  a series of posts on 30 tips to becoming a better developer. If you would like to keep up to date with the topics that I am covering, just check the main post.

Why Learning a New Programming Language?

Few habits maintained by a good developer can be as beneficial as learning a new programming language from time to time. The reasoning is simple: no language is perfect, and most languages have some useful feature or abstraction that may enrich your pallete of abstractions. Consider for example what happens when you are used to strongly typed languages, such as Java. You start your program design looking for what types would be necessary to implement the concepts you are trying to represent. While I am not saying that this is the wrong way to go, there are other options that you can take when working with other languages. For example, if you work on a functional language such as LISP, you will be more interested in designing a domain specific language (DSL) that can more easily represent your problem — and this might be an even easier and more productive way of looking at the problem. Different languages have different things to teach about how to approach problems. By learning different languages, you are learning different techniques that would be unnatural in your original language. For example, it is possible to write OO programs using plain C. However, as everyone writing interfaces in GTK+ knows, it may be a process that is a little cumbersome. Learning a new programming language may be just a simple way to try a concept that you want to use somewhere else. For example, you may want to understand how a dynamically typed language works, and use some of the concepts in Java using reflection, for example. The combinations of possibilities are endless, you just need to start searching around.

How to do it

There are several opinions on how to go about learning new programming languages. Some people think that you should try to learn a language that is very different from the one you are using. For example, if you are using VB, try Ocaml (that might be a little too much, but you get the idea). Another option, which I think is easier to implement, is to use your current knowledge as a leverage for learning other languages. In that case, if you start with VB.NET, you can next try C#, which uses the same library and concepts, so it is an easier jump. Then, you can try Java to learn at least a different environment. You can also learn new languages based on the task you want to perform. For example, if you need to create a new website, you might try Ruby and a framework such as RoR. If you are going to write an AI based application, LISP or Prolog might be good options. You just need to search around for people that are working on the problem you want to solve, and try to learn the language they are using. Whatever the way you do it, I think it is good to try. You can’t learn all computer languages that have been created, but the experience of trying to master some of them will make you a more flexible developer, that knows the tool to use for a given problem.

Further Reading

  • If you want at least to understand the differences between programming languages, the best thing is to take a general book on programming languages, such as Concepts of Programming Languages, By Robert Sebesta.

Go to the next post of the series:

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • HackerNews
  • Reddit
  • StumbleUpon
  • Twitter

Day 3: Keep a Programming Diary

Hi, this is the third part of  a series of posts on 30 tips to becoming a better developer. If you would like to keep up to date with the topics that I am covering, just check the main post.

One of the things that make software developers different from other professionals is that they work with a very flexible material: programming code instead of iron or cement. However, given this big difference, some developers forget that beyond anything else they still are professionals. They fail on some basic tasks that everyone else has already figured out.

One of the things that most of us fail is on keeping proper records of our accomplishments. Some developers rely exclusively on tools such as source control and bug management software to define their tasks and record accomplishments. I think this is not enough.

Keeping a programming diary is a good practice that can not only become a learning tool, but also avoid a lot of trouble in the future.

What is a Programming Diary?

A programming diary is not a place to register your emotions and particular stream of thoughs. Leave this to your personal diary, if you keep one. A programming diary is more similar to a dayly recording of facts, decisions, questions, and conversations. It works just as a repository of information that is not structured enough to keep on the official design documents for your project. For example, here are some things that you would like to record in your programming diary:

  • Important discussions about the project with your boss and co-workers;
  • Changes on decisions about design;
  • Things that went wrong;
  • Ideas for improvement;
  • Area of the code that are dangerous, or difficult to understand.

Keep it Online

One thing that we learned about information in the last few years is that it becomes more useful when more people have access. That is why your programming diary needs to be made available to the whole team, as the other resources, such a bug tracking database, are.

Think of this as an internal blog that contains information that should not be shared to the general public. In this digital diary you can keep the information flowing between you and the members of the group.

Some groups, like the company where I work, use a wiki for sharing this kind of information. I think this is a great resource, but having a personal diary for each developer is even more useful. The reason is that it makes each one to take personal responsability for the information that is shared. Wikis are nice, but there is always one person or another that takes control of the conversation (even if they are doing this nicely). With personal diaries, each one is responsible for the information they are sharing.

Conclusion

Many people use these ideas in one way or another, but I have seen no consistency in their use. Having a development diary is such a useful thing, however, that I recommend everyone to try it for a while. Understanding your own problems and decisions can make you learn more about where you need to improve. Ultimately, this is a discipline that can make you a better developer.

Further Reading

  • The Pragmatic Programmer is a seminal book, that provides advice on this and related issues. This books is not from the pre-blog era, but still have useful guidelines that I recommend.

Go to the next post of the series:

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • HackerNews
  • Reddit
  • StumbleUpon
  • Twitter

How to become a better developer in 30 days

This is the main post on a series about how to become a better developer in 30 days. If you would like to keep up to date with the topics that I am covering, just click on the links bellow:

Day 2: Write Shorter Methods

Hi, this is the second part of  a series of posts on 30 tips to becoming a better developer. If you would like to keep up to date with the topics that I am covering, just check the main post.

Today I will talk about a technique that made my code to improve in a fantastic way, probably more than anything else that I had tried before: writing shorter methods. The technique is so simple and so useful that I don’t understand why everyone isn’t REQUIRED to do this.

Why Short Methods?

The main idea behind using short methods is that you keep yourself from doing too much in any one location. If you ever tried to write more than a few lines of code in any method you most certainly were mixing levels of abstraction, which ultimately leads to methods that are difficult to understand and to maintain.

For example, consider a method that updates information about employee salaries. If the same method is writting information to the database, it is trying to do two things at once. So, if you change the code to update salaries you will need to change the method. The same happens if the database changes. That is, if you add different responsabilities to a method you make it more unstable, just because there are more reasons for it to change.

On the other hand, when you have methods that are really short, you are required to go straight to the central issue that you are trying to solve. No space for secondary concerns exist.

It all boils down to the issue of using the right abstraction level. With long methods, abstractions are not well maintained because you may spend too much space with instructions that are a level bellow from where you are working.

What is Wrong with Long Methods?

Long methods are bad in a lot of ways. It would require a lot of space to write all that I don’t like about long methods, but here are some of the most important issues:

  • Long methods are difficult to understand. With size comes complexity, and if you are maintaining code where methods are huge, you need to be very careful about side effects. Remember, all local variables are in practice global inside the scope of a method. If you have lots of things going on, it is difficult to see what the collateral damages might be.
  • Long methods are hard to optimize. Even after you make you long method correct, it is difficult to make the changes necessary to optimize it. For example, if you decide that a certain variable is used in a wrong way, you still need to spend time proving that everything works after a change. A simple change like this may create bugs that you will take a long time to notice.
  • Long methods obscure the intention of the code. Since in a long method you are certainly going into more detail than necessary, you are creating code in two or more levels of abstraction. This also contributes to make the code difficult to understand.
  • Long methods can slow down your code. This is true if you are using a manged language such as Java or C#. These languages use JIT engines that transform methods into executable code. However, a long method takes longer to convert into native code than a short one. Also, long methods will have parts that may not be used frequently, so there is no justification for compiling it, other than the fact that it is part of the long method.

How short is short enough?

This is a subjective issue of course, and as such there is no clear answer. However, I like the guideline that 10 lines is already too much. This is a guideline used by Robert Marting in his books. I think it is a very reasonable rule of thumb, but for me it is more of an upper limit. Methods with 3 to 6 or 7 lines are even more interesting, because they condense the main ideas of the abstraction you are trying to convey.

Additional Advantages

Short methods also make some other things easier, like creating good method names. Since the method is allowed to do only a small number of actions, it is easier to figure out what it is doing — and thus given it a proper name.

Another area that is positively affected by short methods is testing. It becomes much easier to see the cases that should be tested when there is just a small number of operations involved. Instead of trying to cast a big net over a sea of options, testing small method tends to be a much more focused experience.

Conclusion

I think that this item covers a practice that, by itself, has the potential of making your code much better. Writing small methods helps with separating the different concerns of your application. It gives a simple guideline that helps on making decisions about how code should be organized. It also helps in eliminating many of the problems in the areas of testing, revising, and maintaining existing code.

Additional Reading

  • The first book that really showed me the importance of small methods was Clean Code, by Robert Martin. It covers a lot of ground in matters of code organization, which I believe are essential.
  • Another book that explains why long methods are bad is Effective C#, which will convince you that such methods can slow down managed code in languages like C#.

Go to the next post of the series:

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • E-mail this story to a friend!
  • HackerNews
  • Reddit
  • StumbleUpon
  • Twitter