Created
October 20, 2014 05:54
-
-
Save 3846masa/6218a252ea5b75385552 to your computer and use it in GitHub Desktop.
if文の中が1行のとき,どれがベストなのか
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
if文の中が1行のとき,どれがベストなのか | |
コメント募集(下記以外の方法でも可) | |
*/ | |
// 01 | |
if (true) println("true"); | |
// 02 | |
if (true) | |
println("true"); | |
// 03 | |
if (true){println("true");} | |
// 04 | |
if (true){ | |
println("true"); | |
} | |
// 05 | |
if (true) | |
{ | |
println("true"); | |
} |
ためになる
ちょっとこのif外してみようかなって時に楽なので2,5をつかってます
// if (true)
println("true");
// if (true)
{
println("true");
}
なるほど! 確かに使いやすそうですね!!
昔は 1 の書き方を多用していましたが、ここ 10 年くらいは以下で統一してます。
if (true) {
}
理由ですが、1行も複数行の範疇に含まれるからです。
1行の時だけ条件わけすると無駄に書く時に覚えないと行けない条件が増えるからです。
例えばプログラム的にソースコードを処理する時などにはなるべく判別するべき条件が少ない方がバグがでにくいです。
ちなみに C++ の場合には変数スコープの話も絡んできて括弧ありとなしではまるで意味が違うので、できれば1行も複数行も統一して置いた方が良いと思います。
スコープ絡んでくると,囲むほうが無難な気もしてきますね....
なるほど,参考になります!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考 http://www.textdrop.net/google-styleguide-ja/cppguide.xml#%E6%9D%A1%E4%BB%B6%E6%96%87