備忘録と戯言

学生がプログラミングの備忘録となんか印象に残ったことを綴る

コードの見やすさ

今回はコードの見やすさに着目してみました。

ですが、コードの書き方、見やすさの判別は十人十色!グループで制作しない限り自分の見やすいコードの書き方で書くと思います。


が!今回はどちらが見やすいかハッキリと判断できると思います。(たぶん・・・)

本題

今回やったことは「変数名や関数名を日本語にするか英語にするか」です。


作業環境 VS2013 for Desktop

まずC++版から

 #include <iostream>

 void test_1();
 void テスト_2();

 int main()
 {
     test_1();
     テスト_2();

     return 0;
 }

 void test_1()
 {
     int num = 10;
     bool flag = true;
     float f_num = 0.7f;
     std::string str = "asdfg";

     if (flag)
     {
         num += 3;//13
         f_num -= 0.2f;//0.5f
         str = "qwerty";//"qwerty"
     }

     std::cout << "test_1" << std::endl;
     std::cout << num << std::endl;
     std::cout <<f_num << std::endl;
     std::cout << str.c_str() << "\n" << std::endl;
 }

 void テスト_2()
 {
     int 数字 = 10;
     bool 何か知らないけどなんかのフラグ = true;
     float f_数字 = 0.7f;
     std::string とある文字列 = "asdfg";

     if (何か知らないけどなんかのフラグ)
     {
         数字 += 3;//13
         f_数字 -= 0.2f;//0.5f
         とある文字列 = "qwerty";//"qwerty"
     }

     std::cout << "test_2" << std::endl;
     std::cout << 数字 << std::endl;
     std::cout << f_数字 << std::endl;
     std::cout << とある文字列.c_str() << "\n" << std::endl;
 }


結果は同じ値を示します。

test_1
13
0.5
qwerty

test_2
13
0.5
qwerty

続行するには何かキーを押してください . . .


次はC#

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;

 namespace ConsoleApplication3
 {
     class Program
     {
         static void Main(string[] args)
         {
             test_1();
             テスト_2();
         }

         static void test_1()
         {
             int num = 10;
             bool flag = true;
             float f_num = 0.7f;
             string str = "asdfg";

             if(flag)
             {
                 num += 5;//15
                 f_num -= 0.4f;//0.3f
                 str = "zxcvb";//"zxcvb"
             }

             Console.WriteLine("test_1");
             Console.WriteLine("{0}\n{1}\n{2}\n", num, f_num, str);
         }

         static void テスト_2()
         {
             int 数字 = 10;
             bool 何か知らないけどなんかのフラグ = true;
             float f_数字 = 0.7f;
             string とある文字列 = "asdfg";

             if (何か知らないけどなんかのフラグ)
             {
                 数字 += 5;//15
                 f_数字 -= 0.4f;//0.3f
                 とある文字列 = "zxcvb";//"zxcvb"
             }

             Console.WriteLine("test_2");
             Console.WriteLine("{0}\n{1}\n{2}\n", 数字, f_数字, とある文字列);
         }
     }
 }


同じく結果同じ値を示した

test_1
15
0.3
zxcvb

test_2
15
0.3
zxcvb

続行するには何かキーを押してください . . .


上記2つは2通りの書き方で同じ値を示しました。

さて、どちらの方が見やすいでしょうか?


私は圧倒的に英語なのですが中には英語が苦手で日本語の方が見やすいという方がいるかもしれません。


英語で書くといつの間にかボキャブラリーが増えてる、という利点もあります。(私の場合は・・・)



では。