Idioms, Style and Best Practices
48.1 The D Style Guide (Highlights)
4-space indentation, Allman braces (Phobos style), camelCase functions and variables, PascalCase types, lowercase modules.
Use dfmt for formatting and dscanner --styleCheck for linting.
48.2 Idiomatic D
✅ Use ranges and UFCS pipelines for data processing.
✅ Use auto for complex range types. Use const/immutable liberally.
✅ Mark functions @safe, pure, nothrow, @nogc where possible (at least @safe).
✅ Use structs for values and RAII. Use classes for polymorphic identity objects.
✅ Use scope(exit) instead of try/finally chains.
✅ Write unittest blocks next to the code. Use documented unittests as examples.
✅ Use enforce for runtime input validation and assert/contracts for programmer errors.
✅ Use local and selective imports to reduce compile times and namespace pollution.
✅ Use template constraints to document generic requirements.
✅ Use std.sumtype for variants, Nullable for optional values, Flag for boolean parameters.
✅ Keep @trusted blocks tiny and review them carefully.
❌ Don't catch Error. Don't rely on class destructors for deterministic cleanup.
❌ Don't overuse string mixins when template mixins or plain code suffice.
❌ Avoid __gshared except for C interop. Avoid casting away immutable.
❌ Don't ignore GC allocations in latency-sensitive code.
48.3 Project Hygiene
CI with GitHub Actions (dlang-community/setup-dlang), testing on DMD and LDC, multiple OSes, dub test, coverage (codecov), and dub lint.
Semantic versioning, dub.selections.json committed for applications, docs published (dub build -b ddox or adrdox).