你们更偏向那种形式? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
请不要在回答技术问题时复制粘贴 AI 生成的内容
66450146

你们更偏向那种形式?

  •  
  •   66450146 2014 年 11 月 20 日 5682 次点击
    这是一个创建于 4173 天前的主题,其中的信息可能已经有所发展或是发生改变。
    35 条回复    2014-11-22 10:41:19 +08:00
    21grams
        1
    21grams  
       2014 年 11 月 20 日
    一般来说第二种,可读性更好
    yjsslab
        2
    yjsslab  
       2014 年 11 月 20 日
    尽量不在if里使用否定吧,也就是尽量不用unless
    vulgur
        3
    vulgur  
       2014 年 11 月 20 日
    我是第一种,不过不写“is not None”,就是“if something”
    tabris17
        4
    tabris17  
       2014 年 11 月 20 日   1
    分清主次

    big_block_of_code()是程序的主干

    something is None 之后是分支,次要的

    应该把次要的分支写在if里
    jimwoo
        5
    jimwoo  
       2014 年 11 月 20 日
    - -!什么东西??看不懂!!我只看到了标题!
    yufz
        6
    yufz  
       2014 年 11 月 20 日
    @jimwoo 因为lz放了个gist,但是由于某些原因就看不到了 你懂的
    mulog
        7
    mulog  
       2014 年 11 月 20 日
    @vulgur 这两个语义是不一样的 不是任何时候都能替换的哦。。

    至于LZ的问题 我个人同意@tabris17 把相对来说更“异常”的情况处理写到if里
    rwx
        8
    rwx  
       2014 年 11 月 20 日
    个人来说第二种
    习惯把异常或者错误情况排除之后再写主逻辑,尽可能减少if嵌套
    onlyice
        9
    onlyice  
       2014 年 11 月 20 日 via Android
    @jimwoo 我也看不到东西,是图片没刷出来?
    faceair
        10
    faceair  
       2014 年 11 月 20 日   1
    @jimwoo
    @onlyice

    gist被dns污染 推荐chinadns
    Ge4Los
        11
    Ge4Los  
       2014 年 11 月 20 日
    我用第二种, 优先跳出异常或错误, 然后执行主要代码;
    主要代码的缩进会靠前, 更容易阅读
    ytll21
        12
    ytll21  
       2014 年 11 月 20 日
    说看不见的看这个
    # First style
    if something is not None: # or whatever condition
    big_block_of_code()

    # Second style
    if something is None: # or whatever condition
    continue # or return None or whatever
    big_block_of_code()
    jimwoo
        13
    jimwoo  
       2014 年 11 月 20 日
    @faceair 我擦~我第一次来V2EX……果断落伍了!
    xidianlz
        14
    xidianlz  
       2014 年 11 月 20 日
    if something is not None:
    为啥不用
    if something:
    这样不是更pythonic一点?
    Sylv
        15
    Sylv  
       2014 年 11 月 20 日
    @xidianlz 这两个是有区别的,例如 something = 0
    ozking
        16
    ozking  
       2014 年 11 月 20 日
    第二个
    ozking
        17
    ozking  
       2014 年 11 月 20 日
    相对来说少缩进一层,
    imn1
        18
    imn1  
       2014 年 11 月 20 日
    貌似这两个逻辑不同

    单一条件不会有太大区别,只是可读性的问题,如果多条件,可以考虑以下短路方式减少判断
    rebornix
        19
    rebornix  
       2014 年 11 月 20 日
    如果代码比较复杂,我觉得通过fast return来减少内嵌层级比较好。倾向第二种。
    clino
        20
    clino  
       2014 年 11 月 20 日
    @faceair 我用chinadns一样不行阿

    2014-11-20 14:51:10 INFO request gist.github.com
    2014-11-20 14:51:10 INFO response gist.github.com: [('203.161.230.171', 1, 1)]
    xidianlz
        21
    xidianlz  
       2014 年 11 月 20 日
    @Sylv something = 0时候
    something和something is not None不都是True么?
    regmach
        22
    regmach  
       2014 年 11 月 20 日
    我喜欢第二种
    Sylv
        23
    Sylv  
       2014 年 11 月 20 日 via iPhone
    @xidianlz 你试试 something = 0 和 something = '' 的情况就知道了
    yetone
        24
    yetone  
       2014 年 11 月 20 日
    第二种学名是防御式编程,推荐的写法。
    mulog
        25
    mulog  
       2014 年 11 月 20 日
    @xidianlz
    显然不是。。。
    a = 0
    if a:
    print a
    什么也不会print
    hustlzp
        26
    hustlzp  
       2014 年 11 月 20 日
    个人第二种
    cvrock
        27
    cvrock  
       2014 年 11 月 20 日
    显然是要分情况的,第一种情况可能会导致if嵌套太深,第二种情况可能会导致每个if内都有一个返回的出口,需要根据具体场景决定选择哪种写法。
    fish748
        28
    fish748  
       2014 年 11 月 21 日
    现在用第二种,嵌套少一层。
    lightening
        29
    lightening  
       2014 年 11 月 21 日
    if some_condition
    do_something
    end

    def do_something
    big_block_of_code
    end
    dreampuf
        30
    dreampuf  
       2014 年 11 月 21 日
    TLTR: “等于”比“不等于”更符合直觉,避免嵌套优先执行异常分支。选第二种。

    via. Code Complete 2nd

    Summary of Techniques for Reducing Deep Nesting

    The following is a list of the techniques you can use to reduce deep nesting, along with references to the sections in this book that discuss the techniques:

    - Retest part of the condition (this section)

    - Convert to if-then-elses (this section)

    - Convert to a case statement (this section)

    - Factor deeply nested code into its own routine (this section)

    - Use objects and polymorphic dispatch (this section)

    - Rewrite the code to use a status variable (in Section 17.3)

    - Use guard clauses to exit a routine and make the nominal path through the code clearer (in Section 17.1)

    - Use exceptions (Section 8.4)

    - Redesign deeply nested code entirely (this section)
    dingyaguang117
        31
    dingyaguang117  
       2014 年 11 月 21 日
    第二种
    whalegia
        32
    whalegia  
       2014 年 11 月 22 日
    这种这么细节的知识你们是在哪里看的啊……orz
    我不看到这种讨论贴就根本不知道
    jox
        33
    jox  
       2014 年 11 月 22 日
    我尽量不在选择分支里进行这种判断,选择分支里的值只有两个,逻辑真或逻辑假,如果在某个对象的值不存在的时候会使用函数或者提前计算得到布尔值,然后再进行判断。类似这样的:

    if isItExist(object):
    -- ...
    else:
    -- ...

    函数isItExist()的返回类型是布尔值
    66450146
        34
    66450146  
    OP
       2014 年 11 月 22 日
    @jox 如果是对数值进行合法性验证呢?
    jox
        35
    jox  
       2014 年 11 月 22 日
    @66450146 合法就是逻辑真,否则就是逻辑假呗

    if isValid(object):
    -- ....
    else:
    -- ....

    我觉得限制选择分支里的类型为布尔类型,尽量少用0或者void来代替逻辑假,用0或者void(None)之类的代替逻辑假会导致逻辑混乱
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1295 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 138ms UTC 17:27 PVG 01:27 LAX 10:27 JFK 13:27
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86