I think there is no right style. I don't even believe that all team members should be forced to stick to the same style guide. But everyone should have his own consistent style and at least each project file should have a consistent style.
For me, a consistent style helps to write software faster. If other people unfamiliar with my style need longer to read my code it is a tradeoff I am willing to do. I need less thinking when I type, because many details are written by reflexes. I think when I read code (my own or someone else's), the style does not matter that much as long as it is consistent. Of course, some details of my own style help me understand my code much faster like '_' in front of global class variables or '_' at the end of method argument variables etc. and I miss them when I read other peoples code and don't see them. But then there are other ideas how to put extra information in the code that I don't use and other people used to it might miss this in my code.
Anyway, writing source code is a very personal thing, and I believe different styles should reflect different personalities. To force people to use uniformly the same style is communism in my book ;-).
This document is for people who have to or want to read my code and also want explanations about some stylistic details.
The content of this document is basically ripped together from some other Java coding standards and mixed together to my own liking. See the links section for the original works this one is based upon.
/* * The contents of this file are subject to the Mozilla Public License * Version 1.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the * License for the specific language governing rights and limitations * under the License. * * The Original Code is the reusable ccl library * (http://www.kclee.com/clemens/java/ccl/). * * The Initial Developer of the Original Code is Chr. Clemens Lee. * Portions created by Chr. Clemens Lee are Copyright (C) Chr. * Clemens Lee. All Rights Reserved. * * Contributor(s): Chr. Clemens Lee <clemens@kclee.com> */
package ccl.util;
The first component of a unique package name is always written in all-lowercase ASCII letters. Personally I don't care for the convention of naming packages after domain names :-).
Package names should use singular words like java.awt.event, jacob.tool, or com.websentric.presto.dialog. Create a new java package for each self-contained project or group of related functionality. Create and use directories in accord with java package conventions.
Import statements should be explicit and without statements like
import java.awt.*;
Such statements make it very hard to read and find classes your source makes references to. Also the imported classes should ordered alphabetically and without empty lines. I don't use empty lines because of my Jacob development tool that inserts import statements automatically for me and expects this sorted order.
/** * Example class does this and that ... * * @author <a href="http://www.kclee.com/clemens/"> * Chr. Clemens Lee</a> * <<a href="mailto:clemens@kclee.com"> * clemens@kclee.com * </a>> * @version $Id: CCLJavaCodingStandard.html,v 1.2 1999/07/15 11:05:08 clemens Exp clemens $ */ public class Example { ...
Rationale:
Tools like less expect a tab setting of 8. If your editor
uses a different setting, these tools are next to useless for a quick look
at your code. Personally I used an indentation level of 3 for a long time,
but with a value of 4 it is better possible to mix tabs and spaces. Nevertheless,
if you editor is able to replace every tab with spaces anyway, this saves
a lot of trouble, for example when checking in and out of cvs.
if only spaces <-> tabs changed, this irritates diff
quite a lot. Also, no matter how other people did set their tabs value
in their editor, if only spaces are used, your code looks the same in every
editor.
Tabs and indentation depth are one of the few things that should be explicitly specified for a whole team, but not necessarily the same as in this document.
packages:
lowercase
classes:
CapitalizedWithInternalWordsAlsoCapitalized
methods:
firstWordLowerCaseButInternalWordsCapitalized
start the method name with a verb
private or protected methods:
_firstWordLowerCaseButInternalWordsCapitalized
constants (finals):
UPPER_CASE_WITH_UNDERSCORES
string constants (finals):
S_UPPER_CASE_WITH_UNDERSCORES
float/double constants (finals):
F/D_UPPER_CASE_WITH_UNDERSCORES
private or protected class variable:
_leadingUnderscore
method local variables:
firstWordLowerCaseButInternalWordsCapitalized
method parameter variable:
trailingUnderscore_
Exception class:
ClassNameEndsWithException
Interface. Java interface names end with the suffix "-able" or "-ible"-or
(occasionally) "-er.":
Interfacable, DoSomethingPossible, or
Listener
Car car = new Car();
Just like in C I use p as a prefix. In this case: pCar. Other choices would have been, theCar, aCar, or rCar (r for Reference). But I like p for Pointer even thought technically JVM seldom use Pointers to implement References internally.
In the literature and on usenet there a big flame wars going on in favor and agains Hungarian Notation. For me the way I handle it, it helps me a lot to know the type where I see a variable without the need to scroll around to look it up, while still everything is pretty readable (at least for me :-).
Below are some examples.
|