Code quality problems from the perspective of C/C ++ and Java language design

Source: Internet
Author: User
Tags multiple inheritance in c
The evolution of programming languages of different generations has attempted to solve the difficult problem of different Supplementary attributes. From the result of reflecting on the various problems of C ++ in Java, we can see the difficulties it is trying to solve. Java has a major feature: GarbageCollection (GC ). The garbage collection mechanism allows programmers to configure the required memory space without having to handle the memory space themselves.

The evolution of programming languages of different generations has attempted to solve the difficult problem of different Supplementary attributes. From the result of reflecting on the various problems of C ++ in Java, we can see the difficulties it is trying to solve.

Why does Java adopt the garbage collection mechanism?

A major feature of Java is the Garbage Collection mechanism (GC ). The Garbage collection mechanism allows programmers to safely configure the required memory space without having to handle the issue of releasing the memory space themselves, because the Garbage Collector (Garbage Collector) the system automatically determines the memory space used by the program and releases it.

Programmers who are familiar with C/C ++ programming languages know that the memory configured by themselves must be properly released, whether it is not released or released repeatedly, it will cause bugs in the program. A program designer configures the memory and releases the memory by himself. However, configuration, regardless of release, is another programming model.

Java inherits the C/C ++ lineage, but chooses another programming model that processes memory configuration and release completely differently. why? Before the advent of Java, the garbage collection mechanism already exists, but few mainstream programming languages were adopted, because of its performance problems. Because it takes a lot of computing to automatically determine whether or not each memory block should be recycled and recycled, it affects the program efficiency. The determination of Java inventors to replace efficiency with this garbage collection mechanism is a major decision. why? This is because there are far-reaching problems for programmers to configure their own memory and release their own program design models, and it is easy to breed bugs.

From the pointer and array, C/C ++

In addition to memory configuration and release, there are also many problems that have long plagued C/C ++ programmers, such as pointer problems.

Pointers are very powerful syntax elements in C/C ++. using pointers can achieve many magical effects and write very elegant program code. The use of pointers can be said to be an art in the C/C ++ language. However, unfortunately, the problem of memory configuration and release is more likely to become a hotbed of bugs. I believe that programmers with sufficient C/C ++ experience can understand the misuse and misuse of indicators, which can easily lead to bugs in programs and is quite imperceptible. The pointer allows you to point to any location in the memory, but once it points to a memory address range that is not accessible by the application, or the data pointed to in the memory is interpreted incorrectly, this will cause serious errors.

The array's bitwise access is similar to the pointer. This type of problem can even be exploited by deliberate operations to generate a buffer overflow attack. Such problems, as long as there is sufficient experience in C/C ++ programming, should be able to understand these problems that may cause in the programming process. They are not only prone to bugs, but also have a very high proportion of bugs, which will be hidden in the most obscure corner of the program, making it hard for you to find out.

How can we choose between strong effects and easy mistakes?

The C/C ++ program designer is extremely free of charge in terms of memory processing. The C/C ++ programming model for memory processing is very powerful, but it is easy for programmers to make mistakes, the programmer must spend several or even dozens of times writing the mental and time of the error to locate the error and correct it. This is of course a question worth thinking about. Do we need to put a very powerful program model in a language that can easily make mistakes by programmers?

Apparently, the inventor of Java chose not to do this. he would rather give up a powerful weapon than hurt himself while hurting the enemy.

This also reflects the design philosophy of the Java language in a wide range of aspects. it is better to remove seemingly elastic and seemingly powerful syntax elements and reduce the chance of program designers making mistakes. Therefore, Java will get a comment on "C ++. As a successor program language with the same blood source, it tries to cut down. It may be expected that it will be filled with a lot of reflection.

Therefore, we can see that the pointer is removed in Java and the reference is used to point to the object without the ability to point the pointer to any memory address. Once the program designer performs out-of-bounds array operations, the Java virtual machine throws an exception and cannot perform this operation. Similarly, programmers cannot access null reference values, which are controlled by virtual machines, throwing corresponding exception objects for application processing.

From the perspective of productivity, quality is the key

The above examples are all prone to mistakes in traditional C/C ++ programming. Java attempts to fundamentally eliminate these problems from the language. These language designs also reflect the basic philosophy of Java-reducing the chance for programmers to make mistakes.

Why does Java pay so much attention to this issue? This greatly improves the productivity of software development. In fact, as mentioned earlier, once a mistake is written, it takes several or even dozens of times to write the mental and time of the error, this error can be found and corrected. You can imagine how much impact this development has on productivity.

The amount of time spent writing program code may be one of the key to productivity, but the total amount of time spent on getting the program code of sufficient quality is the real productivity pointer shown at the end. We can say that Java is a program language that regards productivity as one of the highest guiding principles, and one of the important ways it wants productivity is to avoid mistakes made by programmers at the language level.

C and C ++ are prone to errors in programming languages. This is why Java requires C ++ and then subtraction.

For example:

Int a = 3; int B = 1; if (a = B) // if (a = B) {// execute some actions}

This method is valid in C or C ++. however, have you noticed that in the if conditional method, the program designer may have dropped the equal sign (=, he originally wanted to write the conditional expression a = B, but he did not write it as a = B, and this is still the C/C ++ program code that can be compiled, its semantics changes from the original condition that a is equal to B to the condition that B is assigned to a. if a is greater than 0, the condition is true.

I believe that all program designers who have written C/C ++ may make such errors, and they can all understand these errors simply because of errors in the input program code, but it may take you a lot of time to find it. In terms of syntax, this is a syntax that is available in the C era. it is very powerful and flexible, and can write very clever program code, however, there is still room for programmers to make the above mistakes. Of course, this syntax is no longer allowed in Java.

You can find more powerful and flexible syntaxes in C ++, and program designers can achieve amazing results by virtue of them. However, to use them, you must be more careful, cautious, or even have a better understanding of the language, and be more skillful in order to apply them to the fullest extent, it will make an uncorrected error. It is not difficult to correct itself, but to find out why and why.

Operations like subloads in C ++ are basically not available in Java programming languages, while multiple inheritance in C ++ also provides only the implementation of multiple interfaces in Java. From this perspective, you can say that Java is relatively weak, but this is to reduce the chance of program designers making mistakes by deliberately cutting the language elements that are prone to errors, even if the language power becomes weak, it may seem less clever, and it may take a lot of program code to do the same thing, and the program code may look stupid, but it is not easy to make mistakes, instead, it can increase productivity.

This is an important direction for Java to try to increase productivity.

Address of this article: http://www.nowamagic.net/librarys/veda/detail/172,welcome.

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.