Page 221 - DCAP305_PRINCIPLES_OF_SOFTWARE_ENGINEERING
P. 221

Unit 10: Coding Standards



                 Indenting the code yields easier-to-read code, such as:                          Notes
                     If … Then
                        If … Then
                          …
                        Else
                          …
                        End If
                     Else
                        …
                     End If

               •  Establish a maximum line length for comments and code to keep away from having to
                 scroll the source code window and to allow for clean hard-copy presentation.
               •  Use spaces before and after most operators when doing so does not alter the intent of the
                 code. For example, an exception is the pointer notation used in C++.
               •  Put a space after each comma in comma-delimited lists, such as array values and arguments,
                 when doing so does not alter the intent of the code. For example, an exception is an ActiveX
                 Data Object (ADO) Connection argument.
               •  Use  white  space  to  provide  organizational  clues  to  source  code.  Doing  so  creates
                 “paragraphs” of code, which aid the reader in comprehending the logical segmenting of
                 the software.
               •  When a line is broken across several lines, make it obvious that the line is incomplete
                 without the following line.
               •  Where appropriate, avoid placing more than one statement per line. An exception is a
                 loop in C, C++, Visual J++, or JScript, such as for (i = 0; i < 100; i++).
               •  When writing HTML, establish a standard format for tags and attributes, such as using
                 all uppercase for tags and all lowercase for attributes. As an alternative, adhere to the
                 XHTML specification to ensure all HTML documents are valid. Although there are file size
                 trade-offs to consider when creating Web pages, use quoted attribute values and closing
                 tags to ease maintainability.
               •  When writing SQL statements, use all uppercase for keywords and mixed case for database
                 elements, such as tables, columns, and views.
               •  Divide source code logically between physical files.
               •  In ASP, use script delimiters around blocks of script rather than around each line of script
                 or interspersing small HTML fragments with server-side scripting. Using script delimiters
                 around each line or interspersing HTML fragments with server-side scripting increases
                 the frequency of context switching on the server side, which hampers performance and
                 degrades code readability.
               •  Put each major SQL clause on a separate line so statements are easier to read and edit,
                 for example:
                     SELECT FirstName, LastName
                     FROM Customers
                     WHERE State = ‘WA’
               •  Do not use literal numbers or literal strings, such as For i = 1 To 7. Instead, use named
                 constants,  such  as  For  i  =  1  To  NUM_DAYS_IN_WEEK,  for  ease  of  maintenance  and
                 understanding.

                                             LOVELY PROFESSIONAL UNIVERSITY                                   215
   216   217   218   219   220   221   222   223   224   225   226