Read agile development essential skills-(1)

Source: Internet
Author: User

I don't know if this is a good book, but it's a little bit confusing for me. These skills are very well-taught. Of course, after reading this book, it is consistent with the previous classic "refactoring-improving existing code design. However, this book is even more exciting. Why? What our programmers like most is the source code. In any case, you will scream "Cuihua, source code ". Therefore, the example in this book gives me a quick grasp of the skills I have introduced (unfortunately, I have not completed most of the exercises, and I feel very uncomfortable ). Don't talk nonsense, lest you forget what you want to do! Write some reading notes!

The skills in the book are all based on the actual problems in real programming, but we need to pay attention to some usage scenarios when getting this skill.

Remove duplicate code

How can I find repeated code? I believe every programmer knows it! Think about the DaO code, and you will be chilling. There are too many repeated codes! I personally feel that the same logic structure, the same processing result, and the appearance of the above two places all need to be determined by myself. There is no written answer.

Why remove repeated code? We should not expect that the more lines of code, the greater the sense of superiority (I was very proud to write a longer program when I was a beginner in programming, but this is not desirable at work ). Why? If a program of the same function appears in a certain N multiple places. Every change you make is a nightmare! Enjoy it slowly. Today's rivers and lakes require more refined code.

Convert comments to code

In software development, the best thing we can teach is to add appropriate comments to the Code to facilitate code maintenance in the future. You don't need to guess the functions of a specific piece of code like a riddle, after all, you add comments, right!

But, dear, do you like this code? Will you secretly scold the person who writes this code? I think I know what you think (>_<)

What can we do here? Do I remember giving a variable a meaningful name? This is to let people know at a glance what this variable represents! Instead of adding a comment. This prevents you from remembering the meaning of the variable in the first few rows, but after a period of time or writing to N rows, I thought you forgot what the change brother did.

Are you willing to delete additional comments? Note has a function that makes it easier to understand the code. The problem is that, because the code is often not clearly written, you can find a shortcut to comment on this tool, to make the program easier to understand. We tend to ignore the need to properly organize the code structure. After years and years, leave behind: the Code itself is not clear, and some incorrect comments are added.

Suggestions in the book:Every comment is a good opportunity to improve the code.

For the above code, you can use the [convert annotation to variable name] technique to solve the problem. In addition to this method, there are also the following columns for your reference:

The annotation of the parameter is converted to the parameter name (in fact, the line above)

Convert annotation into a part of the Method

Delete useless comments (do not leave useless comments because of poor work results)

You can use a method name to express the meaning of a comment (small methods are used to refine the large method body, so that the code is within the effective scope of the meaning that you need to express)

.....

Code odor removal

The reason for code odor is that the Code is unstable. Before writing code, how can I determine whether the code is stable? An effective and passive method is as follows:When you modify the code for the third time, consider that the code is unstable and smelly!

The common code odor is the category code and switch expression. The following lists some odor information:

Too many comments

Category Code

Switch if-then-else-if

If you want to give a variable, method, or class a good name, how can this problem be solved?

Using commands like XXXUtil, XXXManager, XXXController, and others

Use the words "And", "Or" in variables, methods, Or class names.

Some instance variables are sometimes useful and sometimes useless.

There are too many codes for a method, or the method is too long.

The code of a class is too large, or the class is too long.

Both classes reference each other (mutual dependency)

A method has too many parameters.

For general category code removal, there are two common methods:

1. Replace different classes with different subclasses based on the unified parent class

2. Replace different classes with different objects of a class.

 

For the "Code odor", we need to modify the internal class in case of a change, rather than modifying it through extension or other methods that do not change the original class. Such a code structure is problematic because it does not take into account the additional workload caused by future code changes. It does not comply with the [open and closed principle ]. We should pay more attention to this basic principle in coding, so as to avoid your code from entering the abyss during future maintenance!

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.