Errors are a GOOD Thing When Learning a Programming Language

This may sound counter productive, but encountering errors when learning a language is a good thing.  It helps you to understand how the language works and how to debug the code.  All code has errors, whether it is syntax or logical.  Logical errors are usually the hardest to find because the program is still working, it’s just not doing what it should be doing.  It could be as simple as something like setting a database column to Tiny Int, but then trying to store a Integer size value in there.  It will store the value, it just won’t be the value you started out with. You may not even know something is wrong until you go to view the value and you notice that it is obviously incorrect.

Syntax errors however are generally mistypes.  An upper case letter in a class name, versus a lower case letter, missing semi-colon, undeclared class, etc. All of a sudden the system is complaining that it can’t find a class and you have to figure out why.  Even though in general syntax errors should be easier to find, especially if you develop in a module format, constantly testing the modules as you go along, it can be very difficult when just learning.

For instance, I just got this cryptic error while going through the Zend in Action book….

Fatal error: Uncaught exception ‘Zend_Controller_Dispatcher_Exception’ with message ‘Invalid controller specified (error)’ in /home/../../lib/Zend/Controller/Dispatcher/Standard.php:242 Stack trace: #0 /home/../../lib/Zend/Controller/Front.php(946): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #1 /home/../../app/bootstrap.php(57): Zend_Controller_Front->dispatch() #2 /home/../../public/index.php(6): Bootstrap->runApp() #3 {main} thrown in /home/aboutnj/ on line 242

Now that doesn’t really tell me much. It doesn’t tell me where the error is occurring, I see the process that it is going for the Zend components, but none of my classes, other than the bootstrap.  So, since I am learning, I go through the system from where it last worked.  Since I am doing this step by step, I know that it was just working before I tried to include the menu items.  So it has to be related to the menu functionality.  One of the ways to narrow down a location of a syntax error is to comment out code from the beginning of the process and work your way down. If you are using a unit testing program, then you can put break points in to step through the program.  I generally use a mixture of both and I do think it may be better to just comment out code or purposely introduce errors into the code to see how it works when just learning.

One of the things I did for instance was purposely misspell one of the Zend classes that were in an if statement to make sure that it was finding it properly and there was no problem with that.  I walked through the system, commenting a line at a time that made external calls. I knew that after this statement – “$actionStack->pushStack($menuAction);” on page 73 was the problem.  But that line was not the actually cause of the problem. I confirmed that everything was okay in the IndexController.  After going through each process – I eventually commented out this line “<?php $this->headLink()->appendStyleSheet($this->baseUrl().’/css/menu.css’)?>” in the menu.phtml file.  Now many of you may be able to pick up the error right away – but when you are learning it is sometimes easy to forget what the proper case is for function names.  In this instance, the problem was with appendStyleSheet().  The correct name for the function is appendStylesheet – with “sheet” having a lower case “s”, not upper.  Once I corrected that, everything was good to go and I could continue developing the system.

Of course no where in the stack trace did it indicate to me that it was in the menu.phtml file that was causing the problem. I am sure that as I get more fluent in the Zend Framework, these error messages will be less cryptic to me or that I will discover better ways of narrowing them down, just like when I was an expert at PowerBuilder.  By purposely introducing syntax errors into the code, I could see how Zend was reporting these back and helped me to narrow down what type of error might be causing the problem.

Posted in Computers/Programming, New Developers.
Notify of

Inline Feedbacks
View all comments