What do young programmers need to learn most? Self-discipline!

Source: Internet
Author: User
What do young programmers need to learn most? Self-discipline!

Over the past seven and a half years, I have guided more than a dozen programmer interns at Ronimo game company and reviewed hundreds of resumes. I found that most of them need to learn one thing. You may think this is a certain technique, algorithm, mathematics, or other form of knowledge. Of course, they do need to make up for this knowledge, but in my opinion, these are not the most important. The most important thing they want to learn is self-discipline. This self-discipline is embodied in: writing code as clearly as possible; refactoring code to eliminate confusion caused by subsequent development changes; removing code that has never been used and adding comments.

Most of the time I guided interns not to explain advanced technology or engine details, but to let them write better code. I always ask intern applicants: What do you think is important to become a good programmer? Their answer is usually: the code should be clear, easy to understand, and easy to maintain. This is of course what I want to hear, but few young programmers can practice it all at once.

Self-discipline is required to do this, because this means that the code cannot stay "implemented ". If all the variables are named randomly, the code can still run perfectly, but the readability is poor. In the short term, there is little return from "functional code" to "clear code": the code can be run originally, and the code can still run after it is cleared. This is why self-discipline is required to complete this step, which is also helpful for internships: A good mentor will pay great attention to the quality of the code (although different people have different definitions of "good code"), so that the intern can further improve and improve the code to the next stage.

The following are several examples of problems that I often see in the code written by novice programmers:

Non-real-name functions/Variable/Class

What these functions, variables, and classes do is not what their names imply. these names are deceptive. Obviously, the name should reflect the actual content, but I am surprised that this situation often occurs.

For example, I recently accidentally saw two classes written by an intern: EditorGUI and EditorObjectCreatorGUI. this code is used to process the interface in the editor. To my surprise, the code of the button for creating a new object is placed in EditorGUI, while EditorObjectCreatorGUI is used to process operations between different objects, which is totally different from what the name implies! Although the code is relatively simple, it took me a long time to figure it out, because I made a completely wrong assumption based on the class name. The solution to this case is simple: rename it EditorObjectCreatorGUI and EditorObjectNavigationGUI. simply taking a small step can greatly improve readability.

The name is not accurate. This is because the code is constantly evolving. The name may be correct at first, but after the code is complete, the name may become inaccurate or even incorrect. This trap reminds us to keep names in mind. when you add a piece of code, we need to figure out whether it is commensurate with the name of a function or class.

Ambiguous class

Another problem is the ambiguous class, that is, a class does a lot of irrelevant things. This problem may occur when you focus on the same piece of code for a long time. The new features are implemented in the simplest way. To some extent, the class will become bloated and a lot of irrelevant things have been done. Sometimes the code size does not apply to the bloated class: a class may have only a few hundred lines, but it contains code that does not belong to this class function.

For example, if a GUI class needs to "analyze which textures are available" (imagine a button for selecting textures), if the GUI class is the only class that requires such analysis results, it is reasonable to implement it in the GUI class. However, a completely unrelated gameplay class also needs the analysis result information. Therefore, you pass the GUI class to the gameplay class to query the texture information. At this time, the GUI class has one more thing: it is a GUI class and also a TextureAnalyser class. The solution in this case is simple: a separate class is separated from the TextureAnalyser class, which can be used by both the GUI class and the gameplay class.

The best way to avoid this problem is to think twice before writing code: does the function I added here match the class name? If not, rename the class, divide it into independent classes, or place the code in other classes.

If you cannot find a name that matches the class, it is usually a Bad Smell ). If you cannot find a proper name to describe the class, it may be because it is too mixed. It can be divided into several parts, and each part is described by an appropriate name.

Bulky class

This problem is very similar to the class mentioned above: over time, more and more code is added to a class, making it bloated. In this case, although it is reasonable to put a class, the volume of the class becomes very large. Super-large classes are difficult to process. when many codes operate on the same private member variable, bugs are easy to occur, and many details are easy to ignore.

Splitting a super-large class is quite boring. This is also very challenging when the code is highly staggered. Code separation requires a high degree of self-discipline, because it only adds or modifies existing code and keeps the original functions unchanged.

Ronimo has a rule that keeps the class code below 500 lines and the function code below 50 lines. Sometimes this is not feasible or unreasonable, but in general, no matter which class or function exceeds this rule, we will find a way to refactor or split it into smaller ones, fragments that are easier to manage. (This makes me curious: how many lines do you think the limit should be? You can leave a message in the comment .)

Code comment

Almost all the sample code sent to us by the intern applicant has some annotated code blocks, but it does not explain why this annotation is made. Is there an error in the code that needs to be modified? Or do I need to update the old code? Why is the commented-out code here? When we ask the applicant, they are also confused about the annotated code, but the strange thing is that there will always be some codes that are commented out for unknown reasons.

Code already exists

Another problem I often see is that code with similar functions is repeated.

For example, the texture name may show the purpose of the object, such as TreeBackground. dds. To know whether the texture can be used for a Tree, we check the file name starting with the Tree. Maybe we can find it quickly after using the SDK, just use beginsWith ("Tree. This code is very short. if you need it, paste it there. This is code duplication, and everyone knows that code duplication should be avoided. if repeated code is short, the most attractive way is to copy and paste it directly. The problem here is obvious: if we want to check whether the texture is suitable for other things in the future, we need to make a bullet correction, one place and one place.

Generally, it is better to put the code into a function instead of copying it if it has special functions. Although the code is short and short, and calling a function requires more code than pasting, you must learn to do so, which also requires a high degree of self-discipline.

The topic discussed in this article is very simple, and most people have already learned it when they are in the first year of college. It's hard to remember from knowing these things to taking the time to follow them. This is why the most important thing learned by all people who have worked in Ronimo is not knowledge, but self-discipline.

Get the LAMP brother's original PHP video tutorial CD/detailed PHP Essentials edition for free. for details, contact the customer service on the official website:

Http://www.lampbrother.net

Http://yun.itxdl.cn/online/cto/index.php? U = 5 This is a CTO course of ox X

Http://yun.itxdl.cn/online/server/index.php? U = 5 mobile internet server development course

Http://yun.itxdl.cn/online/weixin/index.php? U = 5 development courses

Http://yun.itxdl.cn/online/yingxiao/index.php? U = 5 micro-marketing courses

Http://yun.itxdl.cn/online/phpcms/index.php? U = 5phpcms secondary development course


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.