In computer science, imperative programming is a programming paradigm that uses statements that change a program's state.
Imperative programming focuses on describing how a program operates.
is a programming paradigm [...] that expresses the logic of a computation without describing its control flow.
Many languages that apply this style attempt to minimize or eliminate side effects by describing what the program must accomplish in terms of the problem domain, rather than describe how to accomplish it as a sequence of the programming language primitives (the how being left up to the language's implementation). This is in contrast with imperative programming, which implements algorithms in explicit steps.
don't use unrestrained GOTO (Robert C. Martin)
aimed at improving the clarity, quality, and development time of a computer program by making extensive use of subroutines, block structures, for and while loops—in contrast to using simple tests and jumps such as the go to statement which could lead to "spaghetti code" causing difficulty to both follow and maintain.
Functional programming is a style of programming which models computations as the evaluation of expressions. This article is meant to describe it briefly; however, the best way to understand functional programming is to learn the basics of one of the functional programming languages
Very quick TLDR : avoids shared state & mutable data, functions always returns a value & a function with given args will always returns the same value.
don't use assignment (Robert C. Martin)
No assignment means var are immutables.
a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions or declarations instead of statements.
N.B. : An expression is not a statement but a valid unit of code that resolves to a value. Not all statements resolve to a value.
OO makes code understandable by encapsulating moving parts. FP makes code understandable by minimizing moving parts.
Functional programming (FP) helps you write robust, powerful, and maintainable programs well suited to multicore and cloud computing by focusing on controlling state and effects.
Functional Programming Principles
Own Your Context with Explicit State Some people call it “stateless”, but I prefer “explicit state”. Instead of having data implicitly “somewhere over there” and reading that (implicit & shared) data while in the body of a function, simply pass all data that the function needs explicitly as arguments.
Limit Side Effects This is the output side of explicit state. As much as possible, avoid talking to the world outside of the function, except for the final return of the function.
Referential Transparency When you limit yourself to simple input/output for every function (with no changes to the world outside it), you can swap out a function for its return value.
Composition With (top-down) inheritance, you specialize a general class into more specific subsets. For example, a general Car class can be used to make more specific SportsCar and Minivan classes. This works well for a certain problems, but fails when you want to mix and match functionality. You can quickly end up with an endless regress of more and more general classes, and huge messaging interfaces between different parts of your application. With (bottom-up) function composition, we take small, single responsibility functions, and combine them with other functions to build up larger, more complex behaviours. We do this either by passing the result of one function as the argument of another, or by calling them in sequence (if we’re side-effecting).
Partial application means taking a function with n arguments and returning a new function with n-i arguments (given i<n), where the i arguments are bound to the new function.
const add = (a, b) => a + b; const add5 = _.partial(add, 5); // now `a` is bound (locked-in) to 5 const add5 = (b) => 5 + b; // this is what it looks like inside add5(3); // equivalent to invoking add(5, 3);
Function currying is the process of successive partial applications, until the last argument is given at which point the result of the function is returned.
const add = (a) => (b) => a + b; add(1)(2); // Repeated partial application.
A composed version of:
const add8 = (x) => add5(add3(x));
Could be written with more visual clarity using composition:
const add8 = compose(add5, add3);
TLDR : Build functions with partial application at the root level of your app (here is a concrete example)
don't use pointers to functions (Robert C. Martin) (He is talking about pointers in C++)
based on the concept of "objects", which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. A feature of objects is that an object's procedures can access and often modify the data fields of the object with which they are associated (objects have a notion of "this" or "self"). In OOP, computer programs are designed by making them out of objects that interact with one another.
TLDR : imperative versus functional === control flow versus data flow
For example, in imperative programming variables and loops are common when handling state, while in functional programming the state is handled via parameter passing, which avoids side-effects and assignments.
TDLR : If logic evolves more then the model : choose FP and if model evolves more then the logic : choose OOP
OOP is good when you have a fixed set of operations on things. Your code evolves, you primarily add new things. Adding new classes which implement existing methods (operations).
FP is good when you have a fixed set of things Your code evolves, you primarily add new operations (functions) on existing things (data types).
When evolution goes the wrong way, you have problems (The Expression Problem : Philip Wadler 19981112):
Adding a new operation to an OOP program may require editing many class definitions to add a new method.
Adding a new kind of thing to a FP program may require editing many function definitions to add a new case.
Functional languages excel at manipulating symbolic data in tree form.