Answer questions: Discussion on professional code design principles

Source: Internet
Author: User

This is the topic that a netizen asked questions when I first got to work this morning. I personally felt that it was quite common. So I copied my QQ message and compiled it into an article.

This is not a big topic, but I think the design principles embodied by professional programmers in Coding are of great reference. You can refer to them.

Netizen 9:39:17
Class Memory: public NativeObject {
Public:
Memory ();
~ Memory ();
Void * malloc (long size );
Block * getBlock (long size );
Private:
Number * size;
Number * minisize;
Number * maxsize;
Std: vector <Block *> blocks;
};

Xiao 9:39:55
?

Netizen 9:40:06
I have defined a class like this, and blocks is not initialized in the constructor stage.
I now initialize blocks in the malloc function.
How can this problem be solved?
Initialization on the constructor should be Memory (): blocks (){}
But I don't want him to initialize blocks in the construction phase.

Xiao 9:45:03
You can put block initialization in malloc.

Netizen 9:46:43
Void * Memory: malloc (long size ){
Blocks = new blocks ();
Return NULL;
}
So?

Xiao 9:46:56
Maybe not.
There must be a bool volume.
If the initialization has passed, do not initialize it.
If (! Blocks) blocks = new blocks ()

Netizen 9:48:15
Blocks in your place should be a pointer.
I defined std: vector <Block *> blocks;

Xiao 9:49:04
Suggestions
We usually use pointers for nested Member objects.
Then explicitly remove the new
In practice, almost no member object entity is declared, because there are many additional problems.
We are used to controlling everything by ourselves, and seldom use the default constructor and destructor of C ++.

Netizen 9:50:43
I also think that the operations in the Destructor will be tangled.
Thank you, Mr. Xiao.

Xiao 9:51:48
The default constructor of C ++, and its construction time is uncertain
If two objects are dependent, and you write their object declaration in parallel
For example, if A depends on B, you declare:
A;
B B;
After the C ++ compiler is compiled, it is uncertain which one should be executed first.
If A is executed first, B will crash because it is not initialized.
The cause of this crash is generally not found, because this is the code added by the compiler, you do not see
This problem occurs with VC ++.
It is reflected in the well-written program. It may crash forever after a compilation, and it has always been good before.
The cause cannot be found.

Netizen 9:54:25
Understand
I have a usage problem.
Let me ask another question. I want to mark a unique string for my own defined class.
I want to find the classes I have defined using strings
And instantiate the object
What should I do?

Xiao 9:56:15
Why?

Netizen 9:57:13
I tried to make a simple configuration that can be performed outside the program to generate some objects.

Xiao 9:57:54
This is a pseudo-proposition. Although it looks cool, you are involved in a high-level application of dynamic class queries during runtime. I was fascinated for a while when I was young, and it feels cool.
However, it is not practical. According to the modern distributed computing theory, there are many ways to avoid this problem in practice. At least I have never used it in practice.
This is not required
C ++ is a little difficult to solve this problem independently, but it is easy to rely on dll, so, and other operating system resources. You can check the COM interface for dynamic query and enumeration, this is what we do.
It is recommended that you do not drill this tip, it is very effort-consuming, not helpful for making money

Netizen 10:00:35
The fourth point is my tangle.
But if I am not powerful enough, how can I make money?
So I don't know how to balance it.

Xiao 10:01:11
At least this is useless.
According to the theory of cloud computing and Distributed Server Clusters, computing is a service. Now we are generally trying to make computing an independent network service, instead of following the standalone model.

Netizen 10:01:53
In fact, I first try to create an OO

Xiao 10:02:00
How to dynamically instantiate an object to implement built-in computing
OO needs to be used in future models for a limited amount of time.
C ++ is currently not highly practical. It is recommended to learn more C
C is the root

Xiao 10:03:21
STL should not look at it again. It is harmful. Even if you have learned it, the first multi-threaded environment will all be finished.
STL is single-threaded with no multi-thread security

Netizen 10:03:54
I designed the expected OO to be single-threaded.
Like Javascript
A cool syntax is:
Devices. [has (child )? This: void]?. [*. Contains (module )? Void: module];

Xiao 10:04:16
Which other single-threaded programs are there?
Don't be so cool. You have to cry.

Netizen 10:04:40
In this way, two cycles are implemented.

Xiao 10:04:51
If something is wrong, how can I debug it?

Netizen 10:04:51
Saves 3 to 4 intermediate Variables

Xiao 10:05:10
Willing to waste more computer resources, not the mental power of programmers
You have racked your brains to save 3 ~ Four intermediate Variables
Save about 12 ~ 16 Bytes
In today's computers, the memory starts at 2 GB. Is it meaningful?
On the contrary, if there is a hidden bug in the middle, it may take half a month to check the error.
You can buy another computer with half a month's salary.

Xiao 10:06:47
Don't be too technical to do things. If you calculate an economic bill, you will know that you should write a few more variables and write a few more lines of the program. You can understand it, others can understand it, and computers can understand it. This is the most valuable thing.
Even if someone else looks at your code, at least I have to worry about it. Well, it will take a whole morning.
What is my morning salary?
Is it worth more than 16 bytes of memory?

Netizen 10:07:52
Maybe I think too much.

Xiao 10:08:09
You don't want to think too much, but the idea is limited to schools.
I always want to write cool code and don't want to write profitable code.
Cool code is often costly and highly risky. Once more code is iterated, no problem can be found.
The final result is that it is recognized that the code you write may be too advanced and you do not understand it, the quality is not good, and you cannot cooperate.
Finally, the boss had to ask you to walk.
So I have been writing a program for a long time. Generally, the more simple the program is, the more vernacular it is.
I 'd rather write two loops
Even, I will split it into two functions, each of which has only one loop.
This is clear.
Efficiency problems
When you talk about the loop, it is estimated that the loop search is needed, right?

Netizen 10:10:46
Hmm

Xiao 10:10:55
If the set is large enough, for example, tens of thousands
How do you know the internal algorithm used by the compiler for search?
If I want to control it, I can configure two Hash
One-Step high-speed Retrieval

Netizen 10:11:27
This kind of circulation method is definitely not made of gold.

Xiao 10:11:35
This is more efficient than any other algorithm.
Never trust the Compiler
It does not know your needs
Split the action into several optimization points.

Netizen 10:12:46
What you are talking about is quite rational. I will digest it.
In terms of technology and making money, I have to think about it.

Xiao 10:13:55
Well, it's a big topic to be a commercial programmer.
 

This article from the "Xiao blog" blog, please be sure to keep this source http://tonyxiaohome.blog.51cto.com/925273/969689

Related Article

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.