How to Make naming elegant and elegant

Source: Internet
Author: User

How to Make naming elegant and elegant
1. Overview

At the time of writing this article, I have been brewing for a long time. During coding, I often suffer from naming variables, functions, and classes elegantly and beautifully. Sometimes, looking back at the code, we can find that when a variable or function name, especially a word combination, is combined, it looks awkward. For example, type statistics: typeStats, statsType, categoryStats, and statsCategory. 2 is the most awkward, 3 is the second, 1 and 4 are acceptable. Of course, this is not to say 2, 3, No. Here I just want to explain that before naming, I try to combine as many variables as possible to make the final name more elegant.

2. Experiences

Here, I will summarize my experiences in using more resources at ordinary times, so I will summarize my experiences as follows:

2.1 different multi-code segments use different segment naming lengths
Type Length
Loop Variable 1
Method 1-2
Class 2-3
Global Variables 3-4

 

2.2 Use specific naming rules for variables

"Value", "equals", and "data" are not effective naming methods in any situation.

2.3 Use meaningful names

The variable name must accurately reflect its meaning and content.

2.4 you do not need a prefix label to make a variable yourself.

For example, n _, obj _, _ p

2.5 Follow the company's variable naming rules and stick to the same variable naming method in the project

For example, txtUserName, lblUserName, cmbIsAdmin, etc. Otherwise, it will affect readability and make the find/replace tools unavailable.

2.6 follow the naming rules for variables in the current language

In naming conventions, the rules must match the current naming rules. Common problems: userName, username, UserName, USER_NAME, etc.

Take Java rules as an example:

  • Class Name: AppDedail
  • Package name in lower case: cn. company. utils (cn. company. Business name)
  • The variable uses the lower-case camper name: appInfo
  • Use uppercase for constants: MAX_SIZE
  • The enumeration class adopts the hump naming method, and the enumerated values are in uppercase: Configure, APP, APP_NAME
  • Do not use underscores (_) except constants and enumeration values.
2.7 do not reuse variable names in different scenarios of the same class

  

1 public class App {2 public App (String appName) {3 String name = appName; 4 //....... omitting the code ...... 5} 6 7 public void show (AppDetail app) {8 String name = app. getAppName (); // The name variable 9 String appName = app has been used in the constructor. getAppName (); 10} 11}

For example, in methods, initialization methods, and classes, this can improve readability and maintainability.

2.8 do not use the same variable name for different purposes

Same as 2.7, they are given different names, which is important for readability and maintainability.

2.9 do not use non-ASCII characters for variable names

The consequence of doing so will cause problems in cross-platform use.

2.10 do not use too long variable names

Variable names should not exceed 50 characters. Long variable names may lead to ugly code and difficulty reading, and may also cause compatibility problems in some compilers due to character restrictions.

2.11 use only one natural language for naming

For example, using English and Japanese names at the same time may lead to different understandings and reduce the readability of the Code.

2.12 Use meaningful method names

The method name must accurately express the behavior of the method. In most cases, it must start with a verb, for example, createPassword.

2.13 follow company naming rules

In the project note, the same method is used for naming, such as getTxtUserName (), isAdmin (), and updateUserInfo (). Otherwise, readability may be affected, in addition, the search/replacement tool is unavailable.

2.14 follow the variable naming rules of the current language

Do not use case-insensitive letters during naming, such:

Take Java as an example:

  • Method: getStudentInfo
  • The method parameter uses the upper-case and lower-case camel name: insertStudentInfo (Student stu)
2.15 Use meaningful method parameter naming

In this way, you can try to "understand what you mean" without a document ".

3. Summary

The details are very important. We usually need to pay attention to some details during coding. Remember that we code to make it easier for others to read your code, rather than "confuse" others. Once I read the code, I found that the comments I wrote by my old buddy were warm and warm. The general content is as follows:

Private int getSummary (Manager manager) {// destroys the elegant structure of the Code ( ̄ ▽  ̄) for the BT requirement )}

Finally, it is a programmer. How can it be difficult for a programmer !!!

 

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.