Day 7: Give Only One Responsibility to Each Class

Hi, this is the 7th 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 easiest ways to design software is to use the responsibility of each class as a guiding tool to improve your code. However, few people use this simple technique to create truly outstanding software.

The single responsibility principle (SRP) is a software development principle that states that each class should have one and only one major responsibility in the system. Any class with more than one major responsibility should be broken up into smaller classes (we can also merge together classes that do the same thing). Another definition, from wikipedia.org/wiki/Single_responsibility_principle:

“… the single responsibility principle states that every object should have a single responsibility, and that all its services should be narrowly aligned with that responsibility.”

Let us first understand why this works, and then how we can use the single responsibility principle (SRP) in practice.

Reasons Why the SRP works

The main reason is that giving a single responsibility for each class is just a compact way of describing object orientation. Let us try to describing object oriented code through the lights of the SRP:

  • Methods are associated to a class because they have something to do with that class. If a class has too many responsibilities, the methods there have no reason to exist inside the class. So, the idea that objects have associated methods makes sense only if the SRP is used.
  • Polymorphism is necessary because classes in the same hierarchy respond to messages in different ways. However, this is possible only if there is a standard set of messages that make sense for a class. The SRP says that each class in the hierarchy must have a single purpose, and this purpose is defined by how they answer to a specific message, by its polymorphism.

Moreover, giving a class a single responsibility is a great way of defining the scope of a class. Instead of piling methods together, you are just looking for a functionality set that go together well.

How to Apply the SRP in Practice

The easy answer for that is just: refactoring! First, go through your design and try to identify the responsibility of each class. The way I like to do this is to create a comment on the top of each class starting like this: “this class is responsible for …”

If you cannot state in a few words what the responsibility of the class is, then you are probably doing too much.

Another clue is given by abstract descriptions like: “this class is a manager for …”. Such descriptions hide the fact that a class has no focus. Try to give a better description of what this class does, or change it to improve its focus.

Further Reading

The single responsibility principle has been explained by many writers. Here are a few that I like:

Go to the next post of the series:

[foto credit: www.flickr.com/photos/papalamour/2856099282]

Using Default Values in C# 4.0

One of the new features of C# 4.0, the new version of C# included in Visual Studio 10 beta, is the use of default values.
If you are on the road long enough to be a C++ programmer, you will remember that default values was one of the features of member functions in C++. Quickly stated, a default value in a member function can be used to provide a value for an argument when it is not introduced by the calling code.
Having default values saves some time when writing code because you don’t need to type standard values for arguments that are rarely used.

Problems With Default Values

Default values, however, are not without their problems. First, the use of a default value can lead to confusion when creating methods. Suppose for example that you have two methods:

void f(int a, int b=0) { }
void f(int a) { }

This may trigger a compile time error, because there may be an ambiguity when using f with only one argument.
The worse problem in C++, however, is that the value of the constant (in this case 0) is not guaranteed to be the same between definitions of f in the base class and derived classes. This is a big problem that C++ users are still trying to solve today.

C# implementation

The advantage of C# in this issue is that, having a smaller compiler, they can make it much smarter. For example, the C# compiler will always give precedence to methods that have no default arguments, so there is less possibility for confusion.
Despite this, we should try to avoid default values in methods whenever possible. Default values can still make your code confusing and hard to maintain.
To avoid this kind of confusion, for example, you can try to get rid of methods with parameters that are used only some times. One way of doing this is creating separate methods for different cases. Also, you should give different names to methods. For example:

void FuncWithExtraArg(int a, int b) { }
void Func(int a) { }

is a better way to name the methods described above, so we don’t need to use a default argument.
Default arguments were added to C# to simplify coding for Excel and Word, where method have a lot of parameters of this kind. If you don’t need this feature, however, you probably should avoid default arguments altogether, and use them only when really necessary.

Day 6: Write a Tech Spec

Hi, this is the sixth 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.


Among all practices that could make you a better developer, perhaps one of the most overlooked is writing tech specs.

In my opinion, the reason for this is that many feel that, since writing tech specs are not coding, so it must be boring and useless.

Boring it may even be, but useless it is certainly not. The time you spend to write a good Tech Spec can be the most useful hour of your whole development effort.

What is a Tech Spec?

A Tech Spec is the lingo used in software development for a document describing the technical features of a piece of software you want to write. Most of the time, tech specs are written in boring language — the kind of stuff that only a boss will read when creating another report.

It doesn’t need to be like this though. Although most Tech Specs are created using highly technical language, you can create your own tech specs in a language that is easy for you to understand. And, in fact, this is the way that I recommend that things be done.

Here are some tips that I try to follow when creating my own tech specs:

  • Create your tech spec at a level of detail that you are comfortable with. Add examples, pictures, sample code, whatever makes the concept easier for you to grasp. Don’t try to start with something formal and bureaucratic – it usually doesn’t work well.
  • If your boss requires a formal tech spec, create one from your original, personal spec. It will be easier to do, and after you get practice on doing this kind of things, it takes only a few minutes.
  • Append your original spec as supporting documentation. You want your fellow developers (or yourself, later) to have access to your original ideas, which are certainly easier to understand.

What If You Don’t Have The Time?

You certainly do have the time. This is one of the things where you will end up spending more time if you don’t write a tech spec. So, the issue is that you don’t have the time NOT to write a tech spec before coding. It is like building a house without a blueprint: you can do it, but you shouldn’t.

How Can I Learn to Do This?

It really takes some time and experience, but here are some tips that I think can help:

  • Write down all the functionality that you want to implement (a good functional spec might be required for this step — more about this later);
  • Describe how each functionality can be implemented: the classes used, major algorithms, and/or databases. Also, provide a high-level overview of the functionality that will be implemented.
  • Give some idea of the time necessary to implement the functionality. This is an additional step that will help you a lot in determining the scope of what can be done.

Conclusion

Writing tech specs is not easy, but it can become natural if you practice. Moreover, if you find out how this works it can save lots of time for your and your team. I highly recommend starting with simple specs for small functionality, and then extend this to more parts of the system. In the end, it is all about planning before executing.

Go to the next post of the series: