[Newbies] Spaghetti code

Blake blake at kingdomrpg.com
Sun Feb 18 00:11:22 UTC 2007


I'm half thinking-out-loud here and soliciting thoughts on a programming  
lesson. If this sort of thing bugs you, feel free to ignore.
---
My son is coding this flow-chart as a Q&A type application:

http://media.www.gamespy.com/articles/633/633817/img_2913464.html

So, it says, "You found something!" then asks "Is it alive?" and then  
branches out accordingly. I'm letting him muddle through with the idea of  
using his code as a platform to teaching him a better approach (or  
approaches).

Looking at the chart, it looks like spaghetti code, and reminds me of some  
of the stuff I did in BASIC. And that's sort of what he's doing within  
Smalltalk. I'm actually tempted to show him how it would look in BASIC  
using GOTOs and line numbers. It would have the advantage of being easy to  
see all in one glance.

But I want to, of course, show him a better approach and explain why it's  
better. My first thought was to make a state machine, but I don't think  
that'd be much better, in fact.

Then I thought of what I would do, professionally, in a similar context:  
The problem with the spaghetti code and even a state machine is that it's  
static, and adding bits and pieces tends to require everything to be  
massaged around the new parts. In the real world, I know that the chart's  
not going to be static.

I'd probably end up create a linked list of one-exit nodes and two-exit  
nodes.

So, that's what I'll probably show him.

The other thing I'd do, though, is put this all in an editable text file,  
like:

1,foundsomething,"You found something",alive
2,alive,"Is it alive?",friend,smash
2,friend,"Is it a friend?",lately,scary
1,smash,"Smash it with a stick!",shiny

But I'm wondering if this last wouldn't be too much.

Thoughts?


More information about the Beginners mailing list