stephCurry
V2EX  ›  Java

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

  •  
  •   stephCurry · Jul 6, 2020 · 4335 views
    This topic created in 2315 days ago, the information mentioned may be changed or developed.

    我先来,引用别人的 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 replies  •  2020-07-31 20:15:04 +08:00
    cjh1095358798
    cjh1095358798
    Jul 6, 2020
    1
    感觉很牛皮啊
    neroransom
    neroransom
    Jul 7, 2020 via Android
    2
    层 层 外 包
    drizztdu
    drizztdu
    Jul 7, 2020 via iPhone
    3
    手动套娃
    Still4
    Still4
    Jul 7, 2020
    4
    我见过这么用的,人家给我的解释是需要知道堆栈
    matepi
    matepi
    Jul 7, 2020
    5
    如果 if 内部是个异常处理的通用做法,这个很可以理解
    但这个就是个 throw……要么就是准备以后写成通用做法,暂且这样写,后面便于批量找到后重构吧

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

    `
    felinx
    felinx
    Jul 7, 2020
    9
    // @todo xxx
    System.err.println(e.toString());

    这样写,估计楼主就不惊讶了
    ragnaroks
    ragnaroks
    Jul 7, 2020
    10
    csharp 的

    public async Task ActionAsync(){
    //
    }

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

    public async Task ActionAsync(){
    ____//
    }

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

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

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

    //Typescript 代码,如何把布尔值复杂化
    //为了可以用 const 关键字不择手段
    bigNewsMaker
    bigNewsMaker
    Jul 7, 2020 via iPhone
    15
    #define ";" ";"
    Anarchy
    Anarchy
    Jul 7, 2020
    16
    我猜啊,可能原来是需要打印堆栈的,后来改了
    Ahaochan
    Ahaochan
    Jul 8, 2020
    17
    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
    122006
    Jul 31, 2020
    18
    new DeviceNotFoundException 里有逻辑呗。。。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1211 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 69ms · UTC 16:45 · PVG 00:45 · LAX 08:45 · JFK 11:45
    ♥ Do have faith in what you're doing.