views:

290

answers:

3

Somebody said that when your PHP Code and application use global variable then it must be a spaghetti code (i assume this). I use wordpress a lot. As far as i know, it's the best thing near a great php software. And it use many global variables to interact between its components.

but forget about that, cause frankly, that's the only thing i know. so it's completely biased ;D

so 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 great answer for somebody who have a little experience on developing full web application on PHP (for example, stack-overflow website)

+10  A: 
  • 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. (I.e. all-encompassing classes or functions)
  • Long functions.
  • Poor naming.
  • No consistent coding style throughout.
  • No clear interface contract between implementation and clients of code. (I.e. 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. Copy-Pasta)
  • 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 have a little experience on developing full web application on PHP (for example, stack-overflow website)

Just an FYI, but StackOverflow was not developed with PHP.

Alex
@alex : what ? i assume every web in this world develop using PHP ;D
justjoe
Guess we found the one that doesn't!
Alex
+6  A: 

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

Col. Shrapnel
Well.. it's not just that it's source is unknown, but it's not properly encapsulated. If you start throwing everything to global space, you start getting name clashes, and other problems...
Mark
+3  A: 

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.

APC