HTML and CSS Reference
In-Depth Information
Teamwork: The Danger of Spaghetti Code
Spaghetti code
is a pejorative programming term that refers to convoluted or poorly
written code. One hallmark of spaghetti code is the frequent branching from one section
of code to another, making it difficult to track the program line-by-line as it is executed.
A change in one part of the program could lead to unpredictable changes in a completely
different section of the code.
Most developers discourage the use of
break
,
continue
, and
label
statements unless
absolutely necessary. They can confuse a programmer trying to debug code in which a
program loop can end before its stopping condition, or code in which statements are not
processed in the order that they are written in a document. Almost all of the tasks you
perform with these statements also can be performed by carefully setting up the conditions
for program loops.
Even with the best of intentions, spaghetti code easily can occur in environments in
which the same code is maintained by several people or passed from one employee to
another. Each programmer adds a particular feature that is needed
today
without ade-
quately documenting the changes made to the code, and without considering the impact of
those changes on the larger program.
To avoid or at least reduce the occurrence of spaghetti code, you always should docu-
ment your code and develop a structure that is easy to follow. Break up tasks into smaller
functions that are easier to manage and can be reused in other parts of your programs.
Also, avoid global variables whenever possible because a change in the value of a global
variable can have repercussions throughout the entire code. Instead, use local variables
with their scope limited to small, compact functions. If a variable must be used elsewhere
in your code, it should be passed as a parameter value with the meaning and purpose of
the parameter well documented within the program.
By practicing good coding techniques, you can make your programs more accessible to
your colleagues and make it easier to pass your code on to your successors.
Session 12.3 Quick Check
1.
What is a conditional statement? What is the most commonly used conditional
statement?
2.
What code writes the text
Internet Explorer Browser
to the document if the
Boolean variable
WebBrowser
equals
true
?
3.
The
WebBrowser
variable has been changed to a text string variable that can
equal either
IE
or
Mozilla
. Write an
if else
statement to display the text
Internet Explorer Browser
if the
WebBrowser
variable equals
IE
, and to display
Mozilla Browser
if otherwise.
4.
The
WebBrowser
variable can now equal
IE
,
Opera
,
Safari
, or
Firefox
. Write a
series of
else if
statements that write the name of the browser to the docu-
ment. If
WebBrowser
equals none of the four text strings listed above, write the
text
Generic Browser
to the document.
5.
Answer the previous question using a
switch
statement. Use a
break
statement
to break off from processing the
switch
statement once a match has been found.
6.
What command extracts the day of the week value from a
Date
object variable
named
thisDate
?
7.
What command can be used to break out of the current iteration in a
for
loop?
8.
What command forces a script to go to the next iteration of the current
program loop?