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