Tuesday, April 25, 2006

Why I hate the default constructor


Hmm, I think I should provide a more detailed (boring) explanation on the reasons why the default constructor is in the list of my most hated anti-patterns.

Back to basics
OO theory provided us with the gift of encapsulation: the ability to hide details of the internal structure of an object by providing access control to attribute and methods thus allowing to split the exposed behaviour of an object (its interface) from the internal structure (the implementation).

The rule of thumb says that you should define all of your attributes private, and then provide public methods for the interactions with other classes. It’s a common practice (a short way to say that I quite disagree with that, but this will drive me out of scope) to provide also public methods to access the properties of the object, in a JavaBeans like fashion.

JavaBeans specification
JavaBeans specification defined that a special category of java objects, the Beans, was identified by the following properties
  • presence of a public parameterless constructor

  • presence of public methods to access properties of the object, such as getters and setters in the form of getPropertyName() and setPropertyName(…)
Plenty of discussions arose on the subject “is JavaBeans specification breaking encapsulation?” arguing that making attributes public would have had the same effect.

Unfortunately, what most of the developers forgot was that java beans specification were meant to be used by tools like IDEs (one of the first implementation of the spec was used to add user defined graphics object in a graphical environment, so that you could create your components and add them to the JBuilder palette) or frameworks like Castor or Hibernate. Java Server Pages also use Java Beans as the underlying object for the pages. Java Beans specification allowed tools to dynamically access properties of given (and unknown) objects by relying on introspection, driven by the coding conventions mentioned above.

Having a commonly accepted syntax for accessor methods, was a good result indeed, but was largely abused. But this gave new popularity to the empty constructor anti-pattern.

Why is the empty constructor evil?
Ok, I need a basic example to start from. Suppose we have a simple class like this:
public class Person {

private String name;
private String surname;

public Person() {}

public String getName() { return this.name; }
public String getSurname() {return this.surname; }

public void setName(String name) { this.name = name; }
public void setSurname(String surname) { this.surname = surname; }
}

By creating an empty object, and
then setting its property in a
Person p = new Person();
p.setName(“John”);
p.setSurname(“Smith);
fashion, we make a series of mistakes. At the end of the execution the result is just like
Person p = new Person(“John”,”Smith”);
assuming that we defined in Person.java a constructor like
Public Person (String name, String surname) {
this.name = name;
this.surname = surname;
}
But unfortunately the result it’s not the same during the execution. In other words object initialization is not atomic in the former case, while it is atomic in the latter. Moreover, the second constructor encapsulates creation logic, knowing that you need both name and surname to have a fully functional Person object. Using the default constructor, anybody trying to initialize the class should be aware of the internal details of the Person class to initialize the class.

Suppose then that you need instances of your class in different areas of your software. Every time, you should create your objects in the correct fashion. To do so, you normally would use the best replacement for OOP: cut & paste (which is a form of reuse after all…). Suppose then that you need to add an extra attribute to the Person class, namely birthDate, and that this attribute is required. Guess what? Changing the robust constructor to
public Person (String name, String surname, Date birthDate) {
this.name = name;
this.surname = surname;
this.birthDate = birthDate;
}
raises compilation problem on every invocation of the constructor. Is this a bad thing? Not at all! It allows you to correct every single invocation. It can be a simple job, or a tricky one (if the newly needed parameter is not so easy to get), but it ensures that the object is in the correct state. The other way round raises no compilation error, you know that you have to correct the code anyway, so you have the following options:
  • search every instance of new Person() throughout the code base and check that all the three setters are called (do you think that it’s fastest than fixing the compile errors?)

  • search some known instance of new Person(), add the new setter, and have a coffee

  • add the setter where we need it and let somebody else deal with the nullPointerException they are 99% likely to get.
Conclusion (where I explain my bitter mood)
What really puzzles me is that, every in the simplest situation, invoking the default constructor is almost always wrong! If your class has attributes, then you should favour a robust constructor instead, and makes you write less code. If it doesn’t have one, then maybe you don’t need a constructor at all but you could access static methods or use a factory to create your object. The only reason why we abuse so much this anti-pattern is just because we are damn lazy.

Tags: , ,

Monday, April 24, 2006

I hate the empty constructor


I was looking for inspiration for a short post about the abuse of the parameterless constructor. Then I found this and thought that there’s not so much else to say. If there is still somebody still convinced that

Person p = new Person();
p.setName(“John”);
p.setSurname(“Smith”);

is still as good as

Person p = new Person(“John”,”Smith”);

…please leave a comment.

Ok, then I thought this was a little bit too short, and wrote down some more explanation in the following post.

Tags: ,

Monday, April 17, 2006

Coding, the IKEA way


Today I spent some time mounting handles in an IKEA wardrobe I bought some weeks ago. Mounting handles is somehow a precision job, cause you don’t want your house to look like a cartoon room. So, I took measurements of the drawers, and of the handles, asked my wife in which position the handles looked better, tried to fit the preferred position with some basic proportion (so that the spaces above and below the handle are respectively ¼ and ¾ of the total drawer height), then calculated on paper the exact coordinates for the needed hole. The next step was to apply the measurement on the drawers. I didn’t have a professional measurement tape at home so I double checked all the measurements before marking with the pencil the final target for the drill. When I was satisfied wit the position I made a small hole with a nail in the exact position. Then I took the drill. Everything was in place so the result was pretty close to perfection.

It’s surprising how far we can go from this metaphor if we just turn back to our beloved coding activity. When asked to perform a simple, yet nontrivial, task, the average developer would switch on the IDE (I am optimistic, the IDE is obviously already on by default, and the mail client is now probably an Eclipse plugin) and start coding. A couple of lines there, a TODO marker when an ugly shortcut is taken, another couple of lines there, and the job looks almost complete. Then a failure during test sets the need for another quick fix just before the deadline. Ooops! Time for removing TODOs has gone, the code seem to work and there are more urgent things to do. In other words, the TODOs just made it to production. Cheers!

Let’s get back to my IKEA drawers. Really I should have started from the drill (which is my favourite tool anyway)! Making a couple of holes in the wardrobe (just to check if the drill worked) then refining the hole positioning by successive attempts. This way the handle probably wouldn’t be that aligned, or perfectly fixed, so I would probably have had to put some stuff to have the handles blocked some way. Of course there is only one probability over a million that all the handles are vertically aligned and centered in all the drawers, and I should have used some tape to close the unnecessary holes I did at the beginning, but as long as the whole wardrobe doesn’t fall apart I should have been pretty satisfied.

Ok, I probably went too far. Code can just be erased, while drill holes are irreversible. Well, code can be cleaned if you have time and we all know that you’ll never have…

Tags: ,

Thursday, March 30, 2006

Test Driven Development and the Romantic Programmer


Stressing on test activities is part of my usual job as a consultant. Sometimes we can push till the test first practice as recommended by XP and TDD, sometimes we just promote xUnit test cases to primary level project artifacts, meaning that an iteration is not completed if there’s no working test in place to testify it. Testing is different from developing in many ways – honestly, you need a different mindset to be an effective tester – but after learning a couple of tricks, developers easily comply to the new methodology.

Even if you can achieve some success with heavy testing practices, you can’t leave the field unattended. What I observed in many cases is that quite a few developers tend to shift back to the usual methodology, even if they recognized that test driven development was better. There is no rational answer for this, except that the old way was more amazing. I know what they mean: in some way it’s just gambling applied to software development: it’s the thrill of fixing a bug changing a couple of lines of code and deploying it right in production, or assembling an integration version just the night before the final release. It’s just thousand miles away from being professional, but it’s just the same feeling of the good old days of the university, or the first garage programming experience. Some people are just in search of this thrill, and that’s the reason why they like this job, and they’re probably getting terribly bored, if the system doesn’t do anything else than growing smoothly milestone after milestone without a complete crash in the middle, and a miracle hack recovery, of course…

Tags: , ,

Sunday, March 26, 2006

UserFriendly Strip Comments

Found this some days ago, while browsing Raymond Chen's blog. It's not that far from reality...

UserFriendly Strip

Tuesday, March 21, 2006

Wiki Collective Ownership


The collective ownership of the wiki the content, aside from sounding “too liberal” for many managers I know, has a limitation that you should be aware of. In an XP development team there are some factors that help collective ownership being effective.
  • Team size is relatively small, often sharing the same space: clarifying or correcting a published information is a quick action, involving no ceremony.

  • Teams have a well defined common goal, meaning that the concept of “what is good for the team” is close enough to “what is good for the individual”. Frequent role switching help enforce this perspective also;

  • Development teams are constructing a system. Crafting, and seeing something growing, with everybody’s contribution, helps feeling part of a greater whole, enforcing the community feeling.
You can’t expect these factors to be equally effective when you promote these tools at a company level. In an average company, individual profiles and goals are much more spread around than in a single development team, and have also a longer timescale (XP-like documentation often lacks support for the next project).

Users and contributors
Yet, the wikipedia success story might make things look too easy, when in fact they’re not. The underlying model sounds like this:
  • some pioneer contributor start writing on some known topics

  • readers benefit from the information, and if they have something to add to make the information more complete, they are invited to contribute.
What’s missing here is that this model works with big numbers, let’s say a contributor every thousand readers (I have no stats, but you get the idea). But wikipedia is a world service so this approach works anyway. The same might apply to Linux for example, I know many linux users, but I think none of them added a line of code to it. So, size is probably factor number one if you are expecting “spontaneous” contributions to the content.

On a smaller scale you won’t get the same result, unless you really have a shared “vision” and/or a common “goal” and you actively push for it.

Tags: , ,

Thursday, March 16, 2006

Wiki spontaneous structure gallery


Sort of minor post here. While writing down the follow-up of the last post, I found myself defining a classification of the structures wikis tend to assume when left growing in an uncontrolled fashion. I was tempted to call them patterns, but they’re far from being a proven solution, and often they tend to be part of the problem instead.

Tree-like structure
This is the most common shape, when people are told to use wikis. Information is put on a deeply nested page, on a path that doesn’t have any other content than the link. The overall structure is no different than a deeply nested directory tree, but it’s probably not the way to efficiently use wikis:
  • if you are exploring the wiki you have to click quite a few times to find meaningful information;

  • the information is always in the leaf and not in the node that addresses it, sometimes the information is only in the attached document;

  • a nested directory tree is a personal interpretation of a structure to a set of information that doesn’t yet have one. Put in this way is a sort of arbitrary act: I’ve never seen two person agree on a directory structure “at a glance”, simply because people’s brains are organized differently: it’s easier to agree on a subject than on the way we store it in our personal mind categories.
You can bypass this limitation by accessing information through a search facility, but having a wiki full of meaningless page doesn’t make the media appealing at all. Like it or not, you have to make your wiki look useful in order to have the community use it.

Blog-like structure
In this case, the author puts the whole information needed in a single page that keeps getting bigger and bigger. Aside from readability, this approach fools feed readers, that keep marking the page as modified, leaving the reader the burden of discovering where the page has actually changed.
This structure might fit sequential information (like a sequence of steps in a how-to document), but tends to be discouraging for the readers. Linking the page, as a memo, also turns out to be less efficient, cause you ‘re linking the whole page and not the section.

Document Aggregators
If you have a lot of external documentation to be made available to the wiki community, aggregator pages in a wiki can really make things a lot easier for the newcomer: they can provide extra information about where to start reading, the context the documentation was written, the intended audience and so on. Adding just a note, when attaching a document could really make the difference.

If the documentation is evolving, then we are probably putting ourselves at risk of duplicating the information. Some wikis are more oriented to software development and allow integration with SCM systems, more often this is a duplicated step so misalignments are always possible.

Hypertext blob structure
This is my personal favourite. The information is divided into several pages, each one focused on a single topic. Pages normally contain a lot of outgoing links, some pointing to a related page, some simply acting as a “to do” marker. In this way the information is structured as a “pure hypertext” containing links to all the related pages.
Unfortunately this structure works fine if you are using it as a help file, but tends to make you loose the grip about where the important information is.
The worst situation happens where you have to linearize the content of the wiki: both the structures described before can be represented as trees with a root acting as a starting point, while the pure hypertext has returning links transforming the structure in a graph, thus making linearization harder. To handle these situations some wikis provide a more constrained tree-like structure that can be enhanced by extra links, which have no impact on the main structure.

With such “fully floating wikis”, a technique I often use to help user navigate in complex information structures is to provide one or more access pages that act as reading keys of the whole hypertext. A hypertext might have many of these indexing pages pointing to safe “starting points”.

Tags: , ,