Simple code-learning notes

Source: Internet
Author: User
Tags types of functions
Notes: clean code

Name:

 


1,Meaningful: Reduce the ambiguity of the code and clearly describe the purpose of the Code;

2. avoid misleading: The accountlist type is preferably list;

3. Avoid using names with small differences;

4. Do not use letters l and O because they are like numbers 1 and 0;

5. Make a meaningful distinction and use different names only when the meaning is different;

6. Nonsense refers to meaningless distinction and redundancy;

7. Use a searchable name: it is better to use a macro to define a number than to use a number directly. Avoid using single-letter variables and numerical constants;

8. You do not need to use the Hungarian markup method of the belt type;

9. Avoid using the class name prefix for member variables;

10. Class names and object names are generally nouns and noun phrases;

11. The method name is generally a verb and verb phrase; get, set, is prefix;

12. Use the name in the solution field;

13. Add a meaningful context: Use a meaningful prefix to create a class and declare the variable as a member variable;

14. Accurate naming: do not add meaningless contexts;

Function:

 


1,Short: The indentation level in the function should not be more than one or two layers;

2. The function should do one thing, do one thing well, And do only one thing;

3. Each function has only one abstraction level, and the rest are done at the abstraction layer below;

4. The judgment function only does one thing: it cannot be divided into function segments;

5. Read the code and follow the top-down rule: each function is followed by the function at the next abstraction layer;

6. How to make the switch do one thing: Create the actual class through the class factory and return the parent class reference, and then use the abstract class (parent class reference) to call the function of the actual class override;

7. Use descriptive names;

8. function parameters: to facilitate testing, there should be fewer than two;

9. Three types of functions:

① Ask about the parameter (Judgment), ② the content of the conversion parameter (return value required), ③ the parameter is an event (no return value)

10. If the function is more than 2 RMB, some of the parameters should be encapsulated into a class;

11. Function Name: verb/noun form;

12. Avoid using output parameters: if necessary, modify the object status;

13. A function either performs one thing (Instruction) or answers one thing (Inquiry );

14. Use exceptions instead of returning error codes: the error codes can be separated from the main path code to avoid nesting;

15. Remove try/catch blocks: In a function;

Note:

 


1,Neat and clear code is much better than comments;

2. A true comment means you do not need to write a comment;

3. provide legal information, intention of interpretation, and warning information;

4. Todo comments, prompting incomplete work;

5. Avoid comments after parentheses, reduce nesting, and write them as methods;

6. Delete the commented-out code;

7. annotation is a failure;

Format:

 


1. vertical interval: spaces are used to separate different logics;

2. Vertical closeness: the close relation logic is closer to the logic to make it clearer;

3. Declare variables in the nearest place to use;

4. Related functions: Put them together and the callers are placed on the callers;

5. concept related: the naming patterns are the same and should be put together;

6. horizontal direction: the principle is not to drag the scroll bar;

7. spaces before and after =, + = can play an important role;

8. indent

9. Team rules

Object and data structure (Procedural Code ):

 


Hiding an implementation is related to abstraction, and it is not a simple addition of the valuer and the assignment operator;

1. Anti-symmetry of objects and data structures:

Procedural Code allows you to add new functions without changing existing code);

The object-oriented method allows you to add new classes (objects) without changing existing functions)But if the abstract class adds a new function, you need to modify all the subclasses of the abstract class;

2. The data structure should only have public variables; the object should only have private variables and public functions;

3. Expose the behavior of the object to hide the data. It is easy to add new object types without modifying existing behaviors and adding new behaviors to existing objects.

The data structure exposes data without obvious behaviors. It is easy to add new behaviors to existing data structures, and it is difficult to add new data structures to existing functions.

4. The law of demoter: the module should not understand the internal conditions of the objects it operates on. Train crash, hybrid, hidden structure.

Handle errors:

 


1. Do not return NULL values: In this case, the caller must process null and increase the workload;

Solution: throw an exception or return a special object;

2. Do not pass null values:

3. Exception Handling: throws an exception or returns a special object. If an exception occurs when a third-party API is called, you can create a method or exception class to package the third-party API;

4. Avoid the use of controllable exceptions (checked exception): Because the function header needs to be modified to process them, the open-close principle is violated; the use of uncontrollable exceptions (runtime exception) should be used ),

Keep borders clean: (The ebook is missing)

 


1. Learning Test: Introduce a new version of a third-party API to the project to test whether the project works properly;

2. methods to deal with boundary problems: Use a new class to package third-party APIs; Use the adapter mode to convert our interfaces to interfaces provided by a third party;

More patterns: http://blog.csdn.net/qinysong/archive/2006/08/09/1041818.aspx

Unit test:

 


1. Test-driven development. Neat tests help to make code changes;

2. Standard for clean testing: readability;

3. dual standards: due to differences in runtime environments, the test code and production code can have different standards, such as efficiency and memory;

4. The number of assertions for a single test should be minimized. Only one concept should be tested;

5. f. I .r. s.t rules:

F fast: The test needs to run frequently, so it must be able to run quickly;

I independent: The test should be independent of each other;

R repeatable: The test should be repeated in any environment;

S self-Validating: self-contained verification, the test should be able to see the result of success or failure;

T timely: The test should be written in time and should be written just before the production code;

Class:

 


1. class organization: the top-down principle, the variable list (public before private, static before entity), and the method list (the tool method follows the method );

2. classes should be short: The clearer the class name, the clearer the class's responsibilities;

(1) Each category has a single authority and responsibility. There is only one reason to modify it and works with a small number of other classes to complete the work;

(2) Cohesion: the class contains the least variables, and each method uses each variable. At this time, the cohesion is the highest, and the class should be the cohesion height;

3. Isolated modification: the Implementation Details of specific classes. abstract classes only present concepts. Using Interfaces and abstract classes, you can isolate the risks of changing classes due to changes in details;

System:

 


1. The construction and application code should be isolated: it is like when building a building, the construction of cranes, forklifts and other things in the building does not exist at all when the building is put into use; the software system should separate the startup process from the runtime logic after the startup process, and create application objects during the startup process, there will also be mutual dependencies.

Public service getservice (){

Return new myserviceiml (...);

}

The advantage of this delayed value assignment is that you don't need to care about this overhead structure before using the object; the disadvantage is that you must implement this constructor, otherwise you cannot compile it, even if this object is never used at runtime.

Solution:

(1) break down the main function and move all the construction processes in the system to the main or main module: The main function creates an object and then passes the object to the application, which is directly used by the application;

(2) Factories allow applications to control the creation time of entities;

(3) Dependency injection. Controlling the reversal of IOC is a means of dependency management. It takes out the permission to create the dependent object required by the application from the object and places it in an object that focuses on this matter, the dependency object is passed to the application through dependency injection (Value assignment;

2. Resizing

AOP, orientation becomes. Three Mechanisms in Java and similar aspects: proxy, pure AOP framework, and aspectj

(1) Java Proxy: proxy. after the newinstance (invocationhandler h) method is executed, all the methods of the proxy class will be added with the handler processing logic. This is a simple AOP, but it is too complicated;

(2) Pure AOP framework: Spring AOP (for further understanding)

(3) aspectj Language

The optimal system architecture consists of a modular area of attention, each of which is implemented using a pure Java object. Different fields are integrated with the least invasive or similar tools. This architecture can be tested and driven, just like code.

2011-6-24Read 173Page

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.