写好代码 [comments after India project]

来源:互联网 发布:淘宝上卖盗版书的店铺 编辑:程序博客网 时间:2024/06/06 10:58
这几天项目里事情不忙,我就向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++的产品里去,难道,两年不爽的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:\n\tjava [ -cp . ] Copy.jar source destination \n";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);}}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 宝宝耳朵睡尖了怎么办 睡觉压的耳朵疼怎么办 月子里奶水越来越少怎么办 月子里生气回奶了怎么办 儿童疫苗本丢了怎么办 跖骨2-5骨折了怎么办 耳朵被水堵住了怎么办 耳朵一直流黄水怎么办 两个月宝宝脐疝怎么办 拔牙后一直渗血怎么办 耳朵滴药水堵了怎么办 刚打的耳洞化脓怎么办 耳朵进水了一直嗡嗡响怎么办 婴儿游泳呛水了怎么办 孩子游泳呛水了怎么办 婴儿洗澡呛水了怎么办 小孩脸上长湿疹老是不好怎么办 油耳堵住了耳朵怎么办 耳屎突然变湿该怎么办 小孩有耳屎好硬怎么办 小孩的耳屎深硬怎么办 1岁宝宝喉咙发炎怎么办 牙旁边的肉疼怎么办 鼻头软骨捏的痛怎么办 耳洞发炎肿了怎么办 一上火耳朵就疼怎么办 耳朵像隔了层膜怎么办 感冒引起的耳闷怎么办 5岁儿童视力0.6怎么办 柯基耳朵不立怎么办 宝宝一惊一乍睡觉不踏实怎么办 新婴儿睡觉不踏实怎么办 耳朵里面疼肿了怎么办 生出来的孩子是畸形怎么办 二胎生了缺陷儿怎么办 扣完肚脐眼后疼怎么办 刚出生的婴儿屁股红怎么办 狗狗耳朵流血了怎么办 狗狗不让掏耳朵怎么办 下巴总是反复长脓包怎么办 不胖但有双下巴怎么办