Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the characteristics of spaghetti code?

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

like image 437
justjoe Avatar asked Apr 04 '10 05:04

justjoe


2 Answers

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

like image 115
Alex Budovski Avatar answered Sep 30 '22 19:09

Alex Budovski


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

like image 26
Your Common Sense Avatar answered Sep 30 '22 17:09

Your Common Sense