Link to home
Start Free TrialLog in
Avatar of StormyWaters
StormyWatersFlag for United States of America

asked on

Rewriting Spaghetti Code

I've got a program I wrote a while ago, written in c++ spaghetti code. I don't want to post the code here. It's a basic OCR program, and it works.

However, I want to convert it to a more object oriented approach.

Here's how it looks:

1. Read Image.
2. Display Image.
3. Find Letter Boundaries.
4. Algorithm:
  I. Read in the data from the textfile. Data is:
     Letter [tab] Weight [tab] 25 Boxes of data, representing the percentage of edgepoints
     in each division of the letter.
  II. For each letter in the datafile...
       a. Read in the same 25 boxes of data from the letter itself.
       b. Find the value of the difference between each corresponding box in the
          current character.  Get the total error.
       c. if this value is the lowest value so far, store it as the best possible character.
  III. Prompt the user. If the character is correct, go on to next letter.
  IV. If the total error is 53 or less, average it in with the best match of the corresponding letter.
  V. Else add a new value.
5. Display each character.

Can someone give me guidelines as to how to split this up into classes and objects? Pseudocode only please.
SOLUTION
Avatar of Mike McCracken
Mike McCracken

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
may i know what is spaghetti code? i just step by to ask this question. sorry for unable to help.
Beryl,

Spaghetti code is code whose source is unstable. Small changes in seemingly isolated areas of the code seems to drastically affect almost all other areas. Imagine a bowl of spaghetti and pull one strand. How much of the spaghetti moves?

Paul
Paul:

then why you want to write it at the first place when it is unstable?
or because there is no other way?

beryl
Beryl,

The program is stable, the code is stable, the source is not. There is always another way, money or design factors preclude those ways.

To avoid spaghetti code, design for the future. Be aware not just of what is required but what requirements will be more likely to change. Most spaghetti code is either written by youngsters or grows out of too many programmers and not enough vision. Remember that the too many programmers can come out of the age of the package just as much as mismanagement.

This topic could generate a lot of interest if asked as a separate question. Perhaps I will ask it.

Paul
Beryl,

Take a look here for a continuing discussion (I hope) of Spaghetti code.

https://www.experts-exchange.com/questions/21384213/How-can-we-avoid-spaghetti-code.html

Paul
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of StormyWaters

ASKER

Thank you all for your help. No perfect answers, of course -- you couldn't give me one without me posting my code, and it's far too long to post. But some great guidelines that should help me fix this thing up, not to mention what to look for.
Well, one thing more, actually -- I hope this isn't worth another question!

Should the classes interact with one another through their member-functions or through global functions?
Avatar of Mike McCracken
Mike McCracken

Glad i could help

mlmcc
Your last question:

I'm not an expert in class design. But:
Assume class A using class B (in the pseudocode above, class LetterPatternList using class LetterPattern). If I'd code the interaction into class LetterPattern, I'd create a circular dependency (LetterPatternList using LetterPattern but LetterPattern containing a method using LetterPatternList) - this would make it rather cumbersome to use class LetterPattern somewhere else. Essentially, we'd create "class spaghetti code". So, I'd put the interaction into class LetterPatternList. In this example, class LetterPatternList doesn't make any sense without class LetterPattern anyway, so there's no danger in coding the interaction into class LetterPatternList.

If however, two classes A nd B could very well coexist without interaction (the basically have nothing to do with each other) but they need to interact only in one or two special cases, I'd put the interaction into another class C. Class C would then contain the references to A and B and the methods to interact.

I usually avoid having too many global functions because it would be tempting to use them anywhere. This would then again create spaghetti code (on a higher level).

Hope this helps and thank you for the points.
One of the amazing features of oo development is that you can, initially, decide to write global code to manipulate your objects and then, later on, refactor so that the global code becomes part of further objects.

I was recently involved in the development of some motion sensing code. I started with a brain object that worked on the image data and controlled many other objects. Amazingly, I quickly realised that the test-bed code I'd written to import the images was actually a retina object and there was clearly distinguishable a lens object and an iris object and an optic nerve.

Generally, dont be afraid of writing global code, expect it to become part of another object later.

To answer your second question, "Should the classes interact with one another through their member-functions or through global functions?" you need to decide this for yourself. If the process is global, the function is global. Most would see 'new' and 'delete' as global for example. Write the code that does the interaction and, quite often, the model will evolve into a coherent set of methods, objects and global code.

Paul