Computer Science I / Modular Decomposition and Reuse
Practice question · Sort into groups

A date-handling module is being designed. Sort each item by whether it should be public or private.

Groups: Public interface · Private internals

Hints
  1. Ask of each item: does another module need it to do its own job, or is it only machinery?
  2. Anything a caller could break by touching, or that could be rewritten tomorrow, should be private.
Show the answer

Public interface: A function that formats a date as a string, A function that reports whether a year is a leap year, A function that adds a number of days to a date

Private internals: The lookup array of month lengths used internally, A helper that converts a date into a day count since 1970, The scratch buffer the formatter reuses between calls

Why

The public interface should be small and stated in terms of dates, not of the machinery that manipulates them: format, leap-year test and date arithmetic. Month-length tables, internal day counts and scratch buffers exist only to make those work, and publishing them would let callers couple to details you wanted to be free to change.

Read the lesson: Modular Decomposition and Reuse →

Practise Modular Decomposition and Reuse

The app has 6 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.

More questions on Modular Decomposition and Reuse