Over a million developers have joined DZone.

Why Care About Design Principles?

DZone's Guide to

Why Care About Design Principles?

We can all understand design principles fairly well, but applying them in practice is the key to truly great code.

· Java Zone ·
Free Resource

Delivering modern software? Atomist automates your software delivery experience.

“The computer industry is the only industry that is more fashion-driven than women’s fashion”

— Larry J. Ellison, Oracle

Technology is changing more rapidly than ever before – with old buzzwords getting quickly replaced by new ones (do you still remember buzz words like “grid computing” and “semantic web”?). Recently I came across what Craig Larman (respected for his insightful work on Lean and Agile development and OO design) said on software development: “the critical design tool for software development is a mind well educated in design principles”. This quote certainly resonated with me.

Of course, as developers we know that design principles and practices are key for excellent software design. But the challenge we face is not in understanding design principles but in applying them in practice. Fortunately, there is an effective way to learn to how to apply them in practice: by observing how other developers applied them effectively! Let us see an example.

Have you used Date, Calendar, and other related classes in java.util package? I have used them early in my career and found these classes to be painful to work with. Here is an example. I was trying to compare two Date objects and found an unusual result:

Date today = new Date();
Date todayAgain = new Date();
System.out.println(today.compareTo(todayAgain) == 0);

And it printed:
Thu Mar 17 13:21:55 IST 2016
Thu Mar 17 13:21:55 IST 2016

As you can see the date and time values printed in the console are same but the compareTo() method returns false. Why? Because the Date objects differ by a few milliseconds! The toString() method does not print the millisecond values but the compareTo() considers milliseconds – that is unintuitive. It appears to be an implementation problem but when I dug deeper I found it to be a design problem. How? The Date class has time values associated with it; in Java 7 and earlier there is no way to deal with only date related values. According to Interface Segregation Principle (ISP): “no client should be forced to depend on methods it does not use”. Hence, the underlying design in Java 7 for date and time support clearly violates ISP.

The figure given above on java.util.Date and its derived types java.sql.Date and java.sql.Time illustrates another design problem. The java.sql.Date type supports only date related methods and rejects time-related methods. Similarly, the java.sql.Time supports only time-related methods and rejects date related methods. When derived classes reject methods they inherit from its base class, it is known as “Rebellious Hierarchy” (more common known as “refused bequest”) smell. Hence, the underlying design violates Liskov's Substitution Principle (LSP). Suggested refactoring would be to use delegation instead of inheritance.

Refused Bequest smell in java.util.Date hierarchy

These are just two examples of smells in classes related to date and time in Java 7 and earlier. When faced difficulties in using date and time-related functionality in the Java library, Stephen Colebourne created a library of his own. He later released it to public as Joda API. The Java 8 package java.time is largely derived from the Joda API. Let us revisit the problem of comparing two dates in java.time package:

LocalDate today = LocalDate.now();
LocalDate todayAgain = LocalDate.now();
System.out.println(today.compareTo(todayAgain) == 0);

It correctly prints: 2016-03-17

There are separate classes for dealing with date, time and time zones. We can combine date, time and time zone functionality as needed. Analyzing the design of java.util’s date and time-related classes and classes in java.time package gives us many insights on software design. For instance, the java.time package uses “fluent interfaces” – this design approach makes the client code more readable and dealing with date and time functionality more pleasant to developers.

In general, awareness of design smells – indicators of common design problems – helps us understand mistakes made while designing and apply design principles for creating high-quality designs. Looking at how other architects and designers apply design principles in practice can help us become effective designers.

Start automating your delivery right there on your own laptop, today! Get the open source Atomist Software Delivery Machine.

refactoring ,java ,java 8 ,java libraries ,design principles ,software design ,software architecture ,design antipatterns

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}