布尔类型 [英] Boolean types

查看:110
本文介绍了布尔类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code检讨,我发现我们的C#code的许多地方,看起来是这样的:

During code review I discovered many places of our C# code that looks like this:

if(IsValid()) {
     return true;
}
else {
     return false;
}

甚至是更好:

return (IsValid()? true : false);

我一直想知道为什么不只是写code是这样的:

I always wondered why not just write the code like this:

return IsValid();

这是我写这篇code的方式。我不是质疑的开发人员的技能,但也许试图寻找到开发者的灵魂。为什么开发商倾向于更为复杂的code,而不是更简单和直观?或者,也许原因是,这是很难接受布尔类型作为一等公民?

This is the way I would write this code. I ain't questioning the skills of the developers, but maybe trying to look into the developer's soul. Why would a developer favor more complex code and not a more simple and intuitive? Or maybe the reason is that it is hard to accept the Boolean type as the first-class citizen?

推荐答案

是的,你应该照你说的去做。这些人在做这些过于冗长的事情,因为他们第一次了解到这样的说法,也许在CS 101,它永远不会发生给他们空间去外面的他们所知道的作品的,看看是否有更好的,更简单的方法。

Yes, you should do it as you say. These people are doing these overly verbose things because they first learned it that way, perhaps in CS 101, and it never occurs to them to go outside the space of what they know works to see if there is a better, easier way.

这也给他们的能力说话。好的程序员需要多一点体贴和少了很多墨守成规。

This does speak to their competence. Good programmers need to be a little more thoughtful and a lot less hidebound.

又见<一个href=\"http://stackoverflow.com/questions/423823#424005\">http://stackoverflow.com/questions/423823#424005, <一href=\"http://stackoverflow.com/questions/423823#423915\">http://stackoverflow.com/questions/423823#423915.

这篇关于布尔类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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