Created
October 7, 2013 10:47
-
-
Save soren/6865920 to your computer and use it in GitHub Desktop.
Example of my Java code formatting rules.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.twonky.demo; | |
import java.util.HashMap; // Always import fully qualified packages | |
import java.util.Map; // The imports should be sorted alphabetically | |
// import third party classes here, e.g. import oracle.jdbc.pool.OracleConnectionCacheImpl; | |
// import you own classes here, e.g. import net.twonky.demo.StringUtil; | |
/** | |
* <p>This is a sample of my java code formatting rules. It is based on the | |
* <a target=_new href="http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html">Code Conventions for the Java™ Programming Language</a>. | |
* | |
* <p>If you have a Java Development Kit installed, the sample can be compiled like this | |
* | |
* <pre> | |
* mkdir target | |
* javac -d target JavaCodeSample.java | |
* </pre> | |
* | |
* <p>Run it like this | |
* | |
* <pre> | |
* java -cp target net.twonky.demo.JavaCodeSample | |
* </pre> | |
* | |
* <p>You can generate documentation like this | |
* | |
* <pre> | |
* javadoc -d target JavaCodeSample.java | |
* </pre> | |
* | |
* <p>Point you web browser to target/index.html to view the documentation. | |
* | |
* <p>The gist of the rules are as follows. | |
* | |
* <h3>Code Formatting</h3> | |
* | |
* <ul> | |
* <li>Indention is <em>four</em> (4) spaces. | |
* <li>Indention must use spaces, tabs are <strong>not</strong> allowed. | |
* <li>I'm using K&R-style blocks. | |
* </ul> | |
* | |
* <h3>Naming</h3> | |
* | |
* <ul> | |
* <li>Only 7-bit ASCII is allowed (i.e. I do not use danish characters) in names (members, classes, etc.) | |
* <li>All names are in English. | |
* </ul> | |
* | |
* | |
*/ | |
public class JavaCodeSample { | |
private String firstName = ""; | |
private boolean developer = false; | |
private DeveloperDb developerDb; // Note: Db and *not* DB | |
private class DeveloperDb { | |
private Map<String, String> developers; | |
public DeveloperDb() { | |
developers = new HashMap<String,String>(); | |
developers.put("Foo", "bar"); // Note: spaces used to | |
developers.put("Søren", "slu"); // align in two columns | |
} | |
public String initials(String firstName) { | |
return ((developers.containsKey(firstName)) ? | |
(String)developers.get(firstName) : ""); | |
} | |
} | |
public JavaCodeSample(String firstName) { | |
developerDb = new DeveloperDb(); | |
setFirstName(firstName); | |
if (developerDb.initials(firstName).length() > 0) { | |
setDeveloper(true); | |
} | |
} | |
public String getFirstName() { | |
return firstName; | |
} | |
public void setFirstName(String aName) { | |
firstName = aName; | |
} | |
public boolean isDeveloper() { | |
return developer; | |
} | |
public void setDeveloper(boolean isDeveloper) { | |
developer = isDeveloper; | |
} | |
public String toString() { | |
final int sbSize = 1000; | |
final StringBuffer sb = new StringBuffer(sbSize); | |
sb.append(firstName).append(developer ? " is " : " isn't ").append("a developer."); | |
return sb.toString(); | |
} | |
public static void main(String[] args) { | |
JavaCodeSample pernille = new JavaCodeSample("Baz"); | |
JavaCodeSample soeren = new JavaCodeSample("Søren"); // Note: only ascii 7 in names | |
System.out.println(pernille); | |
System.out.println(soeren); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment