Coding Constructs considered Violations of Structured Programming Black Magic Coding Constructs considered Violations of Structured Programming
Background Programming Languages before the 1980’s allowed program flow to “bounce around” anywhere the programmer wanted.
RESULT: “Spaghetti Code” Background RESULT: “Spaghetti Code”
Background “Spaghetti Code” very difficult to Debug almost impossible to Maintain not Reusable
Solution: Structured Programming Background Solution: Structured Programming Organize and encapsulate code into clear, maintainable segments, each with one Entry and Exit point.
Structured Programming: Example 1 Background Structured Programming: Example 1
Structured Programming: Example 2 Background Structured Programming: Example 2
Many battles were fought in the 1960’s-80’s Background Many battles were fought in the 1960’s-80’s
Background Structured Programming Won. A major result: the standard use of the Pascal language to teach new programmers. Pascal enforces concepts of Structured Prog.
C++ There are statements in C (and so C++) that violate Structured Programming to some degree
return from anywhere in a function: Actually a major violation of SS, but commonly used by C++ programmers.
return from anywhere in a function: int main() { ... if (something) return 1; return 0; }
continue jump to the end of a loop somewhat common in C++
while (something) { //loop control continue while (something) { //loop control ... if (something) // jump to beginning of loop continue; }
a little more common in C++ break exit a control block (loop, switch, if, etc.) a little more common in C++
C++ break; while (something) { //loop control ... if (something) break; // exit the loop } // jump to here
C++ “The word that SHALL NOT be uttered” banned by most C++ programmers unrestricted use in C major restrictions in C++
The word that SHALL NOT be uttered: GOTO C++ The word that SHALL NOT be uttered: GOTO
CS 215 The following should NOT be used in any CS 215 programs (labs, projects, tests) continue break (except in a switch) goto
The following is allowed: return in the middle of a function CS 215 The following is allowed: return in the middle of a function
After 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
Test Nothing from this set of slides will be on the tests... except NOT using the “banned” statements.