That is a very structured-programming way of looking at it, and there is
a lot of practical wisdom behind it.
A top-down design gives you a kind of load-bearing architecture. The
main flow describes the problem at a high level, and the lower-level
routines provide the details. When you come back months later, you can
start at the top and follow the story downward.
Something like:
sub MAIN {
ReadConfiguration();
LoadData();
ProcessData();
WriteResults();
}
immediately tells another programmer (including future you) what the
program does. They do not have to read 500 lines of implementation
details before discovering the overall purpose.
The "house of cards" analogy is a good one. Code that grows by accretion
often develops hidden dependencies:
This global variable must be set before that routine runs.
This function has a side effect that another function quietly depends on.
This block only works because something earlier happened to initialize a
value.
The program may still work, but the structure is fragile.
Top-down design tends to push you toward:
Clear interfaces.
Smaller responsibilities.
Fewer surprises.
Easier testing.
Easier replacement of components.
Your earlier example of adding a second subroutine to preserve
compatibility is actually another expression of the same idea. You were
thinking about maintaining a stable interface rather than just modifying
code until the immediate problem goes away.
One of the interesting things about Raku is that it supports both
worlds. You can write a quick exploratory script in a very Perl-like
style, or you can build something with the kind of structure you would
have expected from Modula-2:
Modules define boundaries.
Classes define concepts.
Roles define capabilities.
Signatures define contracts.
Types document intent.
So a programmer with your background can use Raku almost like a modern
structured language, while still having the flexibility that attracted
people to Perl.
And I think your "not a house of cards" comment gets at the real goal of
software design: not making code look pretty, but making it resistant to
change. The code that survives years is usually the code where the
original programmer left a clear map for the next person.
Ad
I call it the "contract". Upper tier and lower tier. I will write the
lower tier that calls the upper tier first. Then create empty sub's in
the upper tier(s) with the names called from the lower tier. Fill them
in later as I figure them out. I lets me create the big pictue first,
then fill in the details, rather than getting my mind all mixed up and
confused.
That is a very recognizable software design approach, and "contract" is
a very good word for it.
What you are describing is essentially designing the interfaces before
implementing the internals. You are creating a skeleton where the
relationships between parts are established first.
The interesting part is that you are working backward from what many
people expect:
You write the lower-level code that expresses what needs to happen.
The lower-level code reveals the services it needs.
You create the upper-level routines as the "promises" that those
services will exist.
You fill in the implementations later.
That is very close to how many large software systems are designed:
define the contracts between components first, then implement the
components behind those contracts.
A Raku example might look like:
sub MAIN {
Load-Configuration();
Process-Orders();
Generate-Report();
}
sub Load-Configuration {
# later
}
sub Process-Orders {
# later
}
sub Generate-Report {
# later
}
The top-level code becomes a map of the application. The empty routines
are not "unfinished code"; they are placeholders defining the architecture.
This also explains why you like Raku's routine declarations. In
languages that require routines to exist before they are called, this
style can be awkward because you spend time arranging declarations
rather than expressing design. Raku lets the program read more like a
design document.
Your approach also prevents a common failure mode: implementation tunnel
vision. A programmer starts writing a small detail, then another detail,
then another, and suddenly the program's architecture has emerged
accidentally. Sometimes it works, but it can become difficult to change
because the structure was never consciously chosen.
Your method keeps the big picture visible:
MAIN
|
+-- Acquire Data
|
+-- Validate Data
|
+-- Transform Data
|
+-- Output Results
Then each branch can be expanded independently.
It is also very close to the old idea of stepwise refinement from
structured programming: start with an abstract description of the
solution, then progressively replace abstractions with concrete
implementations.
Given your Modula-2 background, it is not surprising that this feels
natural. That whole school of thought was