| The third tip in the series is about Conditional | | | | the easier it is to find errors later. |
| statementsA neat suggestion was posted as a | | | | And also dont forget to add comments! |
| comment on my last article, suggesting a really good | | | | |
| way to prevent logic errors from creeping into your | | | | //MyTestClass.java Author: Graham. |
| code. Logic errors being the hardest type of errors to | | | | //Desc: Prints out numbers, then the even numbers |
| find in computer code today.If presented with an if | | | | public class MyTestClass |
| statement: | | | | { |
| if(num == 0) | | | | ___//Variables |
| { | | | | ___public final int SIZE = 5; |
| //do something. | | | | |
| } | | | | ___//Main Method, prints numbers o - 4, then runs |
| | | | doJob() method |
| Now, if you forgot to put the double equals in the | | | | ___public static void main(String [] args) |
| condition like: | | | | ___{ |
| if(num = 0) | | | | _____for (int i = 0; i < SIZE; i++) |
| { | | | | _____{ |
| //Do something | | | | _______System.out.println("number: " + i); |
| } | | | | _____} |
| | | | ____doJob(); |
| An error might not be catched, the suggestion put | | | | ___} //End Main |
| forward was to declare the constant first, and then | | | | |
| the variable after. | | | | ___public doJob() |
| for example: | | | | ___{ |
| if(0 == num) {} , this way if you forget one of the | | | | _____for (int i=0; i < SIZE ; i++ |
| equals, the compiler will catch it.You can take this step | | | | _____{ |
| further, by declaring the 0 as a constant: | | | | _______if (i%2 == 0) |
| public final int ZERO = 0; | | | | _______System.out.println("number: " i); |
| if(ZERO == num); | | | | _____} |
| This just gives your code that extra step to insure | | | | ___}//End doJob |
| good coding practices.Thank you Jeff, for that great | | | | } //END Class |
| tip!Lets continue, | | | | |
| Since white space is never compiled into your | | | | Now, if I was going to critique my own code here, I |
| program, you should never worry about making your | | | | would say that the method doJob() is pretty |
| code extra readible with space. A lot of the code that I | | | | ambiguous, I should have called it countEvenNumbers(). |
| have tested, has been really hard to traverse through | | | | |
| because the authors havn't left white space between | | | | Hope you have found this article useful! Thanks |
| actions, let me demonstrate: | | | | you.Graham McCarthy, has 6 years experiance |
| | | | developing software for both educational and business |
| public class MyTestClass{ | | | | oriented purposes. |
| public final int SIZE = 5; | | | | Website: |
| public static void main(String [] args){ | | | | |
| for (int i = 0; i | | | | Certification: |
| public doJob() { | | | | - A College Diploma in Computer Programming |
| for (int i=0; i | | | | Analysis from Fanshawe College in London, Ontario |
| if (i%2 == 0) | | | | Canada. |
| System.out.println("number: " i);}}}Confused? | | | | - A University Degree in Information Technology /w |
| Frustrated? Good! | | | | Honours from York University in Toronto, Ontario |
| Make your code clear to read; the clearer the code, | | | | Canada. |