1) Attempt to use a "top down" design
Top down design means to take high level concepts and break them down to simpler concepts. If we use the square root code as an example, here is how I would code the program top down (spelled out in plain English with defined variables)
Top level of program
- Get the User input
- Do Simulation
Second level of program
Get the User input
- valueOfC = readDouble("Enter a value to calculate the square root: "); //Lowest level possible
Do Simulation
- Check input versus assumptions
- If the input does not cause error, go to calculation else display error message
Third level of program
Check input versus assumptions (Lowest Possible Level)
- If valueOfC < 0, set canSimulate to false
- If valueOfC = 0 set guessValueOfX = 0, numberOfIterations = 1, canSimulate to true
- If valueOfC = 1, set guessValueOfX = 1, numberOfIterations = 1, canSimulate to true
- If valueOfC between 0 and 1, set rangeMax = 1, rangeMin = valueOfC, guessValueOfX = (valueOfC + 1) / 2, numberOfIterations = 1, canSimulate to true
- If valueOfC greater than 1, set rangeMax = valueOfC, rangeMax = valueOfC, rangeMin = 1, guessValueOfX = (valueOfC + 1) / 2, numberOfIterations = 1, canSimulate to true
| Java code for assumptions |
If no error, do calculation else display error message
- If canSimulate is true go to midpoint algorithm to solve problem else go to error message output
| Java code for checking simulation |
2) Use comments to explain various parts of your code
Comments do not affect the execution of the code. This means if you want to put a paragraph giving a brief explanation of what is happening during a section of the code, is probably valuable for you or another programmer to troubleshoot later. Without putting comments in certain areas, if you run into simulation problems or unexpected behavior, you may not have a good clue what is causing the issue. My general rule of thumb is to comment on what you expect to happen within each function or method you create and the expected state or outcome of the method. Also it is a good idea to have an area for a variable definition/description section. This way you can help yourself and/or other coders understand what a variable or constant may be used for if they don't understand based off the name or usage of the variable.
3) Account for potential user error or program exceptions
In the square root code, there is not many places that you would want to account for error or exceptions but I did attempt to account for an exception in the check assumptions section of the code. If you look at the snapshot of the assumption code above, I coded for all other cases outside of the assumptions we listed to display the error message. Since all possible numbers were accounted for, that portion of the code should never be utilized, but error that is the example I attempted to display. In reality, you would do error checking on inputs (like the user input) and connections to external items like files, databases or something that is dynamic. Our code that reads the user input has built in error checking, so if you enter a letter, it will spit out a message letting you know that is an invalid input and take you back to the collect user input code. One suggestion I would make is that you could add a maximum iteration check. Let's say you lower the tolerance check to 10^-10 trying to get an exact value, you could end up with billions of iterations! For the most part, your computer could handle it but if you have a really high complex simulation you may want to stop at some max value and display some note letting the user know they hit the maximum of iterations.
4) Don't get frustrated when you are debugging
Complex coding can usually get you into many creation of many "bugs" and strange behavior that is not expected even with good coding style. Assuming that you utilize good "top down" coding design and utilize comments whenever possible, you should be able to narrow down what is causing the unintended behavior.
Hopefully this blog is helpful. In the future, I will have good examples of coding for exceptions and how to account for potential error. Also I will write a blog entry on debugging but at this time, I would like to get a few more coding examples out there before I do that.
If you want to find the optimized square root code you can find it at this link.
If you want more information on Java coding style and top down programming watch lectures 2 through 4 on Stanford's YouTube page.
Syd
Next entry - Fun with Graphics part 1 of 5
