Programming Rules

Programming Rules

Hello People, In this short read I'll be writing about some basic programming rules that must be followed while programming in Java, one must know these rules before starting to code in Java. I did some research and haven't found any particular article where I can see all these rules framed together. Also I think most of these programming rules should be followed while programming any language. These rules will help in making your code more readable.

Let's begin...

type-fast-gif.gif

Rule-1

Always write code from RHS to LHS this one is little tricky this will be more clear from example. So suppose you are making reference object of Scanner class.

Scanner sc = new Scanner(System.in);

If you started typing from Scanner sc -> this is LHS to RHS

Now if you start from Right Hand Side, that is starting from new Scanner(); it would be easy for you to decide what will be on the Left Hand Side because that is declaration part.

Rule-2

Deleting complete line if you are a beginner or learning new language than you must follow this, while writing the line of code and there is some typing mistake then instead of correcting that mistake delete the complete line so when next time you will typing same thing then there will be less chances of you committing same mistake. back.gif

Rule-3

Semicolon while programming in Java remember all operations in Java end with a semicolon.

System.out.println("Hello World");

semi.jpg

Rule-4

Class name declaration while declaring a class the first letter of the class name must be in uppercase and no numeric value or special symbol should be used. If there are two words in class name then also first letter of each word should be in uppercase.

class Student {
    //class body
}
class ProjectManager {
    //class body
}

Rule-5

Method declaration there are two styles to follow while declaring a method they are single word style or camel case(camelCase) style.

  • camelCase style :- It is a combination of more than 2 words where first letter of first word must be in lowercase and remaining word first letter must be in uppercase. Remember there should be no space between words. camal.png
    void showData()
    
  • Single word style :- If the method name consists single word then this style should be followed, in this style all the characters will be in lowercase.
    void get()
    

    Rule-6

    Variable declaration while declaring a variable you should ignore using single character, you should either follow single word style or camelCase style.
    int percentage;
    String studentName;
    

    Rule-7

    Constant declaration while declaring a constant all characters must be in uppercase and use underscore(_) if there are multiple words.
    int MAX = 20; // value is not fixed
    int MAX_AGE = 20; // value is not fixed
    final int MAX_AGE = 20; // value is fixed
    

    Rule-8

    Package specification while specifying a package then all the characters must be in lowercase except the first letter of the class specified by package. To read more about packages check my last article.
    import java.util.Date;
    

    Rule-9

    Access level declaration while declaring access levels all the characters of the access levels should be in lowercase.
    private void get()
    

    Rule-10

    Object & reference object creation
    new Student();
    
    This is an object in Java.

Object creation follows these steps:-

  • Instantiation:- The new keyword is a Java operator that creates the object.
  • Initialization:- The new operator is followed by a call to a constructor, which initializes the new object.
    Student st = new Student();
    
    Here st is the reference object in Java.

It includes:-

  • Declaration: A variable name with an object type. This variable is called reference object.

In Java we can access method outside class in 3 ways:-

class Student {
   void get() {

   }
   void show() {

   }
}
  • Way-1 :-
    new Student().get();
    new Student().show();
    
  • Way-2 :-
    Student st = new Student();
    st.get();
    st.show();
    
  • Way-3 :-
    Student().get(); // if method is static
    Student().show(); // if method is static
    

    Okay so that's enough for now follow my this journey to learn more about Java.

    Thank you for reading.

    Please share your thoughts about it and correct me if I'm wrong.

    I hope you liked it and found it helpful.

    Cover:- Rajat Gour

    Connect with me on Twitter or LinkedIn

    My personal blog blog.ritvikdubey.com