Write code like a Architect

Source: Internet
Author: User
To some extent, writing code and writing articles are the same, requiring logic, architecture, and as concise as possible. As we have said before, the creator's schedule is different from the manager's schedule. coding and writing articles are a lonely process that cannot be disturbed. facing the screen, it is a process... "> <LINKhref =" http://www.php100.com//statics/style/headflo

To some extent, writing code and writing articles are the same, requiring logic, architecture, and as concise as possible. As we have said before, the creator's schedule is different from the manager's schedule. coding and writing articles are a lonely process that cannot be disturbed. facing the screen, it is a war between yourself and yourself.

Similarly, if the architect's final finished product is a building, the programmer and software engineer's final product is software. Prior to actual construction, the architect will present every detail of the building on the blueprint. Only programmers and software engineers do not. Maybe that's why the House rarely collapsed, but the software often crashed?

The design blueprint will help architects determine that their design is feasible. "Feasible" is not just to ensure that the building will not collapse, it also means that the building can meet the intended goal of serving people. A customer or developer also uses a blueprint to understand a designer's ideas and what he is going to do.

In contrast, many programmers did not even have a rough framework before they started writing code.

Most programmers think that it is meaningless to do anything that cannot directly produce code. Thinking cannot be directly converted into code, but it makes no sense to rush into code words before there is no overall framework. Before programmers start writing code, they should fully understand the final functions of the code. The process of understanding naturally needs to be considered. writing down the thinking process is a very time-consuming task for programmers.

But the cartoonist Dick Guindon once said: Writing is the best way to find out how bad your thoughts are.

The blueprint helps us understand the architectural architecture. Similarly, before we begin to write code, we also need a similar "blueprint", that is, "annotation ).

"Comments" cannot generate code directly, so they are ignored by many programmers. Without "comments", it is as if the construction contractor directly goes into battle without the designer's drawings.

Some people may say that it is not reasonable to compare programmers with architects. It is difficult to split the wall and rebuild it, but it is relatively easy to delete and rewrite it. Therefore, the programmer can write it first and modify it if he is not satisfied.

This idea is wrong. Why? BecauseThe debugging process is also time-consuming..

I have also developed some programs recently. this process requires a very clear understanding of the program architecture itself. It took me nearly a day to understand the operating mechanism of the entire program, and if there is a comment, it may only take five minutes.

To avoid introducing bugs, I need to understand the possible results of any small adjustment. Without comments, it takes me a long time to understand the meaning and functions of each code. Especially for thousands of lines of code, it is very time-consuming to read it first. to get rid of a line, I must understand the impact of small adjustments on the overall architecture and front-end logic. In the end, I only changed 180 lines of code in more than a week, and the change is very small for a program that is prone to thousands of lines.

Debug is only a small part of code writing. Many of these thousands of lines of code I wrote 10 years ago. although I still have some memories of them, if there are comments, the process of modifying the code will be smoother, I can not only understand the entire framework in the shortest time, but also accurately locate the part I want to modify.

It is even more difficult to change others' code,Everyone has different ways of thinking.If there is no comment, it usually takes more than two times to modify some minor errors.

So what does my so-called "comment" mean? "Comment" refers to a piece of formal description that is attached to the code. But what needs to be separated is that, if we just build a tool room, we don't need a complete set of skyscraper blueprints. Similarly, for small algorithms, we do not need to add annotations to each code.

Most of the programs I want to write recently are "bungalows", not skyscrapers. I will attach comments to each of my Algorithms. some very simple algorithms, I usually only insert one or two comments. I have a very simple rule to help me and others understand my program: Annotations should be as effective as possible to help anyone understand and use my code.

Once I know what a specific line of code is going to do, the write process is actually very simple and straightforward. Some programs also need to use unconventional algorithms. at this time, I will write down the main idea of my algorithm to test its feasibility and help me Debug more efficiently.

Except for the code that is particularly important, my comments are usually informal. In the past ten years, I have not written many accurate and formal comments. However, for a very complex system, the importance of annotations is self-evident. Few engineers spend time writing comments when building a complex system. Some schools will also teach you how to write comments, but more often they will teach you how to write code. This requires practice. if you have not drawn any drawings for building a bungalow, it is difficult for you to draw a blueprint for a skyscraper directly.

Writing comments does not have a simple principle, but you should avoid it as much as possible, that is, using code to explain the code. It's as if you can't use something that people don't understand. use one of them to explain another. Architects cannot directly use bricks to tell you what kind of house he wants to build.

The best way to understand a complex system isSummarize its core with simple concepts and Abstracts. Some basic concepts of junior high school mathematics can be used to help you write comments. for example, you can use some collections, equations, and simple logic to explain your code. For some complex algorithms, you can also introduce concepts that have never been used in mathematics to explain them. In general, if your comments deviate from some basic abstract mathematical concepts, the more difficult it will be to understand.

Thinking cannot ensure that we do not make mistakes, but do not think. mistakes cannot be avoided. Annotations can help minimize errors and improve the efficiency of correction and reduce our time.

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.