3.5 KiB
3.5 KiB
Coding Standards
These rules apply to every file in this repo , for humans and agents alike.
General rules
- Follow standard conventions.
- Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible.
- Boy scout rule. Leave the campground cleaner than you found it.
- Always find root cause. Always look for the root cause of a problem.
Design rules
- Keep configurable data at high levels.
- Prefer polymorphism to if/else or switch/case.
- Separate multi-threading code.
- Prevent over-configurability.
- Use dependency injection.
- Follow Law of Demeter. A class should know only its direct dependencies.
Understandability tips
- Be consistent. If you do something a certain way, do all similar things in the same way.
- Use explanatory variables.
- Encapsulate boundary conditions. Boundary conditions are hard to keep track of. Put the processing for them in one place.
- Prefer dedicated value objects to primitive types.
- Avoid logical dependency. Don't write methods which work correctly only depending on something else in the same class.
- Avoid negative conditionals.
Names rules
- Choose descriptive and unambiguous names.
- Make meaningful distinctions.
- Use pronounceable names.
- Use searchable names.
- Replace magic numbers with named constants.
- Avoid encodings. Don't append prefixes or type information.
Functions rules
- Small.
- Do one thing.
- Use descriptive names.
- Prefer fewer arguments.
- Have no side effects.
- Don't use flag arguments. Split the method into several independent methods that can be called from the client without the flag.
Comments rules
- No comments in the codebase. This covers Go, TypeScript/Vue, HTML templates and config files (YAML, TOML,
.env.example,.gitattributes, …). - Explain yourself in code instead: descriptive names, small functions, named constants, explanatory variables.
- The only exceptions are tool directives the code needs to work, such as
//go:buildand//go:embed. - Don't comment out code; just remove it.
- Prose belongs in
docs/andREADME.md, not in source files.
Source code structure
- Separate concepts vertically.
- Related code should appear vertically dense.
- Declare variables close to their usage.
- Dependent functions should be close.
- Similar functions should be close.
- Place functions in the downward direction (caller above callee).
- Keep lines short.
- Don't use horizontal alignment.
- Use white space to associate related things and disassociate weakly related ones.
- Don't break indentation.
Objects and data structures
- Hide internal structure.
- Prefer data structures when there's no behavior to attach.
- Avoid hybrid structures (half object, half data).
- Keep classes small.
- Each class should do one thing.
- Keep the number of instance variables small.
- Base classes should know nothing about their derivatives.
- Prefer many small functions over one function with a parameter that selects behavior.
- Prefer non-static methods to static methods.
Tests
- One assert per test.
- Readable.
- Fast.
- Independent.
- Repeatable.
Code smells to watch for
- Rigidity — the software is difficult to change; a small change causes a cascade of subsequent changes.
- Fragility — the software breaks in many places due to a single change.
- Immobility — you cannot reuse parts of the code in other projects because of the risk and effort involved.
- Needless complexity.
- Needless repetition.
- Opacity — the code is hard to understand.