Learning Java/Programming tips: Difference between revisions

From Wikiversity
Jump to navigation Jump to search
Content deleted Content added
No edit summary
Line 1: Line 1:
=[http://itubibygucy.co.cc UNDER COSTRUCTION, PLEASE SEE THIS POST IN RESERVE COPY]=
=When to use methods=
=When to use methods=
The ideal method only handles one function, and is named approprately. Using methods increases readability of your code and makes it easier to expand or change your software. You can also call your methods from seperate places, reducing the total number of lines of code.
The ideal method only handles one function, and is named approprately. Using methods increases readability of your code and makes it easier to expand or change your software. You can also call your methods from seperate places, reducing the total number of lines of code.
Methods are also used to retrieve data from an object and store data in objects. These methods are called getters and setters. Example:
Methods are also used to retrieve data from an object and store data in objects. These methods are called getters and setters. Example:
<pre>
&lt;pre>
public class Customer {
public class Customer {
private String firstName;
private String firstName;
Line 23: Line 24:
}
}
}
}
</pre>
&lt;/pre>


Methods are allowed to call other methods, but only if they have a different functions. Try using common sense when deciding whether use methods or not.
Methods are allowed to call other methods, but only if they have a different functions. Try using common sense when deciding whether use methods or not.
Line 30: Line 31:
=About Arrays=
=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.
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>
&lt;pre>
[class]() [var-name];
[class]() [var-name];
//Or...
//Or...
int() var;
int() var;
</pre>
&lt;/pre>
To specify the length of an array:
To specify the length of an array:
<pre>
&lt;pre>
var=new var(LENGTH);
var=new var(LENGTH);
//Put into one statement
//Put into one statement
int() myInts = int(LENGTH);
int() myInts = int(LENGTH);
</pre>
&lt;/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.


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>
&lt;pre>
myInts(2)==3;
myInts(2)==3;
int temp = myInts(3);
int temp = myInts(3);
</pre>
&lt;/pre>
'''Note''': Do NOT use arrays when the members have nothing to do with each other. This causes code errors!
'''Note''': Do NOT use arrays when the members have nothing to do with each other. This causes code errors!


Line 53: Line 54:
==Code formatting==
==Code formatting==
Code formatting is necessary to understand your code more easily. For example, the following is very hard to understand because it is not properly formatted:
Code formatting is necessary to understand your code more easily. For example, the following is very hard to understand because it is not properly formatted:
<pre>
&lt;pre>
public
public
class MessyCoding extends Coding {public
class MessyCoding extends Coding {public
Line 60: Line 61:
){System.out.println
){System.out.println
("This is messy coding. Don't do this");System.exit(0);}}
("This is messy coding. Don't do this");System.exit(0);}}
</pre>
&lt;/pre>
In much better format:
In much better format:
<pre>
&lt;pre>
public class CleanCoding extends Coding {
public class CleanCoding extends Coding {
public static void main(String args[])
public static void main(String args[])
Line 70: Line 71:
}
}
}
}
</pre>
&lt;/pre>
Notice that the code is easier to understand. Every single new block that has { and } should have whitespace preceding it. Example:
Notice that the code is easier to understand. Every single new block that has { and } should have whitespace preceding it. Example:
<pre>
&lt;pre>
public class CleanCoding extends Coding {
public class CleanCoding extends Coding {
public static void main(String args[])
public static void main(String args[])
Line 86: Line 87:
...;
...;
}
}
</pre>
&lt;/pre>


To further improve the readability of your code, you should insert one empty line between methods:
To further improve the readability of your code, you should insert one empty line between methods:
<pre>
&lt;pre>
public null method1()
public null method1()
{
{


}
}
</pre>
&lt;/pre>
A good alternative is:
A good alternative is:
<pre>
&lt;pre>
public null method2() {
public null method2() {


}
}
</pre>
&lt;/pre>
You should make a new line after every statement/declaration. If a statement is too long, you can break it down.
You should make a new line after every statement/declaration. If a statement is too long, you can break it down.


Line 108: Line 109:
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:
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>
&lt;pre>
// tests for parity, sign, and size
// tests for parity, sign, and size
if( x%2 == 0 && x > 0 && x < 1000 ) {
if( x%2 == 0 &amp;&amp; x > 0 &amp;&amp; x &lt; 1000 ) {
System.out.println("x is a positive, even, integer less than 1000");
System.out.println("x is a positive, even, integer less than 1000");


Line 116: Line 117:
x++;
x++;
}
}
</pre>
&lt;/pre>


An example of bad commenting would be:
An example of bad commenting would be:


<pre>
&lt;pre>
if( i1 == i2 | i1 == i3 ) // Check if i1 is equal to i2 and i3
if( i1 == i2 | i1 == i3 ) // Check if i1 is equal to i2 and i3
{
{
Line 126: Line 127:
i1= i1 + 1; // Add 1 to i1
i1= i1 + 1; // Add 1 to i1
}
}
</pre>
&lt;/pre>


== Quick way to convert numbers to strings ==
== Quick way to convert numbers to strings ==
There will be a lot of times you need a String and you're working with numbers. This is one of the faster ways to convert numbers to String, but it's not very neat.
There will be a lot of times you need a String and you're working with numbers. This is one of the faster ways to convert numbers to String, but it's not very neat.
<pre>
&lt;pre>
int a = 5;
int a = 5;
double b = 3;
double b = 3;
Line 141: Line 142:
String a_as_string = String.valueOf(a);
String a_as_string = String.valueOf(a);
String b_as_string = String.valueOf(b);
String b_as_string = String.valueOf(b);
</pre>
&lt;/pre>





Revision as of 04:54, 24 November 2010

UNDER COSTRUCTION, PLEASE SEE THIS POST IN RESERVE COPY

When to use methods

The ideal method only handles one function, and is named approprately. Using methods increases readability of your code and makes it easier to expand or change your software. You can also call your methods from seperate places, reducing the total number of lines of code. Methods are also used to retrieve data from an object and store data in objects. These methods are called getters and setters. Example: <pre> public class Customer {

  private String firstName;
  private String lastName;
  public String getFirstName() {
     return firstName;
  }
  public void setFirstName(String firstName) {
     this.firstName = firstName;
  }
  public String getLastName() {
     return lastName;
  }
  public void setLastName(String lastName) {
     this.lastName = lastName;
  }

} </pre>

Methods are allowed to call other methods, but only if they have a different functions. Try using common sense when deciding whether use methods or not. There are multiple analysis tools to see if you are using methods right and effeciently. You should avoid having methods with a lot of lines of code, it often (but not always) indicates that the method can be broken up in smaller methods.

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. <pre> [class]() [var-name]; //Or... int() var; </pre> To specify the length of an array: <pre> var=new var(LENGTH); //Put into one statement int() myInts = int(LENGTH); </pre> 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: <pre> myInts(2)==3; int temp = myInts(3); </pre> Note: Do NOT use arrays when the members have nothing to do with each other. This causes code errors!

Basic Tips for Improving Code

Code formatting

Code formatting is necessary to understand your code more easily. For example, the following is very hard to understand because it is not properly formatted: <pre> 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);}} </pre> In much better format: <pre> public class CleanCoding extends Coding {

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

} </pre> Notice that the code is easier to understand. Every single new block that has { and } should have whitespace preceding it. Example: <pre> public class CleanCoding extends Coding {

  public static void main(String args[])
  {
     try{
        try{
            ...;
        }
        ...;
     }
     ...;
  }
  ...;

} </pre>

To further improve the readability of your code, you should insert one empty line between methods: <pre> public null method1() {

} </pre> A good alternative is: <pre> 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.

Some Integrated Development Environments like Netbeans IDE have an auto-formatting function built in.

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>

Quick way to convert numbers to strings

There will be a lot of times you need a String and you're working with numbers. This is one of the faster ways to convert numbers to String, but it's not very neat. <pre> int a = 5; double b = 3;

String a_as_string = "" + a; String b_as_string = "" + b;

or

String a_as_string = String.valueOf(a); String b_as_string = String.valueOf(b); </pre>



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