Skip to content

refactor(example): the example module keeps no comment but public scaladoc - #428

Open
rcardin wants to merge 14 commits into
devfrom
refactor/no-comment-example
Open

rcardin wants to merge 14 commits into
devfrom
refactor/no-comment-example

Conversation

@rcardin

@rcardin rcardin commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Why

The maintainer decided that a comment is allowed in one place only: scaladoc on a member of the public API, in main code, and that scaladoc tells a user how to use the member, not its history or the reason for a design. Every other comment is refused, and every comment in a test source too. The reason a comment carried moves to a test or an ADR, or it is deleted, so that the code speaks for itself and no comment can hide a workaround.

This pull request applies the decision to modules/example, one module at a time.

What

Findings of the rule, counted with a syntactic lint rule that is not in the build yet:

Sources Before After
main (src/main) 2 0
test (src/test) 9 0

The lint rule cannot see history or rationale in public scaladoc. The public scaladoc of Cli and Db held some, so it now says only how to use each object.

No behaviour changes. The changes are deletions, one rename (Schema0 is now TourDatabaseSchema), the deletion of AccessProbe, a scaladoc code block for the run commands of Cli, a tour command alias, one new suite in modules/db, and scalafmt on Cli.scala and Db.scala, which were not formatted on dev.

This pull request is wider than modules/example:

  • modules/db/src/test/scala/outside/OneImportFromOutsideEezoSuite.scala is new. It is the home of the guard that AccessProbe claimed to be (see below).
  • build.sbt gets one line, addCommandAlias("tour", "example/Test/runMain example.Tour"), beside the example block. No comment of build.sbt changes.

modules/example has no test framework, and this pull request does not add one. So no reason in it could become a test of the module; each reason that needed one has a home elsewhere or is listed under Candidate issues.

Where each reason went

File Old text (short) Class Where the reason is now
Cli.scala:18 "One source of truth for connection settings: Db already reads these, and Db.withConnection is still how the chapters that demonstrate raw JDBC get a connection." design decision that does not bind the code deleted. The first half restated the code: each override returns the Db value of the same name. The second half was false: nothing calls Db.withConnection.
Cli.scala:5, public scaladoc "This object used to be the CLI ... design/cli.md §3 named it the specification ... That port happened." history deleted. The scaladoc now says to run the db commands against the database that Db names, and how.
Db.scala:5, public scaladoc "Deliberately not part of the db module: every entry point there takes a Connection ..." design decision, stale deleted. It was stale: DbInit.database now builds the pool from databaseUrl. The scaladoc now names the three environment variables and the defaults.
AccessProbe.scala:6, public scaladoc "Kept as a compile time guard on the seam ... the only thing that exercises io.eezo.db.Exports and the inline entry points from outside io.eezo" a rule the code must keep, partly stale the new suite outside.OneImportFromOutsideEezoSuite in modules/db, and AccessProbe is deleted. See "The AccessProbe guard".
Tour.scala:18 "sbt "example/Test/runMain example.Tour" steps through it ... An environment variable rather than an argument ... rather than a -D property" run instructions and a design decision the reason for the environment variable: ADR 0005, section "The Tour reads TOUR_NOPAUSE instead of --no-pause". The run command: the alias sbt tour in build.sbt, which is code. The rest restated what the chapters print.
Tour.scala:32 "The tour starts its own Postgres ... It lives in src/test for the container rather than because it is a test" restated the code, plus a build fact deleted. "Starts its own Postgres" restated the code. The reason for src/test survives only in the comment of the example block in build.sbt, which is not a home; see Candidate issues 4.
Tour.scala:51 "every connection the pool opens creates it if it is missing and sets search_path to it" a rule of the db module the DbAppSuite test "databaseSchema alone puts the tables in that schema, creating it, and status agrees", and docs/how-to/configure-the-database.md.
Tour.scala:84 "── chapters ──" section marker deleted; see Files to split.
Tour.scala:374 "── plumbing ──" section marker deleted; see Files to split.
Tour.scala:376 "The tour works in its own Postgres schema, so it cannot disturb anything else." restated the code deleted. The new name TourDatabaseSchema says which value it is: the one that databaseSchema returns.
Tour.scala:398 "if not exists: the pool fills in the background while this runs, and each new connection creates the schema ... maybe between the drop and here." justifies a workaround deleted; see Workarounds found and Candidate issues 3.
Tour.scala:450 "── output ──" section marker deleted; see Files to split.

The lint rule counts each line of a comment, so the two line comments at Cli.scala:18 and the two at Tour.scala:398 give four findings for two rows.

The AccessProbe guard

The deleted scaladoc said that AccessProbe was "the only thing that exercises io.eezo.db.Exports and the inline entry points from outside io.eezo". An earlier version of this body repeated that claim. It was partly stale:

  • The entry points transact and read are already used from packages outside io.eezo by examples/shop, examples/blog and examples/reminders, and the gate compiles the examples build. So for them the claim was false.
  • The claim was true only for the two aliases Tx and DB: no source outside io.eezo wrote using Tx or using DB except AccessProbe. CapabilitySuite writes using Tx, but in package io.eezo.db, where the alias is in scope without the export.
  • The gate never compiles modules/example (not in the root aggregate, no step in bin/gate or CI). So this guard never ran in CI.

The guard now lives in modules/db/src/test/scala/outside/OneImportFromOutsideEezoSuite.scala, in package outside. It imports io.eezo.db.* and io.eezo.db.Scopes.*, declares one method that takes using Tx and one that takes using DB, and calls them inside transact and read against the suite Postgres, as the other DbSuite suites do. The gate compiles and runs it.

It is a guard: with Exports.scala changed by hand to export io.eezo.db.capability.DB plus private[eezo] type Tx = io.eezo.db.capability.Tx, bin/fast 'db/testOnly outside.OneImportFromOutsideEezoSuite' fails with Not found: type Tx at OneImportFromOutsideEezoSuite.scala:9:30, and with no other error. With Exports.scala restored, it passes, 2 tests.

AccessProbe is deleted: no source, build file or doc names it (grep of *.scala, *.sbt and *.md).

Workarounds found

  1. Tour.prepare drops eezo_tour, then runs create schema if not exists, then set search_path, all on the Tour's own connection from DriverManager.

    • The drop has no use. The container is new on each run, so the drop can only remove a schema that a pool connection made a moment before.
    • The create is necessary. The chapters run on that own connection, not on a pool connection. The DbApp pool starts empty and fills in the background, so when prepare runs no pool connection has to exist yet, and the schema can be missing.
    • if not exists does not settle the race with the pool. When a pool connection and the Tour both find the schema missing, the loser fails on the catalog's unique index. DbInit.enterSchema says so in its scaladoc and catches unique_violation (23505) and duplicate_schema (42P06) for that reason. The Tour's create has no such catch, so the Tour can still fail in prepare.

    The behaviour of the Tour is not changed here. See Candidate issues 3.

Files to split

None. The three markers of Tour.scala divided helpers of one program: the eleven chapters, the database steps they share, and the console output helpers (header, chapter, note, sql, ok, warn, rejected, blank, pause). An earlier version of this body listed the file to split. The independent evaluation of this pull request disagrees, and this body now follows it: the output helpers are about 30 lines, only the Tour uses them, and pause reads the Tour's var paused, so a split must move that state or pass it. They are one program and its private vocabulary, not two different things, and a split adds a file and a package private surface for no reader. If the owner reads the rule as written ("When the marker divided two different things, the file is split"), the split is a follow up that moves statements.

Candidate issues

Each one can be pasted as an issue. None is opened by this pull request.

1. Db.connect and Db.withConnection in modules/example have no caller

  • What: two public methods of the example are dead code.
  • Where: modules/example/src/main/scala/example/Db.scala, connect and withConnection.
  • How to see it: git grep -n "Db\.connect\|withConnection" shows withConnection only at its definition, and no caller of Db.connect; connect() is called only inside withConnection (the Tour has its own private connect). Db.url, Db.user and Db.pass have callers in Cli.scala and Tour.scala.

2. The Tour prints a database URL that it does not connect to

  • What: Tour.prepare prints Db.url, but the Tour connects to its own testcontainers Postgres, whose URL is databaseUrl. The fix is ${databaseUrl} in place of ${Db.url}.
  • Where: modules/example/src/test/scala/example/Tour.scala, prepare, the line note(s"Working in schema ... on ${Db.url}, so nothing else is touched.").
  • How to see it: TOUR_NOPAUSE=1 sbt tour. The output says Working in schema eezo_tour on jdbc:postgresql://localhost:5442/eezo, while the container listens on a random port that testcontainers gives it.

3. Tour.prepare races the DbApp pool when it creates its schema

  • What: prepare drops eezo_tour and runs create schema if not exists on its own connection while the pool fills in the background and each new pool connection creates the same schema. if not exists does not settle that race: the loser fails on the catalog's unique index, and the Tour does not catch it. The drop has no use, because the container is new on each run. One fix: delete the drop and let the pool create the schema before prepare (for example one read { ... } that borrows a pool connection), and keep only set search_path on the Tour's connection. Another: catch the SQL states that DbInit catches (23505, 42P06).
  • Where: modules/example/src/test/scala/example/Tour.scala, prepare; compare DbInit.enterSchema and CreatedMeanwhile in modules/db/src/main/scala/io/eezo/db/DbInit.scala.
  • How to see it: read prepare next to the scaladoc of DbInit.enterSchema. The race is timing dependent, so a run does not show it on demand; its symptom is a Tour run that fails in prepare with a duplicate key error on pg_namespace_nspname_index.

4. The Tour could move to src/main, which removes the need for the reason it lives in src/test

  • What: the Tour is a program, not a suite. It lives in src/test only because testcontainersPg is a test scoped dependency. That reason now survives only in a comment of the example block in build.sbt, which the rule does not accept as a home. modules/example is not published and not in unidoc, so it can take testcontainersPg without the Test scope, and the Tour can move to src/main. Then no reason is left to carry, and the Tour's usage could be public scaladoc. The tour alias and ADR 0005's pointer to the example block change with it.
  • Where: modules/example/src/test/scala/example/Tour.scala; the example block of build.sbt; testcontainersPg in project/Dependencies.scala; ADR 0005, section "The Tour reads TOUR_NOPAUSE instead of --no-pause".
  • How to see it: read the comment on the example block in build.sbt: "The Tour lives in src/test because it starts its own Postgres through testcontainers, and that is a test scoped dependency."

The other deleted texts need no issue: the Cli history and the Db rationale were history or stale, and Cli reading its settings from Db is shown by the code.

How to review

  1. Read the commits in order. The first six follow the first version of the decision. The next three apply its revision: the public scaladoc of Cli, Db and AccessProbe, the name createTourSchema, and a format fix. The last five apply an independent evaluation: the new db suite, the deletion of AccessProbe, createTourSchema inline again and the name TourDatabaseSchema, the Cli scaladoc, and the tour alias.
  2. Read the two public scaladocs as a user of the example. They must say how to use the object and nothing else.
  3. Read OneImportFromOutsideEezoSuite next to modules/db/src/main/scala/io/eezo/db/Exports.scala.
  4. For the Tour.scala:51 row, read the DbAppSuite test in modules/db/src/test/scala/io/eezo/db/DbAppSuite.scala and docs/how-to/configure-the-database.md.
  5. For the TOUR_NOPAUSE row, read ADR 0005. Its pointer, "the example block in build.sbt documents the spelling", is still true: that comment is not changed.
  6. Make sure that the diff of Tour.scala changes no statement: only the rename. createTourSchema was extracted in an earlier commit to carry a name that a later commit removed; it is inline again, because the three statements of prepare read better side by side.

How it was tested

  • The lint rule (local.trusthooks nocomment 0.1.0-ab947017159c) on example/Compile, example/Test and the new db suite: 0 findings each. Before this pull request, example had 2 and 9.
  • bin/fast 'example/Test/compile': success. The root build does not aggregate example, so the gate does not compile it; this is the compile check for the module.
  • TOUR_NOPAUSE=1 bin/fast 'example/Test/runMain example.Tour' and TOUR_NOPAUSE=1 bin/fast tour: exit 0 each, against a new container. 11 checks passed, 6 rejections as expected, 0 warnings, the same as before the change.
  • bin/fast 'db/testOnly outside.OneImportFromOutsideEezoSuite': 2 passed; red with Tx made private[eezo] by hand, as above.
  • bin/fast 'example/scalafmtCheckAll': success. The gate's format step does not cover example.
  • bin/prose-check: exit 0.
  • bin/gate: GATE GREEN on JDK 25, at commit cd29799. The root suites passed 562 and 118 tests, the examples 17 and 25, the site 52, with 0 failures.

🤖 Generated with Claude Code

The comment said that Cli reads its URL, user and password from Db so
that the settings have one source. That is a design decision, and the
code shows it: each override returns the Db value of the same name.

The comment also said that the chapters that demonstrate raw JDBC get
their connection from Db.withConnection. That is no longer true. No
code in the repository calls Db.withConnection or Db.connect; the Tour
opens its own connection to its container.
A test source has no public API, so it carries no scaladoc.

The scaladoc on the object said how to run the Tour and why the switch
is the TOUR_NOPAUSE environment variable and not an argument or a -D
property. The example block in build.sbt gives the command, and ADR
0005 records why it is an environment variable. The rest described
what the chapters print, and the chapters print it.

The scaladoc on the container said that the Tour lives in src/test for
testcontainers and that it asserts nothing. The example block in
build.sbt says the same.

The scaladoc on databaseSchema said that naming the schema makes each
pooled connection create it and set search_path to it. DbAppSuite pins
that: "databaseSchema alone puts the tables in that schema, creating
it, and status agrees".
The scaladoc said that the Tour works in a Postgres schema of its own,
so that it disturbs nothing else. The name TourSchema now says whose
schema it is, and the Tour prints the reason when it starts: "Working
in schema eezo_tour ..., so nothing else is touched."
The comment justified "if not exists" on the create that follows the
drop in prepare. The pool of DbApp fills in the background while boot
runs, and each new connection creates the schema that databaseSchema
names when it is missing. So the pool can create eezo_tour between the
drop and the create, and a plain create would fail. The function name
createTourSchemaRacingThePool now states that constraint.

This is a workaround, and the comment hid it. The Tour starts a new
container on each run, so the drop can only remove a schema the pool
made. The behaviour stays as it is in this change.
Three markers cut Tour.scala into chapters, plumbing and output. The
markers go. They show that the file holds three things: the eleven
chapters of the program, the database steps the chapters share
(connect, prepare, finish, dropAll, attempt, exec), and the console
output helpers (header, chapter, note, sql, ok, warn, rejected, blank,
pause). A later change can move the output helpers to a file of their
own.
… is formatted

The gate checks the prose of each changed file, and it read the two
sbt commands in the scaladoc of Cli as prose with a double hyphen. The
commands are now a code block, one command on each line, which is what
the old line meant. scalafmt also formats the rest of Cli.scala, which
was not formatted on dev; the example project is not in the root
aggregate, so the format step of the gate does not see it.
…e them, not their history

Public scaladoc tells a user how to use the member. The history of Cli,
the reason Db is not part of the db module, and the reason AccessProbe
exists are not usage, so they go. Db.scala is formatted with scalafmt.
A name says what the function does. It creates the schema of the Tour.
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