代码质量控制和代码质量相关工具说明: https://blog.csdn.net/qq_37248504/article/details/125383986
翻译:将连接字符串传递给日志记录方法也可能导致不必要的性能损失,因为每次调用该方法时都会执行连接,无论日志级别是否低到足以显示消息。应该使用内置的字符串格式而不是字符串连接,如果消息是方法调用的结果,则应该完全跳过前置条件,而应该有条件地抛出相关异常。
反例
String message = "Hellow World!";
logger.info("Test Info message:"+message);
String message = "Hellow World!";
String format = String.format("Test Info message:%s", message);
logger.info(format);
System.out.printf("Hellow");
private static final Logger logger = Logger.getLogger(String.valueOf(Demo.class));
logger.info("Hellow");
logger.info(String.valueOf((char)c));
String s2 = String.valueOf((char) c);
logger.info(s2);
throw new Exception("Test");
public class MyException extends RuntimeException{
public MyException() {
}
public MyException(String message) {
super(message);
}
}
throw new MyException("Hellow");
String message = "Hellow Test!";
logger.info(String.format("method error: {}",message));
String message = "Hellow Test!";
logger.info(String.format("method error: %s",message));
private void dealAddNum() {
try {
num++;
Thread.sleep(100);
} catch (InterruptedException e) {
log.info(e.getMessage());
}
}
private void dealAddNum() {
try {
num++;
Thread.sleep(100);
} catch (InterruptedException e) {
log.info(e.getMessage());
Thread.currentThread().interrupt();
}
}
翻译:方法没有方法主体有以下几个原因:这是一个无意的疏忽,应该加以修正,以防止生产中出现意外的行为。它还没有得到支持,或者永远不会得到支持。在这种情况下,应该抛出UnsupportedOperationException。该方法是一个故意为空的覆盖。在这种情况下,一个嵌套的注释应该解释空白覆盖的原因。
反例:
@Override
public void setProperties(Properties properties) {
}
@Override
public void setProperties(Properties properties) {
throw new UnsupportedOperationException();
}
private static final ThreadLocal<SimpleDateFormat> DATETIME_FORMATTER = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
private static final ThreadLocal<SimpleDateFormat> DATETIME_FORMATTER = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
public void test1(){
try{
// do something
}catch(Exception e){
// do something
}finally{
DATETIME_FORMATTER.remove();
}
}
@Test
public void test1(){
try {
try{
}catch (NullPointerException e){
logger.info(e.getMessage());
}
}catch (Exception e){
logger.info(e.getMessage());
}
}
@Test
public void test1(){
try {
logger.info("test messages!");
}catch (Exception e){
logger.info(e.getMessage());
}
}
try {
// do somethings
} catch (Exception e) {
if (e instanceof InvocationTargetException) {
logger.error(LogConstant.LOG_FAILURE_PREFIX + (((InvocationTargetException) e).getTargetException()).getMessage());
} else {
logger.error(LogConstant.LOG_FAILURE_PREFIX + e.getMessage());
}
return result;
}
try{
logger.info("test!");
}catch (NullPointerException nullPointerException){
logger.info(nullPointerException.getMessage());
}catch (IllegalArgumentException illegalArgumentException){
logger.info(illegalArgumentException.getMessage());
}
user.setGender(((int)(10 * Math.random())) % 2 );
Random random = new Random();
user.setGender(random.nextInt(10) % 2 );