Emotion: Write solid code

Source: Internet
Author: User
After analyzing a project code of the company, I was very frustrated. The project analyzed contains about 500 files, which is not very large. I admit that programmers in this project are well-trained. However, they are all well-trained in C language, not c ++.
Why do we choose C ++ as the development language of a software project? I don't think C ++ is more complicated and gorgeous, nor C ++ supports Oo and ob, nor C ++ is the best choice to support GP. In terms of binary compatibility, C ++ is inferior to C. In terms of the benefits of OO support, C ++ is inferior to Java and C # ----- C ++; maybe GP is a good choice, but most programmers are not well-trained to master GP. Of course, I cannot predict why others choose C ++. For me, there are only two reasons for choosing C ++: 1 is a legacy project of C ++, 2. It requires a superior expressiveness of C ++.
However, C ++ is full of traps and customs, and those who do anything will be punished. Even an avid tech follower like me is not more important than writing robust code-because I am a real software engineer first. For all c ++ programmers, there may be no more important book than Objective C ++. Every C ++ programmer should be familiar with this book. Today, we also have "C ++ programming specifications" as our programming guide. As I have said before, it is worth our daily reading of these 101 pieces on the display. I would like to emphasize the two books with no annoyance. They are the bible of all programmers who intend to use C ++ for engineering activities.
For a long time, we have not paid enough attention to code security-the security function of MS is not what I call security code. We must increase the code security to a very important level: security is an integral part of correctness. It may be sensational, but I am more and more confident that this view is completely correct.
Briefly review the serious defects in the project.
The default behavior of copy ctor and assignment is ignored. In an embedded reference counting design, when an object is copied, the reference counting part is also simply copied, which is obviously incorrect. EC emphasizes how to pay attention to copy ctor and default assignment. Such errors can be completely avoided.
The reference count is accompanied by the smart pointer design. It must be realized that writing a smart pointer with industrial strength is very difficult, even if we don't need a general design, achieving a dedicated, robust, and secure smart pointer is not a simple task. There are some problems, even the implementation of Boost: shared_ptr or Loki. smartptr is flawed-Of course, it may be that I am ignorant-how to implement a secure const pointer? What semantics is the best?
The problem of dynamic array allocation. A large number of new type [size]... Delete [] P; code. How can this Code ensure exceptional security? A lot of try... catch makes code a mess. Why not use tools like boost. scoped_array? Even if you don't want to rely on third-party libraries, it's just a dozen lines of code to implement a simple scoped_array!
Looking back at STL, there must be a pair of operations, with only four member functions of allocator. All containers, algorithms, and iostream families do not have functions that must be operated in pairs. In fact, with raiI, we should encapsulate almost all pairing operations-do our best! My so-called pairing operations mean operations that must be executed in pairs, otherwise they cannot be correct, such as new/delete, new []/Delete [].
(Continue)

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.