What are the characteristics of spaghetti code?

前端 未结 4 2223
囚心锁ツ
囚心锁ツ 2020-12-31 08:29

Somebody said that when your PHP code and application use global variables then it must be spaghetti code (I assume this). I use WordPress a lot. As far as I know, it\'s th

4条回答
  •  耶瑟儿~
    2020-12-31 08:46

    Spaghetti code has specific characteristics which distinguish it from plain poor code. Spaghetti is extremely complicated and unstructured, so it is hard to follow the flow of a process through the program. It is like trying to untangle the noodles in a bowl of bolognese.

    This is why GOTO statements (dread word!) are often cited in this context: a GOTO statement transfers control to another arbitrarily defined location in the code base. Most programming languages have commands which can be abused to simulate goto style behaviour; for instance, using exceptions to implement regular business logic rather than handling errors.

    Global variables contribute to spaghetti code because the values are assigned outside of the scope of the current program unit. This can make it difficult to determine where in the code base a variable is set to a given value (or indeed whether it is set to any value at all).

    Spaghetti code can be functionally correct and performative. It's a problem because it's hard to understand, so we can't be sure it is bug free and the lack of structure makes it difficult to troubleshoot. For similar reasons spaghetti code is brittle and difficult to change; the risk of introducing a bug is high.

    Incidentally, the use of goto statements does not mean a program is spaghetti. It is perfectly possible to write clear, well-structured code using goto, it is just requires a lot of self-discipline not to abuse its flexibility. Modern programming languages have made its use unnecessary, and undesirable.

提交回复
热议问题