Header Ads

  • Breaking Now

    Java Coding Standards:Naming Conventions

    While coding it is significant to stick to naming conventions of the language and Java is also no exception in that.The reason of this practice is to make your code look consistent and easy to comprehend.In most of the situations these conventions are set at organization level but some of them on a broader level are summarized under points given below:
    Package
    -Capitals should not be used in the packages names.
    -Automated unit test cases using frameworks like JUnit should be placed in a parallel package hierarchy in a separate source folder.
    -Using the same package naming convention for them would cater to easy testability of complex protected methods

    Classes
    -Class name should be noun in mixed case, with the first letter upper case and the first letter of any subsequent words capitalized.
    -If your class implements as design pattern then a the pattern as a suffix to the class name e.g <ClassName>Singleton, <ClassName>Decorator, <ClassName>Factory, <ClassName>Builder,<ClassName>Strategy.

    Methods
    -Method names should be verbs or verb phrases, with first letter in lower case and first letter of every subsequent word in uppercase. Underscore should not be used in method names. Some examples of conforming names are
    createUserGroup()

    Variables
    -Variables should have names that are nouns, noun phrases, or abbreviations for nouns. Names of fields that are not final should have the first letter in lower case and first letter of the subsequent words in uppercase. Underscore should not be used in variable names. Some examples of conforming names are logger, logManager
    -One –character local variable or parameter names should be avoided, except for temporary and looping variables, or where a variable holds an undistinguished value of a type.
    -Variables should not have the same name as one in a super class.
    -Collection classes, such as vectors and hashes should always be pluralized Member variables. E.g deviceNames, deviceNamesVector etc.
    -All member variables should be prefixed with f. E.g. fMyMemberVariable. Use of the this.variable definition should be avoided for scope resolution between local and class level variables.

    Constants
    -The names of constants in interface types and final variables of class types should be a sequence of one or more words, acronyms, or abbreviations, all uppercase, with words separated by underscore "_" characters. All constants should public/private static final.
    -Some examples of conforming names are
    MIN_VALUE
    MAX_VALUE

    Exceptions
    -Exceptions should follow the same naming conventions as those for naming classes; they must additionally be suffixed by the word Exception. For example, if more than one Exceptionvariable is in scope exception variables should be suffixed by Ex. Normal IDE behavior is to create an initial Exception variable in a try catch block as simply (e). This is acceptable. If more than one variable is required then these should be distinguished using the naming model defined below.

    -The exception variables should be prefixed by some acronym. Commonly used ones are locEx for LocalizedException, sqlEx for SQLException, ioEx for IOException.

    JUnits
    Test must be used in the name of the junit as suffix.
    Example:
    Class name: UserLogon
    Junit Test Class name: UserLogonTest

    1 comment:

    1. Sweet web site, I had not noticed www.interviewjava.com earlier during my searches!
      Carry on the superb work!

      ReplyDelete

    Post Top Ad

    Post Bottom Ad