Old code farmer: How to write code that satisfies you

Source: Internet
Author: User
Keywords Code farmers

Intermediary transaction http://www.aliyun.com/zixun/aggregation/6858.html ">seo diagnose Taobao guest cloud host technology Hall

Today, a friend asked me this question on Weibo:

"@ Old code agricultural plots, recently for the purpose of learning to write a management system, more to the back, the more I feel that the code in front of you rotten." Senior, I want to let the code write better, can you talk about your experience, give me some pointers! "

I have just written a few words in the reply, I realized that 140 words difficult to put my ideas clearly, in the spirit of reserve Better-without, I decided to write my answer into a blog post.

The first thing to say is that I am writing this blog does not mean I think I write the code how good. In fact, I know my level, as an application system programmer, and those who do the framework of the system is not a level of Daniel. And even in the application level, my level is probably only two or three flow, just because love programming so have been working hard, but anyway, can do their favorite work I have been very satisfied. So I doctored the question a little bit, focusing on the point of "writing poorly in front of my own code", how the friend asked how to "make the code a little better" and changed it to "How to write code that satisfies you."

To get to the point, my own experience is that writing code is like writing a composition, beginning to write the process of thinking is the most critical. I remember high school, a language teacher taught me the experience is, at least spend one-third of the time to conceive, and repeatedly consider the central idea, the main ideas of the paragraphs, the context of the article, the major rhetorical devices, and so on. Think of all these elements and write them down to coherent.

I think writing code is the same, thinking is the most critical. Assuming that the technology platform, framework, tools, etc. have been identified, spend more than one-third of the development time to consider all the data structures and their interrelationships before you start writing. For example, you need to define several classes, how the relationships between classes and classes are, what attributes are in each class, what methods each class provides, and so on, which are the core. These data structures need to be considered as thin as possible, such as functional implementations may be fine, but performance is not ideal, which means that your data structure design needs to be improved. These details should be reconsidered and tested until they are thoughtful. On this basis, pay attention to the implementation of the details, test cases, code readability, you should be able to write their own satisfaction with the code. The details are as follows:

1. Data structure and core algorithm

About the importance of data structure, great God Linus Torvalds said so, I feel very agree: "Bad programmers worry about the code." OK programmers worry about data structures and misspelling relationships. (Low-level programmers always consider code, and high-level programmers always consider the data structure and its relationships)

The data structure is well considered, and the core algorithm comes out naturally, which is the question of how each method of each class is implemented. For example, you need to implement a median query method, if you previously determined that the format of data preservation is a list, then you can consider the insertion of the sort method; If the data format is a self-balanced binary sort tree (AVL), then simply return to the root node directly.

Data structure determines the algorithm, so when you consider the data structure, it is important to make the structure of the data and its natural properties match, otherwise the implementation will be a nightmare. For example, you define a multi-level structure as a two-dimensional array, it seems to be the same as the maintenance of an organization chart in a table, but when you do the department increase or decrease, this level of the array elements moved from needless to say, the following levels of elements move easily chaos, and poor performance, Maybe you wrote 2000 lines of code and there are many boundary conditions that can go wrong. Conversely, if you use a child sibling list to represent this tree structure, it is very easy to operate, and maybe 100 lines is enough.

2. Function realization

After the idea is determined, the implementation process also needs a lot of conceptual activities. Encounter you more familiar with the field of experience, you can naturally, but inevitably there are some unfamiliar with the technology you need to try. Some students are more exclusive of this field, such as I finally mastered struts 2, leadership and let me go to learn grails framework, I would feel very uncomfortable, probably looked at it to pick out a bunch of problems, and then can hide how far away. However, I would like to say that such a mentality will hinder themselves to continuously improve the level of technology. As a programmer, the biggest challenge is also the greatest pleasure, is to constantly learn new technology, no such mentality, will soon fall behind.

Well, what about unfamiliar technology? My experience is, don't rush to implement the code in the project, in addition to maintain a test project, in the inside of the search document while learning, side to do a small function, all need to implement the function in the project run through the test project, and then write the project code. The advantage of this is to isolate individual technical issues from other potential bugs and to quickly learn new technologies. Otherwise, when you write code directly in the project, it takes several times more to determine the source of the problem.

3. Test

Testing is important, and designing test cases is as critical as designing data structures at development time. When you design a test case, forget all the ideas that you designed for your data structure at that time, or find someone else to design the test case, or you will automatically test the places you have taken into consideration. This test is run through, users may use a variety of boundary conditions will be everywhere problems.

Some people would recommend TDD by designing test cases first and then ensuring that all tests pass during the development process. I personally do not like this approach, although it is acknowledged that TDD is necessary from the point of view of development quality management and long-term maintenance, but the result of my personal attempt is that after designing the test case, the goal of development is not to implement the function, but to run through the test, it is no fun to say. I feel conflicted about that.

Writing here I also think of the great god Linus said another word: "Regression testing" What's that If it compiles, it is a; If it boots up and it is perfect. (regression test?) What is this thing? If the code compiles, it's good, and if it starts, it's perfect. )

Of course, the great God level is there, he has capital supercilious, I really do not deserve to imitate. But I still think TDD has a problem with TDD, and testing is important, but putting it to the height of the drive development is a bit of the cart before the horse. This is my own point of view, I do not understand TDD in-depth, if there are errors, please advise.

4. Code readability

To be satisfied, the code must be readable. It's easy to see what you're thinking and realizing when you get the code you wrote a year or even a few years later. This involves naming and commenting issues.

The name is like the commodity label in the supermarket, want to let the person at a glance know what this is a thing, for example your employee class has two attributes are to post Date and date of separation, define them as Date1 and date2 is not much readability, The definition of Dateonboard and Datequit is quite clear.

Annotation is also important, it can be used to illustrate the role of a piece of code, the design of the algorithm, or the method call parameter format requirements. Some people think that naming is a comment, and the code itself speaks for itself. I think it's good to emphasize the importance of naming conventions, but it's not biased to say that you don't need annotations. Just imagine, if Dijkstra first invented the shortest path algorithm, he gave the code does not have a line of comments, even if all the variable naming is accurate and rigorous, how many people can read his algorithm? So, in important or complex places, you need to write some notes in detail, People who see the code clearly understand your ideas.

Finally, summarize: to write their own satisfied with the code, first of all, do not rush to start, to carefully think about the idea of things, especially the data structure, and then boldly try to verify the implementation process, design test cases to ensure the readability of the code, you can display the highest level of their own. But after all, the level is different, and their satisfaction is not equal to other people appreciate. My view on this is, not to be satisfactory, but I am satisfied with the heart, enough. Finally, the technical level can be improved slowly, but good programming habits need to form from the beginning, it will make you on the way forward with less effort, the benefit of life.

"Update" The uncertainty of the requirements mentioned in the comments leads to design and implementation difficulties, I wrote another article on the needs Analysis: "On the analysis of the needs of some experience," welcomed the criticism.

The Writer: bole online-old code agriculture

This article link: http://blog.jobbole.com/47966/

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.