Program paradigm
The more complex a program becomes, the harder it is to work on it. So how do programmers deal with complexity? There are many general approaches that reduce complexity in a program or make it more manageable.
One of the main approaches is a programming paradigm. Let's dive into programming paradigms! The term programming paradigm refers to a style of programming. It does not refer to a specific language, but rather it refers to the way you program. There are lots of programming languages that are well-known but all of them need to follow some strategy when they are implemented. And that strategy is a paradigm. You give the computer little orders to do and it does them one at a time and reports back.
The paradigm consists of several statements, and after the execution of all of them, the result is stored. In an imperative programming paradigm, the order of the steps is crucial, because a given step will have different consequences depending on the current values of variables when the step is executed. To illustrate, let's find the sum of first ten natural numbers in the imperative paradigm approach. In the above example, we are commanding the computer what to do line by line. Finally, we are storing the value and printing it.
Procedural programming which is also imperative allows splitting those instructions into procedures. NOTE: Procedures aren't functions.
The difference between them is that functions return a value, and procedures do not. More specifically, functions are designed to have minimal side effects, and always produce the same output when given the same input. Procedures, on the other hand, do not have any return value.
Their primary purpose is to accomplish a given task and cause a desired side effect. A great example of procedures would be the well known for loop. The for loop's main purpose is to cause side effects and it does not return a value. To illustrate, let's find the sum of first ten natural numbers in the procedural paradigm approach.
In the example above, we've used a simple for loop to find the summation of the first ten natural numbers. OOP is the most popular programming paradigm because of its unique advantages like the modularity of the code and the ability to directly associate real-world business problems in terms of code.
The key characteristics of object-oriented programming include Class, Abstraction, Encapsulation, Inheritance and Polymorphism. Objects are instances of classes. Abstraction separates the interface from implementation. Encapsulation is the process of hiding the internal implementation of an object. Inheritance enables hierarchical relationships to be represented and refined.
Polymorphism allows objects of different types to receive the same message and respond in different ways. To illustrate, let's find the sum of first ten natural numbers in the object-oriented paradigm approach. We have a class Addition that has two states, sum and num which are initialized to zero. We also have a method addValues which returns the sum of num numbers.
In the Main class, we've created an object, obj of Addition class. Then, we've initialized the num to 10 and we've called addValues method to get the sum. Parallel processing is the processing of program instructions by dividing them among multiple processors. A parallel processing system allows many processors to run a program in less time by dividing them up. Declarative programming is a style of building programs that expresses the logic of a computation without talking about its control flow.
Declarative programming is a programming paradigm in which the programmer defines what needs to be accomplished by the program without defining how it needs to be implemented.
In other words, the approach focuses on what needs to be achieved instead of instructing how to achieve it. Imagine the president during the state of the union declaring their intentions for what they want to happen. On the other hand, imperative programming would be like a manager of a McDonald's franchise. They are very imperative and as a result, this makes everything important. They, therefore, tell everyone how to do everything down to the simplest of actions.
So the main differences are that imperative tells you how to do something and declarative tells you what to do. The logic programming paradigm takes a declarative approach to problem-solving. It's based on formal logic. The logic programming paradigm isn't made up of instructions - rather it's made up of facts and clauses.
It uses everything it knows and tries to come up with the world where all of those facts and clauses are true. The first line can be read, "Socrates is a man. The second line can be read, "X is mortal if X is a man;'' in other words, "All men are mortal. We can test the program by asking the question:. Prolog will respond " yes ''. Another question we may ask is:.
To give you an idea, John is Bill's and Lisa's father. The paradigm consist of several statements and after execution of all the result is stored. Advantage: Very simple to implement It contains loops, variables etc. Skip to content. Change Language. Related Articles.
Table of Contents. Improve Article. Save Article. Like Article. String name;. String emailid;. Objects respond to messages by performing operations, generally called methods. Messages can have arguments. A society of objects, each with their own local memory and own set of operations has a different feel than the monolithic processor and single shared memory feel of non object oriented languages.
One of the more visible aspects of the more pure-ish OO languages is that conditionals and loops become messages themselves, whose arguments are often blocks of executable code. In a Smalltalk-like syntax:. Many popular languages that call themselves OO languages e. Control flow in declarative programming is implicit : the programmer states only what the result should look like, not how to obtain it.
No loops, no assignments, etc. Whatever engine that interprets this code is just supposed go get the desired information, and can use whatever approach it wants. The logic and constraint paradigms are generally declarative as well. In functional programming , control flow is expressed by combining function calls, rather than by assigning values to variables:.
0コメント