Return to site
📝🧭 CODE COMMENTS THAT SCALE: HOW TO USE JAVADOC, BLOCK, AND LINE COMMENTS
📝🧭 CODE COMMENTS THAT SCALE: HOW TO USE JAVADOC, BLOCK, AND LINE COMMENTS
·

🔸 TL;DR
- ▪️ Javadoc → document contracts (what/why, params, returns, exceptions, invariants).
- ▪️ Block comments (/* ... */) → provide context (design intent, trade-offs, references).
- ▪️ Line comments (//) → clarify weirdness (non-obvious code, edge cases, hacks with reasons).
🔸 WHY THIS MATTERS
- ▪️ Comments aren’t decoration—they’re interfaces for humans.
- ▪️ Good comments reduce onboarding time, prevent regressions, and make code reviews faster.
- ▪️ Choose the right kind of comment for the job to keep noise low and signal high. ✨
🔸 COMMENT TAXONOMY (RECOMMENDED PRACTICE)
▪️ Javadoc = Contract
- Use on public types/methods (and important internal APIs).
- Describe purpose, inputs/outputs, pre/postconditions, error cases, thread-safety, performance notes.
- Keep it stable and testable—if a test can assert it, it belongs here.
▪️ Block Comment = Context
- Use to explain design decisions: why this algorithm, trade-offs, links to tickets/specs/JEPs.
- Summarize alternatives considered and why rejected.
- Great for modules, classes, or complex code paths.
▪️ Line Comment = Weirdness
- Use sparingly to justify non-obvious code: workarounds, edge-case guards, perf tricks.
- Be specific: what’s weird + why + source (bug ID, API quirk, platform note).
- Remove when the code becomes obvious again.
🔸 TINY EXAMPLES
🔸 TEAM CONVENTIONS THAT HELP
- ▪️ Write a short “Comment Style Guide” in your repo’s README/CONTRIBUTING.
- ▪️ Enforce Javadoc on public APIs via build checks (e.g., Maven/Gradle plugins).
- ▪️ In PRs, ask “Contract? Context? Weirdness?” when a comment is added.
- ▪️ Prefer links to sources (tickets/benchmarks/specs) over long narratives.
🔸 COMMON PITFALLS TO AVOID
- ▪️ Narrating code (“add 1 to i”) — the code already says that.
- ▪️ Stale comments—update or delete when behavior changes.
- ▪️ Hiding bad design behind comments—fix the code first, then comment what remains non-obvious.
- ▪️ Mixing types—don’t put long context in line comments or contracts in block comments.
🔸 TAKEAWAYS
- ▪️ Treat comments as API for humans: precise, minimal, and valuable.
- ▪️ Match the comment type to the intent (Contract / Context / Weirdness).
- ▪️ Keep comments living documents: reviewed, tested, and maintained. ✅
🔸 YOUR TURN
- ▪️ Add a Comment Style Guide to your repo today.
- ▪️ Pick one module and upgrade comments using this taxonomy.
- ▪️ In your next PR, label each new comment with Contract / Context / Weirdness.
Go further with Java certification:
Java👇
Spring👇