吐槽一下 ShardSDK 为 cocos2d-x 写的 Sample 代码。

2015-11-08 13:11:22 +08:00
 xpol
这两天再基于 ShareSDK Cocos2d-x 的 Sample 代码做 Lua 的分享接口。

有些 java 代码不得不来吐槽一下,顺便给出我的重构代码,请大家帮忙指正下:

## 片段一

```Java
if (!(content.get("title") == null)) {
map.put("title", content.get("title"));
}
if (!(content.get("description") == null)) {
map.put("comment", content.get("description"));
}
if (!(content.get("url") == null)) {
map.put("url", content.get("url"));
map.put("titleUrl", content.get("url"));
}
if (!(content.get("site") == null)) {
map.put("site", content.get("site"));
}
if (!(content.get("siteUrl") == null)) {
map.put("siteUrl", content.get("siteUrl"));
}
if (!(content.get("musicUrl") == null)) {
map.put("musicUrl", content.get("musicUrl"));
}
if (!(content.get("extInfo") == null)) {
map.put("extInfo", content.get("extInfo"));
}
```


碍眼的地方:

1. 莫非 Java 里面 (!(content.get("content") == null)) 和 (content.get("content") != null) 有什么不同?我 Java 学的少,别骗我。
2. 这段采用重复手法的代码,让我一个用惯 Lua 的人情何以堪啊。用一个名字 的映射表,然后循环就搞定的事情。

``` Java
private static final Map<String, String> NATIVE_FIELDS;
static {
Map<String, String> map = new HashMap<String, String>();
map.put("text", "content");
map.put("title", "title");
map.put("comment", "description");
map.put("url", "url");
map.put("titleUrl", "url");
map.put("site", "site");
map.put("siteUrl", "siteUrl");
map.put("musicUrl", "musicUrl");
map.put("extInfo", "extInfo");

NATIVE_FIELDS = Collections.unmodifiableMap(map);
}


for (Map.Entry<String, String> entry : NATIVE_FIELDS.entrySet())
{
String nativeName = entry.getValue();
String wantedName = entry.getKey();

Object value = content.get(nativeName);
if (value != null)
{
map.put(wantedName, value);
}
}
```



## 片段二


```Java
if (map.containsKey("text")) {
oks.setText(String.valueOf(map.get("text")));
}
if (map.containsKey("imagePath")) {
oks.setImagePath(String.valueOf(map.get("imagePath")));
}
if (map.containsKey("imageUrl")) {
oks.setImageUrl(String.valueOf(map.get("imageUrl")));
}
if (map.containsKey("title")) {
oks.setTitle(String.valueOf(map.get("title")));
}
if (map.containsKey("comment")) {
oks.setComment(String.valueOf(map.get("comment")));
}
if (map.containsKey("url")) {
oks.setUrl(String.valueOf(map.get("url")));
}
if (map.containsKey("titleUrl")) {
oks.setTitleUrl(String.valueOf(map.get("titleUrl")));
}
if (map.containsKey("site")) {
oks.setSite(String.valueOf(map.get("site")));
}
if (map.containsKey("siteUrl")) {
oks.setSiteUrl(String.valueOf(map.get("siteUrl")));
}
```

比片段一难一点,但是在一个习惯了 Lua 的 字段就是字符的人眼里,还是看着碍眼。
Java 不是有反射吗?当然反射的代码有点麻烦,还要异常处理。 Android 的 Java 是不是 Java 8 哦?

```Java
private static final Map<String, Method> OKS_FIELDS;
private static Method getSetter(String name) throws NoSuchMethodException {
return OnekeyShare.class.getMethod(name, String.class);
}
static {
Map<String, Method> map = new HashMap<String, Method>();
try {
map.put("comment", getSetter("setComment"));
map.put("imagePath", getSetter("setImagePath"));
map.put("imageUrl", getSetter("setImageUrl"));
map.put("site", getSetter("setSite"));
map.put("siteUrl", getSetter("setSiteUrl"));
map.put("text", getSetter("setText"));
map.put("title", getSetter("setTitle"));
map.put("titleUrl", getSetter("setTitleUrl"));
map.put("url", getSetter("setUrl"));
}
catch (NoSuchMethodException e)
{
Log.e("share", "error", e);
}

OKS_FIELDS = Collections.unmodifiableMap(map);
}


try {
for (Map.Entry<String, Method> entry : OKS_FIELDS.entrySet()) {
String field = entry.getKey();
Method set = entry.getValue();

String value = (String)content.get(field);
if (value != null) {
set.invoke(oks, value);
}
}
}
catch (Exception e)
{
Log.e("share", "error", e);
}
```


## 最后总结

1. ShareSDK 在 Android 和 iOS 上的消息格式不一样,导致这里的字段转换,不知道有没有基于 JSON 的借口。
2. Java 语法还是有点麻烦啊, 比如 初始化 HashMap ,要一个一个设置。
4989 次点击
所在节点    Android
6 条回复
ruixianxx
2015-11-08 15:46:56 +08:00
Sample 代码最重要的就是可读性啊。。
我倒是觉得写的简单粗暴挺好的 毕竟谁都能看得懂
xpol
2015-11-08 15:55:37 +08:00
@ruixianxx 也是。想拿来直接用的就痛苦了。
liuxey
2015-11-09 09:31:24 +08:00
为什么我觉得 if else 更舒服。。。
Tneciv
2015-11-09 09:38:59 +08:00
@liuxey 不是 switch 更舒服吗?
coolxiao
2015-11-24 14:21:54 +08:00
应该是 pairs 不是 ipairs 吧
xpol
2015-11-24 20:01:29 +08:00
@coolxiao 果然。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/234561

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX