Jump to content

Archive:Java/Programming tips: Difference between revisions

From IdeaWazaWiki
wikademia>Cremisis13
wikademia>Cremisis13
Line 25: Line 25:
Tabbing is necessary to understand your code more easily. For example, the following is very complicated to understand without any tabs:
Tabbing is necessary to understand your code more easily. For example, the following is very complicated to understand without any tabs:
<pre>
<pre>
public class MessyCoding extends Coding {public
public
class MessyCoding extends Coding {public
static void main(String
static void main(String
args[]
args[]
Line 57: Line 58:
}
}
</pre>
</pre>
==When to press Enter==
==When to press Enter==

Revision as of 23:11, 26 November 2006

When to use methods

About Arrays

Arrays should be used for variables that have something to do with each other. Basically, arrays are a list of variables, and arrays are a variable themselves. Here is how to make an array

[class][] [variable-name];
//Or...
int[] myInts;

To specify the length of an array:

myInts=new int[LENGTH];
//Put into one statement
int[] myInts = new int[LENGTH];

LENGTH = array size. It is simply how many items are in the array.

You need to initialize each part of the array. To "access" the members of the array:

myInts[2]==5;
int temp = myInts[3];

Note: Do NOT use arrays when the members have nothing to do with each other. This causes messy code!

Basic Tips for Improving Code

Tabbing

Tabbing is necessary to understand your code more easily. For example, the following is very complicated to understand without any tabs:

public
class MessyCoding extends Coding {public
static void main(String
args[]
){System.out.println
("This is messy coding. Don't do this");System.exit(0);}}

In much better format:

public class CleanCoding extends Coding {
   public static void main(String args[])
   {
      System.out.println("This is clean coding. DO THIS!!");
      System.exit(0);
   }
}

Notice that you actually can understand the code! Every single new block that has { and } should have contents that have an extra tab. Example:

public class CleanCoding extends Coding {
   public static void main(String args[])
   {
      try{
         try{
             ...;
         }
         ...;
      }
      ...;
   }
   ...;
}

When to press Enter