开发者

How can this kind of code be optimized?

Earlier today I saw one of my friends share a post on Google Reader (too long, at the end of this question).

Personally I think this piece of code is acceptable. But I am wondering what can be done to optimize this kind of code.

What I have come up with so far is to have a builder to get things done. However, it does not help much, especially when all the fields are mandatory.

Any insights?

EDIT:

By optimize, I mean, when other programmers see this piece of code ,they won't say WTF.


Code: Do not try to understand the names of the fields. They are initials of non-English.

public CktsQfxxHsh(String hjBh, String fqfZt, String jsfZt, String qcrDm,  
        String qcrXm, String fhrDm, String fhrXm, String qfrDm,  
        String qfrXm, String nsrsbhGf, String nsrxmGf, String nsrsbhXf,  
        String nsrxmXf, String swjgDmJsf, String swjgDmFqf,  
        String swjgJcFqf, String swjgJcJsf, String bfpryDm, String bspryMc,  
        String lxrXm, String lxrDh, String lxrDz, String lxrYb, Date sjSc,  
        Date sjFs, Date sjTjfh, Date sjTjqf, String hjLx, BigDecimal fpfs,  
        BigDecimal jeHj, BigDecimal seHj, BigDecimal jshjHj, String qtqk,  
        BigDecimal tseY, BigDecimal tseZbbl, String fhsm, Date fuhjzrq,  
        Date rqTk, Date sjFuhfs, String dyfhBh, String yqfhBz, String fhBz,  
        String cbBz, Date sjCbhfs) {  
    this.hjBh = hjBh;  
    this.fqfZt = fqfZt;  
    this.jsfZt = jsfZt;  
    this.qcrDm = qcrDm;  
    this.qcrXm = qcrXm;  
    this.fhrDm = fhrDm;  
    this.fhrXm = fhrXm;  
    this.qfrDm = qfrDm;  
    this.qfrXm = qfrXm;  
    this.nsrsbhGf = nsrsbhGf;  
    this.nsrxmGf = nsrxmGf;  
    this.nsrsbhXf = nsrsbhXf;  
    this.nsrxmXf = nsrxmXf;  
    this.swjgDmJsf = swjgDmJsf;  
    this.swjgDmFqf = swjgDmFqf;  
    this.swjgJcFqf = swjgJcFqf;  
    this.swjgJcJsf = swjgJcJsf;  
    this.bfpryDm = bfpryDm;  
    this.bspryMc = bspryMc;  
    this.lxrXm = lxrXm;  
    this.lxrDh = lxrDh;  
    this.lxrDz = lxrDz;  
    this.lxrYb = lxrYb;  
    this.sjSc = sjSc;  
    this.sjFs = sjFs;  
    this.sjTjfh = sjTjfh;  
    this.sjTjqf = sjTjqf;  
    this.hjLx = hjLx;  
    this.fpfs = f开发者_如何学运维pfs;  
    this.jeHj = jeHj;  
    this.seHj = seHj;  
    this.jshjHj = jshjHj;  
    this.qtqk = qtqk;  
    this.tseY = tseY;  
    this.tseZbbl = tseZbbl;  
    this.fhsm = fhsm;  
    this.fuhjzrq = fuhjzrq;  
    this.sjFuhfs = sjFuhfs;  
    this.dyfhBh = dyfhBh;  
    this.yqfhBz = yqfhBz;  
    this.fhBz = fhBz;  
    this.cbBz = cbBz;  
    this.sjCbhfs = sjCbhfs;  
}


Disregarding the variable names (as I think you are requesting) the only other problem is that there are too many of them. Does the caller of this constructor (I'm assuming Java) really have all those values sitting around as discrete variables, or do groups of them form sub-objects? It looks like there's a systematic structure to your variable names. The code might be improved by reflecting that structure in your object model rather than relying on naming conventions.


If any of the parameters generally come from objects that could group values together, or could sensibly be combined into objects that group values together, you could shorten the code by passing those objects instead of passing the individual values. But I think this will only reduce coding if those other objects already exist or can be used more optimally elsewhere too.


I suspect you use Java language. In this case if CktsQfxxHsh() is constructor and arguments is a names of fields which marks as final then you may autogenerate constructor by using Lombok:

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class CktsQfxxHsh {
    final String hjBh;
    final String fqfZt;
    ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜