Tutorial [IMPORTANT: READ] Java Naming Convention

Discussion in 'Resources' started by pyraetos, Aug 26, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    pyraetos

    I've noticed some developers are not using proper naming conventions, and this can be confusing, and even a violation of the rules, i.e. the Bukkit in the package name.
    This is a paste from java.about.com for the purpose of clarifying how you should name your Objects, and other things such as packages.
    **************************************************
    Why Use Naming Conventions?

    Different Java programmers can have different styles and approaches to the way they program. By using standard Java naming conventions they make their code easier to read for themselves and for other programmers. Readability of Java code is important because it means less time is spent trying to figure out what the code does, leaving more time to fix or modify it.
    To illustrate the point it's worth mentioning that most software companies will have a document that outlines the naming conventions they want their programmers to follow. A new programmer who becomes familiar with those rules will be able to understand code written by a programmer who might have left the company many years before hand.
    
    Picking a Name for Your Identifier

    When choosing a name for an identifier make sure it's meaningful. For instance, if your program deals with customer accounts then choose names that make sense to dealing with customers and their accounts (e.g., customerName, accountDetails). Don't worry about the length of the name. A longer name that sums up the identifier perfectly is preferable to a shorter name that might be quick to type but ambiguous.
    
    A Few Words About Cases

    Using the right letter case is the key to following a naming convention:
    • Lowercase is where all the letters in a word are written without any capitalization (e.g., while, if, mypackage).
    • Uppercase is where all the letters in a word are written in capitals. When there are more than two words in the name use underscores to separate them (e.g., MAX_HOURS, FIRST_DAY_OF_WEEK).
    • CamelCase (also known as Upper CamelCase) is where each new word begins with a capital letter (e.g., CamelCase, CustomerAccount, PlayingCard).
    • Mixed case (also known as Lower CamelCase) is the same as CamelCase except the first letter of the name is in lowercase (e.g., hasChildren, customerFirstName, customerLastName).
    

    Standard Java Naming Conventions

    The below list outlines the standard Java naming conventions for each identifier type:
    Packages: Names should be in lowercase. With small projects that only have a few packages it's okay to just give them simple (but meaningful!) names:
    Code:
    package pokeranalyzer  package mycalculator 
    In software companies and large projects where the packages might be imported into other classes, the names will normally be subdivided. Typically this will start with the company domain before being split into layers or features:
    Code:
     package org.bobscompany.application.userinterface 
    Classes: Names should be in CamelCase. Try to use nouns because a class is normally representing something in the real world:
    Code:
     class Customer  class Account 
    Interfaces: Names should be in CamelCase. They tend to have a name that describes an operation that a class can do:
    Code:
     interface Comparable  interface Enumerable 
    Note that some programmers like to distinguish interfaces by beginning the name with an "I":
    Code:
    interface IComparable  interface IEnumerable 
    Methods: Names should be in mixed case. Use verbs to describe what the method does:
    Code:
     void calculateTax()  string getSurname() 
    Variables: Names should be in mixed case. The names should represent what the value of the variable represents:
    Code:
     string firstName  int orderNumber 
    Only use very short names when the variables are short lived, such as in for loops:
    Code:java
    1. for (int i=0; i<20;i++)
    2. {
    3. //i only lives in here
    4. }

    Constants: Names should be in uppercase.
    Code:
    static final int DEFAULT_WIDTH  static final int MAX_HEIGHT 
    ************************************************************************
    For my package name, I always use org.nerdycast.PluginName because its my website, and many plugin developers have a website.
    If you don't, the HUGE Plugin Tutorial on the Bukkit Wiki I believe recommends using something like me.<myname>.PluginName
    A couple of external links on Naming Convention:
    http://en.wikipedia.org/wiki/Naming_convention_(programming)
    http://www.oracle.com/technetwork/java/codeconv-138413.html
    http://www.ibm.com/developerworks/library/ws-tip-namingconv.html
     
    Roweenah likes this.
  2. Offline

    xMrPoi

    Why did you bump this? And with such a non-important comment at that
     
  3. Offline

    eyamaz

    With some of the new plugins coming in, I allowed this to be necroed with something insignificant with the hopes maybe a few new devs that don't know any better will read it...
     
  4. Offline

    teej107

    @eyamaz Can this at least go to the resource sections then?
     
    eyamaz likes this.
  5. Offline

    mythbusterma

    @teej107

    Should be stickied honestly.
     
    Avygeil and teej107 like this.
  6. Offline

    teej107

    @mythbusterma Well I would've complained about this having nothing to do with Bukkit but the OP slipped something about it in at the end. Anything other than this thread TBH or sticky!
     
Thread Status:
Not open for further replies.

Share This Page