Feeds

First among equals

The contract metaphor is an effective way of approaching API design

Secure remote control for conventional and virtual desktops

This is about the simplest, contract-satisfying implementation you can imagine. A contract overspecified to require the result to -1, 0 or +1 would not be a great benefit to the caller but it would be a burden to the implementer:

public final class Date implements Comparable
{
    ...
    public int compareTo(Object other)
    {
        return sgn(day - ((Date) other).day);
    }
    private static int sgn(int value)
    {
        return value < 0 ? -1 :
               value > 0 ? +1 : 0;
    }
    private int day;
}

Alternatively, the implementer could write out the logic longhand and inline within the compareTo method.

The same justification of the less strict contract also applies to field-based representations [7]. Instead of comparing field by field, treat the date as an ordered but discontinuous number range and subtract the right-hand side from the left-hand side. For example, the dates 6th April 2002 and 21st February 1998 can be treated as decimal numbers, 20020406 and 19980221, which are easily obtained by multiplying up and adding the respective year, month and day fields. To discover the relative ordering of 6th April 2002 and 21st February 1998 one simply has to subtract the second figure from the first, which yields a positive number and, therefore, the conclusion that 6th April 2002 naturally orders after 21st February 1998.

Most programmers attempting this task get caught up in a logic and control flow thicket that, as a by-product, happens to return precisely -1, 0 or +1. Neither the code expressing the logic nor the precision of the result is necessary. Part of the art of decision making is knowing which decisions need not be taken: letting arithmetic take the place of control flow sometimes offers such a route; having a contract that is no stricter than is necessary makes that path an easier one to follow. ®

This article originally appeared in Application Development Advisor.

References / bibliography

  1. Kevlin Henney, "Sorted", Application Development Advisor, July 2003
  2. Bertrand Meyer, Object-Oriented Software Construction, 2nd edition, Prentice Hall, 1997
  3. Kevlin Henney, No Memory for Contracts, Application Development Advisor, September 2004
  4. Kevlin Henney, "Objects of Value", Application Development Advisor, November 2003
  5. Kevlin Henney, Conventional and Reasonable, Application Development Advisor, May 2004
  6. Barbara Liskov, Data Abstraction and Hierarchy, OOPSLA '87 Addendum to the Proceedings, October 1987
  7. Kevlin Henney, The Taxation of Representation, artima.com, July 2003

Kevlin Henney is an independent software development consultant and trainer. He can be reached at http://www.curbralan.com

Top 5 reasons to deploy VMware with Tegile

More from The Register

next story
New 'Cosmos' browser surfs the net by TXT alone
No data plan? No WiFi? No worries ... except sluggish download speed
iOS 8 release: WebGL now runs everywhere. Hurrah for 3D graphics!
HTML 5's pretty neat ... when your browser supports it
Mathematica hits the Web
Wolfram embraces the cloud, promies private cloud cut of its number-cruncher
Mozilla shutters Labs, tells nobody it's been dead for five months
Staffer's blog reveals all as projects languish on GitHub
'People have forgotten just how late the first iPhone arrived ...'
Plus: 'Google's IDEALISM is an injudicious justification for inappropriate biz practices'
SUSE Linux owner Attachmate gobbled by Micro Focus for $2.3bn
Merger will lead to mainframe and COBOL powerhouse
iOS 8 Healthkit gets a bug SO Apple KILLS it. That's real healthcare!
Not fit for purpose on day of launch, says Cupertino
prev story

Whitepapers

Secure remote control for conventional and virtual desktops
Balancing user privacy and privileged access, in accordance with compliance frameworks and legislation. Evaluating any potential remote control choice.
Intelligent flash storage arrays
Tegile Intelligent Storage Arrays with IntelliFlash helps IT boost storage utilization and effciency while delivering unmatched storage savings and performance.
WIN a very cool portable ZX Spectrum
Win a one-off portable Spectrum built by legendary hardware hacker Ben Heck
High Performance for All
While HPC is not new, it has traditionally been seen as a specialist area – is it now geared up to meet more mainstream requirements?
Beginner's guide to SSL certificates
De-mystify the technology involved and give you the information you need to make the best decision when considering your online security options.