Jump to content

Procedural code: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
No edit summary
Line 1: Line 1:
In [[computer science]], '''procedural coding''' is an [[anti-pattern]] that involves solving a problem by incrementally taking somewhat vague steps towards a perceived solution potentially without a full analysis of the desired goals or problem domain. This linear approach often has the effect of producing a monolithic and un-reusable solution. A counter approach is [[object-oriented programming]]. The polar opposite approach is [[declarative programming]], which starts with the goal and works backwards to find a solution previously conceptualized.
In [[computer science]], '''procedural coding''' is an [[anti-pattern]] that involves solving a problem by incrementally taking somewhat vague steps towards a perceived solution potentially without a full analysis of the desired goals or problem domain. This linear approach often has the effect of producing a monolithic and un-reusable solution. A counter approach is [[object-oriented programming]]. The polar opposite approach is [[declarative programming]], which starts with the goal and works backwards to find a solution previously conceptualized.

By way of illustration, consider the analogy of driving to a destination. The procedural solution is to follow a set of step-by-step driving instructions without using a map. E.g. "turn left at Main Street, go 2 blocks and turn right...". The driving directions are highly context sensitive and of little use to those who do not share the same point of origination and destination. If we lose our place we may need to back trace through the instructions to find out where we are. If an unanticipated detour occurs a lot of unnecessary analysis is required to get back on course. Also, one wrong turn could lead to hours of lost time or the inability to arrive at the destination.


The main casualties of this anti-pattern are:
The main casualties of this anti-pattern are:
Line 7: Line 9:
# Robustness: Since everything depends on everything else, there are an exponentially large number of failure points.
# Robustness: Since everything depends on everything else, there are an exponentially large number of failure points.


Note that even if a program is not using [[Object-oriented|objects]], it does not inherently fall into this trap; likewise programs that use objects can also suffer from the same maladies as procedural software. The anti-pattern only occurs if the modules of the program are not factorable.
As way of illustration consider the analogy of driving to a destination. The procedural solution is to follow a set of step-by-step driving instructions without using a map. E.g. "turn left at Main Street, go 2 blocks and turn right...". The driving directions are highly context sensitive and of little use to those who do not share the same point of origination and destination. If we lose our place we may need to back trace through the instructions to find out where we are. If an unanticipated detour occurs a lot of unnecessary analysis is required to get back on course. Also, one wrong turn could lead to hours of lost time or the inability to arrive at the destination.

Note that even if a program is not using [[Object-oriented|objects]], it does not inherently fall into this trap; programs that do use objects can do this as well. The anti-pattern only occurs if the separate parts of the program are inseparable.


It is impossible to develop a solution without any proceduralism anywhere using today's computer architectures, the question is where this proceduralism occurs and how. Most programming paradigms in use today aim to break up the procedures into small re-usable chunks. See [[Object-oriented]] programming for one example of this.
It is impossible to develop a solution without any proceduralism anywhere using today's computer architectures, the question is where this proceduralism occurs and how. Most programming paradigms in use today aim to break up the procedures into small re-usable chunks. See [[Object-oriented]] programming for one example of this.

Revision as of 21:37, 25 September 2005

In computer science, procedural coding is an anti-pattern that involves solving a problem by incrementally taking somewhat vague steps towards a perceived solution potentially without a full analysis of the desired goals or problem domain. This linear approach often has the effect of producing a monolithic and un-reusable solution. A counter approach is object-oriented programming. The polar opposite approach is declarative programming, which starts with the goal and works backwards to find a solution previously conceptualized.

By way of illustration, consider the analogy of driving to a destination. The procedural solution is to follow a set of step-by-step driving instructions without using a map. E.g. "turn left at Main Street, go 2 blocks and turn right...". The driving directions are highly context sensitive and of little use to those who do not share the same point of origination and destination. If we lose our place we may need to back trace through the instructions to find out where we are. If an unanticipated detour occurs a lot of unnecessary analysis is required to get back on course. Also, one wrong turn could lead to hours of lost time or the inability to arrive at the destination.

The main casualties of this anti-pattern are:

  1. Re-use: Since the granularity of the solutions are not very fine, it is difficult to re-apply one solution to another problem.
  2. Readability: In order to understand the problems being solved it is necessary to step through code while trying to keep track of the complex state of the world.
  3. Maintainability: Since dependencies are highly complex, it is difficult to make a change and know what the outcome will be.
  4. Robustness: Since everything depends on everything else, there are an exponentially large number of failure points.

Note that even if a program is not using objects, it does not inherently fall into this trap; likewise programs that use objects can also suffer from the same maladies as procedural software. The anti-pattern only occurs if the modules of the program are not factorable.

It is impossible to develop a solution without any proceduralism anywhere using today's computer architectures, the question is where this proceduralism occurs and how. Most programming paradigms in use today aim to break up the procedures into small re-usable chunks. See Object-oriented programming for one example of this.

Another paradigm gaining in commercial popularity aims to break the solution up into two independent stages, a declarative and a interpretive stage. The target state is declared in one stage independent of the solution, and the actual processes used to arrive at any given target are left up to the interpreter. In order to handle all possible permutations of processes needed to arrive at the target states that the various declarative stages might desire, the interpreter is forced to break the problem domain down into its natural components for re-use. See declarative programming.

See also

Spaghetti code