本文介绍了什么是首选编码风格,用于检查某事是否为假?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如下:

bool isCorrect = theAnswer == 42;

这是首选测试方式 false

if (!isCorrect)
  // throw exception

if (isCorrect == false)
  // throw exception

因为我们在工作的高级开发人员建议我们应该总是使用后一种方法,因为它增强了可读性,并确保其他开发人员可以清楚地看到虚假检查;感叹号容易错过。我更喜欢前者,因为它更简洁和可读性足够我。

The reason I ask is because one of our senior developers at work suggests we should always use the latter approach as it enhances readability and ensures other developers can clearly see the false check; an exclamation mark is easy to miss. I much prefer the former as it's more concise and readable enough to me.

我理解这可能是一个主观问题,所以想知道是否有一个具体的优先级提到任何编码风格。

I understand that this may be a subjective issue so was wondering if there was a concrete preference mentioned in any coding style.

推荐答案

我看到高级开发人员包含这样的代码的生产代码:

I have seen production code from senior developers containing such code:

if (isCorrect.ToString().Length == 5)

但我仍在使用:

if (!isCorrect)

使用你认为对你更可读的,所有开发者之间没有统计数据))

Use what you think is more readable for you, there is no statistics among all developers))

这篇关于什么是首选编码风格,用于检查某事是否为假?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 23:20