HackerNoon.com
Published in

HackerNoon.com

When Procedural Is Better Than Declarative Programing

Procedural programming allows you to reason through complex processes, which can help you solve otherwise unapproachable problems, even if more work is required.

Puzzle 1: The Impossible Bottle

The Impossible Bottle Puzzle

Puzzle 2: Thor’s Hammer

Only those worthy can wield the hammer of Thor

So What’s The Difference?

Declarative Programming Hides Process, Reveals Relationships

functions are simply mathematical maps between inputs and outputs
-- zip
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
-- recursive
fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
n = 37
main = do
print "zip lists"
print (fibs !! n)
print (fibs !! (n+1))
print (fibs !! (n+2))
print (fibs !! 2000)
print ""
print "recursive"
print (fib n)
print (fib (n + 1))
print (fib (n + 2))
-- edited Nov 2018

So the infinite list of Fibonacci numbers can be calculated by prepending the elements 1 and 1 to the result of zipping the infinite list of Fibonacci numbers with the tail of the infinite list of Fibonacci numbers using the + operator.

#include <stdio.h>
int fibonacci(n){
int a = 1;
int b = 1;
int i;
int tmp;
for(i = 0; i < n - 1; ++i){
// swap a and b
tmp = a;
a = b;
b = tmp;
// add a and b
a = b + a;
}
return a;
}
int main(){
printf("\nfibonacci(0) = %d", fibonacci(0));
printf("\nfibonacci(1) = %d", fibonacci(1));
printf("\nfibonacci(2) = %d", fibonacci(2));
printf("\nfibonacci(3) = %d", fibonacci(3));
printf("\nfibonacci(4) = %d", fibonacci(4));
printf("\nfibonacci(5) = %d", fibonacci(5));
printf("\nfibonacci(6) = %d", fibonacci(6));
printf("\nfibonacci(10) = %d", fibonacci(10));
printf("\nfibonacci(20) = %d", fibonacci(20));
printf("\nfibonacci(40) = %d", fibonacci(40));
printf("\nfibonacci(50) = %d", fibonacci(50));
printf("\nfibonacci(100) = %d", fibonacci(100));
printf("\ndone");
printf("\n");
return 0;
}

Declarative Programming is Great For Common Tasks Where You Need Help With The Details

Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne, 100, 200);
Rectangle rectTwo = new Rectangle(50, 100);

Procedural Is Better When You Care About The Steps

Final Thought: Procedural Programming Gives You More Control, But Is it Worth It?

--

--

Elijah McClain, George Floyd, Eric Garner, Breonna Taylor, Ahmaud Arbery, Michael Brown, Oscar Grant, Atatiana Jefferson, Tamir Rice, Bettie Jones, Botham Jean

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store