Function - Clean Code

1. Small and the indent level should not be greater than one or two.
2. Do One Thing - Functions should do one thing. They should do it well. They should do it only.
    Steps of function are one level of abstraction below the name of the function. Then the function is doing one thing.
    Another way to know function is doing more than one thing is if you can extract another function from it.
3. One level of abstraction per function
    The stepdown rule - reading code from top to bottom.
    switch
4. Use Descriptive Names
    Don't be afraid to make a name long.
    A long descriptive name is better than a short magic name.
    A long descriptive name is better than a long descriptive comment.
    Dont't be afraid to spend time choosing a name.
    Be consistent in your names.
5. Function Arguments
    The ideal number of arguments for a function is zero.
    Three arguments should be avoided.
    Flag Arguments - can be split into two function.
    Aruments Objects - group the variables and abstract concept if possible.
    Verbs and keywords - function and argument should form a very nice verb/noun pair.
6. Have no side effects
7. Command Query Separation - Functions should either do something or answer something, but not both.
8. Prefer Exceptions to returning error codes.
    Extract try/catch blocks, error handling is one thing.
9. DRY - don't repeat yourself - especially the duplicated logic.

posted on 2011-08-25 08:45 *** 阅读(321) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问  
 
<2011年8月>
31123456
78910111213
14151617181920
21222324252627
28293031123
45678910

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜