Comments
Richard Davies wrote: The UK has a good crop of technology pioneers in cloud computing - for example ElasticHosts, FlexiScale, Flexiant, OnApp - and also some strong government initiatives such as G-Cloud. We will have to see whether this kind of technical leadership converts into swift mass-market adoption or not.
Cloud Expo on Google News


2008 West
DIAMOND SPONSOR:
Data Direct
SOA, WOA and Cloud Computing: The New Frontier for Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
GOLD SPONSORS:
Appsense
User Environment Management – The Third Layer of the Desktop
Cordys
Cloud Computing for Business Agility
EMC
CMIS: A Multi-Vendor Proposal for a Service-Based Content Management Interoperability Standard
Freedom OSS
Practical SOA” Max Yankelevich
Intel
Architecting an Enterprise Service Router (ESR) – A Cost-Effective Way to Scale SOA Across the Enterprise
Sensedia
Return on Assests: Bringing Visibility to your SOA Strategy
Symantec
Managing Hybrid Endpoint Environments
VMWare
Game-Changing Technology for Enterprise Clouds and Applications
Click For 2008 West
Event Webcasts

2008 West
PLATINUM SPONSORS:
Appcelerator
Get ‘Rich’ Quick: Rapid Prototyping for RIA with ZERO Server Code
Keynote Systems
Designing for and Managing Performance in the New Frontier of Rich Internet Applications
GOLD SPONSORS:
ICEsoft
How Can AJAX Improve Homeland Security?
Isomorphic
Beyond Widgets: What a RIA Platform Should Offer
Oracle
REAs: Rich Enterprise Applications
Click For 2008 Event Webcasts
SYS-CON.TV
Top Links You Must Click On


A Common Coding for Java
A Common Coding for Java

Coding style means nothing to a compiler; it will happily accept any legal code, no matter how badly formatted. Not so for us humans. Our ability to read and understand a program written by another is extremely sensitive to format. Imagine trying to read this article if every letter was in a different font and each line randomly aligned. Clearly, format affects readability, and it is just as true for reading programs as for text.

A consistent style also leads to lower maintenance costs by making it easier to debug and enhance the code. This is especially true for teams of programmers working on the same project, who must often read and edit each other's code. In this case, it is important that each team member follow not only a consistent style, but the same style. A common style also enables the development of automated tools to assist in program development, such as formatting tools and editor macros.

In spite of these benefits, some developers are reluctant to adopt a common style. One reason might be a belief that it requires too much effort. It is true that it takes a bit of time initially to learn the new rules (and unlearn old bad habits). However, once learned, they become automatic, and it takes no more effort to write code in a good style than in a poor style. In fact, it probably saves time by allowing programmers to concentrate on the semantics of the program, instead of consciously worry about the right format for each statement.

A larger reason for lack of a consistent style, however, is the lack of widely available, comprehensive and well thought out coding standards. This article is one attempt at improving this situation.

Space does not permit the presentation of an entire coding standard here. Rather, this article gives a selected set of the most important coding style rules, in somewhat condensed form. Listing 1 shows a code example demonstrating most of these style rules.

Indentation
One of the most important rules addresses the horizontal alignment of source lines.Line indentation is always four spaces per indentation level. [This is a difference from the predominant indentation style of eight spaces used in C programs; it is an acknowledgment that typical Java programs tend to have more levels of nesting than typical C programs.] The construction of the indentation may include tabs as well as spaces in order to reduce the file size; however, do not change the hard tab settings of your editor to accomplish this. Hard tabs must be set every 8 spaces. Soft tabs may be set every 4 spaces if your editor supports them.

Line Length
Lines should be limited to 80 columns. Longer lines should be broken into one or more continuation lines, as needed. All the continuation lines should be aligned and indented from the first line of the statement. The amount of the indentation depends on the type of statement.

If the statement must be broken in the middle of a parenthesized expression, such as for the parameter list in a method invocation or declaration, the next line should be aligned with the first character to the right of the first unmatched left parenthesis in the previous line. In all other cases, the continuation lines should be indented by a full standard indentation (4 spaces).

Braces Style
The preferred style for placement of braces in compound statements is the style commonly referred to as the "K&R" style. [Named after Brian Kernighan and Dennis Ritchie, authors of the first book on the C language.] Unlike C, in Java this style is used in all cases, including class and instance initializers and array initializers. This style is specified as follows:
1. The opening left brace "{" is at the end of the line beginning the statement.
2. The closing right brace "}" is alone on a line, indented to the same column as the first line of the statement.
3. The statements inside the enclosed braces are indented one more level than in the statement.

Naming Conventions
Another aspect of coding style that makes a big difference is consistency of naming. Package names should use only lower-case letters and digits, and no underscore. If the package will be publicly distributed, prepend a unique prefix to the package by using the Internet domain name components in reverse order (e.g., com.sun, graphics.util).

All class and interface names should use the InfixCaps style. Start with an upper case letter and capitalize the first letter of any subsequent word in the name. All other characters in the names are lower case. Do not use underscores to separate words. Class names should be nouns or noun phrases. Interface names depend on the purpose of the interface. If it is primarily to endow an object with a particular capability, then the name should be an adjective that describes the capability (e.g., Searchable, Sortable). Otherwise, use nouns or noun phrases.

Variables (except static final types, see below) use the infixCaps style. Start with a lower case letter and capitalize the first letter of any subsequent word in the name, as well as any letters that are part of an acronym. all other characters in the name are lower case. Do not use underscores to separate words. Variable names should be nouns or noun phrases (e.g., numberOfPoints).

Static final primitive types being used as constants should be all upper case, with underscores separating words (e.g., MAX_BUFFER_SIZE).

Method names also use the infixCaps style. Method names should be verbs or verb phrases (e.g., drawCircle()).

White Space Usage
Blank lines can improve readability by grouping sections of the code that are logically related. A blank line should also be used:

  • After the package declaration and import section
  • Before a class or method declaration
  • Before a block or single-line comment, unless it is the first line in a block.

    A single blank space (not tab) should be used to separate all tokens in a statement, except for the following. Blanks should not be used:

  • Between a constructor or method name and its opening parenthesis
  • Before or after a . (dot) operator
  • Between a unary operator and its operand
  • Between a cast and the expression being casted
  • After an opening parenthesis or square bracket, or before a closing parenthesis or square bracket

    Comments
    Comments can help the reader understand the code. Here are some general guidelines for comment usage:

  • Comments should help a reader understand the purpose of the code. They should guide the reader through the flow of the program, focusing especially on areas which might be confusing or obscure.
  • Avoid comments that are obvious from the code, as in the famously bad comment example:

    i=i+1; //Add one to i

  • Remember that misleading comments are worse than no comments at all.
  • Avoid putting any information into comments that is likely to become out-of-date.
    About Achut Reddy
    Achut Reddy is a staff engineer at Sun Microsystems in the authoring and development tools group, which is currently working on Java performance issues.

  • In order to post a comment you need to be registered and logged in.

    Register | Sign-in

    Reader Feedback: Page 1 of 1

    Enterprise Open Source Magazine Latest Stories . . .
    Apache Deltacloud, the Red Hat-contributed ReSTful API that abstracts differences between clouds so services on any cloud can be managed – provided of course there’s a driver – has graduated from the Apache Foundation’s incubator and is now a full-fledged Top-Level Project (TLP). The...
    With Cloud Expo 2012 New York (10th Cloud Expo) just four months away, what better time to start introducing you in greater detail to the distinguished individuals in our incredible Speaker Faculty for the technical and strategy sessions at the conference... We have technical and st...
    AMD said late Tuesday that its chief sales officer Emilio Ghilardi had left the company and that CEO and president Rory Read is going to do his job while a replacement is sought. AMD didn’t say why Ghilardi left but it’s assumed Read wants his own people. Read is relatively new to th...
    During the lifespan of M3 (Monitis Monitor Manager) there has always been something lacking – timers. M3 execution procedure was outlined in this previous article. The execution mentioned in the latter was a one-time-execution, whereas server monitoring requires periodic invocati...
    Red Hat is putting its bought-in Gluster scale-out NAS storage technology, acquired in October, on the Amazon cloud. It’s styled Red Hat Virtual Storage Appliance for Amazon Web Services and other clouds are supposed to follow in short order.
    A new episode of the screencast series is now available at the OpenNebula YouTube Channel. This screencast demonstrates the new easily-customizable self-service portal for cloud consumers. Its aim is to offer a simplified access to shared infrastructure for non-IT end users. The scree...
    Subscribe to the World's Most Powerful Newsletters
    Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
    Click to Add our RSS Feeds to the Service of Your Choice:
    Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
    myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
    Publish Your Article! Please send it to editorial(at)sys-con.com!

    Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


    SYS-CON Featured Whitepapers
    ADS BY GOOGLE