What are the characteristics of spaghetti code?
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
- 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.
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.
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.