DEV Community

Cover image for Programming Rules
Ritvik Dubey
Ritvik Dubey

Posted on • Edited on

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);
Enter fullscreen mode Exit fullscreen mode

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");
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode
class ProjectManager {
    //class body
}
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode
  • 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()
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

Rule-9

Access level declaration while declaring access levels all the characters of the access levels should be in lowercase.

private void get()
Enter fullscreen mode Exit fullscreen mode

Rule-10

Object & reference object creation

new Student();
Enter fullscreen mode Exit fullscreen mode

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();
Enter fullscreen mode Exit fullscreen mode

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() {

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

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

Top comments (7)

Collapse
 
justjoma profile image
Zhomart • Edited

At 10th rule little bug(Student().get(); // if method is static) -> Student.get();

Collapse
 
ritvikdubey27 profile image
Ritvik Dubey

Oh thanks man, I Just checked my notes there I have written it correct but while I did this. thanks ๐Ÿ‘

Collapse
 
biffbaff64 profile image
Richard Ikin

Rules 1 and 2 are debatable.

Collapse
 
ritvikdubey27 profile image
Ritvik Dubey

Haha yess I know, but as a beginner I found them helpful so just added them.

Collapse
 
_charu_ profile image
Charu

Beautifully articulated Ritvik. ๐Ÿ™Œ

Specially I loved the way you explain Rule 3. ๐Ÿ˜

Collapse
 
ritvikdubey27 profile image
Ritvik Dubey

Thank you so much Charuโค๏ธ๐Ÿ˜Š
HahaโœŒ๏ธ๐Ÿ˜‚

Collapse
 
aesthytik profile image
Vipin Kumar Rawat

Good one ritvik