IF语句格式化最佳实践,你的风格是什么? [英] IF statement formatting best-practise, what's your style?
问题描述
寻找改进我的IF语句,我想保持我的代码看起来漂亮
Looking to improve my IF statement, and I want to keep my code looking pretty
我现在在做什么,是可读,改进的任何空间?
This is what I am currently doing, is it readable, any room for improvement?
SomeObject o = LoadSomeObject();
if( null == o
||
null == o.ID || null == o.Title
||
0 == o.ID.Length || 0 == o.Title.Length
)
$ b b
我没有人问。这就是为什么我来到这里在第一名。请不要关闭我的问题:(
I don't have anyone to ask around. That's why I came here in first place. Please don't close my question :(
推荐答案
您的详细程度导致可读性较差的代码,我认为以下格式最佳:
Your verbosity is leading to a less readable code, I think the following format is best:
if ( null == o || null == o.ID || null.Title || 0 == o.ID.Length || 0 == o.Title.Length )
{
// do stuff
}
我们都有高分辨率/宽屏显示器的原因,没有理由锁定你的代码在一些可怕的简短的语法。此外,我只需创建一个名为IsIDEmpty的函数,代码可能看起来像
We all have high resolution/widescreen displays for a reason, there's no reason to lock your code at some horribly short syntax. Also, I would simply create a function named IsIDEmpty so that the code could look like
if ( IsIDEmpty(o) )
{
// do stuff
}
以保持代码更简单和更清洁。返回一个布尔值,我相信这是你可能已经重用的反正,加上它作为一个简单的方式代码更自我记录/注释。
to keep the code simpler & cleaner. The function would perform the actual checks and return a boolean. I'm sure this is something you might have re-use for anyways, plus it serves as a simple way for code to be more self documenting/commenting.
这篇关于IF语句格式化最佳实践,你的风格是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!