都吐槽下你们遇到的“十分惊艳”的代码吧

2020 年 7 月 6 日
 stephCurry

我先来,引用别人的 jar 包时,看到源码还能这么玩。。。

 if (deviceInfoModel == null || (deviceInfoModel.getDevices() == null || deviceInfoModel.getDevices().size() == 0)) {
     try {
         throw new DeviceNotFoundException("Android Device Not Found !!!");
     } catch (DeviceNotFoundException e) {
         System.err.println(e.toString());
     }
}

这 throw 了又 catch,秀操作呢?!

大伙说说遇到过的惊艳代码,任何语言都可以

3932 次点击
所在节点    Java
18 条回复
cjh1095358798
2020 年 7 月 6 日
感觉很牛皮啊
neroransom
2020 年 7 月 7 日
层 层 外 包
drizztdu
2020 年 7 月 7 日
手动套娃
Still4
2020 年 7 月 7 日
我见过这么用的,人家给我的解释是需要知道堆栈
matepi
2020 年 7 月 7 日
如果 if 内部是个异常处理的通用做法,这个很可以理解
但这个就是个 throw……要么就是准备以后写成通用做法,暂且这样写,后面便于批量找到后重构吧

……编不下去了
timothyye
2020 年 7 月 7 日
左手抛出异常,右手立马接住,把异常玩弄于鼓掌之间
LongMaoz
2020 年 7 月 7 日
一朋友的:
if(res.data.Msg === "成功"){
message.Show("成功")
}
supuwoerc
2020 年 7 月 7 日
我看到过这样的:
`
if(a===true){
a=false;
}else{
a=true;
}

`
felinx
2020 年 7 月 7 日
// @todo xxx
System.err.println(e.toString());

这样写,估计楼主就不惊讶了
ragnaroks
2020 年 7 月 7 日
csharp 的

public async Task ActionAsync(){
//
}

public async Task Invoke(){
await Task.run(async()=>{
await this.ActionAsync();
});
}
ragnaroks
2020 年 7 月 7 日
好像被 trim 了,csharp 的

public async Task ActionAsync(){
____//
}

public async Task Invoke(){
____await Task.run(async()=>{
________await this.ActionAsync();
____});
}
sadfQED2
2020 年 7 月 7 日
@Still4 举个栗子,安卓上面检测 xposed 就需要这样写,查看堆栈中有没有 xposed 相关类
wmhx
2020 年 7 月 7 日
检测一个字符串是不是数值, 最简单的方法不就是 Integer.valueof() 看有没有异常么?
35aZ4P8mT576683q
2020 年 7 月 7 日
interface Status {
success: boolean;
failure: boolean;
}

const result: Status = { success: true, failure: false }

if (!data) {
result.success = false;
result.failure = true;
}

//Typescript 代码,如何把布尔值复杂化
//为了可以用 const 关键字不择手段
bigNewsMaker
2020 年 7 月 7 日
#define ";" ";"
Anarchy
2020 年 7 月 7 日
我猜啊,可能原来是需要打印堆栈的,后来改了
Ahaochan
2020 年 7 月 8 日
public class PrivilegeProvider {
public String selectById(final Long id){
return new SQL(){
{
SELECT("id, privilege_name, privilege_url");
}
}.toString();
}
}

Mybatis 的源码,第一次看时居然没有看懂.
https://blog.ahao.moe/posts/Ignored_initialization_block.html
122006
2020 年 7 月 31 日
new DeviceNotFoundException 里有逻辑呗。。。。

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

https://v2ex.xtra.eu.org/t/687715

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

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

© 2021 V2EX