0
0

ラジオボタンのようなコード

Last updated at Posted at 2024-09-01

はじめに

ひとこと

アクセスしていただきありがとうございます。
ラジオボタンのような複数の中から1つに対してはある操作を、その1つ以外には別の操作をするときのコードのアイデアです。

なんとなく好きなので記事にしました。

目的

  • ラジオボタンのようなコードのアイデアを共有すること

検証済みの環境

  • ECMA2023(JavaScript)
  • .NET 4.8

ご注意

  • この記事の情報は 2024/09/02現在 のものです

1.前書き

ラジオボタンとは

ラジオボタン(英: radio button)またはオプションボタン(英: option button)は、GUIウィジェットの一種で、事前定義された選択肢のうち1つを選択する場合に使う。名称の由来はカーラジオの選局ボタンで、1つのボタンを押すと他の押されていたボタンが押されていない状態に戻り、常に1つのボタンだけが押された状態になる特徴を表している。

「ラジオボタン - Wikipedia」より引用

2.各言語での実装

@tnbkyk689 様のコメントを基に、アイデアCを加筆しました。
ありがとうございます。

JavaScript

const radioButtons = ['A', 'B', 'C']

// A
// idx は 0 <= idx < radioButtons.length の整数が代入されるとする
function selectRadioButtonA(idx) {
    for (let i = 0; i < radioButtons.length; i ++) {
        radioButtons[i] = radioButtons[i].toUpperCase()
    }
    radioButtons[idx] = radioButtons[idx].toLowerCase()
    console.log(radioButtons)
}

// B
// idx は 0 <= idx < radioButtons.length の整数が代入されるとする
function selectRadioButtonB(idx) {
    radioButtons[idx] = radioButtons[idx].toLowerCase()
    for (let i = 1; i < radioButtons.length; i ++) {
        let targetIdx = (idx + i) % radioButtons.length
        radioButtons[targetIdx] = radioButtons[targetIdx].toUpperCase()
    }
    console.log(radioButtons)
}

// C
// idx は 0 <= idx < radioButtons.length の整数が代入されるとする
function selectRadioButtonC(idx) {
    for (let i = 0; i < radioButtons.length; i ++) {
        if (i === idx) {
            radioButtons[i] = radioButtons[i].toLowerCase()
        } else {
            radioButtons[i] = radioButtons[i].toUpperCase()
        }
    }
    console.log(radioButtons)
}


selectRadioButtonA(1)
// > ['A', 'b', 'C']

selectRadioButtonB(2)
// > ['A', 'B', 'c']

selectRadioButtonC(0)
// > ['a', 'B', 'C']

C#

class Program
{
    static string[] radioButtons = ["A", "B", "C"];

    // A
    static void SelectRadioButtonA(int idx)
    {
        for (int i = 0; i < radioButtons.Length; i++)
        {
            radioButtons[i] = radioButtons[i].ToUpper();
        }
        radioButtons[idx] = radioButtons[idx].ToLower();
        Console.WriteLine(string.Join(", ", radioButtons));
    }

    // B
    static void SelectRadioButtonB(int idx)
    {
        radioButtons[idx] = radioButtons[idx].ToLower();
        for (int i = 1; i < radioButtons.Length; i++)
        {
            int targetIdx = (idx + i) % radioButtons.Length;
            radioButtons[targetIdx] = radioButtons[targetIdx].ToUpper();
        }
        Console.WriteLine(string.Join(", ", radioButtons));
    }

    // C
    static void SelectRadioButtonC(int idx)
    {
        for (int i = 0; i < radioButtons.Length; i++)
        {
            if (i == idx)
            {
                radioButtons[i] = radioButtons[i].ToLower();
            }
            else
            {
                radioButtons[i] = radioButtons[i].ToUpper();
            }
            
        }
        
        Console.WriteLine(string.Join(", ", radioButtons));
    }

    static void Main(string[] args)
    {
        SelectRadioButtonA(1);
        SelectRadioButtonB(2);
        SelectRadioButtonC(0);
    }
}

// > A, b, C
// > A, B, c
// > a, B, C

3.コードの解説

A

概要

まず、すべての要素のループで初期化し、次に選ぶ要素を操作(サンプルコードでは小文字に変換)します。

メリット

  • シンプルで誰が見てもわかる
  • 操作が初期化に依存していても使える

デメリット

  • 選ぶ要素を初期化することが無駄
  • 要素を選ぶ操作が処理の最後になる

B

概要

まず選ぶ要素を操作(サンプルコードでは小文字に変換)し、選ぶ要素の次の要素からのループで初期化します。

メリット

  • 選ぶ要素を初期化する無駄がない
  • 要素を選ぶ操作を処理の最初でも最後でも実行できる(役に立つ状況があるか分かりませんが)

デメリット

  • 少しわかりにくい
  • 操作が初期化に依存しているならば使えない

C

概要

すべての要素でループし、選ぶ要素では選ぶ操作(サンプルコードでは小文字に変換)を、それ以外の要素では初期化をします。

メリット

  • シンプルで誰が見てもわかる
  • 選ぶ要素を初期化する無駄がない

デメリット

  • ループ内に条件分岐がある
  • 操作が初期化に依存しているならば使えない

おわりに

最後までお読みいただきありがとうございます。

果たしてラジオボタンのようなコードを実装しないといけない状況はどれくらいあるのだろうか...

0
0
2

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
0
0