Skip to content

Policy: initialize every non-static field in the constructor - #360

Open
dfa1 wants to merge 1 commit into
mainfrom
field-init-policy
Open

dfa1 wants to merge 1 commit into
mainfrom
field-init-policy

Conversation

@dfa1

@dfa1 dfa1 commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Summary

  • New rule in AGENTS.md (mirrored in CONTRIBUTING.md): every non-static field is initialized in the constructor, never at its declaration or in an instance initializer; static final constants are unaffected, and a field with no meaningful value yet is left unassigned. An object's whole state and build order then read in one place, and a constructor that must call something last (childOf(...)) can see every field is assigned.
  • Applies it everywhere: NativeObject, NativeObjectChild, NativeDB, PerfContext, PlainTableOptions, ReadOptions, RocksIterator, UpcallRegistry (new package-private constructor), JfrEventNotifier, SstDumpTool.Request, the anonymous iterators in LiveFiles/LiveFilesStorageInfo (int i keeps its default), CloseFromAnotherThreadTest and the three JMH benchmark classes.
  • No checkstyle rule: the built-in ExplicitInitialization only catches default values, so this stays a convention for now.

Test plan

  • ./mvnw test (all modules)
  • CI on all platforms

🤖 Generated with Claude Code

AGENTS.md and CONTRIBUTING.md state the rule; the remaining inline field initializers in
core, tools, tests and the JMH benchmarks move into constructors.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant