Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importance of Coding Style. How to code a GUI properly. And share your coding style :
#1
There are many people who want to learn java and they do learn it properly. But they fail miserably due to their coding style. With proper coding style it becomes easier for another programmer to understand a pre-existing code. Good coding style also helps keeping the code clean. Although this can be achieved by using a good IDE but people who make IDEs don't use another IDE to program them. (Well they may, but you get my point, right?) They code it in a simple editor.

So a good coding style should include:
  • Introduction to the code
    Before writing the code an introduction to the program or file with the author's name should be written. This can also include the project title and whatever information you want to add.

  • Good Token Identifiers
    Proper names (Token identifiers) for variables,classes,methods, or objects. The name should be relevant to what the variable is associated with. It you want to store a phone number then the variable name should be "phone_no" or "phoneNo" and not "num1"

  • Declaration of variables
    Variables should be declared before anything else. Searching for variable declaration in between codes is hectic.

  • Tab-spacing
    Proper tab-spacing for proper depiction of method/class body.
    Would you prefer this:
    Code:
    method1()
    {
            method2()
            {
                     method3()
                     {
                     }
            }
    }

    OR this:
    Code:
    method1()
    {
    method2()
    {
    method3()
    {
    }
    }
    }


  • Comments

    There are two ways to comment in a Java code.
    • Using these /*.....*/
    • Using these //

    /*....*/ are used for paragraphs. Use them!! Please don't comment like this:

    Code:
    //Hi there
    //Wassup. I know its ******* irritating commenting with these: "//"
    //But some programmers still use them.
    //and they unnecessarily increase the code file size by some bits/bytes.
    //But when there are thousands of these: "//" It may also
    //increase file size by MBs

    "//" These are used for one line comments

    Comments should be written in English or whatever language with proper grammar. Do not write codes in comments. And comments should depict logic of the program or at least it should explain the working of the code.

Keeping the above points in mind I always use the following layout:

Code:
/*
Project name: <project name>
Filename: <filename>
Purpose: <purpose>
Author: <Author name>
*/

import <packages>;

class <classname> extends <classes>
{
     <Declaring Objects/Components here>

     private void initializeComponents() // Initializes the Components.
     {
     }

     private void addComponents() // Adds the components to the ContentPane.
     {
     }

     public static void main(String args[]) // The "main" method.
     {
          <Set properties of the class>
          
          <Call initializeComponents()>
          <Call addComponents()>

          <Show self>    
     }
}

The code below is written strictly according to the style given above.

Code:
/*
Project name: Puma Desktop Environment
Filename: loginDialog.java
Purpose: Creates a login dialog.
Author: aHardyX
*/

//Importing the regular java packages
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//importing MigLayout package

import net.miginfocom.layout.*;
import net.miginfocom.swing.*;

public class loginDialog extends JDialog
{
        // Declaration of Objects/Components/Variables

        JTextField         ID;
        JPasswordField    passwd;
        Container         ContentPane;
        JButton         loginButton, cancelButton;
        JLabel          textLabel,idLabel, passwdLabel;
        JRootPane          rootPane;
    
    private void initializeComponents() throws Exception // Initializes the Components.
    {
        
            textLabel = new JLabel("Please login with your credentials:");
            idLabel = new JLabel("User: ");
        passwdLabel = new JLabel("Password: ");
        
        ContentPane = this.getContentPane();

        ID = new JTextField();
            
        passwd = new JPasswordField();
            
        loginButton = new JButton("Login");
            
        rootPane = this.getRootPane();
        rootPane.setDefaultButton(loginButton);

        cancelButton = new JButton("Cancel");
    }
    
    private void addComponents() throws Exception // Adds the components to the ContentPane.
    {
        ContentPane.add(textLabel, "span, wrap, gaptop 5, gapleft 10, gapright 10");
        ContentPane.add(idLabel, "gaptop 10, gapleft 10, gapright 10");
        ContentPane.add(ID, "w 200, h 26, wrap, gaptop 10, gapleft 10, gapright 10");
        ContentPane.add(passwdLabel, "gaptop 10, gapleft 10, gapright 10");
        ContentPane.add(passwd, "w 200, h 26, wrap, gaptop 10, gapleft 10, gapright 10");
        ContentPane.add(loginButton, "w 100, h 26, align left, gaptop 10, gapleft 10, gapright 10");
        ContentPane.add(cancelButton, "w 100, h 26, align right, gaptop 10, gapleft 10, gapright 10");
    }
    
    public static void main(String args[]) // The "main" method.
    {
        try
        {
            loginDialog obj = new loginDialog();
            obj.setLayout(new MigLayout());
            obj.setTitle("Project Puma: Login");
            obj.setSize(350,200);
            
            obj.initializeComponents();
            obj.addComponents();    
        
            obj.setVisible(true);
        }
        catch(Exception error)
        {
           System.out.println("Error: "+error.toString());  
        }
    }    
}

Output:

[Image: screenshot7hy.png]

Don't worry about the migLayout. Its just another package. You can download it from http://www.miglayout.com

I use MigLayout because its somewhat like CSS Tongue I will post a tutorial using this layout.

You have the liberty to adopt this style. If you think you have a better style, feel free to share it with others. Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  java GUI ? prince76 8 3,602 03-22-2012, 08:55 AM
Last Post: redpois0n
  need help in coding [ c, c++ , java , ] prince76 4 1,397 01-23-2012, 10:36 PM
Last Post: prince76
  My first GUI: Adding Calculator nevets04 25 5,419 01-02-2012, 06:38 PM
Last Post: MineCrack

Forum Jump:


Users browsing this thread: 1 Guest(s)