views:

551

answers:

7

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.

+2  A: 

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.

cletus
+5  A: 

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.

Noldorin
+3  A: 

The difference is spaghetti code is written by other developers. Your code is never spaghetti code.

Andomar
so true . :D
Arnis L.
I really don't see how this answer is so helpful!
Noldorin
@Noldorin It`s not, just funny.
Arnis L.
But it is! Really, this answer is THE answer to this question :P
shylent
I'm going to have to call bullshit on this. I'm all over spaghetti code like Clint Eastwood is all over spaghetti westerns.
Nosredna
A: 

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_drow
"Try to use as many X as possible" is *always* the wrong answer, no matter what X stands for. In your case, it's a recipe for over-engineered OO code that can be just as incoherent as classic spaghetti code.
Michael Borgwardt
A: 

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.

Michael Borgwardt
A: 

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.

Gerry
+1  A: 

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

Eric
You can click 'add comment' below each answer to specifically reply to that answer, instead of adding a new answer with no initial answering content, for future reference.
Oliver Stubley
I tried but it said I need 50 reputation to comment.
Eric
I would have pointed out the same thing you did when I first saw the answer but also couldn't for the same reason. I think it's very silly that you can leave a an answer, but not a comment even though they are both pretty much equal powers. Also it's so hard to get points when you're just starting out, so I've given you some. :)
Gerry