工作教训

可笑场景1:

if(true==choose()){
	业务逻辑1
	
}eles{
	业务逻辑2
	
}
public boolean choose(){
	...
	写了一些逻辑判断
	...
	return true;
}
改进
if(choose())
	业务逻辑1
业务逻辑2
	
public boolean choose(){
	...
	写了一些逻辑判断
	...
	return true;
}

  

package com.zyqtest;

public class Test {
    public static void main(String[] args) {
        Test test = new Test();
        System.out.println(test.quicktest());
    }

    public boolean quicktest(){
        try{
            int i=1/0;
        }catch (Exception e){
            System.out.println("==========="+e);
            return false;
        }finally {
            System.out.println("+++++++++++");
            return true;
        }
    }
}

  

运行结果;

===========java.lang.ArithmeticException: / by zero
+++++++++++
true

  

改进:

package com.zyqtest;

public class Test {
    public static void main(String[] args) {
        Test test = new Test();
        System.out.println(test.quicktest());
    }

    public boolean quicktest(){
        try{
            int i=1/0;
            return true;
        }catch (Exception e){
            System.out.println("==========="+e);
            return false;
        }finally {
            System.out.println("+++++++++++");
        }
    }
}

  

运行结果:

===========java.lang.ArithmeticException: / by zero
+++++++++++
false

  

workdate in(#{workdate},#{workdate}-1,#{workdate}-2)

改进:

workdate between #{workdate}-2 and  #{workdate} 

 

我使用的是oracle数据库,注意使用plsql查看这两条SQL语句的执行计划,会发现,在筛选条件是连续值的时候,使用between and 语法,执行效率更加高效。

还有就是我们在自己的类中创建的方法,只有自己类内部使用,最好将方法定义成private的,不要定义成public的。

 

下面是工作中写的不太美观的代码,在这里进行一下改进。

public void makefile(File file){
String xmlfile="ys_"+file.getname.split("-")[5]+".xml";
假设你下面也有用到这个传入的File文件进行一系列的操作。
。。。。。
}
最好不要这样写,因为这样写出的代码真的很不美观
public void makefile(File file,String dateStr){
String xmlfile="ys_"+dataStr+".xml";
假设你下面也有用到这个传入的File文件进行一系列的操作。
。。。。。
}
也就是说,在一开就将日期处理好,传进去,别等到以后再传。如果下面的有多个方法都需要File这个类进行入参,并且需要由文件名切割出来的日期,最好是将在一开始就处理好,将处理好的日期也当做入参,好看。

posted on 2021-08-21 13:04  ~码铃薯~  阅读(37)  评论(0编辑  收藏  举报

导航