4

I am reading Uncle Bob's Clean Code. In chapter 16, the book shows how to refactor an example. There is one part that I cannot catch the purpose of writing in such way.

  • What the purpose of using protected keyword here?
  • Why do some methods like _getMinimumYear() start with an underscore?
  • Why do use a pair of method with same name rather than an abstract method like
    public abstract int getMinimumYear();

public abstract class DayDateFactory {

    private static DayDateFactory factory = new SpreadsheetDateFactory();
    public static void setInstance(DayDateFactory factory) {
        DayDateFactory.factory = factory;
    }

    protected abstract DayDate _makeDate(int ordinal);
    protected abstract DayDate _makeDate(int day, Month month, int year);
    protected abstract DayDate _makeDate(int day, int month, int year);
    protected abstract DayDate _makeDate(java.util.Date date);
    protected abstract int _getMinimumYear();
    protected abstract int _getMaximumYear();

    public static DayDate makeDate(int ordinal) {
      return factory._makeDate(ordinal);
    }

    public static DayDate makeDate(int day, Month month, int year) {
      return factory._makeDate(day, month, year);
    }

    public static DayDate makeDate(int day, int month, int year) {
      return factory._makeDate(day, month, year);
    }

    public static DayDate makeDate(java.util.Date date) {
      return factory._makeDate(date);
    }

    public static int getMinimumYear() {
      return factory._getMinimumYear();
    }

    public static int getMaximumYear() {
      return factory._getMaximumYear();
    }
}

    public class SpreadsheetDateFactory extends DayDateFactory {
    public DayDate _makeDate(int ordinal) {
    return new SpreadsheetDate(ordinal);
    }

    public DayDate _makeDate(int day, Month month, int year) {
    return new SpreadsheetDate(day, month, year);
    }

    public DayDate _makeDate(int day, int month, int year) {
    return new SpreadsheetDate(day, month, year);
    }

    public DayDate _makeDate(Date date) {
    final GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(date);
    return new SpreadsheetDate(
    calendar.get(Calendar.DATE),
    Month.fromInt(calendar.get(Calendar.MONTH) + 1),
    calendar.get(Calendar.YEAR));
    }

    protected int _getMinimumYear() {
    return SpreadsheetDate.MINIMUM_YEAR_SUPPORTED;
    }

    protected int _getMaximumYear() {
    return SpreadsheetDate.MAXIMUM_YEAR_SUPPORTED;
    }
}
jaychang0917
  • 1,880
  • 1
  • 16
  • 21
  • 6
    Using a special naming syntax for methods or fields (other than camel case) is bad practice. The Java coding guidelines say not to do it. I'd say that Uncle Bob's *Clean Code* isn't a very good book on Java. – markspace Jul 25 '14 at 16:38
  • In the future, questions like this are usually better received on Programmers.SE. – Qix - MONICA WAS MISTREATED Jul 25 '14 at 16:38
  • http://stackoverflow.com/questions/1899683/is-there-a-standard-in-java-for-underscore-in-front-of-variable-or-class-nam – Frederic Close Jul 25 '14 at 16:48
  • Saying Clean Code is a bad book is just non-sense. This is the book every serious Java developer should have read. It is even required for a job application here at some places. – Vaelyr Jul 25 '14 at 18:39
  • @markspace: i think this is a very minor violation as coding guideline violations go, since it is contained; it seems unreasonable to judge this book based solely on this. I've read most of it, it's not my favorite Bob Martin book but it contains some good advice. – Nathan Hughes Jul 28 '14 at 14:18

1 Answers1

5

Python uses a leading underscore to say that the method is internal, and not part of any contract with the outside world. It seems like Uncle Bob is doing something similar, except here of course there's no tool support. Over time he has shifted his focus from writing for an audience familiar with C++ to writing for those familiar with scripting languages; he's probably expecting his readers have enough familiarity with Python or Ruby to recognize this sort of thing. So he is using a convention, just not a Java one.

Here Uncle Bob is putting underscores on the instance methods of the factory that he introduces. It seems like he's not intending that those methods be exposed (they are visible only to subclasses and to classes in the same package), subclasses of his factory will have to implement them but code outside of the package will not see them. He also wants to use the same names for the factory methods as he uses for the public static methods, but he needs a convention to keep them straight. I think he's trying to minimize the potential for confusing the instance methods of the internal factory with the exposed static methods, without introducing a separate interface for the factory.

Community
  • 1
  • 1
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276