目錄 – 文字 – 連結



目錄 – 文字 – 連結

0 0


clean_code_ch5_slides


On Github yungchienliu / clean_code_ch5_slides

Formatting

Purpose

Code formatting is about communication, and communication is the professional developer's first order of businiess.

Vertical formatting

Detail increases as we move downward.

OPENNESS

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 {
		...
	}
}

density

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);
	}
}

density

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.

distance

Keep related concepts together

distance

Variables should be declard as close to their usage as possible

eg Control variables for loops should usually be declared within the loop statement

horizontal formatting

how wide should a line be?

You should never have to scroll to the right!

120 characters with wide monitors

density

Add some white spaces to make statements clear

density

return (-b-Math.sqrt(determinant))/(2*a);

density

return (-b - Math.sqrt(determinant)) / (2*a);

alignment

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.

alignment

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

indentation

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])))){
...
}

indentation

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]))) ){
...
}

team rules

team rule

A team should agree upon a single formatting style

Formatting