Sonntag, 11. November 2012

Describing Attributes of Classes

In many of the classes I saw over the last years were no comments to describe the attributes or member variables. The most frequently used style of commenting attributes from my experience is reformulating the variable-identifier in natural language. Consider the following Java-class as an example:


public class Invoice {

 /**
  * The order
  */
 private Order order;

 /**
  * The invoicing amount
  */
 private double invoicingAmount;

 /**
  * The id
  */
 private int id;

 /**
  * The due date
  */
 private Date dueDate;

 [...]
}

I believe that these comments will help nobody, because they are just redundant and contain no further information. But how to do it better? 

Well, in English one could describe the overall and general meaning in form of clauses. A clause is the simplest grammatical unit that can express a "meaning". There are several forms of clauses, e.g. relative ("I like the car, which is red.") or temporal ones ("After cleaning the house, please wash the car.").

I made the experience, that a simple patterns fits the problem of commenting attributes. You can use the main clause "This attribute represents ..." + CLAUSE. Since the main clause is always that same, you can omit it and just write the relative or temporal CLAUSE into your code. Look, how this pattern applies to the example from above:

public class Invoice {

 /**
  * What the customer has to pay for
  */
 private Order order;

 /**
  * What the customer has to pay including all discounts.
  */
 private double invoicingAmount;

 /**
  * What is used to find/identify this invoice.
  */
 private int id;

 /**
  * When this invoice has to be paid.
  */
 private Date dueDate;

 [...]
}

All of these clauses have the type of an content clause. They have the same role as an object. These clauses simply replace the object noun. I think, these comments express explicitly the general purpose the attributes. So we have an added value. When formulating it with clauses, this seem to come automatically. 

Please let me know if this patterns works for you :).

2 Kommentare:

  1. hi jan,

    ich gebe dir vollkommen recht, das erste muster der kommentare dient nur dazu, daß halt ein kommentar vorhanden ist, er nützt aber keinem etwas (ausser einem tool, daß prüft, ob ein kommentar vorhanden ist und 100% signalisiert, alles wurde mit einem kommentar versehen).

    dein vorgeschlagenes pattern findet meine zustimmung. mit diesem kommentar kann ich etwas anfangen.

    aber wie kann man sicherstellen, daß dieses pattern auch verwendet wird/wurde?

    egal, ob die kommentare auf deutsch oder englisch (oder gemischt?) sind, daß sollte maschinell überprüfbar sein ...

    hast du dafür auch schon eine idee?

    das könnte natürlich auch mal mdeine habilitationsschrift werden ;-)

    dieses plugin für eclipse würde ich auf jeden fall direkt laden!

    mit den besten grüßen aus alsdorf

    jocki

    AntwortenLöschen
  2. Hallo Jocki,

    vielen Dank für deinen Kommentar :). Mein Bauch sagt mir, dass das schon maschinell prüfbar wäre. Aber ich habe dafür noch keinen konkreten Algorithmus. Ich werde in dieser Sache aber weiter am Ball bleiben.

    Viele Grüße

    Jan

    AntwortenLöschen