Writing high-quality code: 157 suggestions for improving C # programs
Author: Lu mintech
Press: Machinery Industry Press
Publishing year: 2011-10
Page: 247
Pricing: 59.00 RMB
Frame loading: Flat Loading
ISBN: 9787111356493
Douban: http://book.douban.com/subject/6849791/
Dangdang: http://product.dangdang.com/product.aspx? Product_id = 22507675
Directory
Preface
Part 1 Language
Chapter 1 BASIC Language Elements
Suggestion 1: Correct string operation
Recommendation 2: use the default Transformation Method
Recommendation 3: differentiate between forced transformation and as and is
Recommendation 4: tryparse is better than parse
Suggestion 5: Use Int? To ensure that the value type can also be null.
Recommendation 6: Use readonly and const differently
Recommendation 7: use 0 as the default enumeration Value
Recommendation 8: Avoid providing explicit values for enumeration elements.
Recommendation 9: Used to overload Operators
Recommendation 10: consider whether to implement a comparator when creating an object.
Recommendation 11: differential treatment = and equals
Recommendation 12: rewrite gethashcode when rewriting equals.
Suggestion 13: format a string for Type output
Suggestion 14: Perform the shortest copy and deep copy correctly.
Suggestion 15: use dynamic to simplify reflection implementation
Chapter 2 collections and LINQ
Suggestion 16: arrays should not be used when the number of elements is variable.
Recommendation 17: Use foreach for loop traversal in most cases
Suggestion 18: foreach cannot replace
Recommendation 19: use more effective objects and set Initialization
Recommendation 20: Use a generic set instead of a non-generic set
Suggestion 21: select the correct set
Recommendation 22: Ensure the thread security of the Set
Recommendation 23: Avoid using list as the base class of the custom collection class.
Recommendation 24: The iterator should be read-only.
Suggestion 25: exercise caution when performing write operations on set attributes.
Recommendation 26: use an anonymous type to store the results of the LINQ Query
Recommendation 27: Use a Lambda expression in a query
Suggestion 28: understand the difference between latency evaluation and Active Evaluation
Recommendation 29: differentiate ienumerable and iqueryable in a linq Query
Suggestion 30: Replace the comparator and iterator in the set with LINQ
Recommendation 31: Avoid unnecessary iterations in the LINQ query.
Chapter 2 generic, delegated, and event
Recommendation 32: always give priority to generics
Recommendation 33: avoid declaring static members in a generic type.
Recommendation 34: Set constraints for generic parameters
Recommendation 35: use default to specify the initial value for a generic variable.
Suggestion 36: Use the delegation statement in FCL
Recommendation 37: Use lambda expressions instead of methods and anonymous methods
Suggestion 38: Be careful with the traps in the closure.
Suggestion 39: understand the essence of delegation
Suggestion 40: Use the event keyword to apply protection to the Delegate
Recommendation 41: implement a standard event model
Recommendation 42: use generic parameters to be compatible with the non-variability of generic Interfaces
Suggestion 43: Make the generic parameters in the interface support covariant
Suggestion 44: Understand the covariant in the delegate
Recommendation 45: specify an inverter for generic parameters
Chapter 4 Resource Management and serialization
Suggestion 46: The interface idisposable must be inherited to explicitly release resources
Recommendation 47: provide implicit cleanup in the terminator even if the explicit release method is provided.
Suggestion 48: The dispose method should be allowed to be called multiple times.
Suggestion 49: a protected virtual method should be extracted in dispose mode.
Suggestion 50: The managed and unmanaged resources should be treated differently in the dispose mode.
Suggestion 51: The types with releasable fields or local resources should be releasable.
Suggestion 52: release resources in time
Suggestion 53: if necessary, assign a null value to the reference of an object that is no longer in use.
54. Not serialized for useless field annotations
Recommendation 55: use custom features to reduce serializable Fields
Recommendation 56: Use the inherited iserializable interface to control the serialization process more flexibly
Recommendation 57: the sub-type implementing iserializable should be responsible for serialization of the parent class
Chapter 4 Exceptions and custom exceptions
Suggestion 58: Use throwing an exception instead of returning the error code
Recommendation 59: Do not raise exceptions in inappropriate situations.
Suggestion 60: Use Inner exception when an exception is thrown again
Suggestion 61: Avoid writing Invalid code in finally
Suggestion 62: Avoid nested exceptions
Recommendation 63: Avoid "eat" exceptions
64 recommended: add the tester-doer mode for the loop instead of placing try-catch in the loop.
Recommendation 65: always handle uncaptured exceptions
Recommendation 66: correctly capture exceptions in multiple threads
Recommendation 67: use custom exceptions with caution
Recommendation 68: derive an exception from system. exception or other common basic exceptions
Recommendation 69: Use finally to avoid resource leakage
Recommendation 70: Avoid record exceptions at lower call stacks
Chapter 2 Asynchronization, multithreading, task and Parallelism
Recommendation 71: differentiate asynchronous and multithreading application scenarios
Recommendation 72: Use semaphores in thread synchronization
Recommendation 73: avoid locking inappropriate synchronization objects
Recommendation 74: guard against thread isbackground
Recommendation 75: Be cautious that the thread will not start immediately
Recommendation 76: guard against thread priority
Suggestion 77: Stop the thread correctly
Recommendation 78: avoid excessive threads
Recommendation 79: Use threadpool or backgroundworker to replace thread
Recommendation 80: Use task instead of threadpool
81: use parallel to simplify the use of tasks in synchronization
Recommendation 82: parallel is simplified but not the same as default task behavior.
Recommendation 83: Be careful with the traps in parallel
Recommendation 84: Use Plinq
Recommendation 85: Exception Handling in task
Recommendation 86: Exception Handling in parallel
Recommendation 87: differentiate the thread models of WPF and winform
Recommendation 88: parallel processing is not always faster
89: exercise caution when using locks in parallel method bodies
Part 2 Architecture
Chapter 2 member Design
Recommendation 90: do not provide public constructor for abstract classes
91: visible fields should be restructured into attributes.
Recommendation 92: exercise caution when using arrays or collections as attributes
Recommendation 93: primary attributes and fields should be initialized in the constructor.
Suggestion 94: Treat override and new differently
Suggestion 95: Avoid calling virtual members in the constructor
Recommendation 96: members should give priority to making public the base type or interface
97. Priority is given to passing the base type or interface as a parameter.
Recommendation 98: Use Params to reduce repeated Parameters
Recommendation 99: Do not use subclass parameters during Rewriting
Recommendation 100: there is no difference between the static method and the instance method
Recommendation 101: Use the extension method to "add" The existing type.
Chapter 2 Type Design
Recommendation 102: application scenarios that distinguish interfaces from abstract classes
Recommendation 103: application scenarios that differentiate combinations and inheritance
Recommendation 104: replace conditional statements with Polymorphism
Recommendation 105: Use a private constructor to enhance the singleton
Recommendation 106: Add a static constructor to a static class
Recommendation 107: differentiate static classes and Singleton instances
Suggestion 108: identify the type as sealed
Recommendation 109: Use Nested classes with caution
Recommendation 110: Use a class instead of Enum
Suggestion 111: Avoid bidirectional Coupling
Recommendation 112: Abstract objects in the real world into classes and enclose reusable objects as namespaces.
Chapter 2 Security Design
Suggestion 113: Consider the maximum value before declaring a variable
Recommended 114: MD5 is no longer secure
Suggestion 115: use hash to verify whether the file has been tampered
Recommendation 116: Avoid using asymmetric algorithms to encrypt files
Recommendation 117: use SSL to ensure data security in communications
Suggestion 118: Use securestring to save secret strings such as keys
Recommendation 119: Do not use your own encryption algorithm
Recommended 120: specify a strong name for the Assembly
Suggestion 121: Set the running permission for the Application
Part 3 coding specifications and habits
Chapter 2 naming rules
Recommendation 122: use. As the namespace
Recommendation 123: the Assembly does not have to have the same name as the namespace.
Recommendation 124: consider using the plural number in the namespace
Suggestion 125: Do not use the FCL type name to name your own type.
Suggestion 126: Use nouns and Noun groups to name the type
Suggestion 127: use an adjective group to name an Interface
Suggestion 128: Let the name of the derived class be suffixed with the base class name.
Suggestion 129: the generic type parameter should be prefixed with T
Suggestion 130: Name the enumeration type in the plural and name the enumeration element in the singular.
Recommendation 131: Use pascalcasing to name a public element
Recommendation 132: consider using the class name as the attribute name
Recommendation 133: Use camelcasing to name private fields and local variables
134: Use prefix with conditions
Recommendation 135: use affirmative phrases to name Boolean attributes
Recommendation 136: prefix the suffix to indicate a new version of the existing type.
Recommendation 137: Add a superior suffix to the delegate and event types
Suggestion 138: use verb or adjective phrase to name events and delegate Variables
Suggestion 139: The Event processor name should be combined.
Chapter 4 Code cleanliness
Recommendation 140: use the default access modifier
Suggestion 141: if you do not know whether to use braces, use
Recommendation 142: always provide meaningful names
Suggestion 143: The method abstraction level should be at the same level
Recommendation 144: Only one thing for one method
Recommendation 145: Avoid too long methods and too long classes
Recommendation 146: only publish necessary operations
Recommendation 147: refactor multiple related attributes into a class
Recommendation 148: No repeated code
Recommendation 149: use the table driver method to avoid too long if and switch branches
Suggestion 150: use an anonymous method or Lambda expression instead of a Method
Suggestion 151: Use the event accessors to replace the public event member variables
Recommendation 152: Least, or even not comment
Suggestion 153: if an exception is thrown, you must comment it out.
Chapter 4 standardized development behavior
Recommendation 154: Do not over-design, experience the fun of refactoring in Agile
Suggestion 155: Submit the unit test code together with the production code
Recommendation 156: use features to provide multiple versions for the Application
Recommendation 157: perform automated testing starting from the first interface.