Forum Moderators: coopster

Message Too Old, No Replies

How I write PHP

...or any other language for that matter

         

vincevincevince

1:15 pm on Sep 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's how I calculate my logic, however simple or complex:

First, I think through manually performing the task and, where possible, work it once by hand.

Next, I think back through what I did, tiny step by tiny step and write it down as a long list of comments. If the concept you're writing down doesn't seem easily calculated by a computer, think through exactly and precisely how you execute the concept and write it down as multiple steps.

Finally, I turn the comments into code - normally one line of code per comment I'd written.

I don't believe in 'magic leaps of the human brain' when it comes to most tasks which can be expressed in numbers and words... when you break things down small enough they almost always result in small logical instructions.

Example:
Finding the coordinates of the row and column dividers of a table from an image of the table. Sound complex but it's actually simple. If I printed the table and gave you a ruler you could do it without a problem.

What are you doing? On the highest scale you're recognising row and column dividers and measuring their location.

Divide it into two: one set of recognising row dividers, one set of measuring column dividers.

To recognise row dividers, you are moving from the top to the bottom of the image.
//loop from y=0 to y=y_max

As your eye moves downward, you stop when you recognise a line which runs across the table. What does that mean? What is a line? A string of dark colours. So, find the longest string of dark colours.
//subloop from x=0 to x=x_max
// - if current location > threshold darkness then
// - - add to test_counter
// - else
// - - if test_counter>counter
// - - - set counter as test_counter
// - now reset test counter and try for another long string of dark on this row
//end subloop
//if counter > threshold * x_max then record this as a row, unless previous y value was also a row (to avoid multiple counts for thick lines)
//end loop on y
Column logic is the same, just with the loop parameters exchanged.

So - how do you work out your logic?

d40sithui

6:05 pm on Sep 21, 2007 (gmt 0)

10+ Year Member



i usualy just wing it =p

gettopreacherman

7:12 pm on Sep 24, 2007 (gmt 0)

10+ Year Member



amen! Wing it is the way to go! unless it's a huge system you're designing, a lot of times, winging it gives me a creative solution that I never would have "Logically Deduced..."

Habtom

5:46 am on Sep 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



winging it gives me a creative solution

You could be getting a good creative solution at the expense of the best one you might have got by logically dividing the task at hand.

I don't believe many people do care about the long term maintenance expenses of the codes and how tough or easy it is going to be to add the smallest task possible.

You see it all the time.

jatar_k

11:32 am on Sep 25, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I do relatively the same thing V3 though I don't always write it down in detail. I am more likely to write down the higher level portions and then go through them individually.

I break it down in a similar way and then start at the beginning. Then step by step and piece by piece.

If something is more complicated then I do exactly as you laid out.

gettopreacherman

1:56 pm on Sep 25, 2007 (gmt 0)

10+ Year Member



Habtom,

I'm not saying I don't think things through, if it's a big project, of course I plan, but if it's small, I wing it, I have come up with some good stuff in the past that way.

henry0

3:20 pm on Sep 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First I map it
Two break it in components
Three gather reusable chunks, functions and classes
And build missing pieces