7+1 hints in programming naming

Source: Internet
Author: User

7+1 hints in programming naming

The other day, Neo wrote "Naming Design in programming," and there's another article named after the program, which you can look at from another angle.

1.-variables should be as much as possible to understand the text. Never use the naming method in the textbook.

    • Good variables : daysdaterange, Flightnumber, Carcolor.
    • Bad variables : Days, Drange, temp, data, aux ...

In our daily work, there are a large number of developers who prefer to use short variable names rather than variable names that have meaning. This is mainly because of the examples of our university textbooks, people are preconceived, so the textbooks of those very abstract, with the demonstration of the variable name affected our generation of programmers, and affected them for many years. Although those short, textbook-style variable names may make you less likely to hit some words, but in fact, this is very very bad. Because the maintenance cost of software is far greater than the development cost of software, if you do not take a good bit of variable name, then when the code review, when the bug fixing, when the code refactoring, when the code maintenance, your name of a variable may let you confused, do not know what to do, It also allows you to go into a trap, resulting in greater time costs. So, a readable code is bound to be a good variable, which can also make your software indirectly better quality.

2.-variable name not too long, as short as possible

Only simple and short variable names are easy to read. Because your variable name is bound to be used in the program statement, so, in order for your program statement to look short, your variable name should be shorter, otherwise the expression will appear very complex.

Of course, in some cases, there may be some conflict between a meaningful variable name and a short variable name. It's quite an exercise in our language skills-if we have the most refined words to express the richest meanings. If you can't do it, it's better to take a meaningful variable name than to take a short variable name. In any case, we hope that short and rich meaning, but if not both, it has a higher priority.

    • Bad variables : Howlondoesittaketoopenthedoor, howbigisthematerial ...
    • Good variables : Timetoopenthedoor, Materialsize.

3.-can use abbreviations, but need some comments

There are times when we need to use some abbreviations to name variables, such as: Use USR to represent the user, use GP to represent group, use Conf to represent the configuration, and use CWD to represent current working directory, Use PTR to code point to reference, and so on. Abbreviations are generally used in people to understand, rather than to shorten a word for abbreviations, of course, if you add the abbreviated variable name annotated, it is more secure. For some of the customary abbreviations, see Appendix I to this article.

4.-using the appropriate Hungarian naming rules

Here is a very good English article to tell you "what is the right Hungarian name", this article also tells you how to use him. Basically, the Hungarian nomenclature is primarily a prefix to a variable to identify the type of the variable, or the function of a method. The basic principle is: variable name = attribute + Type + object description.

For example: In the description type aspect: pointer p, function fn, long integer l, Boolean B, floating point type (sometimes also referred to as file) F, double word dw, string sz, short integer n, double precision floating point D, unsigned u ... Wait a minute. For more naming conventions, see Appendix two .

Note that Hungarian naming also has a bad place, such as you want to change a shape into a floating-point type, you want to change the variable's name in addition to changing the variable. This is quite a hassle. And, at some point, the naming of this prefix can be a rather confusing one. In addition, in C + +, with classes, this naming method is not easy to implement. So, the idea behind the proper use of Hungarian nomenclature is crucial.

5.-do not use anti-logic to name

    • good name : IsEnabled.
    • name of the bad: Isnotenabled.

In reading, we prefer the forward logic rather than the reverse logic. This rule is not only named, in the conditional statement, we also want to try not to use this negative logic. such as: if (! (ISAdmin | | isuser)), such statements are not in line with the habit of reading code, written like this will be better--if (!isadmin &&!isuser).

6.-Maintain consistency

Maintain consistency of all code. Use the same naming conventions. There is no best naming convention outside the world. But one thing you can be sure of is that in a code base, you should use consistent naming conventions, even if the rules are not that good, but the whole team is good to use the same.

7.-echoing the domain terminology of the application

In different fields, different ideas will have very special and different meanings. For example: the word "order" does not always mean "sub-shun", sometimes it means "order", sometimes it means "order", sometimes, meaning "rule". So, in a certain field, some words will have different meanings, so this requires our command to go along with these fields.

Golden Rule-take some time to think and weigh your variable name

When you design a good one variable name a function name, don't worry about using him, stop, think about this variable name is appropriate, whether there is better? Perhaps you are using a very bad variable name. Some times, we need to weigh the pros and cons, and perhaps to discuss with colleagues.

In short, the variable name is the first step of programming, the first step to go well, the back to go well. Imagine how easy it is for you or your colleagues to program with some good variable names.

Appendix: part of the abbreviation specification
Full word Abbreviation
A
Average Avg
B
Back Bk
Background Bg
Break Brk
Buffer Buf
C
Color Cr,clr
Control Ctrl
D
Data Dat
Delete Del
Document Doc
E
Edit EDT
Error Err
Escape Esc
F
Flag Flg
Form frm
G
Grid Grd
I
Increment Inc
Information Info
Initial Init
Insert Ins
Image Img
L
lable Lab
Length Len
List Lst
Library Lib
M
Manager Mgr,mngr
Message Msg
O
Oracle Ora
P
Panorama Pano
Password Pwd
Picture Pic
Point Pt
Position Pos
Print Prn
Program Prg
S
Server Srv
Source Src
Statistic Stat
String Str
Sybase Syb
T
Temp Tmp
Text Txt
U
User Usr
W
Window Win,wnd

Appendix II, Hungarian nomenclature
  A array of type B bool (int) Boolean (integer) by Unsigned Char (Byte) Unsigned character (bytes) c Char character (bytes) cb Count of bytes bytes Cr Color Reference VA       Lue Color (Reference) value CX Count of x (short) x set (shorter integer) DW DWORD (unsigned long) double word (unsigned longer integer) f   Flags flag (usually with multiple digits) FN function G_ Global globally  h Handle handle I integer integer l long integer                   LP long pointer long pointer m_ data member of a class for the member n short int      Short integer p Pointer pointer s string string sz Zero terminated string String terminated with 0 TM text metric text rule u Unsigned int unsigned integer ul Unsigned long (UL    ONG) unsigned long integer w   WORD (unsigned) unsigned short integer x, y x, coordinates (short) coordinate value/shorter integer v void null 

The global variables for the project start with G_, class member variables with M_, local variables if the function is larger, you can consider using L_ to show that it is a local variable.

Prefix       type        example g_      global variable       G_SERVERSC       class or struct   cdocument,cprintinfom_      member variable       m_pdoc,m_ Ncustomers

VC Common Prefix list:

Prefix   type   description                      example ch     char    8-bit character                   Chgradech     TCHAR   16-bit Unicode type character       Chnameb      bool    boolean variable                  benabledn      int     integral type                      nlengthn      UINT    unsigned integral type                nlengthw      WORD    16-bit unsigned integer            wposl      LONG    32-bit signed integer            loffsetdw     DWORD   32-bit unsigned integer            DWRANGEP      *       Memory module pointer, pointer variable   PDOCLP     far*    long pointer                    lpdoclpsz   LPSTR   32-bit string pointer           Lpsznamelpsz   LPCSTR  32-bit constant string       pointer   lpsznamelpsz LPCTSTR 32-bit Unicode type constant pointer  Lpsznameh      Handle  Windows object handle           HWNDLPFN   (*FN) () callback function pointer              lpfnabort

Windows object name abbreviation:

Windows Object Example variables  MFC class       Example object hwnd        hwnd;      cwnd*       pWnd; Hdlg        Hdlg;      cdialog*    Pdlg; HDC         HDC;       cdc*        PDC; Hgdiobj     hgdiobj;   cgdiobject* Pgdiobj; Hpen        Hpen;      cpen*       Ppen; Hbrush      Hbrush;    cbrush*     Pbrush; Hfont       Hfont;     cfont*      Pfont; Hbitmap     hbitmap;   cbitmap*    Pbitmap; Hpalette    Hpalette;  cpalette*   Ppalette; HRGN        HRGN;      crgn*       prgn; HMENU       HMENU;     cmenu*      Pmenu; HWND        Hctl;      cstatic*    pstatic; HWND        Hctl;      cbutton*    pbtn; HWND        Hctl;      cedit*      PEdit; HWND        Hctl;      clistbox*   Plistbox; HWND        Hctl;      ccombobox*  Pcombobox; (end of full text)

7+1 hints in programming naming

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.