Saturday, June 22, 2024

Code Smell 246 – Expiration Date

Programming LanguageCode Smell 246 - Expiration Date


You need to model an expiration date. Which object will you use?

TL;DR: Model real word expiration dates with… expiration dates

  1. Honor the bijection and model correct behavior

In many systems, the expiry date of a credit card is often represented by simply using a Date object.

This can lead to potential issues and misunderstandings, especially when dealing with operations such as comparisons and calculations based on the expiry date.

it’s generally considered a better practice to represent the expiry date with an adequate object.

Wrong

import java.util.Date;

public class CreditCard {
    private String cardNumber;
    private Date expiryDate;

    public CreditCard(String cardNumber, Date expiryDate) {
        // Not a complete date
        this.cardNumber = cardNumber;
        this.expiryDate = expiryDate;
    }

    public boolean isExpired() {
        Date currentDate = new Date();
        return expiryDate.before(currentDate);
        // How will it work? 
        // using the last day of the month?
    }
}
Enter fullscreen mode

Exit fullscreen mode

public class CreditCard {
    private String cardNumber;
    private int expiryMonth;
    private int expiryYear;

    public CreditCard(String cardNumber, int expiryMonth, int expiryYear) {
        this.cardNumber = cardNumber;
        this.expiryMonth = expiryMonth;
        this.expiryYear = expiryYear;
        // No validations on number ranges?
    }

    public boolean isExpired(int currentMonth, int currentYear) {
        return (expiryYear < currentYear) ||
            (expiryYear == currentYear && expiryMonth < currentMonth);
    }
    // Inappropriate intimacy code smell

}
Enter fullscreen mode

Exit fullscreen mode

Right

class CreditCard {
  private String number;
  private MonthOfYear expiryDate;
  // expiryDate is the role
  // MonthOfYear is the type
}

class MonthOfYear {
  private Month month;  
  private Year year;
  // These are other small objects

  public MonthOfYear(Month month, Year year) {
    // You don't need to add validations since 
    // month is a valid month
    // year is a valid year
    this.month = month;
    this.year = year;
  } 

 public boolean isBeforeEndOfMonth(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar.get(Calendar.DAY_OF_MONTH) <= getDaysInMonth();
  }

  // This protocol is just for MonthOfYears  
  public Day[] getDaysInMonth() { }

  public boolean isLeapYear() { } // ...

  public void iterateDays() { } // ...

}
Enter fullscreen mode

Exit fullscreen mode

[X] Manual

This is a design smell

[x] Intermediate

This is not an obvious design decision

Actual AI assistants are not very good at creating these kinds of small objects

Gemini detected this is a possible smell

ExpiryDate explicitly separates month and year, making the code more readable and easier to understand the specific information needed for expiry.

It can also encapsulate logic specific to expiry dates, such as calculating days remaining, validating expiry, or iterating through days in the month.

While Date objects offer date manipulation functionalities, they don’t inherently represent the specific concept of a credit card expiry.

Finding small objects with concrete behavior is always a difficult task.

Code Smells are my opinion.

Photo by CardMapr.nl on Unsplash

Thank you, Hernan Wilkinson for this tip


Standards are always out of date. That’s what makes them standards.

Alan Bennett


This article is part of the CodeSmell Series.

Check out our other content

Check out other tags:

Most Popular Articles