Talk about the programming paradigm

Source: Internet
Author: User
Tags ocaml

Programming languages have many genres and ideas, and some programming languages support multiple programming paradigms at the same time.

Static type programming paradigm

In a programming language with statically typed programming paradigms, the variables need to be explicitly specified types. Representative language: C,c++,pascal,objective-c,java,c#,vb.net,swif,golang.

The benefits of doing this are:

1. The compiler can identify type errors at compile time.

2, the compiler will know the type information when compiling, you can improve performance.

This paradigm holds that programmers must know the type of variable, and if you don't know the type of the variable, don't mix it up! At compile time, the program will error.

Both Swift and go languages are statically typed programming languages, but they do not need to specify the type explicitly, but can be inferred by the compiler to automatically determine its type.

Dynamic type programming Paradigm

A programming language with a statically typed programming paradigm, whose variables do not need to be explicitly specified. Any variable that can point to any type of object. Representative language: Python,ruby,javascript.

The philosophy of dynamic type can be summed up by the concept of duck type (English: ducktyping). Jameswhitcombriley's duck test can be said: "When you see a bird walking like a duck, swimming like a duck, and barking like a duck, the bird can be called a duck." ”

This paradigm holds that programmers must know the type of variables and the methods and properties that they support, and if you don't know the type of the variable, don't mix it up! The run-time program will crash! Who's the program crash? Blame yourself, you are not a qualified programmer!

The benefits of a dynamic type are:

There is no need to explicitly define interfaces and abstract types. As long as a type supports the required methods and properties, then OK. The program will be quite flexible and simple. C++,java,c# as the lifeblood of the interface/base class, in dynamic language here as nothing!

The disadvantages are:

1, if the type is not correct, the compiler can not find the error, but the run-time program crashes.

2. Because the compiler does not know the type of the variable, it cannot optimize performance.

Object-Oriented Programming paradigm

The object-oriented programming paradigm began to emerge from the late 70. It supports instances of classes and classes as modules that encapsulate code. Representative language: Smalltalk,c++,objective-c,java,c#,vb.net,swift,go,python,ruby,actionscritp,ocaml.

Early programming languages are process-oriented. is the order, condition, loop, form a function. As the size of the code grows, it is found necessary to modularize the code. A concept corresponding to the code is placed in a file, which facilitates concurrent development and code management.

The law of "program = data structure + algorithm" has also been found. Therefore, a concept corresponding to the data structure and function should be placed in a file. This is the concept of classes.

The object-oriented programming paradigm, which does greatly improve the production efficiency, has been widely used, so it is very popular to support the object-oriented programming paradigm in language level.

Although the C language does not support the object-oriented programming paradigm at the language level, modern C language development applies object-oriented modular thinking, putting the same class of data structures and functions in a file, using a similar naming method.

After all, C doesn't support object-oriented at the language level, so there are a lot of programmers who want to add object-oriented support to the C language. The representatives are C + + and objective-c.

C + + is a new language, but most of the language elements are compatible with C.

The OBJECTIVE-C is fully compatible with C. Objective-c is adding a thin layer of syntactic sugar to C to support interfaces (that is, classes in other languages) and protocols (that is, interfaces to other languages). Even the first implementation of OBJECTIVE-C is a C-language precompiled compiler. Objective-c Frankly, the object-oriented system design is quite subtle except that the added syntax does not conform to the C-flow. Mr Jobs's early eyes on the beads objective-c the capsule because it was closed in the apple/nextstep system, so few people knew it. With the popularity of iOS systems, Objective-c has only renowned in recent years.

Functional Programming Paradigm

The functional programming paradigm is a programming language invented by mathematicians who think that a program is a mathematical function. Representative language: Lisp,erlang,javascript,ocaml,prog.

Many of Daniel's most powerful advocates of functional programming languages have been highly revolutionary. But I think they overestimate the power of the functional programming paradigm, and I don't think the functional programming paradigm is as clever as the object-oriented programming paradigm.

Functional programming languages, the core is functions, they do not have the concept of class. But its function is not the traditional process-oriented language function, its function supports the concept of "closure".

In my opinion, functional programming language functions, that is, "closures", plainly speaking, is actually "class". The development of programming language to today, is the need for modularity, is the need to "data structure" and "algorithm" together. No matter what language, do not combine them in the programming way, there is no way out.

Object-oriented programming language, using classes to combine "data structure" and "algorithm". The core of a class is the "data structure", which is its "attribute", not "algorithm", its "function". In a class, a function is attached to a property.

In the functional programming language, the "Data structure" and "algorithm" are combined with closures. Is that the function is able to fetch external fields. Is "attribute" attached to "function".

"Class" is essentially equivalent to "closure". Many object-oriented programming languages now include support for closures. Observing their code, we can see that they are actually using "classes" to implement "closures".

Who is easier to use for "classes" and "closures"? is obviously "class".

"Closures" are more concise, so "closures" are often used in object-oriented programming languages to replace anonymous classes. A class with only one function, written as a class, is too cumbersome to be written as a closure and more concise.

Spit it out. OCaml language, the predecessor of the CAML language itself is a very good functional language, abruptly added a complete set of object-oriented mechanism, while supporting object-oriented and functional programming paradigm, it is easy to like C + + brain crack.

There are also many object-oriented language controls that look at JavaScript wearies and always want to add object-oriented support to JavaScript. ActionScript is one of those attempts. I've used it, and it's really not much different from Java.

Then spit out the ExtJS. ExtJS and jquery were compared when choosing the Web front-end development framework.

ExtJS is obviously developed by Java experts, abruptly with JavaScript to simulate the design of swing, a set of UI library.

JQuery developers clearly understand the functional programming paradigm of JavaScript, creating a UI library based on the features of JavaScript's dynamic functional programming language, instantly killing extjs in seconds.

From the ExtJS and jquery stories, we can see how important it is to have multilingual programming capabilities. ExtJS's author is proficient and loves Java, so he used the scalpel JavaScript as a hammer Java to make, a thankless.

Functional programming language, there are some tips such as tail recursion. Tail recursion can be used without stacks, preventing the stack overflow when recursive calls.

Template Programming Paradigm

Template programming, that is, the type as a parameter, a set of functions can support any number of types. Representative Language: C + +.

The need for template programming is invented when developing a container library in C + +. Because containers need to hold any type of object, there is a need for generics.

The template programming of C + + is to create the corresponding type code at compile time according to the usage in the source code. In addition to the C + + approach, java,c# has a similar mechanism called generics, but they are implemented in a different way from C + + templates. Their compilers do not generate new code, but instead implement them in a way that enforces type conversions.

In a programming language without templates/generics, how do you store objects in a container? The object that accesses the public base class type (java,c#), or the void* pointer (C), can be removed by forcing the type to be cast to the actual type. Dynamic type language, do not care about the type, it does not matter, any object directly into the container to throw in, take out the direct use can.

Some C + + experts in the template based on the "template meta-programming." Because the template programming, is the C + + compiler to get it done, template meta-programming is to let the compiler operation, compile the results also even out. I don't know what's the use of this thing besides research and dazzle.

Summary

Whether a language is worth learning , I think there are several criteria:

1, whether to use, you have to learn, so no doubt. After all, we all have to eat.

2. Does the language feature give you a refreshing feeling? If it is, it will be worth the price. If the go language is out of the ordinary, return multiple values instead. I thought so. I've actually been active for years. Because, I think since C does not support the exception also live very well, why need an exception? The error code is returned. Irreparable error, the direct Abort program can be! Moreover, exceptions are actually violations of process-oriented programming principles. A function should have only one entry for an exit. Throw out the exception is more export.

3, whether or not good at a certain area. If you have only one hammer in your hand, you can only hammer all the tasks as nails. But if there are many tools in the toolbox, it is much easier to face different tasks.

Talk about the programming paradigm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.