Course contentsShow
← Java
Lesson 728 of 2,87017. Access Control in DepthFree lesson

Code Review Checklist: Auditing Access Modifiers

Get a practical checklist for reviewing access control decisions to prevent accidental over-exposure of internals.

Code Review Checklist: Auditing Access Modifiers

What You'll Learn

You'll get a practical checklist to systematically review access control decisions in code, preventing accidental over-exposure of class internals.

Why a Checklist Matters

Even experienced developers can accidentally make fields or methods too accessible. A structured checklist ensures you catch these issues before they become part of your public API—changes you can't easily undo without breaking client code.

Think of it like a pre-flight checklist for pilots: simple, systematic questions that prevent avoidable mistakes.

The Access Control Review Checklist

When reviewing code (yours or others'), ask:

For Each Field

  • Is this field private? (Default assumption: yes)
  • If not private, why? Document the reason.
  • Could this be final to prevent modification?

For Each Method

  • What's the minimum access needed? Start with private, widen only if necessary.
  • Is this method part of the class's contract, or an implementation detail?
  • If protected: Are you certain subclasses need this? Could package-private work instead?

For Each Class

  • Is this class public by necessity? Most classes should be package-private.
  • If public, does it represent a stable interface you're willing to maintain?
  • Are all constructors as restrictive as possible?

Cross-Cutting Questions

  • Can any public or protected member be narrowed without breaking functionality?
  • Are collections returned directly, or through defensive copies?
  • Does any protected member leak mutable internal state?

In Practice

Review access modifiers after initial implementation when functionality is clear, and before code review meetings. This catches over-exposure early while requirements are fresh in mind.

Key Takeaway: A systematic access modifier audit prevents accidental API expansion—always start restrictive and widen only when requirements explicitly demand it, documenting the reason for each exception to the private-by-default rule.