Guest User

Untitled

a guest
Jan 20th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. Consider the below code (exerpted from Uncle Bob’s Book – clean code):
  2.  
  3. ```c#
  4. public List<int[]> getThem()
  5. {
  6. // this list value is pre-populated
  7. // but for the purposes of this exercise
  8. // we will just instanciate it here:
  9. List<int[]> theList = new List<int[]>();
  10.  
  11. List<int[]> list1 = new List<int[]>();
  12.  
  13. foreach (int[] x in theList)
  14. {
  15. if (x[0] == 4)
  16. {
  17. list1.Add(x);
  18. }
  19. }
  20.  
  21. return list1;
  22. }
  23. ```
  24.  
  25. #### Exercise 1:
  26. What questions (if any) do you have about the above code? Write them down. Here are my answers - which are comparable to Uncle Bob's.
  27.  
  28. Here are the questions I have:
  29. 1. What is getThem?
  30. 2. What are the two lists? (theList and List1)?
  31. 3. What is the significance of 4, and the first element of each array within theList? (i.e. looks like you need to know something about the array before using it.).
  32.  
  33. #### Exercise 2:
  34.  
  35. Given the following facts, go back and rename/change the method:
  36.  
  37. * theList represents a minesweeper game board (it’s basically like a chess board).
  38. * The first value (index 0) represents whether it has been flagged or not.
  39. * The value 4 means that it has been flagged.
  40. * We are getting a list of all flagged rows (i.e. getThem).
  41.  
  42. Here’s what I came up with:
  43.  
  44. ```c#
  45. public List<int[]> getFlaggedRows()
  46. {
  47. // this list value is pre-populated
  48. // but for the purposes of this exercise
  49. // we will just instanciate it here:
  50. List<int[]> gameBoard = new List<int[]>();
  51.  
  52. List<int[]> flaggedRows = new List<int[]>();
  53.  
  54. foreach (int[] row in gameBoard)
  55. {
  56. if (row[0] == 4)
  57. {
  58. flaggedRows.Add(row);
  59. }
  60. }
  61.  
  62. return flaggedRows;
  63. }
  64. ```
  65.  
  66. What I don’t like is that:
  67.  
  68. * We are forced to know about the data structure (but I suppose that it is unavoidable.)
  69. * We are forced to know that the first element of each row in the game board is a special column.
  70. * We are also forced to know that the number 4 means that it is flagged. This should be represented as distinct elements in a class.
  71.  
  72. ```c#
  73. public List<GameRow> getFlaggedRows()
  74. {
  75. // this list value is pre-populated
  76. // but for the purposes of this exercise
  77. // we will just instanciate it here:
  78. List<GameRow> gameBoard = new List<GameRow>();
  79.  
  80. return gameBoard.Where(gameRow => gameRow.IsFlagged == true).ToList();
  81. }
  82.  
  83. public class GameRow
  84. {
  85. public bool IsFlagged { get; set; }
  86.  
  87. public int[] RowValues { get; set; }
  88. }
  89. ```
  90.  
  91. Uncle Bob suggests something similar – except that instead of using a property (IsFlagged) he is using a method to return that value. That is the better approach – because, according to Martin Fowler – we should reduce temporary variables when we can. This allows to more easily see the big picture and more easily refactor when the time comes (and it will come).
  92.  
  93. ```Java
  94. public List<Cell> getFlaggedCells() {
  95. List<Cell> flaggedCells = new ArrayList<Cell>();
  96. for (Cell cell : gameBoard)
  97. if (cell.isFlagged())
  98. flaggedCells.add(cell);
  99. return flaggedCells;
  100. }
  101. ```
Add Comment
Please, Sign In to add comment