簡體   English   中英

重構:如何將其改進為干凈的代碼

[英]refactoring: How can I improve this as a clean code

我目前正在自己學習JS。
並制作我觀看的課程項目的豬游戲。
我總是很好奇如何改進我的代碼,但我不知道從哪里開始。
有什么原則可以改進任何代碼嗎?
如果有辦法,能告訴我嗎?
謝謝!
https://github.com/wonkooklee/pig-game
結果: https://wonkooklee.github.io/pig-game/

以下是主要功能

document.querySelector('.btn-roll').addEventListener('click', function() {
  
  if (gamePlaying) {
    
    dice = Math.floor(Math.random() * 6) + 1;
    diceDOM.style.display = 'block';
    diceDOM.src = `dice-${dice}.png`;

    if (dice === 6 && lastDice === 6) {
      // Player looses score
      scores[activePlayer] = 0;
      document.getElementById(`score-${activePlayer}`).textContent = '0';
      nextPlayer();
    } else if (dice !== 1 && dice !== 6) {
      roundScore += dice;
      document.getElementById(`current-${activePlayer}`).textContent = roundScore;
      lastDice = 0;
    } else if (dice === 6) {
      roundScore += dice;
      document.getElementById(`current-${activePlayer}`).textContent = roundScore;
      lastDice = dice;
    } else {
      nextPlayer();
    }


  }

});

document.querySelector('.btn-hold').addEventListener('click', function() {
  if (gamePlaying) {
    scores[activePlayer] += roundScore;
    document.getElementById(`score-${activePlayer}`).textContent = scores[activePlayer];

    let input = document.getElementById('scoreSet').value;
    let winningScore;

    if (isNaN(input) === false) {
      winningScore = input;
    } else {
      document.getElementById('scoreSet').value = '100';
    }

    if (scores[activePlayer] >= winningScore) {

      document.getElementById(`name-${activePlayer}`).textContent = 'WINNER!';
      document.querySelector(`.player-${activePlayer}-panel`).classList.add('winner');
      document.querySelector(`.player-${activePlayer}-panel`).classList.remove('active');
      diceDOM.style.display = 'none';
      gamePlaying = false;
    } else {
      nextPlayer();
    }

  }

});

我的豬游戲

Martin Fowler 寫了一本很棒的書《重構》 此外,Fowler 有一個很棒的博客Refactoring.com ,您可以在其中找到很多關於重構的信息以及 Javascript 中的示例。 我不擅長 Javascript,但可以給你一些關於你的代碼的建議。

1.簡化條件邏輯

例如像這樣:

if (dice === 6 && lastDice === 6) {
  // Player looses score
  scores[activePlayer] = 0;
  document.getElementById(`score-${activePlayer}`).textContent = '0';
  nextPlayer();
  return;
} 

if (dice !== 1 && dice !== 6) {
  roundScore += dice;
  document.getElementById(`current-${activePlayer}`).textContent = roundScore;
  lastDice = 0;
  return;
} 

if (dice === 6) {
  roundScore += dice;
  document.getElementById(`current-${activePlayer}`).textContent = roundScore;
  lastDice = dice;
  return;
}

nextPlayer();

2.刪除重復代碼並提取function

例如

function someFunctionName(diffRoundScore, lastDiceValue){
  roundScore += diffRoundScore;
  document.getElementById(`current-${activePlayer}`).textContent = roundScore;
  lastDice = lastDiceValue;
}

if (dice !== 1 && dice !== 6) {
  someFunctionName(dice, 0);
  return;
} 

if (dice === 6) {
  someFunctionName(dice, dice);
  return;
}

3.將檢查“骰子6”更改為function

function isDiceEqualsSix { return dice === 6};

if (isDiceEqualsSix && lastDice === 6) {
  // Player looses score
  scores[activePlayer] = 0;
  document.getElementById(`score-${activePlayer}`).textContent = '0';
  nextPlayer();
  return;
} 

if (dice !== 1 && !isDiceEqualsSix) {
  someFunctionName(dice, 0);
  return;
} 

if (isDiceEqualsSix) {
  someFunctionName(dice, dice);
  return;
}

4.將“6”改為常量變量或function

const LIMIT_SCORE = 6;

function isDiceEqualsSix { return dice === LIMIT_SCORE};

if (isDiceEqualsSix && lastDice === LIMIT_SCORE) {
  // Player looses score
  scores[activePlayer] = 0;
  document.getElementById(`score-${activePlayer}`).textContent = '0';
  nextPlayer();
  return;
} 

我希望我對你有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM