Wednesday, November 23, 2005

Tips #3 : Conditional Statements

Today's tip will be really quick, lets talk conditional statements.

A neat suggestion was posted as a comment on my last article, suggesting a really good way to prevent logic errors from creeping into your code. Logic errors being the hardest type of errors to find in your code.

If presented with an if statement:
if(num == 0)
{
//do something.
}

Now, if you forgot to put the double equals in the condition like:
if(num = 0)
{
//Do something
}

An error like that might not be catched, the suggestion put forward was to declare the constant first, and then the variable after like :
if(0 == num) {} , this way if you forget one of the equals, the compiler will be sure to catch it.

You can take this step one further, by declaring the 0 as a constant, like this:
public final int ZERO = 0;
if(ZERO == num);
This just takes your code that 1 extra step to insure good coding practices.

Thank you Jeff, for that great tip!

Lets continue,
Since white space is never compiled into your program, you should never worry about making your code extra readible with space. A lot of the code that I have tested, has been really hard to traverse through because the authors don't leave enough white space between actions, let me demonstrate:

public class MyTestClass{
public final int SIZE = 5;
public static void main(String [] args){
for (int i = 0; i<>
public doJob() {
for (int i=0; i
if (i%2 == 0)
System.out.println("number: " i);}}}

Confused? Frustrated? Good!
Make your code clear to read; the clearer the code, the easier it is to find errors later.
And also dont forget to add comments!

//MyTestClass.java Author: Graham.
//Desc: Prints out numbers, then the even numbers
public class MyTestClass
{
___//Variables
___public final int SIZE = 5;

___//Main Method, prints numbers o - 4, then runs doJob() method
___public static void main(String [] args)
___{
_____for (int i = 0; i (LESS THAN) SIZE; i++)

_____{
_______System.out.println("number: " + i);
_____}
____doJob();
___} //End Main

___public doJob()
___{
_____for (int i=0; i (LESSTHAN) SIZE ; i++

_____{
_______if (i%2 == 0)
_______System.out.println("number: " i);
_____}
___}//End doJob
} //END Class

NOTE: Instead of LESSTHAN you'd obviously put in the symbol, which for some reason Blogger won't let me write.

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 countEvenNumbers().

Hope you have found this article useful! Thanks you.

Tuesday, November 22, 2005

Tips #2 : Variable Naming.

The next fundamental programming tip I can offer you is naming your variables appropriately. I find that a majority of logic or compile errors are caused by a misuse of variable names.

Here are some examples of bad naming conventions:
1. dim newvalue as Integer
2. int x = 0;
3. JLabel label1 = new JLabel();

Sure, laid out here, it is pretty easy to recognize what each variable does, but in a simple program of at least 500 lines of code it can prove rather difficult to track these variables.

Here are some suggestions.

In the first case, think about what newvalue represents. We know it is an integer but what is it used for? If it is a counter for a while loop, then how about doing this instead,
dim iCount as Integer
It is quite clear that this variable is of type interger and will be used as a counter.
The second example is sort of the same situation. A lot of the times one letter variables are declared in C++ and Java, for mathmatical algorithm or for loop statements. In those cases I feel it is perfectly good coding structure to use these variables as is. If this variable was to refer to a randomly produced interger used in a lottery machine, then the use of x as that variable name proves quite ambiguous.
In the third case JLabel label1 = new JLabel(); in this case, you should be more descriptive as to what label1 points to, either what sort of information is produced from this label, or where it is located.
If this label was created as the title of a program, then I would call it JLabel lblTitle = new JLabel(); If it refered to a name of a person I would declare it as JLabel lblName = new JLabel();

Pretty straight forward, and these little tips will definitly help you efficently test your code.

Also, before I forget, a few quick tips on composing your variables.
Hungarian notation is one of the most common format styles.
iNum = an integer value
dNum = a decimal value
sName = a string value
btnSubmit = a button
lblStudentAverage = a label
as Shown above, use the first initial of your variable to represent what data type it is. here are a few other examples
obj -> Object
arr -> Array
txt -> Text Box
cbx -> Combo Box

There you go, If you have any questions or comments, feel free to leave me comments.

Monday, November 21, 2005

Tips #1 : Commenting

I've spent the past couple of days learning a new computer language, PHP. I'm currently in the process of developing an online management system, and after a careful anaylsis PHP seemed to be the right language to develop with.

So I have been going through some tutorials and looking at lots of coding examples. As a pretty experianced programmer, I am still having a hard time understanding some code developed by other people.

Which leads me to the first tip in this series.

The following coding tip is very straight forward, but ESSENTIAL to keeping you effective in the Information Technology Industry, especially when working in a team enviroment.

1. Comment your code:
If anyone is going to be reviewing your code at any point in time, you should always leave appropriate commets to help guide them. Here is how I structure my commenting, lets use JAVA for this example

  • /////////////////////////////////////////
  • // Title of Program Author Name
  • // Date of creation
  • //
  • //Breif description of the program
  • /////////////////////////////////////////
  • public class TestClass
  • {
  • //Variables
  • protected int iCounter; Here is where you would list all your global variables
  • //Methods List all the methods after this for good programing structure
  • ////////////////////
  • // main(String args [])
  • // Do: What is the main function of this method
  • // Input: What is brought into the method
  • // Returns: What the method returns
  • public static void main(String args[])
  • {
  • //
  • }//End main Method--> Keep track of your end braces with a small comment
  • }// EndTestClass
  • Commenting while you program does not take a lot of time, and it will definitly pay off for you later, when you try to review some comple code.

Thanks for checking out my tip! come back soon for another one, or leave a comment if you want me to cover a certain topic.

Saturday, November 19, 2005

Concise Coding.

As the name of my journal suggests, I will be using this space to provide programmers of all expertise with tips to create good quality code as quickly as possible.

In an Industry which changes rapidly, the ability to be an effective team member and adapt to the changing technologies is extremly important. It can quite possibly reflect a programmers security in the company with which he is apart.

So come back soon, and hopefully I will have some tips for you to better your skills.

Post a comment with questions if you wish for me to address any issues in particular

Thanks.

The first Post

I'm just getting things set up here. Come back soon for some programming tips.