21. private List m_list = null;
private int process_file(String str_file_name) {
String str_line;
List list_lines = new ArrayList();
int i_result = read_file(str_file_name, list_lines);
if (i_result == 0) {
List list_record = new ArrayList();
...
22. private List m_list = null;
private int process_file(String str_file_name) {
String str_line;
List list_lines = new ArrayList();
int i_result = read_file(str_file_name, list_lines);
if (i_result == 0) {
List list_record = new ArrayList();
...
メンバ変数だと分
かるよう、先頭に
m_ を付けよう
スネークケースの方が
読みやすいよね
変数は先頭でまとめて宣言
もちろんハンガリアン記法さ!
戻り値を複数返したい時
は、引数に戻り値相当の変数
参照を渡せばいいんだよ
関数の戻り値はもちろん0が正常、
それ以外が異常
23. for (int i = 0; i < list_lines.size(); i++) {
str_line = (String) list_lines.get(i);
Record record = new Record();
i_result = parse_line(str_line, record);
if (i_result != 0) {
return i_result;
}
list_recordord.add(record);
}
m_list = list_record;
return 0;
24. for (int i = 0; i < list_lines.size(); i++) {
str_line = (String) list_lines.get(i);
Record record = new Record();
i_result = parse_line(str_line, record);
if (i_result != 0) {
return i_result;
}
list_recordord.add(record);
}
m_list = list_record;
return 0;
下の関数でエラーが出たら
ちゃんとエラーコードを上に
伝播させないとね
正常終了はいつも
return 0;
26. private List resultList;
private List processFile(String fileName)
throws SystemException {
List lines = readFile(fileName);
List recordList = new ArrayList();
for (int i = 0; i < lines.size(); i++) {
String line = (String) lines.get(i);
Record record = parseLine(line);
recordList.add(record);
}
return recordList;
}
27. private List resultList;
private List processFile(String fileName)
throws SystemException {
List lines = readFile(fileName);
List recordList = new ArrayList();
for (int i = 0; i < lines.size(); i++) {
String line = (String) lines.get(i);
Record record = parseLine(line);
recordList.add(record);
}
return recordList;
}
メンバ変数に接頭辞や
thisはつけない
キャメルケース
エラーはExceptionで
表現して伝播させる
エラーコードではなく
普通に値を返す
変数は
使う直前
に宣言
引数に戻り値への
参照を渡さない
No ハンガリアン
32. private List resultList;
private List processFile(String fileName)
throws SystemException {
List lines = readFile(fileName);
List recordList = new ArrayList();
for (int i = 0; i < lines.size(); i++) {
String line = (String) lines.get(i);
Record record = parseLine(line);
recordList.add(record);
}
return recordList;
}
41. private List processFile(String fileName)
throws SystemException {
List lines = readFile(fileName);
List recordList = new ArrayList();
for (int i = 0; i < lines.size(); i++) {
String line = (String) lines.get(i);
Record record = parseLine(line);
recordList.add(record);
}
return recordList;
}
46. public interface Constants {
public static final int FILE_NOT_FOUND = -1;
public static final int FILE_READ_ERROR = -2;
public static final int FILE_EMPTY = -3;
public static final int RECORD_EMPTY = -4;
public static final int RECORD_SIZE_ERROR = -5;
public static final int RECORD_BODY_EMPTY = -6;
}
47. public class FileProcessor implements Constants {
private List readFile(String fileName) {
List lines = new ArrayList();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(fileName));
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
} catch (FileNotFoundException ex) {
throw new SystemException(FILE_NOT_FOUND, ex);
} catch (IOException ex) {
throw new SystemException(FILE_READ_ERROR, ex);
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException ex) {
// この例外は無視する?
}
}
return lines;
}
}
48. public class FileProcessor implements Constants {
private List readFile(String fileName) {
List lines = new ArrayList();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(fileName));
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
} catch (FileNotFoundException ex) {
throw new SystemException(FILE_NOT_FOUND, ex);
} catch (IOException ex) {
throw new SystemException(FILE_READ_ERROR, ex);
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException ex) {
// この例外は無視する?
}
}
return lines;
}
}
定数インタフェースという
優れたテクニック
定数クラス名を省略して
記載できるんですよ!