Proposed Programming and Design Patterns
Elementary patterns describe standard methods and idioms used when
designing and coding programs. As Bergin writes in the introduction to
Selection,
they are known and probably obvious to a working programmer, but new and
possibly intimidating to a novice. The elementary patterns home page is
http://www.cs.uni.edu/~wallingf/patterns/elementary/.
This page organizes the patterns referred to on that page and
other proposed elementary patterns according to the following categories:
Objects and responsibilities
- Design process patterns go here
- Story (Early Development
- Beck)
- Auer's Reusability
Through Self-Encapsulation contains several patterns useful
to beginners (particularly Behavior-Defined
Class and Encapsulated Concrete State),
although they are written at a more advanced level.
Interacting objects and information passing
- Reading interfaces/contracts (e.g., C/C++ header files,
javadoc documents): signatures, pre/post-conditions, invariants
(Read the Interface for
Constructors and
Read the Interface for Methods are
initial, Java-only prototypes: Brady)
- distinguishing between observers/accessors and modifiers
- including/importing interface information (class and function
declarations)
- creating objects
(Declare-Construct-Initialize is an
initial, Java-only prototype: Brady)
- method invocation
- Information passing
- Between objects:
In:
pass-by-value (and
pass-by-constant-reference in C++),
class variables, global variables
Out:
return value, pass-by-reference/pointer,
class variables, global variables
In/Out:
pass-by-value with modified value returned,
pass-by-reference/pointer,
pass-by-value/return,
class variables, global variables
- Between functions of same object: add instance variables
(are static local variables superceded by instance variables?)
(can be used for In, Out, or In/Out)
- Formal Parameters vs. Actual Arguments
- Using function calls as expressions (embedded, or capture
return value, see Caching Temporary Variable in
Temporary [Local]
Variables (Kent Beck) or description in Loop
Patterns (Astrachan and Wallingford))
- see Monash Univ. paper or get transparencies from Dianne Hagen
Polymorphism
- Types
- primitive, language-supplied, library-supplied,
user-defined
- abstract, concrete
- subtypes
- parameterized types
- etc.
- Named Value
- Local Variable: Temporary Objects and Containers,
Accumulators/Counters, Loop Indices, Iterators, Linked List
Pointers, "Scratch Paper" Copy, Function Results,
other common local variable patterns (see also
Temporary [Local]
Variables (Kent Beck), detailed below, and
Declare-Construct-Initialize
(Local Variables) (Brady))
- Instance Variable
- Class Variable
- Global Variable
- Formal Parameter
- Declare-Construct-Init
- Temporary [Local]
Variables (Kent Beck):
- Temporary Variable (use local variables - local scope
and extent - whenever possible; init as soon as expression
is valid)
- Collecting Temporary Variable (also known as accumulators,
counters, intermediate values, e.g., sum += nextVal)
- Caching Temporary Variable (don't recompute same value)
- Explaining Temporary Variable (var. with good name to
capture complex expression)
- Reusing Temporary Variable (e.g., must capture
result of a die roll to refer to it again rather than re-roll it)
- Role-Suggesting Temporary Variable Name
- Related patterns that are well-documented for beginners in
Astrachan and Wallingford's Loop
Patterns
- Assign Variables Once (Gabriel)
- Local Variables Reassigned Above Their Uses (Gabriel)
- Role-Suggesting Temporary Variable Name (Beck)
- Caching Temporary Variable (Beck)
- Selection
(Bergin)
- Whether or Not (IF), Alternative Action (IF/ELSE), Return
Not Else (IF-Return), Sequential Choice (IF/ELSEIF/ELSE)
- Unrelated Choice (Sequential IFs), Independent Choice
(Nested IFs), Partial Dependence (Unbalanced Nested IF)
- conditional operator, switch
- Short Case First, Default Case First
- Positive Condition, Function for Complex Condition,
Function for Complex Action
- One Liner, Brace All, Braces Line Up, Indent For Structure
- Repetition
- For/Repeat: Repetition with Well-defined Step
General pattern for repetition with a well-defined step to the next
iteration
EXCEPTION: Well-defined step that is equivalent to the initialization
(implemented as a while-loop)
Special Cases:
- While/Do-while: General Repetition (good name?)
General pattern for repetition
Special Cases:
- Loop
and a Half, Polling Loop (Astrachan & Wallingford)
- Infinite Loop, Infinite Loop Until Exception/Event
- Step In Body (e.g., binary search) -- i.e.,
Step depends on calculations in body, or step is
purpose of body
- Step In Condition (e.g., while ((c = getchar()) != EOF)
- Step Equals Init (Don't Repeat Code implies while-loop rather
than for-loop)
- Execution At Least Once
- etc.
- Recursive Repetition
- Overall Loop Patterns
- While/Do-while: Name?
- Loop
and a Half, Polling Loop (Astrachan & Wallingford)
- Infinite Loop, Infinite Loop Until Exception/Event
- Step In Body (e.g., binary search) -- i.e.,
Step depends on calculations in body, or step is
purpose of body
- Step In Condition (e.g., while ((c = getchar()) != EOF)
- Step Equals Init (Don't Repeat Code implies while-loop rather
than for-loop)
- etc.
- Roundabout
(Wallingford) - Recursion
- General Loop Patterns:
I/O Patterns (may be a separate category)
- Display(s) for all objects (similar idea to universal toString
in Java)
- Simple Screen Output
- Unchecked Keyboard Input
- Filtered Input (see Northeastern IOTools project)
- Prompted Input (combination of Simple Screen Output and
either Unchecked or Filtered Input) -- this is same as or
similar to Polling
Loop (Astrachan & Wallingford)
- etc.
Class definition
- External/Client Interface
- method signatures
- constructors, observers, modifiers
- contracts (pre-conditions, post-conditions), object and
class invariants
- inheritance information (need to follow superclasses but not
Java interfaces to learn what methods are inherited)
- Package/Friend/Subclass Interfaces
- Internal Interfaces
- state information: instance and class variables
- method definitions
- low-level class design
- Patterns to help identify needed methods
- Meet Responsibilities
- External vs Other Interfaces
- Inheritance Types (Inheritance, Subtypes, Interfaces,
Parameterized Types/Templates)
- Constructors: default, conversion, clone, initializing
constructors (in C++, the Big Three: copy cons., operator=,
destructor)
- Distinguish between Accessors and Modifiers
- Get and Set Methods (thoughts on set: at most parallel to
constructor; may be superceded by higher-level functions,
e.g., move rather than setPoint)
- I/O Functions (interactive, file, media transmission)
- Repeated Code (Don't Repeat Code, Genericize?)
- Data Private
- Encapsulation Within Encapsulation
Composite structures and algorithms
- Types of objects
- Language Objects (basic types)
- Composition
- Library Objects,
Graphical Building Blocks, Widgets
- Collection/Container
- Active objects
- Types of relationships
- Aggregation (Has-A)
- Generalization/Specialization (Is-A)
- Subtype
- etc.
- Collection/Container Classes
- Linear:
Linear Indexed Data
Structure (Brady)
- Linear Linked DS
- Iterated DS
(any different from Linear Linked DS?)
- Link
(Bergin)
- 2-D, N-D (matrices, etc.)
- Stack, Queue, Deque, Tree?
- Algorithms
- Traversal and Search through linear data structures
(see Repetition above)
- Pre-Order/Post-Order/In-Order Depth-First Tree
Traversals, Breadth-First Tree Traversal
- etc.
- Design
Patterns for Data Structures (Dung Nguyen)
Concurrency
General Programming Strategies and Stylistic Patterns
- From Bergin's Selection:
- Positive Condition, Function for Complex Condition,
Function for Complex Action
- One Liner, Brace All, Braces Line Up, Indent For Structure
- From Bergin's Coding
at the Lowest Level: Coding Patterns for Java Beginners: (some
were written by Bergin, some are originally from other people but
are well-documented for beginners here)
- Brace All, Braces Line Up, Indent for Structure, Be
Spacey, Say It Once, Locals (Only) When Needed, Comment
(Only) When Necessary, Intention Revealing Name, Consistent
Naming, Local Variables Reassigned Above Their Uses, etc.
- Well-documented for beginners in Astrachan and Wallingford's Loop
Patterns
- Simply Understood Code (Gabriel)
- Intention Revealing Name [for functions] (called
Intention Revealing Selector by Beck)
- Composed Procedure (called Composed Method by Beck)
- Don't Repeat Code
- Don't Recompute Same Value (similar to Caching Temporary
Variable -- special case of Don't Repeat Code?)
- Genericize? (generalize functions with parameters,
parameterized container classes, etc)
- Init Immediately
- As Local As Possible
- All Data Private
- Declare-Construct-Initialize ? (applies to variables, objects), or
- Declare-Define-Use ? (applies to variables, functions, objects,
classes)
- There are many patterns at the Portland
Pattern Repository that have useful ideas for teaching novice
programmers, even though the descriptions of the patterns and the forces
going into them are usually too advanced for beginners. In particular,
many of the patterns have usenet-like discussions following them that
would confuse beginning students.
- Series of
patlets (Ward Cunningham)
Several of these patlets seem particularly appropriate for
beginners:
- Program for People (for writing code)
- Generated Documentation (for writing code)
- Think Like the Machine (for reading code)
- Reveal Intention
- Clear Warning
- Simply
Understood Code (R. Gabriel)
The motivations and forces of these patterns
are described in terms of large projects that
will be maintained over time by different people. Nevertheless,
many of these would be useful patterns for novice
programmers if written in a form they could follow.