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