Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand declarative and imperative code

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to understand declarative and imperative code". The content of the explanation is simple and clear, and it is easy to learn and understand. let's study and learn "how to understand declarative and imperative code".

Imperative programming

Imperative programming is the way we start with Assembly (1949) and continue in languages such as Cpene C + +, Cory Magi PHP, and Java. Process and object-oriented programming belong to imperative paradigm.

The code is based on statements that change the state of the program by telling the computer how to perform the action. In other words, the code is based on defining variables and changing their values.

This is an ideal programming example for people who tell machines how to think, who are no less naive than fetishes.

Process programming

Program code uses procedures to manage its structure. A procedure is just a set of operations that run in a specific order, and you can call them repeatedly without having to jump with the goto command.

# include int main () {int num1, num2, num3; int * p1, * p2, * p3; / taking input from user printf ("Enter First Number:"); scanf ("% d", & num1); printf ("Enter Second Number:"); scanf ("% d", & num2); printf ("Enter Third Number:"); scanf ("% d", & num3) / / assigning the address of input numbers to pointers p1 = & num1; p2 = & num2; p3 = & num3; if (* p1 > * p2) {if (* p1 > * p3) {printf ("% d is the largest number", * p1);} else {printf ("% d is the largest number", * p3) }} else {if (* p2 > * p3) {printf ("% d is the largest number", * p2);} else {printf ("% d is the largest number", * p3);}} return 0;}

The above C language example reads three digits, uses pointers, and flows through conditional logic drivers to determine the largest of the three digits. A process language like C can provide a very simple and efficient solution to meet application challenges.

Typically, you need to read each process from top to bottom to understand its role. The trend of process programmers writing spaghetti code may sometimes be criticized. But life is easy for any college student who doesn't rely on spaghetti and ketchup for dinner.

Examples of procedural programming languages are Pascal (1970) and C (1972). Process programming has strong support. Linus Torvalds, the father of Linux, has been open to criticism of C + + and object-oriented programming.

Object oriented programming

Object-oriented programming is to model objects with internal states. The programming code is then based on the relationship between these objects. In classification-based languages, objects are instances of classes.

The code in the object method is still imperative and based on the statement that modifies the state.

Public interface Retile {void walk ();} public class Turtle implements Reptile {@ Override public void walk () {System.out.println ("Turtle is walking!");}} public class Tortoise implements Reptile {@ Override public void walk () {System.out.println ("Tortoise is walking!");}} public class ReptileFactory {public Reptile getReptile (String reptileType) {if (reptileType = = null) {return null } if (reptileType.equalsIgnoreCase ("TURTLE")) {return new Turtle ();} else if (shapeType.equalsIgnoreCase ("TORTOISE")) {return new Tortoise ();} return null;}} public class ReptileDemo {public static void main (String [] args) {ReptileFactory reptileFactory = new ReptileFactory (); Reptile reptile = Reptile.getReptile ("TURTLE") Reptile.walk ();}}

The above is an example of a factory design pattern implemented in Java. Notice how all the code focuses on defining classes and using the relationships between them through interfaces. Typically, all of these classes will be separated into their own files.

Examples of object-oriented languages include Smalltalk (1972), C + + (1985), Python (1991), Java (1995), JavaScript (1995), Ruby (1995), C # (2000), Scala (2004) and Swift (2014).

JavaScript was originally an object-oriented language based on prototypes and without classes. Classes were added to ECMAScript 6 in 2015.

When learning programming, of all the examples, you are most likely to encounter object-oriented code. Although it is the most popular paradigm, it has been severely criticized because of its complexity, the difficulty of understanding design patterns, and the tricky state management throughout the application. At the same time, the object-oriented programming language is very mature and appreciated by professionals.

In JavaScript, the front-end framework Angular is a good example of C #, which has a great impact on developers who want to introduce stronger object-oriented principles into Web development.

Continue to use object-oriented programming in all projects. Because if not, all the people who only know this example will suddenly lose their jobs and live in cardboard boxes. And you don't want to take responsibility for it, do you?

Declarative programming

Declarative code is very common and is represented by domain-specific, logical and functional programming languages. These examples include HTML,SQL,F#,Prolog and Lisp.

Declarative code focuses on expressions of what to say without adding ways. For example, the HTML code tells the browser to display the tortoise image without displaying the browser image. Declarative code usually avoids sudden changes in states and variables.

Domain-specific code

Domain-specific languages are not Turing complete, which means they cannot do everything that other Turing complete languages can do. For example, C # (imperative) and F # (declarative) are Turing complete languages, and everything can be developed in one language or another. HTML is not Turing complete and is only allowed to do certain things.

Look up the SQL code example for employees and their managers in the database:

SELECT e.name, m.name FROM Employee e, Employee m WHERE e.Employee_id=m.Manager_id

Domain-specific languages are usually easy to write and read. Therefore, they are the most popular way of declaring user interface. For example, the JavaScript programming library React uses JSX to define components:

Const myComponent = props = > (Hello, {props.userName}})

Examples of domain-specific languages are SQL (1974), HTML (1993), CSS (1996), and XML (1996).

Logic programming

Logical programming is a declarative paradigm based on formal logic. A program is a set of logical sentences that express facts and rules about certain problem areas.

The most typical logic programming language for Turing is Prolog (1972). This means that everything that can theoretically be written in C can also be written in Prolog.

Prolog is based on predicates that define predicates that define the relationship between their parameters.

Food (salad). %

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report