Code rule
Upcoming SlideShare
Loading in...5
×
 

Code rule

on

  • 2,293 views

Coding rule coding standard

Coding rule coding standard

Statistics

Views

Total Views
2,293
Views on SlideShare
1,434
Embed Views
859

Actions

Likes
1
Downloads
14
Comments
0

14 Embeds 859

http://sunxiunan.com 591
http://www.cnblogs.com 234
http://xianguo.com 9
http://www.sokerspace.com 8
http://www.4ucode.com 4
http://cache.baidu.com 3
http://static.slidesharecdn.com 2
http://reader.youdao.com 2
http://archive.cnblogs.com 1
http://unknownerror.net 1
http://www.dev-club.net 1
http://www.zhuaxia.com 1
https://static.slidesharecdn.com 1
http://www.haogongju.net 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Code rule Code rule Presentation Transcript

  • 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
  •