Create a good design code (based on DelphiVCL)

zhaozj2021-02-08  222

East day documentation

We will write a lot of code for the company, for yourself or for your friends. Sometimes, in order to verify your own ideas, or learn a certain technology, you will write some experimental code. The life cycle of such a code is very short, and it is basically no maintenance, and it can be written freely. However, when you really want to complete a project, the code design is very important. Because such code is a long-term maintenance, constantly modify or enhance. Design messy code will make maintenance is very difficult or impossible, modifying such a code means that more bugs are generated or disaster.

Since the code design is so important, we can't ignore it. So, how to design a code? Object-oriented programming technology can help us. Here, inserting some questions, many programmers will confuse object-oriented programming (OOP) technology and object-oriented (oo) technology. As far as my own understanding, object-oriented technology is a profound understanding. It is a methodology or a world view, and object-oriented programming is only a way to use object-oriented when encoding.

The following is some of the relevant books and some experience they draw in daily practice, I hope to share with you.

First, let the interface code and function code are separated. One principle that needs to be kept in mind is not to write complex functional logic in the interface code. The implementation file of the interface form is only used to store the interface code, and the complex function code is independent. For a simple example, assume that you have a string list from somewhere, then displayed in TListBox, this code is worthy of respect: objectXxxx: = TobjectXXXX.CREATE; listbox1.items: = Objectxxx.getstringlist; Objectxxx.free This will be encapsulated in the implementation code of the complex logic of the list of this string to the TOBJECTXXX class, and the definition of this class can be defined and implemented in one. PAS file for easy maintenance. Separation of interface code and function code has another benefit. The implementation code of a function may be called by multiple interface modules. If you implement the code, you will copy a copy, then you will have multiple identical The module needs to be maintained. If you need to modify it, huh, it is almost impossible to ensure that you won't be wrong.

Second, let each module logic as simple as possible. Experience tells us that for too complex logic, it will bring difficulties. Therefore, as much as possible to make each module's code is simple, do not exceed 25 lines of code. When you find that the logic you wrote is tending to be complicated, then this time is the time you look for an object, see some logic you can independently.

Finally, you should pay attention to the name of the variable. You will find VCL source code, you will find that private member variables in the VCL class are headed by "F", and class names are in such rules such as "T". What is the benefit of this? When someone views such a code, once you see "F" start, you can know that it is the private member of this class, which is convenient for the maintenance of the code.

转载请注明原文地址:https://www.9cbs.com/read-2071.html

New Post(0)