André Leclerc informatics consultant

A simple methodology for specifying the logical behaviours of an information system’s business components

This page proposes a simple methodology for specifying the logic contained in the business components of an information system.

This methodology is based on some business modeling concepts that should be reviewed before continuing with the reading of this page.

As the main goal of this methodology is to specify business logic that is implemented by a business component's method, and as methods are one of the artifacts of an information system’s business domain model, those business modeling artifacts should also be reviewed before continuing with the reading of this page.


Purpose

The main goal of this methodology is to specify business logic in a way that can be understood by both the business people who use information systems and by the technical people who build them.

Even though pseudo code is not the only way of achieving that goal, it is the way that is described here given its common use and high level of understanding amongst its users.

Pseudo code is often referred to as structured English as it is based on a very simple subset of the English language.  Pseudo code results in logic specifications that are not only clear but also complete.  The translation of good pseudo code into real code (what computer programmers produce) is mostly a mechanical task.


Back to opening page Back to background material Back to top of this page

Vocabulary

Because it is all about the business, all pseudo code should be formulated using the vocabulary of the business domain instead of the vocabulary of the information technology.

For example, “Get the next line item that is part of the order.” is a good way of phrasing an action in pseudo code as it uses terms that mean something to the business people.

However, “Increase current line item number by one and read the line item record for current line item number.” is not a good way of phrasing the same action in pseudo code as it uses terms that mean something only to the technical people who will develop and implement the logic.


Back to opening page Back to background material Back to top of this page

Programming Constructs

Only three (3) general programming constructs are required to specify the flow of control in pseudo code:

  1. sequences in which each action is performed sequentially, one after another;
  2. selections in which different series of actions are performed depending on the result of a test or on a data value; and
  3. iterations in which a group of actions is performed iteratively a certain number of times, while a certain condition exists, or until a certain condition occurs.

Selections are further subdivided into two (2) more refined constructs:

  1. the “if ... else” construct that offers one series of actions if a condition exists and another series of actions otherwise; and
  2. the “case” construct that offers any number of series of actions depending on the data value returned by an expression.

Iterations are further subdivided into three (3) more refined constructs:

  1. the “for” construct that performs a group of related actions a certain number of times;
  2. the “while” construct that performs a group of related actions while a certain condition exists; and
  3. the “do ... until” construct that performs a group of related actions until a certain condition becomes true.

Counting the “sequences” construct, the two “selections” ones and the three “iterations” ones, six (6) basic programming constructs are available for specifying the flow of control in pseudo code.   Each one of these constructs can be embedded inside any other construct.

Apart from well chosen key words and symbols, pseudo code does not follow a rigorous notation since it is meant to be read and understood by people, not by computers.

The methodology presented in this document is not a universal standard.   Such a universal standard for pseudo code does not exist.   However, the methodology presented here, for specifying logic in the form of pseudo code, has proven itself useful in many projects and is easy to learn and use.


Back to opening page Back to background material Back to top of this page

1)  Sequences

A sequential construct is specified by writing one action after another, each action on a line by itself, and all actions aligned with the same left indent.

The actions are performed in the sequence (top to bottom) that they are written.

Here is an example of a sequential series of actions:

Brush teeth.
Wash face.
Comb hair.
Smile to mirror.

2)  “If ... Else” Selections

The selection of one series of actions over another, depending on the existence or inexistence of a condition, is specified as follows:

if ( condition to be tested ) {
   sequence of actions
}
else {
   another sequence of actions
}

In the above syntax, the bolded “if” and “else” words are key words indicating the beginning of the two clauses of that construct.  The “else” clause is optional.  The condition to be tested is put inside parentheses.  The two alternate courses of actions are expressed as separate sequences (that is the first kind of construct) of actions listed inside a matching pair of curled brackets.  Here is an example of an “if ... else” selection of actions:

if ( hours worked > number of regular hours ) {
   calculate overtime hours
   display overtime message
}
else {
   display regular time message
}

3)  “Case” Selections

The selection of one series of actions out of many potential ones, depending on the data value returned by an expression, is specified as follows:

switch ( expression to be evaluated ) {
   case first value:
      first sequence of actions
   case second value:
      first sequence of actions
   default:
      default sequence of actions
}

In the above syntax, the bolded “case” and “default” words are key words indicating the beginning of alternate courses of actions corresponding to different values that the expression to evaluate can take.  There can be as many “case” clauses as required and the “default” clause is optional.  The order in which those clauses are specified is important as it is the first one having the right value that gets the control; any following clause gets bypassed whether it has the right value or not.  The “switch” key word marks the beginning of a series of cases referring to the same expression.  The expression to be evaluated is put inside parentheses.  Here is an example of a “case” selection of actions:

switch ( age of client ) {
   case 65 or older:
      charge $2.50
   case 18 or older:
      charge $5.00
   default:
      charge $2.00
}

4)  “For” Iterations

The execution of a group of related actions a certain number of times is specified as follows:

for ( iteration expression ) {
   sequence of actions
}

In the above syntax, the bolded “for” word is a key word indicating the beginning of a series of actions to be performed iteratively a specific number of times, as specified by the iteration expression.  This programming construct is often called a “counting” loop.  The iteration expression appearing inside parentheses determines the exact number of times that the actions within the loop will be executed.  Here is an example of a “for” iteration of actions:

for ( each month of the fiscal year ) {
   calculate total revenues
   calculate total expenses
   calculate profit or loss
   display profit or loss
}

5)  “While” Iterations

The execution of a group of related actions while a certain condition exists is specified as follows:

while ( condition to be tested ) {
   sequence of actions
}

In the above syntax, the bolded “while” word is a key word indicating the beginning of a series of actions to be performed iteratively as long as the condition to be tested exists.  The condition is tested before any action gets executed within one iteration.  The series of actions is performed until the condition is not true any more.  The condition to be tested is put inside parentheses.  Here is an example of a “while” iteration of actions:

search for first transaction involving cash
if ( not found )
   exit
read details of transaction
initialize cash balance to zero
while ( transaction found ) {
   if ( transaction debits cash )
      add transaction amount to cash balance
   else
      subtract transaction amount from cash balance
   search for next transaction involving cash
   if ( found )
      read details of transaction
}
display cash balance

6)  “Do ... Until” Iterations

The execution of a group of related actions until a certain condition becomes true is specified as follows:

do {
   sequence of actions
}
until ( condition to be tested )

In the above syntax, the bolded “do” word is a key word indicating the beginning of a series of actions to be performed iteratively until the condition to be tested becomes true.  The sequence of actions is performed at least once as the condition is tested only after one iteration.  The series of actions is performed until the condition becomes true.  The condition to be tested is put inside parentheses.  Here is an example of a “do ... until” iteration of actions:

initialize cash balance to zero
position at top of transactions
do {
   search for next transaction involving cash
   if ( found ) {
      read details of transaction
      if ( transaction debits cash )
         add transaction amount to cash balance
      else
         subtract transaction amount from cash balance
   }
}
until ( transaction not found )
display cash balance

Back to opening page Back to background material Back to top of this page

The six (6) basic programming constructs described above allow the specification of any set of logical operations, whether those operations are operations of a business organization, of a computer-based application or of a programmable device.  Those operations get executed via the methods that they are grouped into.  The business components of an information system have methods implementing their behaviours and allowing them to fulfill their shares of responsibilities in the information system’s use cases.  The logic of those methods can be specified with pseudo code.  In specifying the logic of those methods, one always encounters pieces of pseudo code that reappear many times within a given method or within many methods.  Those sets of common pseudo code can be specified only once in methods of their own that can be invoked from whatever other method requiring them.  The box below shows a way of declaring and invoking methods, of passing parameters to those methods, and of returning values from the invoked methods to the invoking ones:

method method-name ( parameter1, parameter2, ... ) {
   ... some pseudo code ...
   other-method-name ( value1, value2, ... )
   ... some more pseudo code ...
   return some-value
}

Once declared, the name of a method becomes a key word that must be identified as such.  More key words can be added to the ones encountered so far if they can contribute to clarifying the logic specified by the pseudo code.  For example:


Here are a few guidelines to keep in mind when writing pseudo code:

  1. Use indentation to clearly show the nesting of constructs within other constructs.
  2. Use pairs of matching curly brackets to group actions that are part of the same clause or group.  Make sure that each opening bracket is matched by a closing bracket.
  3. Use methods to group series of actions that are invoked from more than one place or that are so long that taking them out of the main pseudo code and into a separate method would make that pseudo code easier to read.

Back to opening page Back to background material Back to top of this page