(in advance, my answer is mainly to take into account the beginners of some of the ideas of the explanation, so please do not come to the big God to entwine I said not comprehensive and so on, too comprehensive for beginners can not digest)
Well, your problem can be extended to all programming activities, a beginner, how to judge their own code is good or bad, if the taste of the code from 0 to 10 points to score, and should play a bit?
First, on what is good code, in many cases is controversial, most of the language community has several simultaneous popular code style, and then in some details, there are countless differences, so a piece of code, give a look, may play 9 points, to B look, may only play 8 points.
So, is it completely impossible to judge? The answer is no, as in the example above, although A and B will give a different score, but I believe that a only 5-point code, B will not give up to 6 points. Basically, the difference in the taste of the code will only make sense in code that is more than 8 points.
So, how to judge? To be fine, you can write a big paper, but simply say, in fact, the main is two standards:
Readability and maintainability
The ability to cope with change. My head is stuck, I can't think of the word ... )
In the first case, not to say that readability is easy to read the code will be good maintenance, but in turn, good maintenance code must be easy to read, who can not read the code who can not maintain it? As for how to improve readability, how to improve maintainability, I do not bother to start here, a lot of articles in this talk. It is often difficult for beginners to write enough readable code I've had a couple of times to clean up hundreds of lines of code to twenty or thirty lines of simple code, which depends on the accumulation of experience, on the other hand, is also a work attitude problem, when you use hundreds of lines of code to solve a problem, Have you ever thought about it again, did you try to complicate the problem? As far as I'm concerned, in solving an unfamiliar problem, the beginning may open the brain hole, write down hundreds of thousands of code, but really finished, their own to this problem from the overall better grasp, usually the second step is to start simplifying the code. And for familiar problems, usually, the hundreds of lines of code will first flash in the mind, and so on when the editor open to write, you can start from the second step to simplify the code.
Second, the ability to cope with change, this is the most difficult for beginners to grasp, because in terms of readability, frankly, beginners really want to write very complex code, he himself is mostly clear, although not able to simplify the code, but "the code is very complex" the understanding is still there, but the ability to cope with change, This is often a matter of experience, for experienced developers, it is easy to anticipate the future direction of the code and prepare for it, and it is easy to find a reasonable balance between the up-front preparation and the development cost. But the biggest challenge for beginners is how do I know what this code will look like in the future? The second is how to achieve the best balance between changing the development cost and the reality. On the one hand is the accumulation of experience, on the other hand, also depends on your imagination, you have to keep asking yourself in the process of programming, this need will not change, I will declare this variable as an integer, there is no possibility that it will be in the business need to use a string to represent? I'm declaring an array here, is it possible that the array will become so large that there is not enough memory? Oh, on the front end of the main word of the code, can give a beginner common mistake to explain how you should imagine your code, such as you define a CSS class name is Red-ttile, the role is to color the font of the body to red, when you write down red-title, you should ask yourself , will I need to turn the title font green in the future? Is the time to change the red-title to green-title good or at the beginning do not define the color to class name up. This example is very simple, and most teaching CSS textbooks will also emphasize this, to the business object definition class name rather than the actual style to define, because I mentioned here, the ability to deal with change.
The final conclusion is that for beginners, how to judge their own code to write good? First, repeatedly ask yourself, is there any simpler way? Second, open the brain hole and imagine how your code can keep up with the situation after your needs change.