Search topics and sections

Functional design is a design principle in which each part of a system does one job and affects the other parts as little as possible. It is best known from software engineering, where the parts are modules of a program. The same idea also guides hardware, such as electronic circuits built by connecting separate functional stages.[1]

A system built this way is easier to understand, test, fix, and change, because a change to one job touches only one part. The idea grew out of 1960s and early 1970s work on modular programming, including research by Larry Constantine.[1][2] It lives on in related principles such as information hiding, separation of concerns, and the single-responsibility principle.[3]

Modules and jobs

A module is a named piece of a program that other parts can call. Stevens, Myers, and Constantine defined it as one or more consecutive program statements with a name by which other parts of the system can invoke it, and preferably with its own set of variable names. Examples include subroutines of all types.[1]

In a functional design, everything inside a module serves a single function. The paper's examples were "Compute Square Root", "Obtain Random Number", and "Write Record to Output File".[1] A module described as "read next transaction and update master file" does two things, so it is not functional.[1]

Splitting a two-job module into functional modules Two jobs in one module Read next transaction and update master file sequential cohesion One job per module Process transaction Read next transaction Update master file transaction transaction functional cohesion functional cohesion
On the left, one module reads the next transaction and updates the master file, which gives it sequential cohesion. On the right, a caller named Process transaction calls two modules with functional cohesion and passes the transaction between them as a parameter.

The same idea outside software

The authors of the 1974 paper that set out these ideas pointed out that they were not new.[1]

  • An assembly line isolates simple functions in a way that still produces a complete, complex result.
  • Circuits are designed by connecting separate, functional stages, not as one big, interrelated circuit.
  • Car makers stopped driving windshield wipers from engine vacuum, because engine load affected how well the wipers worked.

Cohesion and coupling

Two measures describe how well a program is split into modules. Coupling is the strength of the link a connection makes from one module to another.[1] Cohesion, which the 1974 paper called binding, is how closely the elements inside one module relate to each other.[1]

The two measures work together. Coupling falls when relations between modules are minimized and relations within each module are maximized, so the stated goal is to reduce coupling by striving for high cohesion.[1]

Levels of cohesion

The 1974 paper ranked six kinds of cohesion, from lowest to highest. A module's cohesion is lowered by every pair of its elements that is not functionally bound.[1]

Level What ties the elements together Example from the paper
1. Coincidental Nothing meaningful Unrelated instructions gathered only to remove duplicate code
2. Logical They belong to one class of tasks "Edit all data", or one module for all input and output
3. Temporal Logical, and also run at the same time "Initialization", "clean-up"
4. Communicational They use the same input or output data "Print and punch the output file"
5. Sequential The output of one is the input of the next "Read next transaction and update master file"
6. Functional All serve one single function "Compute square root"

The scale is not even. Functional cohesion is much stronger than all the rest, and the first two levels are much weaker than all the rest.[1] Later writers refined the list: Myers added "classical" and "informational" cohesion, and Yourdon and Constantine added "procedural" cohesion.[2]

Scale of module cohesion levels Much weaker Much stronger than all the rest Coincidental Logical Temporal Communicational Sequential Functional unrelated code lumped together edit all data initialization print and punch output file read transaction, update file compute square root Higher cohesion
Stevens, Myers, and Constantine (1974) ranked six levels of module cohesion, from coincidental (lowest) to functional (highest). The levels are not evenly spaced: coincidental and logical cohesion are much weaker than the rest, and functional cohesion is much stronger than all the others.

What raises coupling

The paper named three factors that set how strongly a connection couples two modules.[1]

Factor Lower coupling Higher coupling
Interface Simple and obvious Complicated and obscure
Type of connection To the module by its name To elements inside the module
What is passed Data Control signals, or hybrid (one module changing another's code)

A control signal is a switch or flag that tells another module how to run, not just what data to work on. Control coupling sits between data and hybrid coupling.[1]

Shared data areas

Shared data areas, such as global variables or FORTRAN COMMON blocks, couple every module that uses them to every other such module. This holds whether or not the modules have anything to do with each other.[1] Each shared element is a separate path along which errors and changes can spread.

The number of such paths can be counted. For a shared area of NN elements used by MM modules, the paper counts

P=NM(M−1)P = N M (M-1)

direct (one-step) paths, where M(M−1)M(M-1) is the number of ordered pairs of modules.[1] Ordered pairs count because sharing couples A to B and also B to A. With N=25N = 25 variables and M=3M = 3 modules, P=25×3×2=150P = 25 \times 3 \times 2 = 150.[1]

Shared data area versus passing parametersShared data areaABCShared area(25 variables)Paths = N × M × (M − 1)= 25 × 3 × 2 = 150Passing parametersABCxyxy
With a shared data area, N=25N = 25 shared variables and M=3M = 3 modules give N×M×(M−1)=150N \times M \times (M - 1) = 150 possible paths between modules. With passed parameters, each call carries only the data it needs, such as xx going down and yy coming back.

Side effects and predictable modules

A predictable module gives the same behavior each time it gets the same inputs, and it works independently of its surroundings.[1] The paper calls this "black-boxness": a user can understand what the module does and use it without knowing what is inside.[1]

A module can be free of errors and still not be predictable. The paper's example is an oscillator module that returns zero and one in turn on each call. If several users share it and one makes an odd number of calls by mistake, all the other users fail.[1]

Hidden state like this is one kind of side effect, an effect a module has beyond the results it hands back. The paper notes that modules that keep track of their own state are usually not predictable, even when error-free.[1]

Checking a design

The one-sentence test

A useful check is to write one sentence that states the module's purpose and then examine it.[1] The 1974 paper gave four tests.

  1. If the sentence must be compound, contain a comma, or contain more than one verb, the module probably does more than one function.
  2. If it contains time words such as "first", "next", "then", "after", "when", or "start", the module probably has sequential or temporal cohesion.
  3. If the verb is not followed by a single specific object, the module is probably logically bound. "Edit all data" fails, while "edit source statement" may pass.
  4. Words such as "initialize" or "clean-up" point to temporal cohesion.

The test has a stated limit. Any functional module can be described part by part with a compound sentence, so the warning sign is only when such wording cannot be avoided in a complete description.[1]

Removing control flags

A module that takes a flag telling it which of two jobs to do can usually be split. In the paper's example, module EXECNCOMM passes a switch called PARSE to GETCOMM to ask for a parsed or unparsed command.[1]

Splitting GETCOMM into GETPCOMM and GETUCOMM removes the switch. The caller is no more complex, since it makes one of two calls instead of setting a switch, and the two small modules together are simpler than the original by the amount of switch testing.[1]

Removing a control flag from a structure chartControl-coupledSplit by functionEXECNCOMMGETCOMMEXECNCOMMGETPCOMMGETUCOMMPARSE (switch)commandcommandcommandno switch to test
A structure chart before and after splitting a module by function, after the 1974 Structured Design paper. On the left, EXECNCOMM passes a PARSE control flag down to GETCOMM. On the right, separate modules GETPCOMM and GETUCOMM each return only a command, so no switch is passed.

Matching the problem

The paper advised that the structure of the design should match the structure of the problem, or "form should follow function".[1] Its example is a module that dials a telephone and a module that receives data.

If DIAL calls RECEIVE because receiving usually follows dialing, a new need to transmit right after dialing cannot reuse DIAL cleanly. Having DIAL's caller invoke RECEIVE avoids this, because receiving is not part of dialing.[1] When design matches problem in this way, a change to one part of the problem changes one module.

Structure charts of designs A and B for dialing and receiving Design A Design B Caller DIAL RECEIVE Caller DIAL RECEIVE TRANSMIT easy to add New need: transmit after dialing; DIAL cannot be reused
Structure charts from the 1974 Structured Design example. In design A, DIAL calls RECEIVE, so DIAL cannot be reused when the program must transmit after dialing; in design B, the caller calls DIAL and RECEIVE separately, so a TRANSMIT module is easy to add.

How far to divide

The 1974 paper left this as an open problem but gave guidelines.[1]

  • Division has probably gone far enough when no subset of a module's elements would be useful alone.
  • Each module should be small enough to grasp all at once, seldom longer than one or two pages of source code.
  • Modules with fewer than five or more than 100 executable statements deserve a closer look. Small ones may not do a whole function, and large ones may do more than one.
  • Early in design, err on the side of dividing too finely, since it is easy to recombine modules later.

Benefits

The authors of the 1974 paper listed several gains from simple, single-function modules.[1]

  • Problems are easier to solve when they can be split into pieces considered separately.
  • Fewer connections mean fewer paths for "ripple" effects, where a change in one part causes errors in another.
  • Modules can be written separately, by different people, at different times, and in different languages.
  • Modules can be tested before the whole program exists, using simple "stub" modules that return preset results.
  • Slow modules can be tuned on their own and put back with little impact on the rest.
  • A growing library of modules means less new code for each new program.

The paper states that structured design reduces the effort needed to fix and modify programs.[1] This matters because of the size of maintenance, the changes made after a program is first delivered: one review cites estimates that about 90% of software life cost falls in the maintenance phase.[4]

A single module for each job means a change lands in one place. In the paper's example, if every program got records from a file through one module, a change of file access method would mean rewriting only that module.[1]

Limits and criticism

Run-time cost

Many small modules cost extra time and memory for each call.[1] The authors argued that, depending on the language, the cost can be small or even negative. For example, error-handling modules that are never called add no run-time cost, and removing control switches removes code that would otherwise run.[1]

Parts that are hard to make functional

Some modules group tasks by time, not by function. An initialization module has low cohesion, but the paper concedes it is sometimes the simplest solution.[1] It suggests one way to avoid one: a read module that opens its file when it finds the file not yet open.[1]

The sentence test can mislead

The introduction to the paper's 1979 reprint in the anthology Classics in Software Engineering called the one-sentence test "fraught with danger".[2] It is too easy to word a bad module so it sounds good, or a good module so it sounds bad. That introduction recommends checking coupling as well: if a design shows strong coupling, its cohesion was probably low whatever the module descriptions say.[2]

Function versus information hiding

David Parnas proposed a different rule for splitting systems in a 1972 paper.[3] He compared two splits of the same program: one with each major processing step as a module, as from a flowchart, and one based on information hiding.[5] In the second, each module hides one design decision, such as how lines are stored, so that a change to that decision stays inside one module.[5]

Parnas concluded that it is almost always wrong to split a system into modules from a flowchart. He proposed starting from the design decisions that are hard or likely to change, and hiding each one in a module.[5] The 1974 paper also warned about flowchart-based modules, noting that they tend to have only sequential cohesion.[1]

Limited evidence

The 1974 paper noted that its method had not been through any formal IBM test.[1] Later work has questioned how well cohesion and coupling metrics guide real redesign. One case study of a restructuring of the Eclipse platform found that the metrics it used did not behave as expected.[6]

Functional design is tied to the history of structured programming and modular programming.

Year Work Contribution
1960s to early 1970s Many papers on "modular design" and "modular programming" General push to split programs into modules[2]
Before 1974 Nearly ten years of research by Larry Constantine Main ideas behind structured design[1]
1972 Parnas, "On the Criteria To Be Used in Decomposing Systems into Modules" Information hiding as a rule for splitting[3]
1973 Glenford Myers Called the same ideas "composite design"[1]
1974 Stevens, Myers, and Constantine, "Structured design" First use of the term "structured design"; cohesion levels and coupling factors[2][1]
1974 Edsger W. Dijkstra, "On the role of scientific thought" Introduced the term "separation of concerns"[3]
1975 and 1979 Yourdon and Constantine, Structured Design Book-length treatment[2]
Late 1990s Robert C. Martin Single-responsibility principle[3]

Martin stated the single-responsibility principle as "each software module should have one and only one reason to change". He also put it as "gather together the things that change for the same reasons" and separate those that change for different reasons, which he described as another way to define cohesion and coupling.[3]

Use in mechanical design

In mechanical engineering, "functional design" also names work that links computer-aided design models to what a product must do. One 2007 method drives parametric CAD models from the product's functional requirements and design rules.[7] Its stated advantage is that parameters stay linked through the product tree, so one change to a functional parameter can update the whole CAD model.[7]

References

  1. Structured design, W. P. Stevens, G. J. Myers, and L. L. Constantine, IBM Systems Journal, 1974.
  2. Structured design (reprint with introduction), Classics in Software Engineering, Yourdon Press, 1979.
  3. The Single Responsibility Principle, Robert C. Martin, The Clean Code Blog, 2014.
  4. Which Factors Affect Software Projects Maintenance Cost More?, Acta Informatica Medica, 2013.
  5. On the Criteria To Be Used in Decomposing Systems into Modules (technical report CMU-CS-71-101), D. L. Parnas, Carnegie Mellon University, 1971.
  6. Legacy Software Restructuring: Analyzing a Concrete Case, arXiv, 2012.
  7. Collaborative, Functional and Knowledge Based Engineering Using a PLM Environment, S. Gomes, J. B. Bluntzer, M. Mahdjoub, and J. C. Sagot, International Conference on Engineering Design (ICED), 2007.

Figure