代码简洁优化---抽取
一,【前言】
在进行编程”日志读取分析算法”时,有出现最后一行读取不到,所以进行业务逻辑的在整理,就是最后对集合 list 清除后添加就可以了.
1,再进行一次清除后 add(line) 就可以了.
2,代码出现了重复,这怎么办?
问题出现了,再次清除后添加,这就让相同的代码重复了, 怎么提高代码的复用性呢?
代码出现重复, idea 有波浪线提示了:
二,【详情】
代码重复度高,怎么解决呢?
1,对方法进行抽取
2,填写一开始那个大方法名即可,parseLog:
代码优化,简洁后如下:
版本一: 张老师进行的是抽离: createAndFlush 代码如下, 但是结果行不通; 我抽离的是 parseLog, 结果是可以的:
publicvoid parseLog(StringfileNamePath, List<Event> eventList) {
do {
long position = positionService.getNowDayPosition(fileNamePath);
log.info("=parseLog=> fileNamePath={},position={} .", fileNamePath, position);
if (null != fileNamePath) {
RandomAccessFile in = null;
try {
in = new RandomAccessFile(fileNamePath, "r");
LogLines lines = new LogLines();
if (in.length() >= position) {
in.seek(position);
}
currentPosition = 0;
for (String line = in.readLine(); currentPosition < in.length(); line = in.readLine()) {
if (null == line || line.startsWith(String.valueOf(DateUtil.getNowYear()))|| lines.size() > MAX_EXCEPTION_LINES) {
creatAndFlush(fileNamePath, eventList, in, lines);
}
lines.add(line);
currentPosition = in.getFilePointer();
}
creatAndFlush(fileNamePath, eventList, in, lines);
if (currentPosition > 0) {
positionService.saveCurrentPosition(fileNamePath, currentPosition);
}
synchronized (eventList) {
if (!eventList.isEmpty()) {
flushEventBatch(eventList);
}
}
} catch (Exception e) {
log.error("=parseLog=> read file " + fileNamePath + " error ", e);
if (currentPosition > 0) {
positionService.saveCurrentPosition(fileNamePath, currentPosition);
}
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
log.error("=parseLog=> close file " + fileNamePath + " error ", e);
}
}
}
} else {
log.info("=parseLog=> no file path to analysis.");
}
if (restart) {
log.info("=parseLog=>sleep {} ms.", restartSleepTime);
try {
Thread.sleep(restartSleepTime);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} else {
log.info("<=parseLog=>thread is over.");
}
} while (restart);
}
版本二: 我进行抽离的是 parseLog, 代码简洁后如下:
publicvoid parseLog(StringfileNamePath, List<Event> eventList) {
do {
long position = positionService.getNowDayPosition(fileNamePath);
log.info("=parseLog=> fileNamePath={},position={} .", fileNamePath, position);
if (null != fileNamePath) {
RandomAccessFile in = null;
try {
in = new RandomAccessFile(fileNamePath, "r");
LogLines lines = new LogLines();
if (in.length() >= position) {
in.seek(position);
}
currentPosition = 0;
for (String line = in.readLine(); currentPosition < in.length(); line = in.readLine()) {
if (null == line || line.startsWith(String.valueOf(DateUtil.getNowYear()))|| lines.size() > MAX_EXCEPTION_LINES) {
parseLog(fileNamePath, eventList, in, lines);
}
lines.add(line);
currentPosition = in.getFilePointer();
}
parseLog(fileNamePath,eventList, in, lines);
if (currentPosition > 0) {
positionService.saveCurrentPosition(fileNamePath, currentPosition);
}
synchronized (eventList) {
if (!eventList.isEmpty()) {
flushEventBatch(eventList);
}
}
} catch (Exception e) {
log.error("=parseLog=> read file " + fileNamePath + " error ", e);
if (currentPosition > 0) {
positionService.saveCurrentPosition(fileNamePath, currentPosition);
}
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
log.error("=parseLog=> close file " + fileNamePath + " error ", e);
}
}
}
} else {
log.info("=parseLog=> no file path to analysis.");
}
if (restart) {
log.info("=parseLog=>sleep {} ms.", restartSleepTime);
try {
Thread.sleep(restartSleepTime);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} else {
log.info("<=parseLog=>thread is over.");
}
} while (restart);
}
版本一我进行测试,没能完成结果,版本二十可以的,主要原因就是一行代码的不同:
版本一: creatAndFlush(fileNamePath, eventList, in, lines);
版本二: parseLog(fileNamePath, eventList, in, lines);
三,【小结】
Don’t talk, show me your code.
编程如人生, 怎么化繁为简,怎么让 code 实现功能,又简洁,性能高是每一个开发工程师要思考的问题!
在一个团队了, 你不思考,自然有人会思考,思考多了, 一不小心你就成了架构师, CTO 之类的人了.---Daniel, 你不是在为公司工作, 你是在为你自己的人生而工作.