Hey,
I've seen "spaghetti" thrown about a lot and I just want a clear explanation on what is spaghetti code and what isn't. Just because I'd rather not be doing it.
References in PHP would help as that is my language of choice.
Hey,
I've seen "spaghetti" thrown about a lot and I just want a clear explanation on what is spaghetti code and what isn't. Just because I'd rather not be doing it.
References in PHP would help as that is my language of choice.
Here's a pretty decent definition of spaghetti code.
It's a subjective term that is used to describe code that is overly-convoluted, badly structured, hard-to-follow, inconsistent, reliant upon side effects (particularly global side effects) or some combination of these.
PHP, like most scripting languages, is loosely typed. You don't need to declare variables before using them. Many variables are used in a "global" scope and there are provisions for including files implicitly (autoloading), just to name a few. None of these are bad features but, used badly, they can very easily lead to spaghetti code.
The term "spaghetti code" is not at all specific to PHP - it applies to all programming languages and should appear quite similar in most languages (at least of the same paradigm). Perhaps you understand this however, and just want an example in PHP.
The Wikipedia article on this subject seems pretty clear:
Spaghetti code is a pejorative term for source code which has a complex and tangled control structure, especially one using many GOTOs, exceptions, threads, or other "unstructured" branching constructs. It is named such because program flow tends to look like a bowl of spaghetti, i.e. twisted and tangled. Spaghetti code can be caused by several factors, including inexperienced programmers and a complex program which has been continuously modified over a long life cycle. Structured programming greatly decreased the incidence of spaghetti code.
This forum thread, Top 100 Signs That I Am Writing Spaghetti Code in PHP, might be of some use to you, as it relates specifically to PHP.
The difference is spaghetti code is written by other developers. Your code is never spaghetti code.
All php code is spaghetti code :D
Seriously now, when you see PHP without a framework that separates html from php imo that creates a lot of mess.
Procedural php code often creates a mess, not always if you do it correctly.
Look here and here for more info :)
EDIT: Edited according to comments.
The classical definition of spaghetti code is "code whose control flow is hard to follow due to the use of unstructured branching". Since PHP, like most languages invented since the 1970s does not support GOTO, you theoretically can't write spaghetti code it in.
However, a good approximation can be achieved with long scripts and functions that make liberal use of global variables. Bonus points for deeply nested if/elseif/else cascades and bad formatting. Add in some wild mixing of HTML, JavaScript, PHP, PHP which generates HTML, and PHP which generates JavaScript which generates HTML, and you ensure that nobody can understand the code, not even the person who wrote it.
In well written code you should be able to clearly follow the flow not only from start to finish but also from finish to start (unless there is a fatal error condition, in which case your handler should have backtrace output). Follow the rule of one entrance and one exit for every function.
One way to avoid spaghetti code is make sure the return statement is the very last thing you write in a function and that you never have a return earlier than that.
If a function returns an unexpected value, clean code would allow you to start at the return statement and work backwards to see where it got it's value. However if the code is spaghetti then about half the time you will waste hours puzzled as to how the function could be returning that value until you happen to stumble across that second return statement buried in the middle of the function.
I strongly disagree with cletus and Andomar's claims that it's all subjective. A return statement in the middle of your function is clearly spaghetti code.
Michael Borgwardt, For some reason goto has been added to PHP 5.3: http://us2.php.net/goto
I haven't seen a good reason why it was added. I don't see it being useful, personally