1
1

More than 3 years have passed since last update.

可読性を高めるif文の書き方-java

Last updated at Posted at 2019-11-11

研修メモ。(自分用)
※総ツッコミをいただきましたので訂正いたします。
ご指摘に感謝申し上げます。2019/11/14

チームで開発する場合プログラムの読みやすさつまり可読性は重要となる。
・積極的にコメントを入れる。
・if文の条件は必ずコメントを入れる癖をつけたほうがよい。

例えば以下のような条件の場合

sample
//aがbと等しい または cがdと等しいかつ eはfと等しくない かつ gはhと等しくない
if (a == b || (c == d && e != f) && g != h) {
 //処理内容
}

条件が複数ありややこしいので
条件を整理する。

sample

            int a = 0;
            int b = 0;
            int c = 0;
            int d = 0;
            int e = 0;
            int f = 0;
            int g = 0;
            int h = 0;


            if (a == b || (c == d && e != f) && g != h) {
                System.out.println("true1");
            }


            if (a == b) {
                System.out.println("true2");
            } else if (c == d & e != f) {
                if (g != h) {
                    System.out.println("true2");
                }
            }

結果:
true1
true2

sample

// 間違っていた処理
            if (g != h) {
                if (a == b) {
                    System.out.println("true2");
                } else if (c == d && e != f) {
                    System.out.println("true2");
                }
            }
        }

処理内容にもよるが、多少行数が増えたとしても
パッと見た簡潔さ、シンプルさを重視する現場は多いとのこと。

1
1
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1