コード匂い86 -変なconst配列


const何か定数を宣言します.変異するか?

TL;DR: Don't rely on languages cheating about directives.


問題

  • 予想外の副作用.
  • 偶然の複雑さ.
  • 解決策

  • より良い言語を使う
  • 用途spread operator
  • サンプルコード


    間違い


    const array = [1, 2];
    
    array.push(3)
    
    //array => [1, 2, 3]
    //Wasn't it constant ?
    //constant != immutable ?
    


    const array = [1, 2];
    
    const newArray = [...array,3 ]
    
    //array => [1, 2] Didn't mutate
    //newArray = [1, 2, 3]
    

    検出


    これが「言語機能」であるので、明示的に禁止することができます.

    タグ

  • 変異
  • ジャバスクリプト
  • 結論


    我々は常に我々のデザインの不可用性を支持しなければならなくて、副作用で余分の世話をしなければなりません.

    詳しい情報


  • クレジット


    写真でZorik D on Unsplash
    ありがとうございました.

    マキシ・コンティ
    @ MCSET 1

    …' const '配列は変更可能ですか?WTF!もう一つのコード匂い?あなたはどう思いますか.
    午後13時06分- 2021年8月24日

    Correctness is clearly the prime quality. If a system does not do what it is supposed to do, then everything else about it matters little.


    バートランドマイヤー


    この記事はCodesmellシリーズの一部です.