In Depth Software Development Strategies, Tip 3: Conditional Statements

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