Learn Topaz

Reading a Real File

Open an unfamiliar source file to locate its shape, contracts, and failure paths without reading every line.

Outcome: Open a file you did not write and locate its shape, contracts, and failure paths without reading line by line.

Prerequisite: Complete First Application. Writing small programs and reading large codebases are distinct skills, and this page focuses on the second.

Reading is not writing in reverse

Reading a large file from top to bottom is rarely the fastest way to answer a specific question. Experienced readers skim for key landmarks, construct a working mental map, and then read relevant sections closely. The same four landmark types provide guidance whenever they appear.

Four passes provide quick orientation. Each pass takes seconds.

Pass one, the imports

The first lines reveal what the file depends on before you examine any other code.

TOPAZ
import std.bytes { Bytes }
import std.json

Imports reveal named module dependencies. A longer import list often indicates that the file coordinates multiple components. An empty list does not prove the file lacks host interaction, because prelude operations and the selected product profile still apply. Treat this pass as a quick map rather than proof.

Pass two, the shapes

Look for the nouns. A record declares a shape that values of that kind must have, often conveying the purpose of a file faster than any comment.

TOPAZ
record StudySession {
    topic: string,
    minutes: int,
    finished: bool
}

function summarize(session: StudySession) -> string {
    if session.finished {
        return "{session.topic}: done"
    }
    "{session.topic}: {session.minutes} minutes left"
}

Reading only the record, you already know this file handles study sessions with a topic, a duration, and a completion state. You learn the domain context without reading a single line of logic.

Pass three, the signatures

Now read function signatures and skip every body. A signature acts as a contract, and contracts compose into a cohesive narrative.

In the example above, summarize takes a session and returns a string. The signature fixes its input and result shape. A Result return would also expose recoverable failure. It does not, by itself, encode every host capability. File and I/O authority also depend on the selected product profile.

This is why the signature pass is useful yet bounded. Reading a set of signatures provides the file's data and recoverable-failure outline. Read imports and the package profile alongside them when evaluating host authority, then descend into the single body relevant to your question.

Pass four, the failure paths

Last, find where the file admits things can go wrong. Look for Result in signatures and for the ? mark in bodies.

A function whose result type is a Result indicates that execution can fail and callers must handle it. The ? mark in a body means failure at that point is passed up to the caller rather than handled here. Together, they outline the error path through the file without requiring detailed reading of error handling logic.

A file with no Result exposes no recoverable failure through its signatures. That does not prove execution cannot fault or that no host operation occurs, so maintain the distinction between a returned error and a runtime fault.

What you now do with a real file

Run those four passes on the source of any example in this documentation, or on your own first-application project. Imports, shapes, signatures, failure paths. You should be able to describe the purpose of a file in one sentence before reading a single function body closely.

When one of the four passes leaves you stuck, the reference explains the exact form. Shapes are in Records & Nominal Data, signatures in Functions & Generics, and failure paths in Null, Option, Result & Faults.

Try this

Read the summarize function again and answer without running it. Can code that receives only the returned string learn from its type whether the session was finished?

Answer

No. The signature promises a string in both cases, so code holding only that result must inspect the text, which is brittle. If downstream code needs the completion state, return it in a structured result rather than encoding it in a sentence. Noticing this from the signature alone, without reading the body, is the exact purpose of the third pass.

Ready to continue when

You can open an unfamiliar file, name its dependencies, its main shapes, two of its contracts, and one place it can fail, in under a minute and without reading every line.

The reference material is organized the same way you just read. Syntax at a Glance is the fastest map of the forms themselves.