IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Clean Code 笔记(1)Name

    Eric Sheng\'s Blog发表于 2015-12-21 20:59:00
    love 0
    • Reveal Your Intent
    • Descpribe Your Problem
    • Avoid Disinformation
    • Avoid Encoding
    • Part of Speech
    • The Scope of Length Rule

    Reveal Your Intent

    use intent-revealing name, if your need a comment ,that's not a good name.

    • bad:
        int d; //    elapsed time in days
    
    • good
        int elapsedTimeInDays;
    

    Descpribe Your Problem

    • bad:
    /***Useful range constant*/
    public static final int INLCUDE_NONE=0;
    /***Useful range constant*/
    public static final int INLCUDE_NONE=1;
    /***Useful range constant*/
    public static final int INLCUDE_NONE=2;
    /***Useful range constant*/
    public static final int INLCUDE_NONE=3;
    

    can you tell me what they mean,did the comment helpful?
    'userful?' are their useless code?'range?'range of what?'Constant' ofco use its a constant

    whenever you have to read the code in order to understand the name, the name has pretty much failed to communicate the intent.It's a bad name.
    Remember names are not for your convienient, it's your primariy tools for communicate intent.Commuicate your intent is always your first priority,it's even more important than the code work.

    Avoid Disinformation

    • disinformation: name in code does not mean what it's said.(worsest sins)
      a misleading name can cous

      Pronounceable Names

    • bad

        int PC_GWDA;
        public int getYYYY(){
            return this.year;
            }
            int qty_tests =0;
            int qty_pass_m=0;
            int qty_pass_s =0;
            int qty_skip = 0;
            int qty_fails=0;
    

    Avoid Encoding

    prifix: p mean point,c mean char, b mean boolean.C for Class,I for Interface.
    C: Class CAccount
    I:Interface IAccount
    p: pointer pAccount
    s: String sName
    st: Null Terminated StName

    now days,IDE can tell all this informations,i won't be using them.That' 1990s legency,silly prifixs.Just use names! Let the IDE,compiller and test do the rest.
    'Account is better than IAccount'

    Part of Speech

    class and variable are norns or norn pharse,methods are verbs.
    ignore silly nosie words.

    • good
      ACOUUNT,MESSAGEPARSER

    • silly nosie words
      MANAGER,DATA,INFO,PROSSOR

    • boolean variabels should been write like predicats,they read well,like isEmpty,isTerminated

    • methods should been verbs or verbs pharese, they read well,like getPirce,postPayment

    • if a methods returns a boolean,then should be predicats,beacuse they likely be used in if statement and they read well,likeisPostable

    boolean isEmpty;
    boolean isTerminated;
    ---
    if(isEmpty){
        if (payment.isPostable()){
            postPayment(payment);
        }
    }
    
    • Enums tends to be states or object descriptors,so they often adj.
    enum Color{RED,GREEN,BLUE};
    enum Status{PENDING,CLOSED,CANCELLED};
    enum Size{SMALL,MEDIUM,LARGE};
    

    remmeber: it's a lot easier to read code if the statements form sentences that read like well written poems.

    if(employee.isLate() employee.reprimand();
    

    The Scope of Length Rule

    • for varibales:

    the longger the scope of variable,the longger veriable names,but the variable with short scope should have short names;

    for (ITestResult tr: m_configIssues){
    Elemtent element = createElement(d,tr);
    rootElement.appendChild(element);
    }
    

    here tr is a good name,but d is bad.element should be shorten to e.

    • for funtions(oppsite):

    the longer the scope,the shorter the function or name should be;
    the shortter the scope,the longer the function should be.

        public void server(Socket s){
            try{
                        tryProcessInstructions(s);
                }catch(Throwable e){
                    slimFactory.stop();
                    close();
                    closeEnclosingServiceInSeperateThread();
                    }
                }
            }
    

    we like long socpe public function names short like serve,convinient to use,long name hard to tell apart;we like short scope private function names long like tryProcessInstructions,closeEnclosingServiceInSeperateThread,only called from here ,it's kind of a comment, explain themselves.
    The same arguement can be applied to Classes.



沪ICP备19023445号-2号
友情链接