Sunday, May 10, 2015

Debugging Programming

Forum post: Linus's Law... Yay or Nay?

************************************************

If you do an advanced search for the word "Linus"...

1. General category
2. Topic titles only

... then you'd learn that there's a scarcity of threads on the topic of Linus's Law.  Which is, from my perspective, a problem.  So here we are!

Linus's Law: given enough eyeballs, all bugs are shallow

The other day I completely failed to explain the broad relevance of this concept to my friend.  Maybe the fact that we were a "bit" drunk didn't help.  So rather than sharing my opinion on why Linus is so cool... I thought it might be fun to put this concept to the test.

It just so happens that I have a perfect problem for us to tackle together.  As some of you know, I really love the idea of micropayments.  Yesterday I decided to put my money where my mouth is.  I spent $60 dollars for a year's worth of hosting and installed a free forum... phpbb.  The forum's database was easy enough to modify... but I encountered a problem when I tried to modify the code to display the relevant information on the forum page.

How many of you know php?  I don't!  It's my first time working with it.  But I do have a decent amount of other programming under my belt.

If you don't know php... are you interested in trying to learn it?  If so, then here's your chance!  We can learn it together.  Playing around with a fully functional php website/forum is the easiest/best way to learn how to program.  It's the equivalent of learning snowboarding by starting off at a double black diamond slope.  Hah.  Seriously though, you already know how to use forums... so that's half of the battle.  The other half of the battle consists of making small changes to the code and seeing what happens.  Do it enough and you'll figure out what does what.

And to be really clear... I'm definitely not starting a micropayments forum to make money.  Because if I was... then I really wouldn't be here endeavoring to encourage and educate you all about programming a forum that facilitates micropayments!  Unless I'm the worst businessman ever.  Which is entirely possible.

Ok, so here are the steps...

Come up with a domain name.  Unfortunately, most of the good names are already taken!  It's really easy to get bogged down at this step.  What I recommend doing is to make a list of some of your favorite adjectives... furry, fluffy, funny... and then make a list of some of your favorite nouns... fork, fish, Fred.  Then come up with the craziest combination possible.  FunnyFork?  And then be surprised when GoDaddy informs you that it's already taken.  Repeat the process until you find a name that's available.  Don't worry if the domain name isn't awesome.  You can always change it later.  And your primary goal is to play around and have fun/frustration learning.

Choose a host.  I chose BlueHost for both my host and domain registration.  It's not really necessary that you choose the same host.  Although it will help a bit with getting started though.  Initially I tried Forumatic but their free version doesn't seem to offer direct access to the code or database.  And I didn't hear back when I e-mailed them about whether their paid version has access.

BlueHost is kinda obnoxious though because they say that they have plans starting at $3.49/month but when you get to the payment page you learn that it's only available if you sign up for 3 years.




I chose the starter price and deselected all the other options.  They might be useful down the road... but they aren't necessary for playing around with programming.

Install the phpbb forum.  This was a bit of an Easter Egg hunt for me...





Click on the "One-Click Installs" icon, scroll down to the "Forums" section and click on the phpBB icon.  You'll be asked where you want to install the forum in your website... I created a folder named "Forum".  The forum is automatically installed pretty quickly.  Within a few seconds your forum is up and running!  How easy and cool is that!

Find the forum database.  This was another Easter Egg hunt for me....





The icon "phpMyAdmin" is on the same page as the "One-Click Installs" icon.  Sign into phpMyAdmin with the same username/password that you used for BlueHost.  You'll want to bookmark the phpMyAdmin page.

Add a field to the posts table.  There are a few other changes you'll eventually need to make to the database but you just need to make one change in order to try and find the bug.





When I logged in, the column on the left was already populated with tables from the database.  I can't remember if this was the case when I logged in for the first time.  If it isn't, then click on the Database tab at the top left of the page.  There should only be one database to choose from.  When you click on it you'll see a list of tables.  Scroll down and click on the table that says "gpa_posts".




When you click on the posts table you'll see all the rows inside of it.  There's only going to be one row... which means that your forum only has one post!  Each row stores the information from one post.  All of these words that I'm typing now will be stored in a row in this forum's posts table.

I replied to the "welcome" post so there are two rows in my table.




Click the "Structure" tab at the top/left of the page...




Scroll down to the bottom of the page and click the "Go" button.  A window with a form will kinda pop up...





In Google's browser, Chrome, the window behaves kinda awkwardly.  You have to resize it a bit to get to the first text box.

Name: post_value
Type: decimal
Length/Values: 13,2

I didn't actually name my field "post_value".  For security reasons I gave it a different name.  I doubt it makes much of a difference though.  Everything's hackable if you try hard enough.

Scroll to the right and hit the save button.  And there you go!  You've modified a database!  You've added a field to the posts table.  This field will store the total amount of money that's been allocated to each post.  Of course it won't store the real money... it will just keep track of the amount of money.

Add $0.05 cents to the "post_value" field.  Click on the Browse tab, scroll all the way to the right and enter .05 into the row's "post_value" field.  Essentially we're saying that the admin (you) allocated a nickle to the first post.... which was created (but not really) by you.

Find the PHP code/files.  This was another Easter Egg hunt.  Go back to your host's home page...





Click on the "File Manager" icon.   Your website should already be selected.  Hit the command button (it's blank in Chrome).  Once you're at the directory page, add it to your bookmarks.  You'll probably want to create a folder for all the necessary pages.

Find the viewtopic.php file.  public_html > forum > viewtopic.php   Clicking on the folders opens/closes them and clicking on the text will display the contents.  This php page is the page that has the bug.  And it's a monster of a page.  You can view the code by right clicking on the file icon and choosing "view".  What this page does is it gets the information from the database.  There's another page that helps to display this information on the page.

Find the viewtopic_body.html file.  This was another Easter Egg hunt!   public_html > forum > styles > prosilver > template > viewtopic_body.html  This is the page that displays the information that the viewtopic.php retrieves from the database.

Save copies of these files.  Before you start messing with the code you should always make sure you save a copy of the original file.  This way, when you tenaciously, methodically, randomly or feverishly tear the code apart looking for the bug, when you finally find it you can put Humpty Dumpty back together again.

Start debugging!

As you can see, it's relatively easy to modify the database.  With a few clicks we added a column to a table.  The problem is that for some reason I can't get the $0.05 cents to display on the webpage!  The world will be a lot better place when displaying information in a database is as easy as modifying the database.

To start debugging... right click on each file and click "edit".  A dialogue box will pop up... just hit "ok" or whatever.

In the html file, hit ctl+f to search for "sig{postrow.POST_ID}".   This will take you to the part of the code that's responsible for displaying our signatures.  How cool is that!  This is right around the area where we might want to add the coin buttons and display the members' valuations of each post.

After the "<!-- ENDIF -->" code... copy and paste the following snippet...

<div class="signature">{SillyGoose}</div>

"SillyGoose" is a variable.  A variable is used to store information.  Just like in math...

x = 2

When I first started learning programming I kept choosing names for variables that were already reserved for other purposes.  This led to problems.  So I picked up the bad habitat of coming up with variable names that I was certain wouldn't already be taken.  In good programming practice you want to use variables that will make it easy for other programmers to figure out what information the variable is supposed to store.

Save the html file and go to the php file.  When you see a forward slash "/"... it means that the computer is going to ignore what follows.  For example...

// now I have the urge to wash my hands :(

This is the programmer telling us, rather than the computer, something.  What do you think he means?  I think it means that he feels kinda dirty because some of the coding is... questionable.  Can I complain though?  Not really.  It's not like my code would have been any better.  Plus, the guy was nice enough to donate his time to helping to create a program that all of us are using right now!  Thanks guy!  Whoever you are.

Search the php file for "$now->getOffset());"  Immediately after this code... hit enter a few times and copy and paste this...

///////////////////////////////*************************************///////////////////////////////
///////////////////////////////*************************************///////////////////////////////


$template->assign_var('SillyGoose', $sql);


///////////////////////////////*************************************///////////////////////////////
///////////////////////////////*************************************///////////////////////////////

The forward slashes and asterisks are just for helping you find this code again.  You can also ctl+f for "silly" or "goose".  What this code does is it will help us figure out what in the world is going on.   I'll break it down...

$template = the dollar sign indicates that we're dealing with a variable.  "Template" refers to the viewtopic_body.html page.

->assign_var = we're putting some information into the variable

SillyGoose = our variable on the html page

$sql = structured query language.  This is how we talk to the database.  Whatever is in this variable will be placed into SillyGoose.

In more or less plain language... this code means something like...  what in the world are we requesting from the database?  Let's find out by sending the request to the html page!

Save the php page and refresh your forum page that displays the first post.  This is what you should see...





Voila!  You've modified the forum!  Perhaps it's not the most spectacular modification but it's something!  If there's no voila then it's probably because you have to purge the cache.  At the bottom of the page, click on the "Administration Control Panel" link.  And then ctl+f "cache" and click "run now".  Say ok, and then refresh the "Voila" page.  You should see it then.

Copy the code...

SELECT u.*, z.friend, z.foe, p.* FROM (gpa_users u CROSS JOIN gpa_posts p) LEFT JOIN gpa_zebra z ON (z.user_id = 2 AND z.zebra_id = p.poster_id) WHERE p.post_id IN (1, 2) AND u.user_id = p.poster_id

...and go back to your database.   Click the SQL tab, paste the code into the text box and hit "Go".  What you should see displayed is a row that contains columns/information from various different tables.  If you scroll to the right you'll see that the column that you created "post_value" is included!  Ah ha!!  The preexisting php code already retrieves the information that we need!!  Unfortunately, something happens in the subsequent code that results in our $0.05 cents being lost.  :(

This is basically how the debugging process works.  It's real detective work in which you try and eliminate the usual suspects.  You have to narrow the search as much as possible so that you can find the bug.  Using the SillyGoose code snippets will allow you to figure out what the different variables contain at different times and places.

According to Linus's Law... the more people who see this code... the greater the chances that the bug will be found.  What do you think?   Are two heads better than one?

What's your prediction?  Is there any member of this forum who can find the bug?  Have I inspired any of you to run out and learn php?  Have I inspired any of you to brush up on your php?  Should programming be a required class like spelling and geography?  Do any of you prefer this topic over my usual topics?

************************************************

Found the bug!  Kinda.  When I started over I did something differently because it worked.

No comments:

Post a Comment