Clean code [Book] -- 03, path -- 03

Source: Internet
Author: User

Clean code [Book] -- 03, path -- 03

Topic: Functions

In the early years of programming, the system consists of programs and subprograms. Later, in the days of Fortran and PL/1, the system was composed of programs, subprograms, and functions. Today, only functions survive. Function types all have the first group of code in the program. This chapter describes how to write functions.

How can we let the function express its intention? What attributes should we assign to the function so that readers can understand what programs they belong to at a glance?

1. Short: the first rule of the function is short. The second rule should be short.

Replace multi-row statement blocks with functions, so that each function can be seen at a glance and each function can only say one thing.

Code block and indentation: if, else, while, and other statements, the code library should have only one line, and the change should be a function call statement, which not only keeps the function short, in addition, the function called in the block has a descriptive name, which increases the value of the document.

This also means that the function should not be large enough to accommodate the nested structure. Therefore, the indentation level of the function should not be one or two layers. This function is easy to read and understand.

If every routine makes you feel deeply satisfied, it is the neat code.

2. function parameters:

The ideal number of parameters is 0, followed by 1, 2; Avoid 3 unless there is enough reason!

Output parameters are more difficult to understand than input parameters.

3. The method name has no side effects. The function name checkedPassword () contains the initialize () function. You can rename checkPasswordAndInitializeSession, But it violates the principle that the function only does one thing.

4. Use the Exception Code instead of returning the error code

5. Remove try/catch: they disrupt the code structure and confuse error handling with normal processes. It is best to extract the active part of the try and catch code blocks and form a function.

6. repetition is the root of all evil in software.

7. Structured Programming: one-in-one

 

The above information comes to the "clean code"

 

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.