Violations of Structured Programming Background Programming Languages before the 1980s allowed program flow to bounce around anywhere the programmer wanted Background RESULT Spaghetti Code ID: 789630
Download The PPT/PDF document "Black Magic Coding Constructs considered" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Black Magic
Coding Constructs consideredViolations of Structured Programming
Slide2Background
Programming Languages before the 1980’s allowed program flow to “bounce around” anywhere the programmer wanted.
Slide3Background
RESULT: “Spaghetti Code”
Slide4Background
“Spaghetti Code”very difficult to Debug
almost impossible to Maintain
not Reusable
Slide5Background
Solution: Structured ProgrammingOrganize and encapsulate code into clear, maintainable segments, each with one Entry and Exit point.
Slide6Background
Structured Programming: Example 1
Slide7Background
Structured Programming: Example 2
Slide8Background
Many battles were fought in the 1960’s-80’s
Slide9Background
Structured Programming Won.A major result: the standard use of the Pascal language to teach new programmers.
Pascal enforces concepts of Structured
Prog
.
Slide10C++
There are statements in C (and so C++) that violate Structured Programmingto some degree
Slide11C++
return from anywhere in a function:
Actually a major violation of SS, but commonly used by C++ programmers.
Slide12C++
return from anywhere in a function:
int
main() {
...
if (
something
)
return 1;
...
return 0;
}
Slide13C++
continue
jump to the end of a loop
somewhat common in C++
Slide14C++
continue
while (
something
) { //loop control
...
if (
something)
// jump to beginning of loop
continue;
...
}
Slide15C++
break
exit a control block
(loop, switch, if, etc.)
a little more common in C++
Slide16C++
break;
while (
something
) { //loop control
...
if (
something)
break; // exit the loop
...
}
// jump to here
Slide17C++
“The word that SHALL NOT be uttered”
banned by most C++ programmers
unrestricted use in C
major restrictions in C++
Slide18C++
The word that SHALL NOT be uttered:GOTO
Slide19CS 215
The following should NOT be used in any CS 215 programs (labs, projects, tests)continue
break
(except in a
switch
)
goto
Slide20CS 215
The following is allowed:
return
in the middle of a function
Slide21After 215
Once you’ve finished CS 215 and “Earned Your Stripes”
you may be allowed to use these statements, but...
Best to ask the Instructor
Slide22Test
Nothing from this set of slides will be on the tests...
except NOT using the “banned” statements.