Page 1 of 2

Debugging Classes

Posted: Wed Jul 26, 2006 3:19 am
by Benjamin
I'm working on a 1000 line class that I wrote. It's very complex in nature and I'm not used to writing classes so designing it has been a pain staking process. Debugging it is a royal pain though.. 8O

It's taking forever! Comments? It gets easier I assume..

Posted: Wed Jul 26, 2006 3:35 am
by Jenk
1,000 lines?

Could you break down the functionality of the class, into sub-classes? (i.e. have you created what is known as a 'blob class'?)

Posted: Wed Jul 26, 2006 3:37 am
by Benjamin
I really don't know, it's all pretty tightly integrated together. I wish I could post it.

Posted: Wed Jul 26, 2006 3:38 am
by GM
1000 lines seems pretty big for a class.

My main debugging tool:

Code: Select all

function preprint_r($in_array, $die = true) {
    echo "<PRE>";
    print_r($in_array);
    echo "</PRE>";

    if($die) die();
}

Posted: Wed Jul 26, 2006 3:44 am
by Jenk
Stick it in a paste-bin (google "pastebin") and post the link here :)

Posted: Wed Jul 26, 2006 3:51 am
by Benjamin
What it does is..

1. Displays a dynamic form and asks some general questions pulled from a database
2. Validates the answers based on possible answers
3. Displays another dynamic form and asks some more questions
4. Validates the answers to this dynamic form based on possible answers
5. Displays 1 or 2 more dynamic forms with questions that are pulled from a database based on the responses to the first questions. There are 4 possible dynamic forms that can be displayed, redOne, redTwo, blueOne, blueTwo, and based on the results of the first form it can display any combination of them in any order.
6. Determines what form to redisplay if anything isn't valid and repopulates all the other selected answers so they don't need to be answered again and marks invalid responses with a red box
7. Saves the results of the questions into a database table upon success

All the forms are created dynamically based on questions stored in a database table. Questions and answers are added via an admin panel.

I can't post it as the code is proprietary (for a client) :(

Posted: Wed Jul 26, 2006 3:55 am
by jamiel
What about splitting it out into a Form_Validator class, Form_Display, Form_Process ...

Otherwise we have found that Extending HTML_QuickForm in the PEAR Libraries keeps our form classes really small and clean as Most validation and display logic is done already.

Posted: Wed Jul 26, 2006 4:01 am
by Benjamin
I'm not familar with PEAR. I figure if I write all my own libraries and framework it will make me a better programmer in the long run.

I'm at the point where I'll have this done by morning and then I have a ton of other stuff to do yet otherwise I would experiment with breaking it apart.

I guess I'll just figure it out as I get more experienced writing complex classes..

Posted: Wed Jul 26, 2006 5:07 am
by Christopher
astions wrote:What it does is..

1. Displays a dynamic form and asks some general questions pulled from a database
2. Validates the answers based on possible answers
3. Displays another dynamic form and asks some more questions
4. Validates the answers to this dynamic form based on possible answers
5. Displays 1 or 2 more dynamic forms with questions that are pulled from a database based on the responses to the first questions. There are 4 possible dynamic forms that can be displayed, redOne, redTwo, blueOne, blueTwo, and based on the results of the first form it can display any combination of them in any order.
6. Determines what form to redisplay if anything isn't valid and repopulates all the other selected answers so they don't need to be answered again and marks invalid responses with a red box
7. Saves the results of the questions into a database table upon success
Yikes, that's an appplication -- not a class. It undoubtedly needs to be split up. I can imagine it has become a real pain to deal with because it is essentially a namespaced procedural program from the looks of it.

Posted: Wed Jul 26, 2006 5:11 am
by Benjamin
arborint wrote:Yikes, that's an appplication -- not a class.
LOL.. Shows what I know :)

It's pretty cool though you can call it with one line of code. I'll get the hang of this OOP stuff soon. I did have to change to an editor that had code folding though lol.

The database class and validation classes are seperate!

Posted: Wed Jul 26, 2006 5:21 am
by Benjamin
Just out of curiousity, how long would it take you guys to make something like that?

Posted: Wed Jul 26, 2006 5:34 am
by Jenk
Depends on the complexity in all fairness, but I would imagine a day or two (in man-hours) to get an initial version up and running, then an extra half-day to a day to iron out bugs :)

Also depends on the availability of pre-defined classes etc. If completely from scratch then a lot longer, however if I have 'common' classes available (such as a database class) then a lot quicker :)

Framework ftw.

Posted: Wed Jul 26, 2006 5:56 am
by Chris Corbyn
astions wrote:Just out of curiousity, how long would it take you guys to make something like that?
Yeah that's not really to much to judge anything by. You should spend roughly twice as young planning and designing it as what you should actually spend coding it. The thing is, if your design is unmanageable it's going to take even longer to finish by the time you get so far in because you end up adding code-hacks to it purely to "get it working" only to find you just broke something else. Yes. We've all been guilty of it. Good design from the start can save you heaps of time, even if it feels like you should be sat writing the code.

Posted: Wed Jul 26, 2006 10:45 am
by Yossarian
Hi,

In terms of testing and debugging at some point you might want to consider using a unit tester, you write tests as you write the code. Google for SimpleTest or PHPUnit.

As for your "god class", as I have seen it termed, I think many of us have done it, though not as spectacularly as this... :wink:

One of the big ideas of using oop is to reuse bits of code in other similar applications, so nice small, tested and reusable classes is the usual target I use.

Posted: Wed Jul 26, 2006 10:57 am
by JayBird
astions wrote:What it does is..

1. Displays a dynamic form and asks some general questions pulled from a database
2. Validates the answers based on possible answers
3. Displays another dynamic form and asks some more questions
4. Validates the answers to this dynamic form based on possible answers
5. Displays 1 or 2 more dynamic forms with questions that are pulled from a database based on the responses to the first questions. There are 4 possible dynamic forms that can be displayed, redOne, redTwo, blueOne, blueTwo, and based on the results of the first form it can display any combination of them in any order.
6. Determines what form to redisplay if anything isn't valid and repopulates all the other selected answers so they don't need to be answered again and marks invalid responses with a red box
7. Saves the results of the questions into a database table upon success.
Whoa man, what were you thinking...all that in one class :lol: Nice effort.

You should definately consider splitting that into smaller classes.