コードを書く[comments after India project]

6084 ワード

ここ数日プロジェクトの中で仕事が忙しくないので、DYに私のいくつかのcommentsを聞いて、意外にも彼が私に小さなかまどを開けてコードをもっと優雅に書く方法を説明してくれた.
メモを取る:
1. Code could be written shorter, functionality is easy to implement, but we need to write it well.
2. Refactor had better not be in task list
3. software could be divided into 3 kinds: Life-Critical, Brand-Critical and Commercial.
Also he gave me and lily an exercise to write a small program cp, I think it's a good way to improve my programming.
Detailed comment from my code:
try catch in I/O operation
less comment, code is comment
linear code, such as:
convert
if (fileExists(src)){
     if(fileExists(des)&&!isCopyOverwrite(des)){
		return false;
	 }
     else if(fileExists(des)&&isCopyOverwrite(des)){
		return true;
     }
	 else{
		return true;
	 }
}
else{
}

to 

if (!fileExists(src)) {
     // src file does not exist
     printError(COPY_FAILURE_MSG);
     printError(src + " does not exist");
     return false;
}

if (!fileExists(des)) {
     return true;
}

if (isCopyOverwrite(des)) {
     printMessage("choose to overwrite the existing file " + des);
     return true;
}

それから彼は私に<<敏捷なソフトウェア開発>の本を読むことを提案した.
最新のニュースによると、2ヶ月後にC++を作る製品に変える可能性が高いという.まさか、2年間の不快なJava生涯はこれで終わるのだろうか.
練習のすべてのコードは以下の通りです.
/**
 * 
 * <p>
 * try to write a java application to simulate ‘cp’ command, coping a source
 * file to a destination file.<br>
 * <br>
 * the basic is to read the source file byte by byte, then write to the
 * destination file.<br>
 * the command line is like:
 * 
 * <pre>
 * # java –cp . copy abc.txt dest.txt
 * </pre>
 * 
 * output proper error message to the user.<br>
 * 
 * @author xxx
 * 
 * 
 */
public class Copy {
	public static final String COPY_FAILURE_MSG = "failed to copy files";
	public static final String COPY_IO_ERROR_MSG = "failed to copy file because an I/O error occurs";
	public static final String COPY_USAGE = "Usage:
\tjava [ -cp . ] Copy.jar source destination
"; public static void main(String[] args) { if (isInputValid(args)) { copyFile(args[0], args[1]); } } /** * validate the input parameters<br> * The parameter should be<br> * * <pre> * args[0] srouce file path * args[1] destination file path * </pre> * * @param args * the input parameters in command line * @return * * <code>false</code> paramters count is not 2 <br> * <code>true</code> if source file exists and destination file does * not exist<br> * <code>true</code> if source file exists and destination file * exist and user choose to overwrite it<br> * <code>false</code> if source file exists and destination file * exists and user choose not to overwrite it<br> * <code>false</code> if source file does not exist <br> */ private static boolean isInputValid(String[] args) { if (args.length != 2) { printError(COPY_FAILURE_MSG); printError(COPY_USAGE); return false; } String src = args[0]; String des = args[1]; if (!fileExists(src)) { printError(COPY_FAILURE_MSG); printError(src + " does not exist"); return false; } if (!fileExists(des)) { return true; } if (isCopyOverwrite(des)) { printMessage("choose to overwrite the existing file " + des); return true; } else { printMessage("choose not to overwrite the existing file " + des); return false; } } /** * check whether file <code>pathname</code> exist * * @param pathname * @return */ private static boolean fileExists(String pathname) { return new File(pathname).exists(); } /** * let use choose whether overwrite the existing file <code>des</code> * * @param des * @return */ private static boolean isCopyOverwrite(String des) { printMessage(des + " already exists, overwrite it? (y/n)"); try { int input = System.in.read(); if (input == 'y' || input == 'Y') { return true; } else { return false; } } catch (IOException e) { printMessage(COPY_IO_ERROR_MSG); return false; } } /** * Copy the file <code>src</code> to <code>des</code> <br> * print error message when failed to copy * * @param src * source file path * @param des * destination file path */ private static void copyFile(String src, String des) { try { printMessage("start to copy file"); doRealCopyIO(src, des); printMessage("succeed to copy file"); } catch (FileNotFoundException e) { printError(COPY_FAILURE_MSG); printError(src + " can't be read or " + des + " can't be written or " + des + " is directory"); } catch (IOException e) { printError(COPY_FAILURE_MSG); printError(COPY_IO_ERROR_MSG); } } /** * do the real copy file I/O * * @param src * @param des * @throws IOException */ private static void doRealCopyIO(String src, String des) throws IOException { BufferedInputStream inputStream = null; BufferedOutputStream outputStream = null; try { inputStream = new BufferedInputStream(new FileInputStream(src)); outputStream = new BufferedOutputStream(new FileOutputStream(des)); int read; while ((read = inputStream.read()) != -1) { outputStream.write(read); } outputStream.flush(); } catch (IOException e) { throw e; } finally { try { outputStream.close(); } catch (Exception e2) { } try { inputStream.close(); } catch (Exception e2) { } } } private static void printMessage(String string) { System.out.println(string); } private static void printError(String string) { System.err.println(string); } }