A programmer's tale

Wednesday, May 24, 2006

class, interface and method-How do I address you?

In java or java like OOP language, all we need to create new classes, interfaces and methods
inside them. Now a days I am suffering from naming phobia of class, interface and methods.
How do I name a class, interface or a method so that it's intuitive and easy to understand and
use. I've gone through Joshua Bloch's Good API design guidelines which is available in JavaPolis2005 archive. It's damn useful to realize the importance of designing good API and how it can affect or effect company's clients. But the less-experienced programmers like me frequently face a very fundamental problems - how to give a meaningful name to them.

Once in our office meeting we, juniors are advised some good naming conventions. It was very
much helpful to me as a fresher. Now I am trying to remember some of them and also sharing my ideas about the same.

The rule of thump is that:

1. interface name - should be Adjective like words like Readable, Appendable etc
2. class name - should be Noun like words like Reader, Writer, Printer, ReportWriter etc.
3. method name - should be Verb like save(), print, edit(), createIcon() etc.

The basic idea behind these types of naming convention is ease of use of the APIs that are
easily understood without documentation or a little documentation. That's a way of increasing
the productivity also. If a developer has to read all the documentation to understand the intention of your API, he/she will be definitely less productive. Here lies the importance of good naming of APIs.

Let us consider a simple example, if I have a class named XML2TextConverter then I will expect a method like convert() inside the class. If I cannao find the method I expected then I have to browse through the documentation which is always if-avoidable like thing to a rapid developer.

The most critical naming problem I frequently face is name of a method. Leys say I'm building a
GUI component like JTable that has also a header, now inside my MyTable class I'm planning to provide a method to attach a header to my MyTable. Now what should be the name of this method setHeader(), setTableHeader(), addHeader(), or addTableHeader(). If I take setter methods then the user of my API natuarally expect a argument like setHeader(Header header). So if the class MyTable internally add the table header, it's more appropriate to use addXXX methods. If you want to give your API user the previleage to add header externally, then setXXX methods are more appropriate here.

In design pattern some very good naming convention is used in developer's community. if your
application or your indivisula falls in these category, then it is better to use the pattern
convention.

During my reding through the book Effective Java: Programming Langueage Guide(Joshua Bloch) I found a very good discussion over naming convention , especialy in item25, 38. This book is a
masterpiece and must-have book for all professional java developer. It's also winner of prestizious
Jozt award.

0 Comments:

Post a Comment

<< Home