Branching on an End Marker

An end marker represents completion of an input sequence and needs a representation distinguishable from any valid record.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Separate a final event from an empty item
  2. Test the last data item
  3. Choose a marker outside the ordinary data domain
  4. Keep termination separate from a malformed record

Separate a final event from an empty item

For a stream of optional notes, an empty line may be a valid note. If an API supplies a distinct end-of-stream object, compare using the appropriate sentinel identity. The code can then stop on exhaustion and keep empty input rows.

Test the last data item

Use notes A, empty, B, then the end marker. The program should retain three notes. Also test a stream ending immediately. Explain which inputs and outputs are observable and whether completion can happen without emitting any result.[1][2]

Choose a marker outside the ordinary data domain

A text reader can accept empty lines as valid records. A dedicated end marker lets the loop stop after consuming all records without treating the first blank line as the end. For input A, blank, B, then an end marker, the caller should receive three records. With just the marker, it should receive none. End-of-file detection shows how a text file interface distinguishes a returned newline from end of input.

Keep termination separate from a malformed record

An empty field in a record can be invalid even when the stream itself is still open. A structured result can communicate an item, completion, or a known read failure under separate forms. Which failures permit continuing belongs to the reader's contract. Structured errors shows why one unqualified empty result can hide those paths.

Test completion immediately after a final record lacking a line terminator. Some readers still return that last content before reporting exhaustion. If a format uses an explicit sentinel token among serialized records, define how its literal appearance in data is encoded so that input and control remain distinguishable.

References

  1. Python Software Foundation, Python Tutorial: Control Flow ↩
  2. Python Software Foundation, Python Language Reference: Expressions ↩