Welcome to Ethical Hackers
Nick:  
Pass:     
Register Help Member List View New Posts View Today's Posts

Thread Closed 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Java Conventions
06-01-2010, 02:14 AM
Post: #1
Java Conventions
So, let's start with the basic header. Your java source file header should be
informative, generally, I use the following template:


Code:
/*
* ProjectName - Short description
*/
Generally, more information should be included, for example, author(s) name(s), revision history and licensing information are included in the header.

Immediately after the header is the imports section. I generally try to organize
them alphabetically. Here's an example.



Code:
import java.io.*;
import java.net.*;

import com.myproject.core.*;
import com.myproject.objects.*;
Also, you'll notice that I separate the runtime imports and the project imports, this isn't necessary, but it's more organized.

Now, class name conventions. Classes should use the Pascal naming conventions.
For those unfamiliar with this terminology, the Pascal naming convention states that the first letter of each word is capitalized, for example TheCat, TheDog,
Entity, PlayerEntity, InventoryItem.

Here's some example class declarations:

Code:
public class PlayerEntity extends Entity
public class Client extends AsynchronousClient
public class Server extends AsynchronousListener

Now, we're going to talk about fields. Fields should use the camel-case naming convention, and generally should be private. The camel-case naming convention
says that the first word in a compound word or phrase should be all lower-case.
For example: theCat, theDog, entity, playerEntity, inventoryItem.
One more rant, when using arrays declare it with the [ ] beside the type, not
beside the name. (See the last example below)

Example field declarations:


Code:
private int id;
private String name;
private String password;
private int accessLevel;
private int[ ] myArray;

Field names should never be overly wordy, but should always be descriptive.
Do not use lame phrases such as "lmao, lol, omg", etc, as variable/field names.

Now, accessors. Accessors are methods (see the next section) that provide a
public means to get and set the private data inside the class. They should also
use the camel-case naming convention, for example, isActive( ), getID( ), setID( ), to name a few.

Code examples:


Code:
int id;

/**
  * Method description
  */
public int getID( ) {
    return id;
}

/**
* Method description
*/
public void setID( int id ) {
    this.id = id;
}
Also, you'll notice I threw in some useless java documentation comments. I suggest you read up on these, you should always comment your classes and methods with java documentation.

Methods, pretty much self explanatory, you have three primary access modifiers, which are public, private and protected, however, since this is a conventions
article and not a beginner java programming article, I'll leave learning that to you.
Methods are functions inside a class, the biggest thing is remember to use
camel-case naming conventions for naming your methods, otherwise the java coding convention god shall smite your mothers. Also, parameters use the
camel-case naming convention as well.

Ok, last rant. Indention is your friend, and use it wisely.

Here's some examples:


Code:
// If statements
if ( condition ) {
    if ( sub condition ) {
        // Code
    } else {
        // Code
    }
} else if ( condition 2 ) {
    // Some code
} else {
    // Some code
}

// try...catch blocks
try {
    // Some code that throws exceptions...
} catch ( Exception ex ) {
    // TODO: Report the exception
} finally {
    // What now?
}
Oh, and don't EVER use spaces for indention, use TAB.

For more information on coding conventions, please visit http://java.sun.com/docs/codeconv/
Grand finale, a example hello world program.



Code:
/*
* HelloWorld - This is a pointless coding conventions example.
*
* Author:
* Mitch
*
* Date:
* 20100531
*/

/**
* This class does this, this and this.
*/
public class HelloWorld {
    private String greetingMessage = "No message.";
    
    /**
     * This method returns the current greeting message.
     */
    public String getGreetingMessage( ) {
        return greetingMessage;
    }
    
    /**
     * This method sets the current greeting message.
     */
    public void setGreetingMessage( string greetingMessage ) {
        this.greetingMessage = greetingMessage;
    }
    
    /**
     * This method says the current greeting message.
     */
    public void sayMessage( ) {
        System.out.println( message );
    }
    
    /**
     * No comment is really neccesary for this method, as
     * generally it simply provides an application entry point.
     */
    public static void main( String[ ] args ) {
        // Create a new instance of the HelloWorld( ) class.
        HelloWorld helloWorld = new HelloWorld( );
        
        // Set the current message and display it.
        helloWorld.setGreetingMessage("Hello World!!");
        helloWorld.sayMessage( );
    }
}

[Image: sig.png]
Find all posts by this user
06-01-2010, 07:56 AM
Post: #2
RE: Java Conventions
:) I'm already liking this one :D

[Image: mybbsig.php]
Visit this user's website Find all posts by this user
06-13-2010, 03:21 AM
Post: #3
RE: Java Conventions
Currently, I am a student at University of technology, my major is engineering computer, it is mean that I know about java but actually, I really hate it, I hate it from the first day I know it, it make my head hurt. Thou I hate it like that but I am still studying it because it necessary. You talk about java but can I ask, you hate it or like it??
Find all posts by this user
Thread Closed 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Java vs C++ reovlu 0 237 01-17-2012 11:51 AM
Last Post: reovlu
  Getting started with Java Programming! bull12rede43 5 660 05-17-2010 03:11 AM
Last Post: Lightpixel

Forum Jump:


User(s) browsing this thread: 2 Guest(s)



Ethical Hackers © 2012.