0
Coding Rule
Name of Variable <ul><li>No prefix like “s” “w” “i” “l” </li></ul><ul><li>Use “m_” for data member , use “p” for pointer <...
Initialization <ul><li>int memberCount = -1; or int memberCount = 0; </li></ul><ul><li>-1 could be the state means the mem...
Name of function <ul><li>Use full word as possible.  </li></ul><ul><ul><li>Initialize() (OK) Init() ( No ) </li></ul></ul>...
Class <ul><li>Don’t need protected </li></ul><ul><li>Data member should be private </li></ul><ul><li>Set/Get Put/Get use a...
<ul><li>Use int as possible. </li></ul><ul><li>Function name should be meaningful. </li></ul><ul><ul><li>AssembleEx (no) A...
Function parameter <ul><li>Value is changed in function, use pointer (int *pValue) </li></ul><ul><li>Value will not change...
<ul><li>Do not use default parameter in function </li></ul><ul><ul><li>Void SetParamValue (int index, int value, int bitOf...
<ul><li>NOT use “switch” with “for/while/dowhile” loop, because the “break” of switch will cause confusion. </li></ul><ul>...
<ul><li>Watch the variable in initialization / running  </li></ul>
 
Upcoming SlideShare
Loading in...5
×

Code rule

1,980

Published on

Coding rule coding standard

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,980
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
15
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Code rule"

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

    Clipping is a handy way to collect important slides you want to go back to later.

×