In Depth Software Development Strategies, Tip 3: Conditional Statements

The third tip in the series is about Conditionalthe easier it is to find errors later.
statementsA neat suggestion was posted as aAnd 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 ifpublic 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 yourNow, if I was going to critique my own code here, I
program, you should never worry about making yourwould say that the method doJob() is pretty
code extra readible with space. A lot of the code that Iambiguous, I should have called it countEvenNumbers().
have tested, has been really hard to traverse through
because the authors havn't left white space betweenHope 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; iCertification:
public doJob() {- A College Diploma in Computer Programming
for (int i=0; iAnalysis 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.