Object-Oriented Programming

Method Stereotypes

Michael L. Collard, Ph.D.

Department of Computer Science, The University of Akron

Method Stereotypes

  • All methods play a role or multiple roles in a class
  • Identifying the role of a method helps with comprehension, but also with the original design
  • A method has a primary role, with some methods a secondary role

Method Stereotype Taxonomy

Documentation

  • Method stereotypes can be embedded in the source code as a comment
  • documentation if the identification is manual
  • redocumentation if the identification is automated
  • Useful to be able to update in place

Taxonomy

  • Accessors - Read access to the state of the object, both direct and derived
  • Mutators - Write access to the state of the object
  • Creational Methods - Create and manage the lifetime of objects
  • Collaborational Methods - Work with objects of other classes
  • Degenerate Methods - No access or changes to state

Accessors

Stereotype Description
get returns the value of a data member
predicate returns a Boolean result computed from data members
property returns information based on data member values
void-accessor returns information about data members through a parameter

Accessor::get

  • Returns the value of a data member
  • The purpose of the method is very simple and primitive
  • Direct access to the value of the data member
  • C++ Rules:
    • method is const
    • returns a data member
    • return type is primitive or container of a primitive

Accessor::predicate

  • Returns a Boolean result computed from data members
  • Result is not a direct data member, but a computation involving data members
  • C++ Rules:
    • method is const
    • returns a Boolean value that is not a data member

Accessor::property

  • Returns information based on data member values
  • C++ Rules:
    • method is const
    • does not return a data member
    • return type is primitive or container of primitives
    • return type is not Boolean

Accessor::void-accessor

  • Returns information about data members through a parameter

Mutators

Stereotype Description
set changes the value of a data member
command executes a complex change of the object’s state
non-void-command command which returns a value

Mutator::set

  • Directly changes the value of a data member
  • The parameter value is stored in the data member
  • C++ Rules:
    • method is not const
    • return type is void or Boolean
    • only one data member is changed

Mutator::command

  • Executes a complex change of the object’s state
  • The change may involve several data members
  • May change the data members either directly or indirectly with another mutator
  • C++ Rules:
    • method is not const
    • return type is void or Boolean
    • complex change to the object’s state is performed, e.g., more than one data member is changed

Mutator::non-void-command

  • Command which returns a value

Collaborational Methods

Stereotype Description
collaborator Works on objects of classes different from itself
controller Works only on objects of classes other from itself

Collaborational::Collaborator

  • Works on objects of classes different from itself
  • C++ Rules:
    • returns void and at least one of the method’s parameters or local variables is an object
    • returns a parameter or local variable that is an object

Collaborational::Controller

  • Works only on objects of classes different from itself

Creational Methods

Stereotype Example/Description
constructor
copy-constructor
destructor
factory Object creation method

Creational::factory

  • Object creation method with the object returned to to the client

Degenerate Methods

Stereotype Description
incidental Does not read/change the object state, and no calls to other methods of the same class
stateless Does not read/modify the object state, with one call to other methods of the same class
empty Method with no statements

Degenerate::incidental

  • Does not read/change the object state, and no calls to other methods of the same class

Degenerate::stateless

  • Does not read/change the object state, with one call to other methods of the same class

Degenerate::empty

  • Method with no statements
  • Typically part of an early implementation
  • Maybe due to functionality moving elsewhere

Clear Design

  • A method should have a single purpose, single responsibility issue
  • Conversely, a method should have a single primary role, i.e., single role issue

Pop or Top vs. Pop and Top

Automated Detection

  • Tool, stereocode, automatically redocuments source code with stereotypes
  • XPath patterns with XPath extension functions
  • Very scalable as it redocuments large systems in minutes