V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
stephCurry
V2EX  ›  Java

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

  •  
  •   stephCurry · 2020-07-06 22:30:35 +08:00 · 2906 次点击
    这是一个创建于 1388 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我先来,引用别人的 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,秀操作呢?!

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

    18 条回复    2020-07-31 20:15:04 +08:00
    cjh1095358798
        1
    cjh1095358798  
       2020-07-06 23:27:15 +08:00
    感觉很牛皮啊
    neroransom
        2
    neroransom  
       2020-07-07 01:11:01 +08:00 via Android
    层 层 外 包
    drizztdu
        3
    drizztdu  
       2020-07-07 09:03:08 +08:00 via iPhone
    手动套娃
    Still4
        4
    Still4  
       2020-07-07 09:45:14 +08:00
    我见过这么用的,人家给我的解释是需要知道堆栈
    matepi
        5
    matepi  
       2020-07-07 09:56:18 +08:00
    如果 if 内部是个异常处理的通用做法,这个很可以理解
    但这个就是个 throw……要么就是准备以后写成通用做法,暂且这样写,后面便于批量找到后重构吧

    ……编不下去了
    timothyye
        6
    timothyye  
       2020-07-07 10:05:34 +08:00   ❤️ 11
    左手抛出异常,右手立马接住,把异常玩弄于鼓掌之间
    LongMaoz
        7
    LongMaoz  
       2020-07-07 10:12:11 +08:00
    一朋友的:
    if(res.data.Msg === "成功"){
    message.Show("成功")
    }
    supuwoerc
        8
    supuwoerc  
       2020-07-07 10:32:39 +08:00
    我看到过这样的:
    `
    if(a===true){
    a=false;
    }else{
    a=true;
    }

    `
    felinx
        9
    felinx  
       2020-07-07 10:58:13 +08:00
    // @todo xxx
    System.err.println(e.toString());

    这样写,估计楼主就不惊讶了
    ragnaroks
        10
    ragnaroks  
       2020-07-07 12:40:15 +08:00
    csharp 的

    public async Task ActionAsync(){
    //
    }

    public async Task Invoke(){
    await Task.run(async()=>{
    await this.ActionAsync();
    });
    }
    ragnaroks
        11
    ragnaroks  
       2020-07-07 12:41:35 +08:00
    好像被 trim 了,csharp 的

    public async Task ActionAsync(){
    ____//
    }

    public async Task Invoke(){
    ____await Task.run(async()=>{
    ________await this.ActionAsync();
    ____});
    }
    sadfQED2
        12
    sadfQED2  
       2020-07-07 12:54:15 +08:00 via Android
    @Still4 举个栗子,安卓上面检测 xposed 就需要这样写,查看堆栈中有没有 xposed 相关类
    wmhx
        13
    wmhx  
       2020-07-07 13:21:30 +08:00
    检测一个字符串是不是数值, 最简单的方法不就是 Integer.valueof() 看有没有异常么?
    liberty1900
        14
    liberty1900  
       2020-07-07 13:44:47 +08:00
    interface Status {
    success: boolean;
    failure: boolean;
    }

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

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

    //Typescript 代码,如何把布尔值复杂化
    //为了可以用 const 关键字不择手段
    bigNewsMaker
        15
    bigNewsMaker  
       2020-07-07 16:46:03 +08:00 via iPhone
    #define ";" ";"
    Anarchy
        16
    Anarchy  
       2020-07-07 19:09:26 +08:00
    我猜啊,可能原来是需要打印堆栈的,后来改了
    Ahaochan
        17
    Ahaochan  
       2020-07-08 00:39:42 +08:00
    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
        18
    122006  
       2020-07-31 20:15:04 +08:00
    new DeviceNotFoundException 里有逻辑呗。。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5588 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 06:39 · PVG 14:39 · LAX 23:39 · JFK 02:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.