Chapter 4 functions in Visual C # Best Practices (iv): Precautions for using functions

Source: Internet
Author: User
ArticleDirectory
    • 4.4 precautions for using functions
Chapter 4 Functions

Many people are still confused about what functions and methods are. In fact, these two concepts are the same thing. Methods are the product of object-oriented. For example, we say that a method of an object is used, and functions are the product of process-oriented. For example, we often say that a function is executed. Both functions and methods are written in the same way. This chapter will introduce the related concepts of functions in detail.
The focus of this chapter:
◆ Function name Design
◆ Function parameter design

4.4 precautions for using functions

The function is written in C #.ProgramIts importance is self-evident. The minor disadvantage of function design can easily lead to incorrect use of the function. Therefore, it is not enough to make the function correctly. This section focuses on the design and implementation of functions.

4.4.1 function name Design

We all know that functions are a very important concept in any programming language. For most beginners, inadequate function name design will lead to later function calls, especially when the project scale is getting bigger and bigger, the roles and differences of different functions are unclear. This creates unnecessary trouble for function calls and operations. It is also important to fully understand the role of function names in programming.
Since most names are constructed by concatenating several words, use a mix of upper and lower cases to simplify their reading. The following describes several common methods:
1. Hungarian naming: It is widely used in Windows programming, proposed by a Microsoft Hungarian programmer. The Hungarian naming method identifies the scope and type of the variable by adding the symbol of the corresponding lowercase letter before the variable name as the prefix. For example, int_age defines a variable that represents age. Currently, this method is rarely used in the C # language, because it is quite convenient to use Visual Studio development tools, so that you can easily know the scope or type of variables and other related information.
2. Camel naming method: the Chinese translation is the camel naming method, because the name using this naming method looks like a camel's camel. The writing rule is that the first letter of each word except the first word is capitalized. The camel naming method can be used in combination with uppercase/lowercase letters and words with underscores.
For example, both userid and user_id belong to the camel naming method.
3. Pascal naming: similar to camel naming, the first letter of Pascal naming is an uppercase letter.
Example: userid
Now we are familiar with the naming methods commonly used in the above three. When designing a function, we generally use the Pascal naming method to name the function name. This is a writing specification that does not affect the program performance, but everyone uses the same style Code Reading brings great convenience. For example, console. writeline (). The writeline method uses the Pascal naming method. The first letter (W and L) of each word is an uppercase letter.
We all know that naming is the most influential help. In addition to case sensitivity, a method is generally named as a verb-object phrase, indicating the meaning of "what to do.
For example:
Public class file
{
Public void createfile (string filepath)
{...}
Public void getpath (string path)
{...}
}
The createfile method uses the create verb as the start and file as the object. Do not write it as filecreate ().
Author's experience:
If a method is used to determine whether it is used, its return value is generally true or false. We can include is in the method name, which means yes/no or true/false. For example, isexists.

4.4.2 function parameter design

Parameters are an important part of functions. This section focuses on the design of function parameters. Although it seems that parameters are only used to pass values, the work is not too complicated, but poor parameter design will often cause a lot of inconvenience to function callers. As the project grows, the development difficulty and complexity will be greatly increased, and the issue of function design will become more and more obvious.
1. Parameter naming
First, let's talk about the parameter naming. For a function, a parameter is like a local variable because it complies with all the rules of local variables. When naming a parameter, It is case-sensitive in the camel format and uses descriptive and clear names.
For example:
Public void getmessagebyid (INT messageid)
{......}
Clear parameter naming can save a lot of time for reading data. When using C # To develop code, you can use the smart prompt function of Visual Studio editor to automatically prompt the parameter type and name, which is very convenient, the caller can use this function correctly by looking at the parameter list.
2. Do not use reserved items
When designing class library functions, we will add reserved items to the function parameters that can be foreseen to be changed. This method is very common in C ++. For example, if a function of the new version needs to be added, it must be named getmessageex or getmessage2. However, since the C # language has the function of reloading functions, we can discard this practice completely. The new functions can still use the original function name without changing it.
3. Parameter order
The order of parameters does not cause actual differences during the running of the program, but in general, it is always used to putting the core data in front, followed by related parameters, options, function switches, etc, the relevant parameters are put together to conform to reading and thinking habits as much as possible.
4. The parameters of the overload function are the same.
In terms of syntax, parameter differences are the most essential differences between function overloading. However, in fact, the differences between parameters in each overload are often very small, so it is very important to maintain its consistency. The parameter consistency of the overload function is mainly reflected in two aspects: Name consistency and order consistency.
That is to say, if the same parameter exists in multiple overloaded versions, the parameter names in each overload should be completely consistent. Based on name consistency, the relative position of the same parameter in the parameter list should remain unchanged between different function overloading.
For example:
Public static filestream file. Create (string path );
Public static filestream file. Create (string path, int buffersize );
Public static filestream file. Create (string path, int buffersize, fileoptions options );
It can be seen that different overloading changes only in the form of actually different parameters. If the two parameters represent the same meaning, the same name and order are precisely used.

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.