< User:Makethebrainhappy

Generally speaking there are two different types of programming: Imperative and Declarative. Languages often contain features which could be considered part of either category. The style which is preferred depends on the project at hand.

A programmer is said to be working "imperatively" if he is creating a step-by-step list of instructions for the computer to follow in order to solve a problem. His algorithm includes every needed variable and strategy to handle the situation. The following is an imperative implementation of the pythagorean theorem in python:


Based on the program code, one can surmise that the author understands the pythagorean theorem (a^2 + b^2 = c^2 for a right triangle where c is the hypotenuse of the triangle).

The same cannot be said for a programmer using a Declarative style.


If you assume that the method was not written by the programmer, then you can surmise that they would not need to know the pythagorean theorem in order to find the length of the third side (c). They would simply just need to plug their value into the method. This can be very helpful when as you simplify large problems by finding pre-built methods which are often faster than the imperitive version you can come up with. Prime number sieves are a very good example of this. You could write a program and use the Sieve of Eratosthenes to generate primes; but the libraries will use faster sieves which are more complex.

Languages such as Mathematica, your calculator (if you consider calculators a programming device), Python (borderline) are usually considered to be declarative while C++ and Java lean more towards being imperative. All four of the languages can be written with either style. Indeed, even the first example program has a declarative element to it. The code is imperative when you are thinking about solving for the hypotenuse of a pythagoren triangle (c). But it is declarative in the sense that it doesn't specify an algorithm for the Square root; it calls upon a math library to complete that operation.

Being able to write declaratively simplfies many tasks. As is shown above, it is hard to categorize a whole program into either one category or another (since they both probably exist!). It is better to looking at a specific task and explain how the program handles that action (i.e. whether it solves the problem declaratively or imperatively).

Code on GitHub: https://github.com/MakeTheBrainHappy/ThePythagoreanTheorem-

Learn more about the author: www.makethebrainhappy.com