We usually write a lot of code, for the company, for ourselves or for friends. Sometimes, in order to verify one of my ideas, or to learn
For a certain technology, some experimental code will be written. The life cycle of such code is very short and basically does not require maintenance. You can just write it as you like.
by. However, when you actually want to complete a PProject, code design is very important. Because such code requires long-term
Maintenance, continuous modification or enhancement. Messy code design makes maintenance very difficult or impossible. Modifying such code means
This will lead to more bugs or disasters.
Since code design is so important, we cannot ignore it. So, how do you design your code? Object-oriented programming techniques can help
Help us. Here, to make some digressions, many programmers confuse object-oriented programming (OOP) technology with object-oriented (OO) technology. At once
From my own understanding, object-oriented technology is a broad and profound knowledge. It is a methodology or a world view, while object-oriented technology is a kind of methodology or a world view.
Object-oriented programming techniques simply provide a way to use object-oriented programming when coding.
The following is the author's experience after reading some related books and daily practice, and hopes to share it with you.
First, keep interface code and functional code separated. One principle that needs to be kept in mind is not to write complex functional logic in the interface code.
Code in. The implementation file of the interface form is only used to store the interface code, and separate the complex functional code. To give a simple example,
Suppose you want to get a list of strings from somewhere and then display it in a TListBox. This code is respectable:
ObjectXXX := TObjectXXX.Create;
ListBox1.Items := ObjectXXX.GetStringList;
ObjectXXX.Free;
In this way, the complex logic of obtaining the string list is encapsulated in the implementation code of the TObjectXXX class, and the definition of this class can be
and implementation are placed in a .pas file independently for ease of maintenance. Separating interface code and functional code has another benefit,
The implementation code of a function may be called by multiple interface modules. If you copy the function implementation code where needed, then
You will have multiple identical modules to maintain. If you need to modify them, haha, it is almost impossible to guarantee that you will not make mistakes.
Secondly, make the logic of each module as simple as possible. Experience tells us that overly complex logic will cause difficulties in people’s understanding.
Disaster. Therefore, make the code of each module as simple as possible, generally no more than 25 lines of code. When you find that the logic you write is tending to
Complex, then this is the time for you to look for objects and see if you can isolate some of the logic.
Finally, pay attention to the naming of variables. Check the VCL source code often and you will find that the private member variables in the VCL class all start with "F"
Starting with, class names all start with "T" and so on. What are the benefits of this? When others look at code like this, once they see the "F"
At the beginning, you can immediately know that it is a private member of the class, which facilitates code maintenance.