Debugging Classes

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Debugging Classes

Post 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..
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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'?)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I really don't know, it's all pretty tightly integrated together. I wish I could post it.
GM
Forum Contributor
Posts: 365
Joined: Wed Apr 26, 2006 4:19 am
Location: Italy

Post 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();
}
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Stick it in a paste-bin (google "pastebin") and post the link here :)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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) :(
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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..
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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!
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Just out of curiousity, how long would it take you guys to make something like that?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
Yossarian
Forum Contributor
Posts: 101
Joined: Fri Jun 30, 2006 4:43 am

Post 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.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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.
Post Reply