Layout and Style
|The prime purpose of a good layout is to make the logical structure of
the code more obvious to people.
Good layout must be:
- accurately represent the logical
structure of the code
- consistent represent the logical
structure of the code
- improved readability
- improved maintainability
- "looking good " is not
|Fundamental Theorem of Formating
A visual layout is
considered good if and only if it shows the logical structure of the program.
- it groups related items
- it separates unrelated items
- it aligns elements that belong
- it indent statements that are
- it adds clarity to the
code which can improve readability of the code
- use more Parentheses
- Pure Blocks
- A well designed language
has clear block structures that lend themselves to a natural indent style.
- This layout styles is only
for the Ada.
while PixelColor =
//in this example, the
beginning of loop(while) should be aligned with its end(end).
- Endline Layout
- Refers a large group of
layout strategies in which the code is intented almost to the end of the line.
- The endline indentation is used
to align a block with the keyword that began it, to make a routine's
line up under its first parameter, and to line up cases in a case statement.
if (SoldCount > 100)
Markdown := 0.10;
Profit := 0.05;
else Markdown :=
- Emulating Pure Blocks
- A better alternative to
using endline layout in languages that don'y have pure blocks is to view the begin
and end keywords as
extensions of the control construct they are used with.
case PixelColor of
- begin and end as Block Boundaries
- A substitute for a pure
block structure is to view begin-end pairsas block boundies. If you take that
approach, you view the
begin and the end as statements that follow the control construct rather then
as fragment that are
part of it.
if PixelColor =
|Layout of Individual Statements
- Limits the length of the statement line
length to 80 characters and use only one statement per line
- Formating continuation lines, end each line at where a normal statement would not end.
TotalAmount = TotalAmount + CustomerPurchase -
CustomerDiscount + Taxes;
- Indent assignment to a point after the "="
= Inventory + Purchses
MyClass.Sale = Sales
MyClass.Salary = Hours * HourlyRate - EI
- Align Data Declarations, and use one data declaration per line.
Always declare data at the start of the code block.
- Align Pointer and reference declarations
float * pPurchases;
int & rTaxes;
- Indent a comment with its corresponding code
// If the user enter
999, then exit the program
if (num = 999)
// Display end of program message.
cout << "Bye-Bye";
C++ good bad
Authors: Alice Mok, Rena Xu, Mandy Wong, Anthony Leung,
Andy Ho, Gary Lo
Reference: Code Complete, A Practical Handbook of Software
Construction. Steve McConnell. Microsoft Press (1993)
Last Modified: June 15, 2000
Copyright 2000 Department of Computer Science, University
[CS Dept Home Page]