A logbook for curious waters
← Software Engineering Field Guide

Hide Method

Before you begin: Public and private methods, classes, and callers.

Hide Method reduces a method’s visibility when it is an implementation detail. A smaller public interface gives callers fewer accidental dependencies and gives the implementation more freedom to change. 1

Original version: an internal formatting step is public

public sealed class Receipt
{
    public string Prefix() { return "Total: "; }
    public string Render(int total) { return Prefix() + total; }
}

Callers can ask for the prefix independently even though the intended service is rendering a receipt. They might begin relying on a formatting detail the class never needed to expose.

class Receipt:
    def prefix(self):
        return "Total: "

    def render(self, total):
        return self.prefix() + str(total)

The method name looks like an ordinary public operation. Nothing distinguishes it as an internal part of rendering.

Refactored version: expose rendering, keep the prefix internal

public sealed class Receipt
{
    private string Prefix() { return "Total: "; }
    public string Render(int total) { return Prefix() + total; }
}

Render still returns Total: 12 for 12. Outside source code can no longer call Prefix directly.

class Receipt:
    def _prefix(self):
        return "Total: "

    def render(self, total):
        return self._prefix() + str(total)

The underscore signals that _prefix is not part of the supported public interface. Python does not enforce that convention; callers can still invoke it deliberately.

Check consumers before narrowing access

Search direct callers, subclasses, reflection, templates, and framework conventions. A method that appears unused may be invoked by name at runtime. For a published library, reducing visibility can be a breaking change even if internal tests pass.

If external callers need a legitimate operation, offer that operation rather than simply hiding everything they currently use. Conversely, tests reaching into every private helper may be over-coupled to implementation details; verify externally meaningful results where possible.

For a class this small, the helper could also be inlined. Hiding and inlining solve different problems: hiding changes what clients may depend on, while inlining removes an internal level of indirection.

Sources

  1. Martin Fowler — Refactoring catalog, for the first-edition Hide Method vocabulary.
  2. Microsoft — Access modifiers and Python — Public and internal interfaces.