Teaching course1 : How to improve your code quality

本文分享了提高代码质量的方法:审查代码以修正错误及改进风格;重构代码以解决潜在问题;并保持各层独立性及减少公共接口,确保系统的稳定性和可维护性。

Hello everyone, today I'm going to share with you about how to improve code quality. Well, as you know that it is very hard to maintain others' code right? Even they have left a lot of comment in the code. Why? not because the logic is complex but bad coding practice. so there are three ways to improve code quality, they are review code, refactor code, and follow the rule that make each layer independent and as less interfaces as possible.

So, the first point is review code. When we finish the code, we have to pay attention to review it to make sure there is not any typo, because somehow, the typo will confuse people to waste their time to understand the logic.  Also find out the potential performance issue, for example querying from database in the loop, it will slow down the performance.  And check the coding style, you know, if you code looks so ugly and complicate, for example there should use more then one page of paper to display a function code,  then you have to separate the function into 2 more functions and redesign your code, consider the part of job, it's part of the principle of code refactor.

Yes, refactor code is my the second point. Now can look at following java code:

static final String projection {
long contactId;
String contactName;
String contactAge;
....
//about 40 fields
String fieldN;
}
static final int IndexOfContactId = 0;
static final int IndexOfContactName = 1;
static final int IndexOfContactAge = 2;
...
static final int IndexOfFieldN = N;
public List<Contact> loadContact() {
  Cursor c = db.query(...projection...);
  long contactId = c.getLong(IndexOfContactId);
  ....
  String fieldN = c.getString(IndexOfFieldN);
}

What do you think about the code, it seems very common in any project.  And at the beginning you may not find out the potential issue, but what about when the requirement changes, for example, do not need to query with the field name "contactAge", you have to reorder the indexes. It's boring and risk. So, please refactor the code, as the same example, you may can refactor it like this:

static final menu projection {
contactId,
contactName;
contactAge;
....
//about 40 fields
fieldN;
}
public List<Contact> loadContact() {
  Cursor c = db.query(...projection.toStringArray()...);
  long contactId = c.getLong(projection.contactId.original());
  ....
  String fieldN = c.getString(projection.fieldN.original());
}

It's strong type and so clear right? you don't worry about removing some fields or adding some fields. It's very safe.

But sometimes it's hard to refactor code because the code is too strong coupling, to avoid the issue, what I am going to introduce is keep each layer independently and less public interface.

We have to design three layers at least,  UI layer, business layer, DAO layer. That's say each layer does each job. Coupling the three layers will cause complex code, issues, what's more, we have to define the public interface and control the number of the interface.

Ok, to sum up, we have to do three things to improve our code quality, they are review code, refactor code, separating to different layers and less interface can be public.

Any questions are welcome. Let start to improve our code quality from now on.

Thanks for your attention, next time I will share  how to improve the performance with you!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值