Spaghetticode

Spaghetti code is a pejorative term for software source code, which has confused control structures. An indication of this may be about the excessive use of jump instructions (such as GOTO ) be, with excessively means that the same goal could also be reached with fewer jumps. Every kinky and also for experienced programmers poorly comprehensible piece source code can be referred to as spaghetti code. Such written software does not function poorly; also means spaghetti code is not necessarily a poor commentary on the individual programming steps.

Spaghetti code can have different causes. Often inexperienced programmers tend to " just go to program ", which inevitably creates spaghetti code. The repeated later expansion of the source code without refactoring is performed, can lead to confusion and thus to spaghetti code.

Compared with clearly structured source code has spaghetti code significantly worse maintainability, including increased testing and maintenance costs over. In addition, Spaghetti code can be reused in similar requirements usually much worse or only with great effort.

Example

The following, created in the programming language BASIC program prints the numbers 1 to 10 together with the corresponding square on the screen. The two successive GOTO statements make the spaghetti code: you are not needed, and they mean stumbling blocks for those who are interested to understand the code. In practice, spaghetti code is usually much more complex.

10 i = 0 20 i = i 1 30 PRINT i; " Squared ="; i * i 40 IF i> = 10 THEN GOTO 60 50 GOTO 20 60 PRINT " Program Fully Completed. " 70 END In plain language, the program says to catch ' at 0, then always raise you a 1 Bring the result to the screen, along with its square. If the number is greater than or equal to 10, jump down. Otherwise, jump to the beginning. Done.

The following program does the same, but comes without the two jumps. It is also shorter and therefore more elegant. Paraphrased, it works like this: Begin with 1 and going to 10, bringing the respective number on the screen, along with its square. Go get the next number from the pool. Done.

10 FOR i = 1 TO 10 20 PRINT i; " Squared ="; i * i 30 NEXT I 40 PRINT " Program Fully Completed. " 50 END There are also other signs of spaghetti code. The GOTO jump in the first program is still in the frame, but may mislead to:

10 CLS 20 i = 0 30 i = i 1 40 PRINT i; " Squared ="; i * i 50 IF i> = 10 THEN GOTO 70 60 GOTO 30 70 PRINT " Program Completed. " 80 INPUT " Do it Again ( j )"; sel $ 90 IF sel $ = "y" THEN GOTO 10 100 END This use of GOTO usually leads to the fact that you can jump back and forth between the program blocks, and thus inflicts a real spaghetti code mess. This is true not only for the GOTO statements. Gladly a " staple Chaos" is also with IF - blocks containing therein a plurality of blocks with IF, FOR, or other sub-procedures, caused, as the following program illustrates using IF, FOR, GOTO:

10 FOR ia = 1 TO 10 20 IF ia = 5 THEN 30 FOR ib = 1 TO 10 40 PRINT " LOOP ", ia; "SUB LOOP :"; ib 50 IF ib = 8 THEN GOTO 80 60 NEXT ib 70 END IF 80 PRINT " SUB LOOP :"; ia; "END" 90 NEXT ia 100 END This example is still manageable, but you should make bigger jumps in several levels, you end up with a source code which is no longer transparent at some point by the writer himself.

The greatest danger to produce themselves as programmers spaghetti code that arises when one uses a language that you do not have looks, or the commands for simple control loop missing, eg Assembler. In these languages, it is essential to work with jump commands so that you can quickly lose track. The best example of pure jumps is a finite state machine.

It is generally recommended to split the code into small, manageable units (methods, functions) and reuse the same parts. It may therefore be advantageous to paper to sketch the ideas of programming, and then build the source code.

740591
de