oop - What is difference between functional and imperative programming languages? -
most of mainstream languages, including object-oriented programming (oop) languages such c#, visual basic, c++, , java designed support imperative (procedural) programming, whereas haskell/gofer languages purely functional. can elaborate on difference between these 2 ways of programming?
i know depends on user requirements choose way of programming why recommended learn functional programming languages?
definition: imperative language uses sequence of statements determine how reach goal. these statements said change state of program each 1 executed in turn.
examples: java imperative language. example, program can created add series of numbers:
int total = 0; int number1 = 5; int number2 = 10; int number3 = 15; total = number1 + number2 + number3;
each statement changes state of program, assigning values each variable final addition of values. using sequence of 5 statements program explicitly told how add numbers 5, 10 , 15 together.
functional languages: functional programming paradigm explicitly created support pure functional approach problem solving. functional programming form of declarative programming.
advantages of pure functions: primary reason implement functional transformations pure functions pure functions composable: is, self-contained , stateless. these characteristics bring number of benefits, including following: increased readability , maintainability. because each function designed accomplish specific task given arguments. function not rely on external state.
easier reiterative development. because code easier refactor, changes design easier implement. example, suppose write complicated transformation, , realize code repeated several times in transformation. if refactor through pure method, can call pure method @ without worrying side effects.
easier testing , debugging. because pure functions can more tested in isolation, can write test code calls pure function typical values, valid edge cases, , invalid edge cases.
for oop people or imperative languages:
object-oriented languages when have fixed set of operations on things , code evolves, add new things. can accomplished adding new classes implement existing methods , existing classes left alone.
functional languages when have fixed set of things , code evolves, add new operations on existing things. can accomplished adding new functions compute existing data types , existing functions left alone.
cons:
it depends on user requirements choose way of programming, there harm when users don’t choose proper way.
when evolution goes wrong way, have problems:
- adding new operation object-oriented program may require editing many class definitions add new method
- adding new kind of thing functional program may require editing many function definitions add new case.
Comments
Post a Comment