博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
代码可读性的改良
阅读量:6902 次
发布时间:2019-06-27

本文共 882 字,大约阅读时间需要 2 分钟。

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

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' )

 

转载于:https://www.cnblogs.com/code-style/p/4573366.html

你可能感兴趣的文章
iOS之CATextLayer属性简介
查看>>
win10系统下cmd输入一下安装的软件命令提示拒绝访问解决办法
查看>>
git报错You are not allowed to force push code to a protected branch on this project
查看>>
times(NULL) Segmentation fault
查看>>
洛谷OJ P1010 幂次方 解题报告
查看>>
Python虚拟环境Virtualen简单使用
查看>>
给春节的宴客小吃来点小惊喜---绿茶甜心曲奇
查看>>
主动调用其他类的成员(普通调用和super方法调用)
查看>>
【2142】数据结构实验之图论二:基于邻接表的广度优先搜索遍历 (SDUT)
查看>>
Prometheus 普罗米修斯监控
查看>>
Only the original thread that created a view hierarchy can touch its views的解决方案
查看>>
教你如何在项目中集成支付功能
查看>>
设计模式:设计模式概述&JDK中的应用
查看>>
select 标签级联 和 html增加/删除行
查看>>
java分模块项目在idea中使用maven打包失败(ps:maven常用到的命令)
查看>>
ARM编译器中预定义的宏
查看>>
几个网站
查看>>
js介绍及内容(1)
查看>>
The Infinite Loop belong to loop statement
查看>>
聊天室
查看>>