28. Code conventions

Level: intermediate · Reading time: 21 min · Prerequisite: book/chapters/27-grammar.html · Track: supplemental · Maturity: draft · Last review: 2026-05-09

TL;DR (5 lines)

  • Design advice must change code, not just prose.
  • Naming and conventions are technical levers.
  • Concrete before/after examples teach better than slogans.
  • Maintainability is visible in small code decisions.
  • The chapter should leave a reusable review rule behind.

Frequent mistakes

  • Giving advice with no concrete code delta.
  • Using generic slogans instead of stable review criteria.
  • Forgetting to connect naming or convention to future maintenance cost.

Prerequisites: book/chapters/27-grammar.html. See also: book/chapters/27-grammar.html, book/chapters/27-grammar.html, book/chapters/31-build-errors.html.

Concrete Problem

Design and convention chapters often become prose-only advice with no proof that the rule changes code quality.

Red Thread (Single Project)

One module is revised from a weak naming or boundary choice into a stronger, more maintainable form.

For what

This chapter helps the reader connect design advice to concrete changes in code review, maintenance, and migration.

Work in this chapter

You will inspect one before/after style or architecture decision, then identify the contract it improves.

Coherent example

space demo/design

form BuildConfig {
  project_name: string
  retry_limit: int
}

proc validate_config(cfg: BuildConfig) -> int {
  if cfg.project_name == "" { give 11 }
  if cfg.retry_limit < 0 { give 12 }
  give 0
}

export *

Complete examples

Each block below is a complete reading unit with its own boundary, data shape, and observable result.

Primary coherent example

This is the compact chapter anchor used by the surrounding explanation.

space demo/design

form BuildConfig {
  project_name: string
  retry_limit: int
}

proc validate_config(cfg: BuildConfig) -> int {
  if cfg.project_name == "" { give 11 }
  if cfg.retry_limit < 0 { give 12 }
  give 0
}

export *

Named configuration contract

This example shows maintainability through explicit domain names.

space examples/design/config

form BuildConfig {
  project_name: string
  max_retries: int
}

proc validate_config(cfg: BuildConfig) -> int {
  if cfg.project_name == "" { give 11 }
  if cfg.max_retries < 0 { give 12 }
  give 0
}

proc main(args: list[string]) -> int {
  let cfg: BuildConfig = BuildConfig { project_name: "demo", max_retries: 2 }
  give validate_config(cfg)
}

export *

Compatibility-preserving wrapper

This variant keeps a stable public name while improving the internal path.

space examples/design/compat

proc validate_name(name: string) -> int {
  if name == "" { give 11 }
  give 0
}

proc validate_project(name: string) -> int {
  give validate_name(name)
}

proc main(args: list[string]) -> int {
  give validate_project("demo")
}

export *

Immediate work for this chapter

  • Turn naming advice into a before and after code contract.
  • Tie conventions to review cost and compatibility risk.
  • Keep the public API promise visible in the example.
  • Keep the first code block complete and aligned with current Vitte docs syntax.
  • Keep the invalid block focused on one broken contract only.
  • Replace generic prose with one concrete rule visible in the code.
  • Keep every example small enough to review from top to bottom.

Chapter override: 28-conventions.html

Dedicated problem

28. Code conventions needs a concrete production-grade anchor around name, compatibility promise, and review expectation. The page should keep the examples, diagnostics, and review rules tied to that exact boundary instead of drifting back to generic tutorial prose.

Specific complete examples

28. Code conventions chapter anchor

The primary example is promoted here as the first chapter-specific production reading unit.

space demo/design

form BuildConfig {
  project_name: string
  retry_limit: int
}

proc validate_config(cfg: BuildConfig) -> int {
  if cfg.project_name == "" { give 11 }
  if cfg.retry_limit < 0 { give 12 }
  give 0
}

export *

Named configuration contract

This example shows maintainability through explicit domain names.

space examples/design/config

form BuildConfig {
  project_name: string
  max_retries: int
}

proc validate_config(cfg: BuildConfig) -> int {
  if cfg.project_name == "" { give 11 }
  if cfg.max_retries < 0 { give 12 }
  give 0
}

proc main(args: list[string]) -> int {
  let cfg: BuildConfig = BuildConfig { project_name: "demo", max_retries: 2 }
  give validate_config(cfg)
}

export *

Compatibility-preserving wrapper

This variant keeps a stable public name while improving the internal path.

space examples/design/compat

proc validate_name(name: string) -> int {
  if name == "" { give 11 }
  give 0
}

proc validate_project(name: string) -> int {
  give validate_name(name)
}

proc main(args: list[string]) -> int {
  give validate_project("demo")
}

export *

Risks and diagnostics

RiskDiagnostic signalAction
Boundary driftThe chapter loses sight of name, compatibility promise, and review expectation.Restate the boundary beside the first code block and every invalid case.
Generic proseA paragraph would still be true in another chapter.Replace it with a code-specific rule from this page.
Weak diagnosticThe failure does not point back to the chapter contract.Reduce the invalid case until one failure explains the rule.

Review checklist

  • The first example is complete and aligned with Vitte docs syntax.
  • The production section names where this construct belongs in real code.
  • The risk table connects each failure to a diagnostic or review action.
  • The avoid list rejects broad misuse without adding quiz-like prompts.

Production use

  • Use this chapter when a code review needs to preserve name, compatibility promise, and review expectation.
  • Keep examples small enough to copy into fixtures or docs smoke tests.
  • Treat the invalid case as regression material for future docs checks.

What to avoid

  • Do not add a second topic that hides the chapter's main contract.
  • Do not expand examples by adding unrelated subsystems.
  • Do not rely on prose when a small Vitte block can show the rule.

Global explanation

Design pages need code because design rules only matter when they improve a concrete contract. Naming, conventions, compatibility boundaries, and review habits are technical because they change how easily the code can be read and evolved.

Invalid case

form Cfg {
  x: string
  y: int
}

This invalid case is intentionally small. It exists to isolate the contract failure that the chapter is trying to teach.

Common pitfalls

  • Giving advice with no concrete code delta.
  • Using generic slogans instead of stable review criteria.
  • Forgetting to connect naming or convention to future maintenance cost.

Short exercise

Rename one vague field or procedure in the example and explain what ambiguity the new name removes.

Summary in 5 points

  1. Design advice must change code, not just prose.
  2. Naming and conventions are technical levers.
  3. Concrete before/after examples teach better than slogans.
  4. Maintainability is visible in small code decisions.
  5. The chapter should leave a reusable review rule behind.

See also

Next best action

Extend the coherent example by one small, justified step and keep the same contract visible from input to output.

Chapter deep dive

28. Code conventions turns conventions into concrete maintainability rules. The chapter is written for a reader making code easier to evolve.

The practical boundary is: name, compatibility promise, and review expectation. Keep that boundary in view while reading the example, the invalid case, and the exercise.

Role in the learning path

Design and convention chapters often become prose-only advice with no proof that the rule changes code quality.

One module is revised from a weak naming or boundary choice into a stronger, more maintainable form.

This chapter helps the reader connect design advice to concrete changes in code review, maintenance, and migration.

Profile-specific deep dive

Maintainability rule

  • A naming or convention rule must improve a concrete code contract.
  • Before and after examples should show reduced ambiguity.
  • Compatibility promises belong in public names and result shapes.

Review value

  • A reviewer should be able to point to the exact improved line.
  • Avoid slogans that do not change code.
  • Keep migration cost visible when changing public surfaces.

Reading the valid example

  1. space demo/design: names the ownership boundary before any behavior appears.
  2. form BuildConfig {: introduces a data contract that later branches can rely on.
  3. project_name: string: supports the chapter contract without adding hidden behavior.
  4. retry_limit: int: supports the chapter contract without adding hidden behavior.
  5. }: supports the chapter contract without adding hidden behavior.
  6. proc validate_config(cfg: BuildConfig) -> int {: states the callable contract: inputs first, result shape last.
  7. if cfg.project_name == "" { give 11 }: guards a failure or edge case before the nominal result.
  8. if cfg.retry_limit < 0 { give 12 }: guards a failure or edge case before the nominal result.
  9. give 0: ends the local path with an explicit result.
  10. }: supports the chapter contract without adding hidden behavior.
  11. export *: supports the chapter contract without adding hidden behavior.

Lesson from the invalid example

  1. form Cfg {: this line helps isolate the failure because it introduces a data contract that later branches can rely on.
  2. x: string: this line helps isolate the failure because it supports the chapter contract without adding hidden behavior.
  3. y: int: this line helps isolate the failure because it supports the chapter contract without adding hidden behavior.
  4. }: this line helps isolate the failure because it supports the chapter contract without adding hidden behavior.

Engineering decisions to preserve

  • Name the boundary before changing code: Design advice must change code, not just prose.
  • Keep the smallest example executable: Naming and conventions are technical levers.
  • Make the invalid path explain one failure only: Concrete before/after examples teach better than slogans.
  • Prefer a visible contract over an implied convention: Maintainability is visible in small code decisions.
  • Leave a review anchor that another maintainer can verify: The chapter should leave a reusable review rule behind.

Context-specific review criteria

  • The page makes the name, compatibility promise, and review expectation boundary visible before the first code block.
  • The intended reader, a reader making code easier to evolve, can follow the valid example through named contracts instead of memorized tokens.
  • The invalid example fails for the same reason the prose discusses.
  • The exercise extends the same contract instead of introducing an unrelated concept.
  • The next chapter can reuse the vocabulary introduced here without redefining it.
  • The chapter stays specific enough that its title materially changes the meaning of the page.
  • Every warning connects to a concrete code shape.
  • The summary leaves one durable engineering rule behind.

Contract matrix

ConcernChapter ruleEvidence to keep
OwnershipCode belongs behind the boundary named by the chapter.The chapter keeps ownership visible through name, compatibility promise, and review expectation.
Input contractThe procedure receives a shape that is named before branching.The valid example names the accepted shape before branching.
Nominal pathThe clean path remains readable without hidden state.The successful result can be found without reading hidden state.
Failure pathThe invalid case isolates one failure reason.The broken example has one main reason to fail.
NamingNames explain the domain rather than only the mechanism.Names remain tied to the chapter goal.
TypesTypes remove ambiguity from values and results.Fields and return values carry domain meaning.
Control flowBranches stay traceable from guard to result.Guards appear before the result they protect.
Module boundaryThe public surface stays smaller than implementation detail.The public surface remains smaller than the implementation detail.
Diagnostic valueThe failure path points back to the exact contract.The invalid example points back to the exact contract.
Test valueRegression evidence covers one passing path and one failing path.One passing case and one failing case cover the lesson.
Refactor valueImplementation cleanup preserves the result shape.The result shape stays stable during local cleanup.
Publication valueThe chapter leaves one concrete engineering rule.The chapter leaves one concrete engineering rule.

Rewrite path for this chapter

  1. Rewrite the opening paragraph so it names name, compatibility promise, and review expectation before naming syntax.
  2. Keep the valid example small enough that the full contract fits on screen.
  3. Move any broad claim back to a specific line in the example.
  4. Preserve one invalid case that fails for the chapter's main reason.
  5. Add one sentence explaining why the invalid case is not a random error.
  6. Make every pitfall actionable by naming the code shape it damages.
  7. Keep the exercise inside the same domain as the example.
  8. Avoid introducing a second unrelated project just to show variety.
  9. Use the summary to restate the chapter rule, not the table of contents.
  10. Check that the next chapter can build on this vocabulary.
  11. Remove any sentence that would still be true in every other chapter.
  12. Keep the last action small, local, and testable.

Diagnostic anchors

  • The first inspected line is the one that declares the chapter's main contract.
  • The central type, field, procedure, or branch carries the chapter's main idea.
  • The invalid example includes a sentence-level explanation of its failure.
  • Refactors preserve the detail that would otherwise mislead a future reader.
  • The behavior that must stay stable is named before implementation changes begin.
  • Vague names are replaced before they become review friction.
  • Regression coverage protects the chapter's main contract.
  • Implementation details stay out of public API unless the chapter explicitly teaches that surface.
  • Beginner-facing diagnostics point to the contract, not to a random syntax detail.
  • The next chapter can assume one clearly named concept from this page.

When extending this chapter

  • Extend toward a reader making code easier to evolve, not toward a broader catalog of features.
  • Add a second example only if it sharpens the same contract.
  • Prefer a small variant over a new subsystem.
  • Keep prose close to code; every abstract claim should point to a visible shape.
  • Do not hide a new concept in the exercise.
  • If a paragraph explains policy, add the concrete code boundary it protects.
  • If a paragraph explains syntax, add the semantic reason the syntax matters.
  • If a paragraph explains architecture, identify the owner of each boundary.
  • If a paragraph explains failure, keep the failing line close to the explanation.
  • Stop expanding when the chapter has one complete, testable lesson.

Failure modes to avoid

  • Giving advice with no concrete code delta.
  • Using generic slogans instead of stable review criteria.
  • Forgetting to connect naming or convention to future maintenance cost.

Practice scenario

Start from the coherent example in 28. Code conventions. Change one identifier, one guard, and one returned value. After each change, write down whether the public contract is still the same contract or a new one.

If the contract changed, update the type or result shape first. If only the implementation changed, keep the external name stable and add one regression note explaining what should not change again.

Before moving on

  • You can state the chapter role: turns conventions into concrete maintainability rules.
  • You can point to the main boundary: name, compatibility promise, and review expectation.
  • You can connect the invalid case to the problem statement: Design and convention chapters often become prose-only advice with no proof that the rule changes code quality.
  • You can perform the exercise: Rename one vague field or procedure in the example and explain what ambiguity the new name removes.