Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Code rule


Published on

Coding rule coding standard

  • Be the first to comment

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>