Day 8: Take the Rest of the Day Off

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


During the last weeks, I have been writing about technical ideas that anyone can use to become a better programmer. However, there are some non-technical ideas that can be just as important for your long term success as any other development practice.

Look around you, and you’ll see that most people working on technical areas are overworked. This is the sad truth. Although improving organizational aspects will reduce the workload, there is no way to avoid this fact.

This is a problem confronted by people working on corporations, small and big. We spend most of our time on meetings, doing reports, and other useless “work” that takes big chunks of our time. Then, we need to crank code as fast as possible to release before the next deadline.

The biggest problems happen when we become stuck on a development issue. May it be a bug, or a feature that is difficult to implement, we are tempted to work on it for many hours, until the issue is solved.

Don’t do this.

Working on tasks that require concentration when you are tired is more unproductive than doing nothing. You quickly lose concentration, and the small method that should take 15 minutes to code and test becomes a 1 hour job with ten hidden bugs.

If you stop working when you are tired, at least you won’t have to fix the mess that you WILL do if you are not properly rested.

Developing software is a complex task, doing it on difficult conditions is even worse. Unless you really need to deliver something the very next day early in the morning, never stay late at night trying to finish a program (unless you are doing so because you feel fine working late).

Even if you are on a corporate environment where you can’t take the rest of the day off, try to do something else: write documentation, add comments where necessary, improve your building tools. Anything that will mean a break from your current development task.

The trick in doing this is that your subconscious mind is always working on a problem. And it works best when you don’t have to worry about something. This is a reason why my best ideas happen when I am taking a walk or before waking up.

You should try it too.

Go to the next post of the series:

(Foto by mondopiccolo on Flickr)

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

Quick Links on Using C# as a Functional Language

I am using C# a lot these days (due to my work with Microsoft Solver Foundation), and for that reason I am getting more and more interested on resources that explain the functional features of the language. Since we have lots of nice features on LINQ, the idea is: why not to use that to simplify our lives?

As it turns out I found a lot of nice resources. First, a presentation showing many of the functional features available on C# (at version 3.5). For example, you will find there great illustrations of how to use closures, functional sorting and generalized algebraic data types.

Another interesting resource I found today is a free library of parallel extensions to C# (which works with version 3.5). With this library, you can write programs that create threads in a functional way using LINQ, and therefore minimizing synchronization problems.

Finally, I would like to point to Bart de Smet’s blog as a fantastic resource to really understand LINQ and functional C#. If you have the time, go through the examples and exercises in that blog and you will really flex your C# muscles.

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

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]

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

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.

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

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:

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