Jump to content

Archive:Java/Programming tips: Difference between revisions

From IdeaWazaWiki
wikademia>Cremisis13
wikademia>Wikademia
 
(18 intermediate revisions by 11 users not shown)
Line 1: Line 1:
{{ntnes}}
=When to use methods=
=When to use methods=
Only use methods when you have a reason to break-up code. You should only use them when the part you are putting into the method has no connection to any other part (or is called at a separate time from it). Methods should finish off a function by themselves. However, long methods can be broken down into shorter methods. They should still, however, follow the steps: no connection, etc.
Only use methods when you have a reason to break-up code. You should only use them when the part you are putting into the method has no connection to any other part (or is called at a separate time from it). Methods should finish off a function by themselves. However, long methods can be broken down into shorter methods. They should still, however, follow the steps: no connection, etc.
Line 5: Line 6:


=About Arrays=
=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
Arrays should be used for variables that have something to do with each other. Basically, arrays are a sets of variables, and arrays are a variable themselves. Here is how to make an array set.
<pre>
<pre>
[class][] [variable-name];
[class]() [var-name];
//Or...
//Or...
int[] myInts;
int() var;
</pre>
</pre>
To specify the length of an array:
To specify the length of an array:
<pre>
<pre>
myInts=new int[LENGTH];
var=new var(LENGTH);
//Put into one statement
//Put into one statement
int[] myInts = new int[LENGTH];
int() myInts = int(LENGTH);
</pre>
</pre>
LENGTH = array size. It is simply how many items are in the array.
LENGTH = array size. It is simply how many items are in the array.
Line 21: Line 22:
You need to initialize each part of the array. To "access" the members of the array:
You need to initialize each part of the array. To "access" the members of the array:
<pre>
<pre>
myInts[2]==5;
myInts(2)==3;
int temp = myInts[3];
int temp = myInts(3);
</pre>
</pre>
'''Note''': Do NOT use arrays when the members have nothing to do with each other. This causes messy code!
'''Note''': Do NOT use arrays when the members have nothing to do with each other. This causes code errors!
 
=Basic Tips for Improving Code=
=Basic Tips for Improving Code=
==Tabbing==
==Tabbing==
Line 64: Line 66:


==When to press Enter==
==When to press Enter==
To make your code look good, you should insert one empty line between methods:
<pre>
public null method1()
{
}
public null method2()
{
}
</pre>
You should make a new line after every statement/declaration. If a statement is too long, you can break it down.
== Good Comments ==
Good comments can help make your code look neat. They should tell what you are doing '''overall''', not what you are doing on each line. For example, you can comment an if statement if it is kind of complex, but not every statement has to be commented:
<pre>
// tests for parity, sign, and size
if( x%2 == 0 && x > 0 && x < 1000 ) {
System.out.println("x is a positive, even, integer less than 1000");
    if( x%3 == 0 ) // tests if divisible by 3
        x++;
}
</pre>
An example of bad commenting would be:
<pre>
if( i1 == i2 | i1 == i3 ) // Check if i1 is equal to i2 and i3
{
  System.out.println( "YES!" ); // Print Yes! to the command line
  i1= i1 + 1; // Add 1 to i1
}
</pre>
{{1lvlnav|[[Java|Java]]|[[Important Java Classes]]|[[Java/Fundamental Exercises|Fundamental Exercises]]}}
[[Category:Java]]
[[Category:Tips]]

Latest revision as of 19:14, 20 June 2009

When to use methods

Only use methods when you have a reason to break-up code. You should only use them when the part you are putting into the method has no connection to any other part (or is called at a separate time from it). Methods should finish off a function by themselves. However, long methods can be broken down into shorter methods. They should still, however, follow the steps: no connection, etc.

Essentially, methods should not be used for every statement. You should not, however, put a huge 1000 line program into the main method... common sense.

About Arrays

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

[class]() [var-name];
//Or...
int() var;

To specify the length of an array:

var=new var(LENGTH);
//Put into one statement
int() myInts = 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)==3;
int temp = myInts(3);

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

Basic Tips for Improving Code

Tabbing

Tabbing is necessary to understand your code more easily. For example, the following is very hard to understand because of many reasons, one being 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

To make your code look good, you should insert one empty line between methods:

public null method1()
{

}

public null method2()
{

}

You should make a new line after every statement/declaration. If a statement is too long, you can break it down.

Good Comments

Good comments can help make your code look neat. They should tell what you are doing overall, not what you are doing on each line. For example, you can comment an if statement if it is kind of complex, but not every statement has to be commented:

// tests for parity, sign, and size
if( x%2 == 0 && x > 0 && x < 1000 ) {
System.out.println("x is a positive, even, integer less than 1000");

    if( x%3 == 0 ) // tests if divisible by 3
        x++;
}

An example of bad commenting would be:

if( i1 == i2 | i1 == i3 ) // Check if i1 is equal to i2 and i3
{
   System.out.println( "YES!" ); // Print Yes! to the command line
   i1= i1 + 1; // Add 1 to i1
}



Project: Java
Previous: Important Java Classes — Java/Programming tips — Next: Fundamental Exercises