简体   繁体   English

重构:如何将其改进为干净的代码

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

I am currently studying JS myself.我目前正在自己学习JS。
And make the pig-game which is project of the course that I watched.并制作我观看的课程项目的猪游戏。
I'm always curious how can I improve my code but I got no idea where do I begin.我总是很好奇如何改进我的代码,但我不知道从哪里开始。
Is there some principle that I can improve any code?有什么原则可以改进任何代码吗?
If there's a way, Could you let me know?如果有办法,能告诉我吗?
Thanks!谢谢!
https://github.com/wonkooklee/pig-game https://github.com/wonkooklee/pig-game
result: https://wonkooklee.github.io/pig-game/结果: https://wonkooklee.github.io/pig-game/

below is main functions以下是主要功能

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 wrote a great book "Refactoring" . Martin Fowler 写了一本很棒的书《重构》 Besides, Fowler has a great blog Refactoring.com , where you can find a lot of information about refactoring with examples in Javascript.此外,Fowler 有一个很棒的博客Refactoring.com ,您可以在其中找到很多关于重构的信息以及 Javascript 中的示例。 I'm not strong in Javascript, but can let you some advices about your code.我不擅长 Javascript,但可以给你一些关于你的代码的建议。

1. Simplify Conditional Logic 1.简化条件逻辑

For example like this:例如像这样:

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.Delete duplicated code and extract function 2.删除重复代码并提取function

For example例如

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.Change check "dice for 6" to function 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.Change "6" to a constant variable or a function 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;
} 

I hope I helped you.我希望我对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM