What are the characteristics of spaghetti code?

2019-02-06 11:42发布

问题:

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 the best thing near great PHP software. And it uses many global variables to interact between its components.

But forget about that, because frankly, that's the only thing I know. So it's completely biased ;D

So, I am just curious, What is the characteristic of spaghetti code?

PS: the only thing I know is WordPress. So, hopefully, maybe this will help somebody give a great answer for somebody who has little experience in developing a full web application on PHP (for example, the Stack Overflow website).

回答1:

  • No modularity (everything in one file, class, module, namespace, package, or whatever your language uses to provide modularity),
  • Plenty of goto's,
  • Poor organization,
  • No clear separation of functionality and purpose. (That is, all-encompassing classes or functions)
  • Long functions.
  • Poor naming.
  • No consistent coding style throughout.
  • No clear interface contract between implementation and clients of code. (That is, no specification of what the inputs, outputs, pre- and post-conditions of functions are)
  • Over-reliance on internals of data structures with little abstraction.
  • Functions randomly permute/modify global state without any mention of it in documentation.
  • Lack of comments or documentation of non-trivial code.
  • Code that is more complicated than it needs to be.
  • Lack of reuse. (plenty of duplicated code, a.k.a. copypasta)
  • No verification or unit testing (it works on faith).
  • Magic numbers.

In essence, a lack of design and forethought, and just a mishmash of hacks slapped together. This applies to any language, not just PHP.

for somebody who has little experience in developing a full web application on PHP (for example, the Stack Overflow website)

Just FYI, but Stack Overflow was not developed with PHP.



回答2:

Well, talking of comment you posted, the explanation is very simple. Using global operator makes source of a variable is unknown, like other end of spaghetti noodle. It can be defined everywhere. So, when you call your function, you have no idea what value this variable has. Instead of it, direct passing a variable makes it plain and clear:

function hello_testing($conditional_random) {
  if ($conditional_random)) {
      echo "foo is inside";  
  }
}

P.S. http://en.wikipedia.org/wiki/Spaghetti_code



回答3:

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).

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.



回答4:

WordPress is the biggest piece of spaghetti code PHP I have seen around. There is a shocking mix of PHP, HTML, JavaScript and all things in between all lumped in the same files. If you want another example of spaghetti code look at osCommerce or Zen Cart.

In fact I dare say a large majority of open source PHP applications are pretty shocking examples of how to program in PHP. If you want to look at a good structured example (that is, non-spaghetti) then look at Yii framework or Zend Framework. Frameworks like CodeIgniter and Kohana, although not spaghetti, are not very good examples of how to structure things in PHP 5 as they use many of the features used in PHP 4 simply because there was no better way of doing them until PHP 5 (for example, using path based inheritance instead of true object inheritance).

If you want a reasonbly good example of procedural programming done right look at Drupal. It might not be the best performing PHP application, because of the complexity, but it sure beats WordPress and you can do many of the same things with it.