Code formatting is about communication, and communication is the professional developer's first order of businiess.
Detail increases as we move downward.
package fitnesse.wikitext.widgets; improt java.util.regex.*; public class BoldWidget extends ParentWidget { ... public BoldWidget(ParentWidget parent, String test) throws Exception { ... } public String render () throws Exception { ... } }
public class ReporterConfig { /** * The class name of the reporter listener */ private String m_className; /** * The properties of the reporter listener */ private List<Property> m_properties = new ArrayList<Property>(); public void add Property(Property property){ m_properties.add(property); } }
public class ReporterConfig { private String m_className; private List<Property> m_properties = new ArrayList<Property>(); public void add Property(Property property){ m_properties.add(property); } }
This is better for reading.
Keep related concepts together
Variables should be declard as close to their usage as possible
eg Control variables for loops should usually be declared within the loop statement
You should never have to scroll to the right!
120 characters with wide monitors
Add some white spaces to make statements clear
return (-b-Math.sqrt(determinant))/(2*a);
return (-b - Math.sqrt(determinant)) / (2*a);
public class FitNesseExpediter implements ResponseSender { private Socket socket; private InputStream input; protected long requestParsingTimeLimit; private FitNesseContext context; } public FitNesseExpediter(...) { socket = s; requestParsingTimeLimit = 10000; }
This fnacy alignment is not useful, and it leads my eye away from the true intent.
public class FitNesseExpediter implements ResponseSender { private Socket socket; private InputStream input; protected long requestParsingTimeLimit; private FitNesseContext context; } public FitNesseExpediter(...) { socket = s; requestParsingTimeLimit = 10000; }
This is just fine
Avoid collapsing scopes down to one line
public CommentWidget(ParentWidget parent, String text) {super(parent, text);} public String render () throws Exception {return ""; } if(cap[e.th] > 0 && dis[e.to] == dis[x] + 1 && (a = find(e.to, min(low, cap[e.th])))){ ... }
public CommentWidget(ParentWidget parent, String text){ super(parent, text); } public String render () throws Exception { return ""; } if(cap[e.th] > 0 && dis[e.to] == dis[x] + 1 && (a = find(e.to, min(low, cap[e.th]))) ){ ... }
A team should agree upon a single formatting style