zoukankan      html  css  js  c++  java
  • 代码可读性的改良

    首先, 有这样的代码,逻辑是没错的,但是长而且可读性不好:

    message = subAction == "add" ?  String.format(format, contentMap.get("owner"), contentMap.get("ownerHomeName"))
             : String.format(format, contentMap.get("ownerHomeName"))

    改良版1,代码没那么长了,但是可读性没什么改善:

    message = String.format(format, contentMap.get("owner"), contentMap.get("ownerHomeName"))
    message = subAction == "add" ? message : String.format(format, contentMap.get("ownerHomeName")) 

    改良版2,多几行,看起来可读性就好多了

    addMessage = String.format(addFormat, contentMap.get("owner"), contentMap.get("ownerHomeName"))
    removeMessage = String.format(removeFormat, contentMap.get("ownerHomeName"))
    message = subAction == "add" ? addMessage : removeMessage 

    朋友给的python版本解决方案,可惜他没看参数个数:

    foo = lambda x: String.format( format , contentMap.get( x) )
    message = foo( 'owner' ) if subAction is 'add' else foo( 'ownerHomeName' )
  • 相关阅读:
    vim高亮
    mengning
    4.4内核osal
    tmpvalgrind
    为什么引入协程
    alloc_call_show(转)
    TSAN
    如何查看哪些进程占用Buffer和Cache高(转)
    ASAN详解其他参考链接
    Linux系统与程序监控工具atop教程(转)
  • 原文地址:https://www.cnblogs.com/code-style/p/4573366.html
Copyright © 2011-2022 走看看