本文介绍了代码风格沉思 - 消除双重否定。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我经常在某个函数中找到这样的逻辑: .... 如果不是IsNull(< some expression>)然后 <默认行动> 否则 <替代行动> 结束如果 .... 如果不是IsNull ...其他事情使代码难以阅读,所以我的下一个 想到的是像这样交换它: .... 如果是IsNull(< some expression>)那么 <替代行动> 否则 <默认行动> 结束如果 .... 现在,首先列出替代操作,然后是默认的 操作,这也很难阅读。接下来,我想我应该写一个 函数,其正面名称评估Not IsNull(< Expression>),而我还是认为'b $ b仍然认为''一个好主意,但是怎么称呼这样的野兽? 如果我称之为NotNull(),这对我的双重负面易读性没有帮助 情况很多,它只是将一个2字的双阴性变成一个单字的 双阴性。如果我称之为HasValue,那么这个名字就不清楚了,因为所有可用的Nothing都没有?在阅读代码时,你怎么知道这是否支持这意味着不是空,不是,不是空,不是缺失,或者是某些 混合的以上?要更改该名称以使其具体,我再次似乎只需重播双重否定案例,例如。 HasNonNullValue,名字 也越来越长。 我意识到这似乎是一个非常微不足道的讨论主题,但它是 我心中的那些小痒之一,我已经过了很长时间才能和你一起生活。 解决方案 下一个 默认的 我 所有 似乎 名称 它是 I often come up with logic like this somewhere in a function:....If Not IsNull(<some expression>) Then<default action>Else<alternative action>End If....That If Not IsNull ... Else thing makes the code awkward to read, so my nextthought is to swap it around like this:....If IsNull(<some expression>) Then<alternative action>Else<default action>End If....Now, though, the alternative action is listed first, followed by the defaultaction which is also awkward to read. Next, I''m thinking I should write afunction with a positive name that evaluates Not IsNull(<Expression>), and Istill think that''s a good idea, but what to call such a beast?If I call it NotNull(), that doesn''t help my double-negative legibilitysituation much, it just turns a 2-word double-negative into a 1-worddouble-negative. If I call it HasValue, the name is unclear because of allthe available kinds of Nothing? When reading the code, how do you know ifthis is suppoed to mean Not Null, Not "", not Empty, not Missing, or somehybrid of the above? To change that name to make it specific, I again seem tobe just replaying the double negative case, eg. HasNonNullValue, and the nameis getting longer as well.I realize this seems like an awfully trivial topic for discussion, but it''sone of those little itches in the middle of my mind''s back that I''ve beenhaving to live with for ages now. 解决方案 nextdefaultIallseem tonameit''s 这篇关于代码风格沉思 - 消除双重否定。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-06 05:07