Kentucky and colleges resource


In Depth Software Development Strategies, Tip 3: Conditional Statements

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 withnumbers
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 equalsruns  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;  iexperiance 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 clearerOntario  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.



1 A B C 45 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95