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

Comments

what do you mean under global variable?

Written by Col. Shrapnel

i meant in this comment in stackoverflow.com/questions/2571474/…

Written by justjoe

Wordpress is one of the worst examples of PHP programming in the mainstream, and the main reason is that they rely extremely heavily on global variables. They have a terrible reputation for security because of it. I'd post examples of why it's bad, but I already wrote a long rant about it (phpvs.net/2009/12/08/…). If you want to get experience building PHP web applications, I'd suggest looking into some of the PHP frameworks out there, such as Zend, Symfony or CodeIgniter.

Written by zombat

@zombat Codeigniter isn't exactly pretty either. @justjoe have a look at stackoverflow.com/questions/2571078 and my answer for an example on how to improve spaghetti code.

Written by Gordon

(reference) sourcemaking.com/antipatterns/spaghetti-code

Written by Gordon

+1 great reference... @gordon : thanks, it's really really helpful. At least, to some extent, it's enlighten me. Even though, i still confuse by the jargon and jokes. ;D

Written by justjoe

@Gordon - 100% agree. Codeigniter had to do some ugly stuff to achieve backwards compatibility with PHP4, and the internal code is pretty messy. However, it has a reasonably straightforward API and decent documentation, so it's a lot easier to make decent code with it. That being said, I don't use it myself anymore ;)

Written by zombat

Accepted Answer

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

Written by Alex
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki