这是我第一次写博客,记录自己在工作中碰到的问题和总结的经验,要学会提炼和总结。
总结:代码的规范化
代码的规范是一个程序员是否合格的标准,如果你连代码的风格都没有体现出来,那么还是赶紧返厂重修吧(个人的愚见)。
驼峰式的风格。定义一个普通的变量时,采用的是驼峰式的命名规范,例如:myName,myClass等等。方法名亦是如此,myMethod(),getMyName()等等。当你采用全英文的拼写的时候,那整个项目建议都用这种方式,而不是某些变量或方法采用英文,其他的采用拼音等,这种风格个人觉得会影响其可读性。
定义常量。你要想清楚这个常量是整个项目都会用到的吗,或者只是当前业务会用到。整个项目都用到的常量建议提供一个常量类,把所有全局的常量放到里面,方便调用。常量的定义一般是全大写的风格,例如:
public final static int MAX_SIZE = 100000; //定义最大取值
注释。注释对于代码的可读性来说也很重要,当你发现你很久以前写的一个模块出现某个bug的时候,你回去看你写的代码可能要很久的时间才能定位到问题的所在,这样是很浪费时间的,效率低。如果你之前在代码里面有注释块,可能你会很容易定位到到底是哪个环节出了问题,有利于你的工作效率和代码的可读性。至于注释的规范我在这里就不提了,推荐一篇关于注释的规范, 引用:http://blog.csdn.net/shiyuezhong/article/details/8205281/
碰到的代码规范问题目前就这几个,经验尚浅。接下来说说项目中碰到的问题。
工作中碰到的项目问题。
- 各种验证问题。首先是参数的验证,我被这个问题坑了无数次(说到底还是没有一套规范和习惯)。在不能确保前端传回的参数是否存在时,我们都要在后台程序去判断这个参数的不存在情况。这个问题还是一个工作了2年的同事给我的建议。例如有一个参数
public void method(String name){...}
你可能跟前端的协议是这个参数必须有值,但是当前端传入的参数出现问题是,那么后台肯定会有空指针之类的异常抛出,呵呵,背锅赶紧的。但是你加上一个对参数的空值判断,提示参数传入异常,甩锅了有木有!碰到这种类似的问题,只能通过自己的经验去处理了,最好是有自己的一套规范。然后是返回值的判断验证问题。这种和上面说的参数差不多,可能你写了一个方法去数据库中查东西,这个返回的数据可能你会在接下来的代码中用到,但是你没有判断空值的问题,也会抛出空指针异常的,呵呵,老老实实背锅。只要你尊崇你的规范,对于某些临界值都去判断其存在与否,那么上述问题就不太可能会出现。 如何解析一个字符串表示的4则运算表达式。这个是让我头疼的问题。可能有人会说百度啊,网上一大堆,呵呵确实是,但是bug也一大堆。我用过3套解决这个问题的代码,全部有bug,而且有些莫名其妙(见识少莫见怪)。先贴一下代码:第一套方案
/** * * @author kidd create at 2016-02-15 * */ public class OperationUtil { private static Stack<Character> theStack; private static char charKey = (char) 97; private static Map<Character, String> hashMap = new HashMap<Character, String>(); private static String result; public static String processBeforeEvaluation(String input) { String regex = "[^0-9.*+/\\()-]"; //简单的验证 Matcher legal = Pattern.compile(regex).matcher(input); if (legal.find()) { System.out.println("Please enter legal arithmetic expression string."); System.exit(0); } StringBuffer sbuf = new StringBuffer(); Matcher m = Pattern.compile("(-\\d+|\\d+)(\\.\\d+)?").matcher(input); //匹配运算表达式的正则表达式 try { while (m.find()) { String temp = m.group(0); //此处有时候会报错,尚未清楚其原因 hashMap.put(charKey, temp); m.appendReplacement(sbuf, charKey + ""); charKey = (char) (charKey + 1); } } catch (Exception e) { System.out.println(input); e.printStackTrace(); return ""; } m.appendTail(sbuf); return sbuf.toString(); } public static String translateToPostfixExpression(String input) { theStack = new Stack<Character>(); result = ""; for (int j = 0; j < input.length(); j++) { char ch = input.charAt(j); switch (ch) { case '+': case '-': case '*': case '/': gotOper(ch); break; case '(': theStack.push(ch); break; case ')': gotParen(ch); break; default: result += ch; break; } // end switch } // end for loop while (!theStack.empty()) { result += theStack.pop(); } return result; } public static void gotOper(char opThis) { while (!theStack.empty()) { char opTop = theStack.pop(); if (opTop == '(') { theStack.push(opTop); break; } else { if (opThis == '*' || opThis == '/') { // 如果是本次是乘除,栈里最多可弹出一个乘号 if (opTop == '+' || opTop == '-') { theStack.push(opTop); } else { result += opTop; } break; } else { // 如果是本次是加减,栈里最多可弹出一次乘除,再加一次加减 result += opTop; } } // end else }// end while theStack.push(opThis); }// end gotOper() public static void gotParen(char ch) { while (!theStack.empty()) { char chx = theStack.pop(); if (chx == '(') break; else result += chx; } // end while } public static double evaluatePostfixExpression(String input) { // System.out.println("the content of the hashMap is :" + hashMap); char ch; double num1, num2, interAns = 0; String str1, str2; theStack = new Stack<Character>(); // 重置栈,后缀表达式求值是将数字放入栈中 try { for (int j = 0; j < input.length(); j++) { ch = input.charAt(j); if (ch >= 97) { // j代表一个double型的数字 theStack.push(ch); } else { str2 = hashMap.get(theStack.pop()); str1 = hashMap.get(theStack.pop()); num2 = Double.parseDouble(str2); num1 = Double.parseDouble(str1); if (num1 == 0.0 && num2 == 0.0) { System.out.println(111); } switch (ch) { case '+': interAns = num1 + num2; break; case '-': interAns = num1 - num2; break; case '*': interAns = num1 * num2; break; case '/': interAns = num1 / num2; break; default: interAns = 0; break; } // end switch hashMap.put(charKey, interAns + ""); theStack.push(charKey); charKey = (char) (charKey + 1); } // end else } // end for loop str1 = hashMap.get(theStack.pop()); interAns = Double.parseDouble(str1); } catch (Exception e) { System.out.println("please enter legal numbers!"); e.printStackTrace(); } hashMap.clear(); theStack.clear(); charKey = (char) 97; return interAns; }