承接第一部分,本文將說明更多良好風格要素,並為本系列畫下句點。
一個良好的 Java 原始碼檔案建議依序包含:
package org.rotpad;
import java.awt.*;
import javax.swing.event.*;
import org.javacogs.*;
import javax.swing.*;
import java.awt.event.*;
class Foo { ... }
public class RotPad extends JFrame { ... }
package org.rotpad;
// Java classes
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
// JavaCogs classes
import org.javacogs.*;
/**
* RotPad is a simple GUI application for performing rotation ciphers on plain
* text.
*
* @author Thornton Rose
* @version 1.0
*/
public class RotPad extends JFrame { ... }
//-----------------------------------------------------------------------------
/**
* Foo is ...
*
* @author Thornton Rose
* @version 1.0
*/
class Foo { ... }
匯入類別眾多時,建議依下列順序分組並加註解:
java.*)javax.*)追求完美者可在各組內按字母排序。
import java.util.*;
import javax.swing.*;
import java.awt.event*;
import com.gensym.com.*;
import javax.swing.table.*;
import com.pv.jfcx.*;
import java.awt.*;
import com.melthorn.util.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.table.*;
import com.gensym.com.*; // BeanXporter
import com.pv.jfcx.*; // ProtoView
import com.melthorn.util.*; // Utilities
// Java classes
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.table.*;
// BeanXporter
import com.gensym.com.*;
// ProtoView GUI components
import com.pv.jfcx.*;
// Application classes
import com.melthorn.util.*;
建議順序:
main() 除外),邏輯分組main() 方法// RotPad -- GUI app. for ROT ciphering
public class RotPad extends JFrame {
private static final String TRANSFORM_ROT13 = "ROT13";
private static final String TRANSFORM_ROT13N5 = "ROT13N5";
private static final String TRANSFORM_ROTASCII = "ROT-ASCII";
private void jbInit() throws Exception { ... }
public static final String TITLE = "RotPad";
public static final String VERSION = "1.0";
public static void main(String[] args) { ... }
public RotPad() { ... }
private JPanel jPanel1 = new JPanel();
private JPanel jPanel2 = new JPanel();
private BorderLayout borderLayout1 = new BorderLayout();
...
}
/**
* RotPad is a simple GUI application for performing rotation ciphers on plain
* text.
*
* @author Thornton Rose
* @version 1.0
*/
public class RotPad extends JFrame {
// Public constants
public static final String TITLE = "RotPad";
public static final String VERSION = "1.0";
// Private constants
private static final String TRANSFORM_ROT13 = "ROT13";
private static final String TRANSFORM_ROT13N5 = "ROT13N5";
private static final String TRANSFORM_ROTASCII = "ROT-ASCII";
// GUI components [JBuilder generated]
private BorderLayout borderLayout1 = new BorderLayout();
private JPanel jPanel1 = new JPanel();
private JPanel jPanel2 = new JPanel();
...
/**
* Construct a new instance of this class.
*/
public RotPad() { ... }
/**
* Initialize UI components. [JBuilder generated]
*/
private void jbInit() throws Exception { ... }
...
//--------------------------------------------------------------------------
/**
* Start the application.
*/
public static void main(String[] args) { ... }
}
大量欄位時依存取層級與性質分組:
輔助準則:
public class CustomerSearchDialog extends JDialog {
private JLabel firstNameLabel = new JLabel();
private JLabel lastNameLabel = new JLabel();
public static final RESULT_SELECT = 1;
private Vector results = new Vector(); // Search results.
private DefaultTableModel tableModel = new DefaultTableModel();
public static final RESULT_CANCEL = 0;
// ...
}
/**
* ...
*/
public class CustomerSearchDialog extends JDialog {
/**
* Indicates that search was cancelled; returned by showDialog() when
* user clicks cancel button.
*/
public static final RESULT_CANCEL = 0;
/**
* Indicates that a customer was selected; returned by showDialog() when
* user clicks select button.
*/
public static final RESULT_SELECT = 1;
private Vector results = new Vector(); // Search results.
private DefaultTableModel tableModel = new DefaultTableModel(); // Grid model.
// GUI fields. [JBuilder]
private JLabel firstNameLabel = new JLabel();
private JLabel lastNameLabel = new JLabel();
// ...
}
public int getTypeCount (String custType)
{
...
}
static public getInstance(){ ... };
public void showRange()
throws RangeException {
...
}
/**
* Return the single instance of this class.
*/
public static CalculationEngine getInstance() {
return instance;
}
/**
* Calculate the consumption coefficient.
*/
public float calculateConsumptionCoefficient(int base, float variance,
int iterations) throws RangeException {
// ...
}
/**
* Calculate the consumption coefficient.
*/
public float calculateConsumptionCoefficient(
int base,
float variance,
int iterations)
throws RangeException
{
// ...
}
無論遵循何種規範,撰寫程式碼的終極目標應是:讓他人能理解並維護。這才是風格的真諦。
同第一部分。作者 Thornton Rose,聯絡信箱 thornton.rose@mindspring.com。