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:

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:

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: