kaneko1102 / pokemon-compatibility

MIT License
0 stars 0 forks source link

リーダブルコード #1

Open kaneko1102 opened 1 year ago

kaneko1102 commented 1 year ago

コードをリーダブルにする

変更前のソースコード(約7ヶ月前に作成、GitHub) 変更後のソースコード(GitHub)

kaneko1102 commented 1 year ago

jsonのスタイルの統一

jsonの{}:,の前後に空白がある場合とない場合があったため、空白を追加してスタイルを統一した。

また、jsonで改行をしているとき、空白を追加してキーと値の位置を揃えることで読みやすくなっている。

-var msg = {normal:"通常の効果",nothing:"効果がない",good:"効果はばつぐん",bad:"効果はいまひとつ"};
+var msg = { normal: "通常の効果", nothing: "効果がない", good: "効果はばつぐん", bad: "効果はいまひとつ" };
-var style = {normal:"\"font-size:24px;color: #007800;\"",
-            nothing:"\"font-size:24px;color: #000000;\"",
-            good:"\"font-size:24px;color: #ff2400;\"",
-            bad:"\"font-size:24px;color: #0078ff;\""
+var style = { normal:  "\"font-size:24px;color: #007800;\"",
+              nothing: "\"font-size:24px;color: #000000;\"",
+              good:    "\"font-size:24px;color: #ff2400;\"",
+              bad:     "\"font-size:24px;color: #0078ff;\""
kaneko1102 commented 1 year ago

変数名をわかりやすく変更

msgは何のメッセージか分からず、styleは何のスタイルか分からないため、何を表しているかという情報を変数名に追加した。

-var msg = { normal: "通常の効果", nothing: "効果がない", good: "効果はばつぐん", bad: "効果はいまひとつ" };
+var effect_msg = { normal: "通常の効果", nothing: "効果がない", good: "効果はばつぐん", bad: "効果はいまひとつ" };
-var style = { normal:  "\"font-size:24px;color: #007800;\"",
+var effect_style = { normal:  "\"font-size:24px;color: #007800;\"",
kaneko1102 commented 1 year ago

if文の意味のコメントを追加

if文がどういう意味であるかをコメントを加えることで伝わりやすくした。

+    // ポケモンが2つのタイプをもつとき
     if (pokemon_type2 != "none" && pokemon_type1 != pokemon_type2) {
kaneko1102 commented 1 year ago

データ形式を配列からjsonに変更

意味が異なる複数の値をreturnするとき配列ではなくjsonにすることで、どういう値かを分かりやすくした。

-    return [magnification, effect];
+    return {"magnification": magnification, "effect": effect};

また、jsonと配列を組み合わせることで、テストを読みやすくした。

-function assertDamage(input, expected) {
-    var actual = damageMagnification(input[0], input[1], input[2]);
-    if (actual[0] != expected[0] || actual[1] != expected[1]) {
-        console.log(`\x1b[31m${input} => ${expected} expected, but got ${actual}\x1b[0m`);
-        Deno.exit(1);
-    }
-}

-function unitTest() {
-    assertDamage(["fire","grass","none"], [2,"good"]);
-    assertDamage(["fire","grass","ice"], [4,"good"]);
-    assertDamage(["normal","ghost","none"], [0,"nothing"]);
-    assertDamage(["normal","ghost","fire"], [0,"nothing"]);
-    assertDamage(["normal","normal","flying"], [1,"normal"]);
-    assertDamage(["electric","water","flying"], [4,"good"]);
-    assertDamage(["ice","grass","ice"], [1,"normal"]);
-    assertDamage(["fighting","ghost","dark"], [0,"nothing"]);
-    assertDamage(["fire","water","none"], [0.5,"bad"]);
-    assertDamage(["fire","water","electric"], [0.5,"bad"]);
-    assertDamage(["fire","fire","rock"], [0.25,"bad"]);
-    assertDamage(["dragon","dragon","none"], [2,"good"]);
-    assertDamage(["ghost","ghost","none"], [2,"good"]);
-    assertDamage(["ghost","ghost","dark"], [1,"normal"]);
-
-    assertDamage(["fire","grass","grass"], [2,"good"]);
-    assertDamage(["fire","water","water"], [0.5,"bad"]);
-}
+// ユニットテスト
+Deno.test("damage test",()=> {
+    let input = [{move_type: "fire", pokemon_type1: "grass", pokemon_type2: "none"},
+                 {move_type: "fire", pokemon_type1: "grass", pokemon_type2: "ice"},
+                 {move_type: "normal", pokemon_type1: "ghost", pokemon_type2: "none"},
+                 {move_type: "normal", pokemon_type1: "ghost", pokemon_type2: "fire"},
+                 {move_type: "normal", pokemon_type1: "normal", pokemon_type2: "flying"},
+                 {move_type: "electric", pokemon_type1: "water", pokemon_type2: "flying"},
+                 {move_type: "ice", pokemon_type1: "grass", pokemon_type2: "ice"},
+                 {move_type: "fighting", pokemon_type1: "ghost", pokemon_type2: "dark"},
+                 {move_type: "fire", pokemon_type1: "water", pokemon_type2: "none"},
+                 {move_type: "fire", pokemon_type1: "water", pokemon_type2: "electric"},
+                 {move_type: "fire", pokemon_type1: "fire", pokemon_type2: "rock"},
+                 {move_type: "dragon", pokemon_type1: "dragon", pokemon_type2: "none"},
+                 {move_type: "ghost", pokemon_type1: "ghost", pokemon_type2: "none"},
+                 {move_type: "ghost", pokemon_type1: "ghost", pokemon_type2: "dark"},
+                 {move_type: "fire", pokemon_type1: "grass", pokemon_type2: "grass"},
+                 {move_type: "fire", pokemon_type1: "water", pokemon_type2: "water"},
+                ];
+
+    let expected = [{magnification: 2, effect: "good"},
+                    {magnification: 4, effect: "good"},
+                    {magnification: 0, effect: "nothing"},
+                    {magnification: 0, effect: "nothing"},
+                    {magnification: 1, effect: "normal"},
+                    {magnification: 4, effect: "good"},
+                    {magnification: 1, effect: "normal"},
+                    {magnification: 0, effect: "nothing"},
+                    {magnification: 0.5, effect: "bad"},
+                    {magnification: 0.5, effect: "bad"},
+                    {magnification: 0.25, effect: "bad"},
+                    {magnification: 2, effect: "good"},
+                    {magnification: 2, effect: "good"},
+                    {magnification: 1, effect: "normal"},
+                    {magnification: 2, effect: "good"},
+                    {magnification: 0.5, effect: "bad"},
+                  ];

-unitTest();
+    
+    for (let i = 0; i < input.length; i++) {
+        var actual = damageMagnification(input[i]["move_type"], input[i]["pokemon_type1"], input[i]["pokemon_type2"]);
+        if (actual["magnification"] != expected[i]["magnification"] || actual["effect"] != expected[i]["effect"]) {
+            throw Error(`${input[i]} => ${expected[i]} expected, but got ${actual}`);
+        }
+    }
+});
kaneko1102 commented 1 year ago

ライセンスの追加

ライセンスがないと、ほかの人が利用する際に著作権などの問題が発生するため、ライセンスを追加した。 ソースコードの中に記入をすることで、READMEやLICENSEファイルなどほかのファイルを参照しに行く必要がなく見やすい。

+/*
+pokemon.js
+
+Copyright (c) 2022 Takuya Kaneko
+
+This software is released under the MIT License.
+http://opensource.org/licenses/mit-license.php
+*/
+