Professional Documents
Culture Documents
STYLE
1
programming STYLE refers to the technique
used in writing the source code for a computer program.
2
programming STYLE
The goal of good programming style is to provide understandable,
straightforward, elegant code.
3
programming STYLE [Busbee & Braunschweig]
---is a set of rules or guidelines used when writing the source code
for a computer program. Following a particular programming style
will help programmers read and understand source code conforming
to the style, and help to avoid introducing errors.
4
programming STYLE [Busbee & Braunschweig]
[cont.]
Discussion:
5
programming STYLE [Busbee & Braunschweig]
[cont.]
7
programming STYLE [Busbee & Braunschweig]
[cont.]
For each of these items, check style guides for your selected programming
language to determine standards and best practices. The following are general
guidelines to consider.
Documentation
Documentation is usually placed at the top of the program using several
comment lines. The amount of information would vary based on the
requirements or standards of the company who is paying its employees
or independent contractors to write the code.
8
Vertical alignment
You see this within the documentation area. All of the items are aligned
up within the same column. This vertical alignment occurs again when
variables are defined. When declaring variables or constants many
textbooks put several items on one line; like this:
9
Vertical alignment [cont.]
This method of using one item per line is more readable by humans. It is
quicker to find an identifier name because you can read the list vertically
faster than searching horizontally. Some programmers list them in
alphabetic order.
10
comments
Experts have varying viewpoints on whether, and when, comments are
appropriate in source code. Some assert that source code should be written with
few comments, on the basis that the source code should be self-explanatory or
self-documenting. Others suggest code should be extensively commented, with
over 50% of the non-whitespace characters in source code being contained
within comments)
In between these views is the assertion that comments are neither beneficial nor
harmful by themselves, and what matters is that they are correct and kept in
sync with the source code, and omitted if they are superfluous, excessive,
difficult to maintain or otherwise unhelpful
11
indentation
For languages that use curly braces, there are two common indentation styles:
In either case, it is important to maintain vertical alignment between the start of the
code block and the closing curly brace.
The number of spaces used for indenting blocks of code is typically two or four
spaces. Care should be taken to ensure that the IDE or code editor inserts spaces
rather than tab characters for indents.
12
Meaningful Identifier Names Consistently Typed
As the name implies “identifier names” should clearly identify who (or
what) you are talking about. Calling your spouse “Snooky” may be
meaningful to only you. Others might need to see her full name (Jane
Mary Smith) to appropriately identify who you are talking about. The
same concept in programming is true. Variables, constants, functions,
and other identifiers should use meaningful names. Additionally, those
names should be typed consistently in terms of upper and lower case as
they are used in the program. Don’t define a variable as: Tiger and then
type it later on in your program as: tiger.
13
Meaningful Identifier Names Consistently Typed [cont.]
14
Any questions?
You can find me at:
▪ zalizam@uum.edu.my
▪ tzalizam@gmail.com
15