The Artima Developer Community
Sponsored Link

Java Buzz Forum
Coding Standards and Naming conventions

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Coding Standards and Naming conventions Posted: Feb 11, 2015 10:42 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Coding Standards and Naming conventions
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • Coding Standards(CS) and Naming conventions(NC) are suggestions given by sun(Oracle).
  • CS and NC help developers to develop projects with more readability and understandability.

Why Coding Standards?

  • A program is written once , but read many times
    1. During debugging
    2. When adding to the program
    3. When updating the program
    4. When trying to understand the program
  • Anything that makes a program readable and understandable saves lots of time , even in the shot run.

Naming Conventions: 

1.Naming a class:

  • Class name should be "noun", because it represents things
  • Class name should be in title case , means Every word first letter should be capital letter.
  • This is also known as UpperCamelCase.
  • Choose descriptive and simple names.
  • Try to avoid acronyms and abbreviations.

  1. package com.instanceofjava
  2. public JavaDemo{
  3.  
  4.  }

2.Naming a Variable:

  • Variable name also should be noun, because it represents values.
  • Variable name should be start with small letter. 
  • Use lowerCamelCase.
  • Example: firstName, lastName, age.
  • For final variable all its letters should be capital and words must be connected with "_".
  • Example:  MAX_COUNT, MIN_COUNT.

  1. package com.instanceofjava
  2. public JavaDemo{
  3.  
  4.  String topicName;
  5.  String post;
  6.  int postNumber;
  7.  final static int MIN_WORDS=300;

  8.  }

3.Naming Method:

  • Method name should be verb because it represents action.
  • Use lowerCamelCase for naming methods
  • Example: setFirstName(), getFirstName(), getLastName().

  1. package com.instanceofjava
  2. public JavaDemo{
  3.  
  4.  String topicName;
  5.   
  6. public void setTopicName(String topicName) {
  7. this.topicName=topicName;
  8.  }
  9.  
  10. public void getTopicName() {
  11. return topicName;
  12.  }
  13.  
  14.  }

4.Naming a package:

  • All letters should be small 
  • like in java io, util, applet
  • Example com.instanceofjava

      1. package com.instanceofjava
      2. public JavaDemo{
      3.  }
      • If you do not follow all above naming conventions compiler wont any Error or exception.
      • But naming conventions are  useful in project and need to follow.
      • But we need to follow some identifier rules as follows.

      Identifiers:

      • Identifier is the name of the basic programming elements.
      • class names, methods names , object names and variables names are identifiers
      • If we define a basic program without a name , compiler throws compile time error.
      • Compilation Error : <identifier> expected.
      • if we take a class without name then compile time error will come : Syntax error on token "class", Identifier expected after this token.

      1. package com.instanceofjava
      2. public {   //  Syntax error on token "class", Identifier expected after this token.
      3.  }

      Rules in defining identifier:

      • While defining identifier we must follow bellow rules, else it leads to compile time error.

      1.Identifier should only contain :

      1. Alphabets(a -z and A-Z)
      2. Digits (0-9)
      3. Special Characters ( _ and $)

      2.Identifier should not start with a digit:

      • A digit can be used from second character onwards.
      • Example : 
      • 1strank : throws error
      • No1Rank : works fine

      3.Identifier should not contain special characters except "-" and "$":

      • Example : 
      • first_name : works fine
      • first#name : throws error

      4.Identifier is case sensitive :

      • identifier is case sensitive
      • For example a and A are different a!=A.

      5.keyword cannot be used as user defined identifier :

      • we can not use keywords as our identifiers
      • For example int class; // Syntax error on token "class", invalid VariableDeclarator.

      Note:

      • we can use predefined class names as identifiers
      • example : int String;
      • There is no limit in identifier length.

      Java comments:

      • A description about a basic programming element is called comment.
      • Comments are meant for developer, to understand purpose.

      Types of comments:

      • Java supports 3 types of comments
      1. Single line comments  - //
      2. Multiline comments  - /* */
      3. Document comment - /** */

      Read: Coding Standards and Naming conventions

      Topic: Interview with Tor Norbye: the Present and the Future of Developer Tools for Android Previous Topic   Next Topic Topic: Plug into the Wall: Interfaces to the Outside World

      Sponsored Links



      Google
        Web Artima.com   

      Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use