代码风格沉思 - 消除双重否定。 [英] Code style musing - eliminating double negatives.

查看:58
本文介绍了代码风格沉思 - 消除双重否定。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常在某个函数中找到这样的逻辑:


....

如果不是IsNull(< some expression>)然后

<默认行动>

否则

<替代行动>

结束如果

....


如果不是IsNull ...其他事情使代码难以阅读,所以我的下一个

想到的是像这样交换它:


....

如果是IsNull(< some expression>)那么

<替代行动>

否则

<默认行动>

结束如果

....


现在,首先列出替代操作,然后是默认的

操作,这也很难阅读。接下来,我想我应该写一个

函数,其正面名称评估Not IsNull(< Expression>),而我还是认为'b $ b仍然认为''一个好主意,但是怎么称呼这样的野兽?


如果我称之为NotNull(),这对我的双重负面易读性没有帮助

情况很多,它只是将一个2字的双阴性变成一个单字的

双阴性。如果我称之为HasValue,那么这个名字就不清楚了,因为所有可用的Nothing都没有?在阅读代码时,你怎么知道这是否支持这意味着不是空,不是,不是空,不是缺失,或者是某些

混合的以上?要更改该名称以使其具体,我再次似乎只需重播双重否定案例,例如。 HasNonNullValue,名字

也越来越长。


我意识到这似乎是一个非常微不足道的讨论主题,但它是

我心中的那些小痒之一,我已经过了很长时间才能和你一起生活。

解决方案

它只是归结为土豆 - potawtoes dosen't it?

你可能会找到一种消除双阴性的方法

过多的时间和指数增加你的功能名称,

或者你可以在它旁边添加一个非常清晰的评论。


Mike Storr
www.veraccess.com

" Steve Jorgensen" <无**** @ nospam.nospam>在留言中写道

news:km ******************************** @ 4ax.com ...

我经常在函数中的某个地方提出这样的逻辑:

...
如果不是IsNull(< some expression>)那么
<默认行动>
其他
<替代行动>
结束如果
...

如果不是IsNull .. 。其他的事情让代码难以阅读,所以我的
接下来想的就是这样交换:

...
如果是IsNull(< some expression>)然后
<替代行动>
其他
<默认行动>
结束如果
...

现在,但是,首先列出替代操作,然后是
默认操作,这也很难阅读。接下来,我想我应该写一个
函数,其正面名称评估Not IsNull(< Expression>),
我仍​​然认为这是一个好主意,但是要调用什么这样的野兽?

如果我称之为NotNull(),那对我的双重负面易读性情况并没有多大帮助,它只会将一个双字双阴影变成一个1字
双阴性。如果我称之为HasValue,由于
所有可用的Nothing类型,名称不清楚?在阅读代码时,您如何知道这是否意味着不是空,不是,不是空,不是缺失,还是上述的某些混合?要更改该名称以使其具体,我再次
似乎只是重播双重否定的情况,例如。 HasNonNullValue和
名称也越来越长。

我意识到这似乎是一个非常简单的讨论主题,但是
它是其中一个小痒我脑子里的那个中间现在我已经和他们一起生活多年了。



当然,但是关键是我在一个典型的
中型应用程序中遇到了这个问题数百次,而且我每年做很多这样的事情。如果我能得出一般的答案,那可能会让我感到很放松。


另外,我觉得好评很重要当时的评论是

当你想不出一种制作代码的方法时你写的是什么?b $ b不言自明。我总是首先尝试使代码尽可能不言自明,然后再添加剩下的评论。


2004年1月20日星期二22:37:19 -0500,Mike Storr < st ****** @ sympatico.ca>

写道:

它只是归结为土豆 - potawtoes dosen't it?
你可能会找到一种消除双重否定的方法,花费过多的时间并指数增加你的功能名称,
或者你可以在它旁边添加一个非常明确的评论。

Mike Storr
www.veraccess.com

Steve Jorgensen <无**** @ nospam.nospam>在消息中写道
新闻:km ******************************** @ 4ax.com .. < blockquote class =post_quotes>我经常在函数的某个地方想出这样的逻辑:

...
如果不是IsNull(< some expression>)那么
<默认行动>
<替代行动>
结束如果
...

如果不是没有...其他事情使代码难以阅读,所以我的


下一个

以为是这样交换:

...
如果IsNull(< some expression>)然后
<替代行动>
其他
<默认行动>
结束如果
...
<但是,现在,首先列出替代操作,然后是


默认的

操作,这也很难阅读。接下来,我想我应该写一个
函数,其正面名称评估Not IsNull(< Expression>),


仍然认为这是一个好主意,但是怎么称呼这样的野兽?

如果我称之为NotNull(),那对我的双重负面易读性情况并没有多大帮助,它只是将一个2字的双阴性变成了一个单字的双阴性。如果我称之为HasValue,由于


所有

可用的Nothing类型,名称不清楚?在阅读代码时,您如何知道这是否意味着不是空,不是,不是空,不是缺失,还是上述的某些混合?要更改该名称以使其具体,我再次


似乎

只是重播双重否定的情况,例如。 HasNonNullValue和


名称

也越来越长。

我意识到这似乎是一个非常简单的讨论主题,但是


它是

在我心中的那些小痒之一,我已经和他们一起生活多年了。




2004年1月21日星期三02:13:06 GMT,Steve Jorgensen

< no **** @ nospam.nospam>写道:


就个人而言,我并不太关心首先采取的行动。

例如我很乐意写:

如果rs.recordcount = 0那么

''这样做

其他

''那样做

结束如果





如果rs.eof那么

''这样做

其他

''这样做

结束如果





如果isnull(myfunction())那么

''这样做

其他

''那样做

结束如果

我不知道是谁想出了共同的想法。动作必须首先发生
,然后发生异常。我不会被

反向打扰,但是像我一样,我被双重否定所打扰。


-Tom。
< blockquote class =post_quotes>我经常在函数的某个地方想出这样的逻辑:

...
如果不是IsNull(< some expression>)那么
<默认行动>
<替代行动>
结束如果
...

如果不是没有...其他事情使代码难以阅读,所以我的下一个想法就是像这样交换它:

...
如果是IsNull(< some expression>)那么
<替代行动>
其他
<默认行动>
结束如果
...

现在,已列出替代行动首先,然后是默认的
动作,这也很难阅读。接下来,我在想我应该写一个
函数,其正面名称评估Not IsNull(< Expression>),我仍然认为这是一个好主意,但是我该怎么称呼这样的野兽?

如果我称之为NotNull(),那对我的双重负面易读性情况并没有多大帮助,它只会将一个双字双阴影变成一个1字
双阴性。如果我称之为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 next
thought 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 default
action which is also awkward to read. Next, I''m thinking I should write a
function with a positive name that evaluates Not IsNull(<Expression>), and I
still 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 legibility
situation much, it just turns a 2-word double-negative into a 1-word
double-negative. If I call it HasValue, the name is unclear because of all
the available kinds of Nothing? When reading the code, how do you know if
this is suppoed to mean Not Null, Not "", not Empty, not Missing, or some
hybrid of the above? To change that name to make it specific, I again seem to
be just replaying the double negative case, eg. HasNonNullValue, and the name
is getting longer as well.

I realize this seems like an awfully trivial topic for discussion, but it''s
one of those little itches in the middle of my mind''s back that I''ve been
having to live with for ages now.

解决方案

It just boils down to potatoes - potawtoes dosen''t it?
You could probably find a way eliminate double-negatives, after spending an
inordinate amount of time and exponentially increasing your function names,
or you could just add a really clear comment next to it.

Mike Storr
www.veraccess.com

"Steve Jorgensen" <no****@nospam.nospam> wrote in message
news:km********************************@4ax.com...

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 next thought 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 default action which is also awkward to read. Next, I''m thinking I should write a
function with a positive name that evaluates Not IsNull(<Expression>), and I still 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 legibility
situation much, it just turns a 2-word double-negative into a 1-word
double-negative. If I call it HasValue, the name is unclear because of all the available kinds of Nothing? When reading the code, how do you know if
this is suppoed to mean Not Null, Not "", not Empty, not Missing, or some
hybrid of the above? To change that name to make it specific, I again seem to be just replaying the double negative case, eg. HasNonNullValue, and the name is getting longer as well.

I realize this seems like an awfully trivial topic for discussion, but it''s one of those little itches in the middle of my mind''s back that I''ve been
having to live with for ages now.



Sure, but the point is that I hit this problem hundreds of times in a typical
mid-sized app, and I do many of those per year. That''s a lot of itching that
could be relieved if I could come up with a general answer.

Also, I feel good comments are essential much of the time, but comments are
what you write when you couldn''t think of a way to make the code
self-explanatory. I always try first to make the code as self-explanatory as
possible, then add what remaining comments are still required.

On Tue, 20 Jan 2004 22:37:19 -0500, "Mike Storr" <st******@sympatico.ca>
wrote:

It just boils down to potatoes - potawtoes dosen''t it?
You could probably find a way eliminate double-negatives, after spending an
inordinate amount of time and exponentially increasing your function names,
or you could just add a really clear comment next to it.

Mike Storr
www.veraccess.com

"Steve Jorgensen" <no****@nospam.nospam> wrote in message
news:km********************************@4ax.com.. .

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


next

thought 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


default

action which is also awkward to read. Next, I''m thinking I should write a
function with a positive name that evaluates Not IsNull(<Expression>), and


I

still 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 legibility
situation much, it just turns a 2-word double-negative into a 1-word
double-negative. If I call it HasValue, the name is unclear because of


all

the available kinds of Nothing? When reading the code, how do you know if
this is suppoed to mean Not Null, Not "", not Empty, not Missing, or some
hybrid of the above? To change that name to make it specific, I again


seem to

be just replaying the double negative case, eg. HasNonNullValue, and the


name

is getting longer as well.

I realize this seems like an awfully trivial topic for discussion, but


it''s

one of those little itches in the middle of my mind''s back that I''ve been
having to live with for ages now.




On Wed, 21 Jan 2004 02:13:06 GMT, Steve Jorgensen
<no****@nospam.nospam> wrote:

Personally I''m not too concerned with which action comes first.
For example I''ll happily write:
if rs.recordcount=0 then
'' do this
else
''do that
end if

or

if rs.eof then
'' do this
else
''do that
end if

or

if isnull(myfunction()) then
'' do this
else
''do that
end if
I don''t know who came up with the idea that the "common" action has to
occur first, and the exception after that. I''m not bothered by the
reverse, but like you I *am* bothered by double negatives.

-Tom.

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 next
thought 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 default
action which is also awkward to read. Next, I''m thinking I should write a
function with a positive name that evaluates Not IsNull(<Expression>), and I
still 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 legibility
situation much, it just turns a 2-word double-negative into a 1-word
double-negative. If I call it HasValue, the name is unclear because of all
the available kinds of Nothing? When reading the code, how do you know if
this is suppoed to mean Not Null, Not "", not Empty, not Missing, or some
hybrid of the above? To change that name to make it specific, I again seem to
be just replaying the double negative case, eg. HasNonNullValue, and the name
is getting longer as well.

I realize this seems like an awfully trivial topic for discussion, but it''s
one of those little itches in the middle of my mind''s back that I''ve been
having to live with for ages now.




这篇关于代码风格沉思 - 消除双重否定。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆