220 likes | 245 Views
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. Background. RESULT: “Spaghetti Code”. Background. “Spaghetti Code”
E N D
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.
Background RESULT: “Spaghetti Code”
Background “Spaghetti Code” very difficult to Debug almost impossible to Maintain not Reusable
Background Solution: Structured Programming Organize and encapsulate code into clear, maintainable segments, each with one Entry and Exit point.
Background Structured Programming: Example 1
Background Structured Programming: Example 2
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
C++ return from anywhere in a function: Actually a major violation of SS, but commonly used by C++ programmers.
C++ return from anywhere in a function: int main() { ... if (something) return 1; ... return 0; }
C++ continue jump to the end of a loop somewhat common in C++
C++ continue while (something) { //loop control ... if (something) // jump to beginning of loop continue; ... }
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++
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
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.