I’m sure that they kindle me for it, but so be it.
It was called the Microsoft.NET library guidelines, but in fact it ’s the views of Brad Abrams ( document here ) - there are other views with good reason.
People tend to hold the majority opinion rather than have good compelling reasons for a particular style.
An important point is the assessment of why a particular style is used and why it is preferable to another style - in other words, there is a reason for choosing a style not only because everyone says what needs to be done - think for yourself.
The main reason for abandoning the use of the Hungarian old style was the use of abbreviations that were different for each team and difficult to learn - it is easy to solve without reducing.
As the available development tools change, the style should change to make more sense, but have a good reason for each style element.
Below are my style recommendations with my reasons - I am always looking for ways to improve my style in order to create more reliable and easy-to-maintain code.
Variable Naming Convention
We all have our own view on variable naming conventions. There are many different styles that can help you create easily maintained, high-quality code — any style that supports basic important information about a variable is fine. The criteria for a particular naming convention should be that it helps to create reliable and easy-to-maintain code. The criteria that should not be used are: ugly Microsoft (i.e., Brad Abrams) says don't use this style - Microsoft doesn't always create the most reliable code, just look at the errors in Expression Blend. When reading the code, it is very important that the variable name instantly conveys three essential facts about the variable: its scope, its type and a clear understanding of what it is used for Scope: Microsoft recommends relying entirely on IntelliSense. IntelliSense is awesome; however, just do not hover over each variable to see its scope and type. Assuming the variable is in scope, it cannot cause significant errors. For example, if a reference variable is passed as a parameter, and it changes in the local area, then this change will remain after the method returns, which may be undesirable. If a field or static variable changes in the local scope, but is considered to be a local variable, unexpected behavior may occur. Therefore, it is extremely important to be able to simply look at a variable (without moving the mouse cursor) and instantly recognize its scope.
The following style is suggested to indicate scope; Nevertheless, any style is perfect for those cases where it clearly and sequentially indicates the scope of the variable: variable m_ of the field p_, passed to the method s_ static variable local variable Type: Serious errors can occur if we assume that they work with a specific type when they actually work with another type - again, we just do not hover over a variable to determine its type, we just assume that we know what type it is, and that is how errors are created.
Abbreviations: Abbreviations are evil because they can mean different things to different developers. One developer might think that leading lowercase "s" means a string, while another might think that this is a signed integer. Abbreviations are a sign of lazy coding - take a little time and enter the full name to understand that the developer must support the code. For example, the difference between "str" and "string" is only three characters — much more effort is required to simplify code support.
General and clear abbreviations for embedded data types only are acceptable, but should be standardized within the group.
Self-documenting code: adding a clear description to the variable name allows another developer to read and understand the code easily - make the name so clear that the team leader can read and understand the code without being a developer.
The order of the parts of the variable name: the recommended order is the description of the scope type, because: IntelliSense will group all similar fields and IntelliSense will group all similar types in each region, which makes searching easier - try to find the variable in another way. It is very easy to see and understand the scope, as well as to see and understand the type. This is a fairly common style and easy to understand, it will pass FxCop
Examples: Here are a few examples: m_stringCustomerName p_stringCustomerDatabaseConnectionString intNumberOfCustomerRecords or iNumberOfCustomerRecords or integerNumberOfCustomerRecords These simple rules will greatly enhance the reliability and usability of code maintenance.
One-line operators of the control structure All one-line operators (if, while, for, etc.) should always be enclosed in curly braces, because it is very easy to add a new operator, not realizing that this operator refers to a control structure that will violate the logic code without generating compile-time errors.
Method exception wrapper All methods must be wrapped with an external trap attempt that captures, provides space for recovery, identification, location, registration, and deciding whether to throw or not. This is an unforeseen exception that causes our applications to crash - by wrapping every method that catches all unhandled exceptions, we guarantee the identification and registration of all exceptions, and also prevent our application from crashing. It takes a little more work, but the results are worth it.
Indentation Indentation is not a serious problem; However, four spaces are suggested without the use of tabs. If the code is printed, the first tab of the printer usually has 8 spaces by default. Different developers tend to use different tab sizes. Microsoft code usually has 4 spaces indented, so if someone uses Microsoft code and uses the other 4 spaces, the code will need to be reformatted. Four spaces make it simple and consistent.