Saturday, June 04, 2011

Real Time Coding Rules

Coding Rules

Commenting Code

Headers - Every test script, functional library, or actions should contain a header comment that displays the name, creator, date, and description, assumptions, preconditions, and post conditions.
Test Script Header Comment.
'****************************************************************************************************************************
‘SCRIPT NAME      :   CL11_FeatureAuditAnnotateSDOC                                                
‘CREATED BY       :    ahamad                        
‘DATE CREATED     : 16/Mar/2011                             
‘DESCRIPTION      :   This Script Checks That Feature Audit for Annotate is not
'                                   Transferred to the audit log   file when a sealed Word document
'                                    is annotated  by inserting comment
' Modification Log :
‘ID     Date      Name           Purpose                           
' ----   -------   ----------    --------------                      
'****************************************************************************************************************************
Function Header Comment.
'****************************************************************************************************************************
‘FUNCTION NAME    :   CheckClientFeatureAudited    
‘CREATED BY           :   ahamad                                       
‘DATE CREATED       :   09/March/2001                                        
‘DESCRIPTION          :   This Function checks that required client feature audit is transferred to auditlog file.                 
‘Parameters                :   strFilePath - Path of decrypted auditlog file.          
'                                       strFeature - Expected feature to be audited.            
'                                       strPublisherID - Publisher ID of sealed content.        
‘Return Values            :   Returns the number of times the client feature is audited in the auditlog file.   
‘Preconditions             :  Audit log files exist
’ Postcondition:  Feature audit transfer to audit log file ' Modification Log:                                                            
‘ID     Date      Name           Purpose                                     
' ----   -------   ----------    --------------                                
'*****************************************************************************************************************************
Commenting single lines / paragraphs – The need to comment an individual line of code is rare.  Two possible reasons for commenting a single line of code are:
The line is complicated enough to need an explanation.
The single line once had an error and you want to record the change.  When modifying a single line of code, the original line should be commented out using the author’s initials and date in brackets, separated by a single dash. 
For example (in VBScript), the following shows a code change by SMB (initials):
‘[SMB-03/09/01]Browser("e.POWER").Page("Worklist").WebRadioGroup("item_select").Select "122|0|23"
Browser("e.POWER").Page("Worklist").WebRadioGroup("item_select").Select strItemID     ‘[SMB-03/09/01]
Use end of line comments to annotate data declarations. 
Example:
Dim intEmpAge                 ‘Stores Employee Age.
Dim strEmpName            ‘Stores Employee Name.
Such data annotations include:  units of numeric data, range of allowable values, limitations on input data, and the meaning of enumerated or constant codes.
Focus paragraph comments on the why rather than the how.  Simply put, a reader should be able to discern from quality code how an operation is performed.  Why that operation is performed will be less apparent. 
When commenting individual lines or paragraphs, indent the comment and align it with its corresponding code.  Also, precede the comment line by a single blank line.
Commenting Control Structures
Place a comment before each block of statements, if, case, or loop.  Use the comment to clarify the purpose of the control structure.
Example:
‘Loop through the each row in the data table.
For intCount=1 to datatable.GetRowCount
‘Check whether content in both columns are equal.
 If datatable("Name1", dtGlobalSheet)= datatable("Name2", dtGlobalSheet) then
 reporter.ReportEvent micPass,”content of both column are equal in row” & intCount
 End If
Next
For long code blocks, comment the end of each control structure.  This makes script code which spans many lines easier to follow.
Example:
For intCount=1 to datatable.GetRowCount
 ‘Check whether content in both columns are equal.
  If datatable("Name1", dtGlobalSheet)= datatable("Name2", dtGlobalSheet) then
 reporter.ReportEvent micPass,”content of both column are equal in row” & intCount
 End If
Next      ‘Loop through the each row in the data table.

Formatting Code

The following are the directives that must be complied with while formatting the code
Do not place multiple statements on a single line.
Placing multiple statements in a single line increase complexity. Do not do it.
Do not exceed 90 Characters on a line
Make sure that not more than 90 characters are placed on a single line.
Use Indentation to show organizational structure
Do proper indentation through out your coding. Flow control statements should be indented one tab length for easier readability
 Example: 
For intCount = 0 to 10
 MyVar = MyVar + 1
  If MyVar = 20
 MyVar =0
 End If
  Next

Using Shared Object Repository

Use shared Object Repository instead of Object Repository per Action. This will an easy way for handling 
changes in the object repository.

Using Relative paths

Use relative paths while calling Shared object repository files, VbScript function library files and Reusable actions.
Using Global Variables
Declare and Assign, all the variables that can be used in many scripts 
(For example: Username, Password, PrinterName etc) in a Reusable Action
 or VBScript file and use these variables in Other Actions. Using this method we can modify the value of variable in 
single global file instead of making modification in many script files.

No comments:

Post a Comment