如何删除以下方法中的重复代码

5f0d552i  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(297)

关闭。这个问题需要更加关注。它目前不接受答案。
**想改进这个问题吗?**编辑这篇文章,更新这个问题,使它只关注一个问题。

3小时前关门了。
改进这个问题

  1. $("#threeToThree").click(function() {
  2. playSound("Start");
  3. executeTimer();
  4. var nextColor = arrangeThreeToThreeColors();
  5. $("#grid .btn").click(function() {
  6. animatePress(this);
  7. if ($(this).attr("id") === nextColor) {
  8. playSound("Success");
  9. totalPoint += 10;
  10. console.log("Total Point: " + totalPoint);
  11. seenColors = [];
  12. nextColor = arrangeThreeToThreeColors();
  13. } else {
  14. playSound("Failure");
  15. }
  16. });
  17. });
  18. $("#fourToFour").click(function() {
  19. playSound("Start");
  20. executeTimer();
  21. var nextColor = arrangeFourToFourColors();
  22. $("#grid .btn").click(function() {
  23. animatePress(this);
  24. if ($(this).attr("id") === nextColor) {
  25. playSound("Success");
  26. totalPoint += 10;
  27. console.log("Total Point: " + totalPoint);
  28. seenColors = [];
  29. nextColor = arrangeFourToFourColors();
  30. } else {
  31. playSound("Failure");
  32. }
  33. });
  34. });
  35. $("#fiveToFive").click(function() {
  36. playSound("Start");
  37. executeTimer();
  38. var nextColor = arrangeFiveToFiveColors();
  39. $("#grid .btn").click(function() {
  40. animatePress(this);
  41. if ($(this).attr("id") === nextColor) {
  42. playSound("Success");
  43. totalPoint += 10;
  44. console.log("Total Point: " + totalPoint);
  45. seenColors = [];
  46. nextColor = arrangeFiveToFiveColors();
  47. } else {
  48. playSound("Failure");
  49. }
  50. });
  51. });
  52. function arrangeThreeToThreeColors() {
  53. for (let i = 0; i <= 12; i++) {
  54. let mod = i % 5;
  55. if (mod === 0 || mod === 1 || mod === 2) {
  56. var randomColor = getValidColor();
  57. manipulateButtons(i, randomColor);
  58. seenColors.push(randomColor);
  59. } else {
  60. continue;
  61. }
  62. }
  63. var nextColor = getRandomColor(seenColors);
  64. $("#next").attr("class", "btn next " + nextColor);
  65. return nextColor;
  66. }
  67. function arrangeFourToFourColors() {
  68. for (let i = 0; i <= 18; i++) {
  69. let mod = i % 5;
  70. if (mod === 0 || mod === 1 || mod === 2 || mod === 3) {
  71. var randomColor = getValidColor();
  72. manipulateButtons(i, randomColor);
  73. seenColors.push(randomColor);
  74. } else {
  75. continue;
  76. }
  77. }
  78. var nextColor = getRandomColor(seenColors);
  79. $("#next").attr("class", "btn next " + nextColor);
  80. return nextColor;
  81. }
  82. function arrangeFiveToFiveColors() {
  83. for (let i = 0; i <= 24; i++) {
  84. var randomColor = getValidColor();
  85. manipulateButtons(i, randomColor);
  86. seenColors.push(randomColor);
  87. }
  88. var nextColor = getRandomColor(seenColors);
  89. $("#next").attr("class", "btn next " + nextColor);
  90. return nextColor;
  91. }

我怎样才能使这个代码清晰?使此代码更具可读性的最佳方法是什么?有没有办法只在一个方法中实现#三对三.click()、#四对四.click()和#五对五.click()内容?我可以将arrangethreetothreecolors()、arrangefourtofourcolors()、arrangefivetofivecolors()的内容组合在一个方法中吗?

q1qsirdb

q1qsirdb1#

在代码更新过程中添加注解,选择最适合您的代码重构部分,您可以根据需要忽略重置或修改。

1.使用常量代替字符串

  1. const SoundStatus = {
  2. Start: "Start",
  3. Success: "Success",
  4. Failure: "Failure"
  5. };

2.全局变量和使用单点更新。

单点更新,对于简单的更新来说似乎太多了。但这在调试时会很有帮助,因为您知道变量在哪里更新。

  1. /**
  2. * I couldn't get the context of these 2 variables; assuming them to be global variables
  3. */
  4. let seenColors = [];
  5. let totalPoint = 0;
  6. /**
  7. * Using Single setter to update global variables; With this it will be easier to track there changes
  8. */
  9. function UpdateTotalPoint() {
  10. totalPoint += 10;
  11. }
  12. function ResetSeenColors() {
  13. seenColors = [];
  14. }
  15. function UpdateSeenColors(color) {
  16. seenColors.push(color);
  17. }

3.删除重复代码

  1. /**
  2. * Abstract attribute update to single function
  3. */
  4. function updateNextAttribute() {
  5. let nextColor = getRandomColor(seenColors);
  6. $("#next").attr("class", "btn next " + nextColor);
  7. return nextColor;
  8. }
  9. function ManipulateButtonAndUpdateSeenColorsWithRandomValue(value) {
  10. const randomColor = getValidColor();
  11. manipulateButtons(value, randomColor);
  12. UpdateSeenColors(randomColor);
  13. }
  14. /**
  15. * Remove contiune on else, as there is no statements to be executed of that; Its understood
  16. */
  17. function arrangeFiveToFiveColors() {
  18. for (let i = 0; i <= 24; i++) {
  19. ManipulateButtonAndUpdateSeenColorsWithRandomValue(i);
  20. }
  21. return updateNextAttribute();
  22. }
  23. function arrangeFourToFourColors() {
  24. for (let i = 0; i <= 18; i++) {
  25. let mod = i % 5;
  26. if (mod === 0 || mod === 1 || mod === 2 || mod === 3) {
  27. ManipulateButtonAndUpdateSeenColorsWithRandomValue(i);
  28. }
  29. }
  30. return updateNextAttribute();
  31. }
  32. function arrangeThreeToThreeColors() {
  33. for (let i = 0; i <= 12; i++) {
  34. let mod = i % 5;
  35. if (mod === 0 || mod === 1 || mod === 2) {
  36. ManipulateButtonAndUpdateSeenColorsWithRandomValue(i);
  37. }
  38. }
  39. return updateNextAttribute();
  40. }
  41. /**
  42. * Grid Updation
  43. * arrangeColor; will be arrangeThreeToThreeColors, arrangeFourToFourColors or arrangeFiveToFiveColors
  44. *
  45. */
  46. function AnimateGridButton(nextColor, arrangeColor, context) {
  47. $("#grid .btn").click(function () {
  48. animatePress(context);
  49. if ($(context).attr("id") === nextColor) {
  50. playSound(SoundStatus.Success);
  51. UpdateTotalPoint();
  52. console.log("Total Point: " + totalPoint);
  53. ResetSeenColors();
  54. nextColor = arrangeColor();
  55. } else {
  56. playSound(SoundStatus.Failure);
  57. }
  58. });
  59. }
  60. /**
  61. * Updating click listiner of 3-3, 4-4 & 5-5
  62. */
  63. $("#threeToThree").click(function () {
  64. initilize();
  65. const nextColor = arrangeThreeToThreeColors();
  66. AnimateGridButton(nextColor, arrangeThreeToThreeColors, this);
  67. });
  68. $("#fourToFour").click(function () {
  69. initilize();
  70. const nextColor = arrangeFourToFourColors();
  71. AnimateGridButton(nextColor, arrangeFourToFourColors, this);
  72. });
  73. $("#fiveToFive").click(function () {
  74. initilize();
  75. const nextColor = arrangeFiveToFiveColors();
  76. AnimateGridButton(nextColor, arrangeFiveToFiveColors, this);
  77. });
  78. /**
  79. * Abstracting inital Start sound and executeTimer
  80. */
  81. function initilize() {
  82. playSound(SoundStatus.Start);
  83. executeTimer();
  84. }

重构后的最终代码

  1. const SoundStatus = {
  2. Start: "Start",
  3. Success: "Success",
  4. Failure: "Failure"
  5. };
  6. let seenColors = [];
  7. let totalPoint = 0;
  8. function updateTotalPoint() {
  9. totalPoint += 10;
  10. }
  11. function resetSeenColors() {
  12. seenColors = [];
  13. }
  14. function updateSeenColors(color) {
  15. seenColors.push(color);
  16. }
  17. function updateNextAttribute() {
  18. let nextColor = getRandomColor(seenColors);
  19. $("#next").attr("class", "btn next " + nextColor);
  20. return nextColor;
  21. }
  22. function ManipulateButtonAndUpdateSeenColorsWithRandomValue(value) {
  23. const randomColor = getValidColor();
  24. manipulateButtons(value, randomColor);
  25. updateSeenColors(randomColor);
  26. }
  27. function arrangeFiveToFiveColors() {
  28. for (let i = 0; i <= 24; i++) {
  29. ManipulateButtonAndUpdateSeenColorsWithRandomValue(i);
  30. }
  31. return updateNextAttribute();
  32. }
  33. function arrangeFourToFourColors() {
  34. for (let i = 0; i <= 18; i++) {
  35. let mod = i % 5;
  36. if (mod === 0 || mod === 1 || mod === 2 || mod === 3) {
  37. ManipulateButtonAndUpdateSeenColorsWithRandomValue(i);
  38. }
  39. }
  40. return updateNextAttribute();
  41. }
  42. function arrangeThreeToThreeColors() {
  43. for (let i = 0; i <= 12; i++) {
  44. let mod = i % 5;
  45. if (mod === 0 || mod === 1 || mod === 2) {
  46. ManipulateButtonAndUpdateSeenColorsWithRandomValue(i);
  47. }
  48. }
  49. return updateNextAttribute();
  50. }
  51. function AnimateGridButton(nextColor, arrangeColor, context) {
  52. $("#grid .btn").click(function () {
  53. animatePress(context);
  54. if ($(context).attr("id") === nextColor) {
  55. playSound(SoundStatus.Success);
  56. updateTotalPoint();
  57. console.log("Total Point: " + totalPoint);
  58. resetSeenColors();
  59. nextColor = arrangeColor();
  60. } else {
  61. playSound(SoundStatus.Failure);
  62. }
  63. });
  64. }
  65. $("#threeToThree").click(function () {
  66. initilize();
  67. const nextColor = arrangeThreeToThreeColors();
  68. AnimateGridButton(nextColor, arrangeThreeToThreeColors, this);
  69. });
  70. $("#fourToFour").click(function () {
  71. initilize();
  72. const nextColor = arrangeFourToFourColors();
  73. AnimateGridButton(nextColor, arrangeFourToFourColors, this);
  74. });
  75. $("#fiveToFive").click(function () {
  76. initilize();
  77. const nextColor = arrangeFiveToFiveColors();
  78. AnimateGridButton(nextColor, arrangeFiveToFiveColors, this);
  79. });
  80. function initilize() {
  81. playSound(SoundStatus.Start);
  82. executeTimer();
  83. }

在所有可能的抽象之后,我们只剩下针对每个函数的特定代码;除了 ManipulateButtonAndUpdateSeenColorsWithRandomValue (请给我一个合适的名字,符合要求)。对于这个问题,仍然可以进行几行抽象 arrageColor 相关职能。既然我对你的要求没有一个完整的了解,那就留给你吧。

展开查看全部

相关问题