SlideShare a Scribd company logo
Coding Rule
Name of Variable No prefix like “s” “w” “i” “l” Use “m_” for data member , use “p” for pointer m_pDataType (OK) m_pdata_type (No) m_pData_Type (No) Use acronym carefully. m_DT4O2T (m_DataTypeForO2T will be better, O2T is common usage acronym, it is OK)
Initialization int memberCount = -1; or int memberCount = 0; -1 could be the state means the member is not initialized or not used. Choose as you need. Definition with initializing. CParamClass* pParam = NULL;  int memberCount = 0;
Name of function Use full word as possible.  Initialize() (OK) Init() ( No ) GetCount() (OK) GetCnt ( No ) Exception: Min/Max IsXXX, DoesXXX, CheckXXX, should be “Verb + detailed action + (with/using/by) conditions (optional)” Don’t do 2 or more action in one function. GetAndSetXXX ( No! )  do assignment in GetXXX function ( No! ) Should look like normal English sentence.
Class Don’t need protected Data member should be private Set/Get Put/Get use as pair If possible, member function should be const Do Not need class name in member function or member m_pEDSDevice (NO!) m_pDevice (OK)  in CAssemblyClass, m_AssemblyMemberCount (No) m_MemberCount (OK)
Use int as possible. Function name should be meaningful. AssembleEx (no) AssembleUsingIndirectParam (OK) GetParam3 (no) GetParamByKeyname (OK) Make name useful. CalcByteSize (OK) CalcBitSize (OK) CalcBitSizeEx (No) CalcSize (If size is only used bit or byte length, it is OK, otherwise, should be more detailed name)
Function parameter Value is changed in function, use pointer (int *pValue) Value will not change, use  const reference  for Class/Structure type. (const COneClass& oneObject) If different return states, use HRESULT.
Do not use default parameter in function Void SetParamValue (int index, int value, int bitOffset = 0, int startBitPosition = 0); ( No ) Change to 2 different functions.  SetParamValue(int index, int value);  SetParamWithBitPosition (int index, int value, int bitOffset, int startBitPosition); for (int i=0; i< xxx; i++) If for is not very simple (there are another for/while/switch/if in for-loop), the i should be another meaningful name, personIndex / bookIndex etc. Do not use overload if you don’t need it. Different name for similar functions.
NOT use “switch” with “for/while/dowhile” loop, because the “break” of switch will cause confusion. Change switch to “if-elseif-else”. (In seldom case you need it for performance, seldom!) UpdateData (bSave); // save or get in MFC Change to 2 pair functions SaveDataFromDialog() and SetDataToDialog()
Watch the variable in initialization / running
 

More Related Content

PPTX
Pointer in c
PPTX
Php-Continuation
PPTX
Pointer to function 1
PPTX
Pointer to function 2
PPTX
Function overloading
PPTX
Pointers in c++
PDF
String searching
PPTX
Function Pointer
Pointer in c
Php-Continuation
Pointer to function 1
Pointer to function 2
Function overloading
Pointers in c++
String searching
Function Pointer

What's hot (20)

PPT
PPTX
Array in c language
PPTX
Pointers in c++
PPT
Pointers - DataStructures
PPTX
functions in C
PPTX
PDF
Pointer in c++ part3
PPTX
One dimensional arrays
PPT
Lecture 6- Intorduction to C Programming
DOC
Lesson 5
PPT
Pointers in C
PPTX
Array, string and pointer
PPTX
Function Pointer in C
PDF
PDF
Day 5a iteration and functions if().pptx
PDF
Chapter12 array-single-dimension
PPTX
C programming - Pointers
PPTX
Array and functions
PDF
Pointer in c++ part2
PPT
SPC Unit 3
Array in c language
Pointers in c++
Pointers - DataStructures
functions in C
Pointer in c++ part3
One dimensional arrays
Lecture 6- Intorduction to C Programming
Lesson 5
Pointers in C
Array, string and pointer
Function Pointer in C
Day 5a iteration and functions if().pptx
Chapter12 array-single-dimension
C programming - Pointers
Array and functions
Pointer in c++ part2
SPC Unit 3
Ad

Viewers also liked (12)

PPTX
The Ethics of Predictive Coding
DOC
Assignments of source coding theory and applications
PDF
Full-Rate Full-Diversity Space-Frequency Block Coding for Digital TV Broadcas...
PDF
Predictive Coding Legaltech
PPT
Coding style for good synthesis
PPTX
Predictive coding
PPTX
Block Truncation Coding
PPT
Huffman Student
PDF
Huffman and Arithmetic coding - Performance analysis
PPTX
Linear block code
PPTX
Linear block coding
PPT
Source coding
The Ethics of Predictive Coding
Assignments of source coding theory and applications
Full-Rate Full-Diversity Space-Frequency Block Coding for Digital TV Broadcas...
Predictive Coding Legaltech
Coding style for good synthesis
Predictive coding
Block Truncation Coding
Huffman Student
Huffman and Arithmetic coding - Performance analysis
Linear block code
Linear block coding
Source coding
Ad

Similar to Code rule (20)

PPS
Rpg Pointers And User Space
PDF
Microprocessor 8086-lab-mannual
PPTX
Assembly Language Tutorials for Windows - 03 Assembly Language Programming
PDF
PPU Optimisation Lesson
PPTX
c++ pointers by Amir Hamza Khan (SZABISTIAN)
PPT
Lecture 3 c++
PPTX
Unit 1
PPT
C++lecture9
PDF
I keep on get a redefinition error and an undefined error.Customer.pdf
PDF
OptimizingARM
TXT
Advance C++notes
PPTX
C++ Object Oriented Programming Lecture Slides for Students
PPT
Cpp tutorial
PPTX
C++ Pointer | Introduction to programming
PPTX
#GDC15 Code Clinic
PDF
0-Slot11-12-Pointers.pdf
PPT
C for Microcontrollers
DOCX
GSP 125 Entire Course NEW
Rpg Pointers And User Space
Microprocessor 8086-lab-mannual
Assembly Language Tutorials for Windows - 03 Assembly Language Programming
PPU Optimisation Lesson
c++ pointers by Amir Hamza Khan (SZABISTIAN)
Lecture 3 c++
Unit 1
C++lecture9
I keep on get a redefinition error and an undefined error.Customer.pdf
OptimizingARM
Advance C++notes
C++ Object Oriented Programming Lecture Slides for Students
Cpp tutorial
C++ Pointer | Introduction to programming
#GDC15 Code Clinic
0-Slot11-12-Pointers.pdf
C for Microcontrollers
GSP 125 Entire Course NEW

More from Wei Sun (20)

PPT
Using google appengine_final2
PPT
Using google appengine_final
PPT
Using google appengine_1027
PPT
Using google appengine (2)
PPT
Python with dot net and vs2010
PPTX
Gc algorithm inside_dot_net
PPT
Code review
PPT
Windbg dot net_clr2
PPT
The best way to learn java script
PPT
Asynchronous in dot net4
PPT
Code quality
PPT
Visual studio 11 developer preview
PDF
Using google appengine
DOC
老友记
DOC
Lua gc代码
PPT
Windbg dot net_clr2
PPT
Dotnetintroduce 100324201546-phpapp02
PDF
Web development overview
PPT
Lua
PPT
DotNet Introduction
Using google appengine_final2
Using google appengine_final
Using google appengine_1027
Using google appengine (2)
Python with dot net and vs2010
Gc algorithm inside_dot_net
Code review
Windbg dot net_clr2
The best way to learn java script
Asynchronous in dot net4
Code quality
Visual studio 11 developer preview
Using google appengine
老友记
Lua gc代码
Windbg dot net_clr2
Dotnetintroduce 100324201546-phpapp02
Web development overview
Lua
DotNet Introduction

Code rule

  • 2. Name of Variable No prefix like “s” “w” “i” “l” Use “m_” for data member , use “p” for pointer m_pDataType (OK) m_pdata_type (No) m_pData_Type (No) Use acronym carefully. m_DT4O2T (m_DataTypeForO2T will be better, O2T is common usage acronym, it is OK)
  • 3. Initialization int memberCount = -1; or int memberCount = 0; -1 could be the state means the member is not initialized or not used. Choose as you need. Definition with initializing. CParamClass* pParam = NULL; int memberCount = 0;
  • 4. Name of function Use full word as possible. Initialize() (OK) Init() ( No ) GetCount() (OK) GetCnt ( No ) Exception: Min/Max IsXXX, DoesXXX, CheckXXX, should be “Verb + detailed action + (with/using/by) conditions (optional)” Don’t do 2 or more action in one function. GetAndSetXXX ( No! ) do assignment in GetXXX function ( No! ) Should look like normal English sentence.
  • 5. Class Don’t need protected Data member should be private Set/Get Put/Get use as pair If possible, member function should be const Do Not need class name in member function or member m_pEDSDevice (NO!) m_pDevice (OK) in CAssemblyClass, m_AssemblyMemberCount (No) m_MemberCount (OK)
  • 6. Use int as possible. Function name should be meaningful. AssembleEx (no) AssembleUsingIndirectParam (OK) GetParam3 (no) GetParamByKeyname (OK) Make name useful. CalcByteSize (OK) CalcBitSize (OK) CalcBitSizeEx (No) CalcSize (If size is only used bit or byte length, it is OK, otherwise, should be more detailed name)
  • 7. Function parameter Value is changed in function, use pointer (int *pValue) Value will not change, use const reference for Class/Structure type. (const COneClass& oneObject) If different return states, use HRESULT.
  • 8. Do not use default parameter in function Void SetParamValue (int index, int value, int bitOffset = 0, int startBitPosition = 0); ( No ) Change to 2 different functions. SetParamValue(int index, int value); SetParamWithBitPosition (int index, int value, int bitOffset, int startBitPosition); for (int i=0; i< xxx; i++) If for is not very simple (there are another for/while/switch/if in for-loop), the i should be another meaningful name, personIndex / bookIndex etc. Do not use overload if you don’t need it. Different name for similar functions.
  • 9. NOT use “switch” with “for/while/dowhile” loop, because the “break” of switch will cause confusion. Change switch to “if-elseif-else”. (In seldom case you need it for performance, seldom!) UpdateData (bSave); // save or get in MFC Change to 2 pair functions SaveDataFromDialog() and SetDataToDialog()
  • 10. Watch the variable in initialization / running
  • 11.