Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Consider the below code (exerpted from Uncle Bob’s Book – clean code):
- ```c#
- public List<int[]> getThem()
- {
- // this list value is pre-populated
- // but for the purposes of this exercise
- // we will just instanciate it here:
- List<int[]> theList = new List<int[]>();
- List<int[]> list1 = new List<int[]>();
- foreach (int[] x in theList)
- {
- if (x[0] == 4)
- {
- list1.Add(x);
- }
- }
- return list1;
- }
- ```
- #### Exercise 1:
- 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.
- Here are the questions I have:
- 1. What is getThem?
- 2. What are the two lists? (theList and List1)?
- 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.).
- #### Exercise 2:
- Given the following facts, go back and rename/change the method:
- * theList represents a minesweeper game board (it’s basically like a chess board).
- * The first value (index 0) represents whether it has been flagged or not.
- * The value 4 means that it has been flagged.
- * We are getting a list of all flagged rows (i.e. getThem).
- Here’s what I came up with:
- ```c#
- public List<int[]> getFlaggedRows()
- {
- // this list value is pre-populated
- // but for the purposes of this exercise
- // we will just instanciate it here:
- List<int[]> gameBoard = new List<int[]>();
- List<int[]> flaggedRows = new List<int[]>();
- foreach (int[] row in gameBoard)
- {
- if (row[0] == 4)
- {
- flaggedRows.Add(row);
- }
- }
- return flaggedRows;
- }
- ```
- What I don’t like is that:
- * We are forced to know about the data structure (but I suppose that it is unavoidable.)
- * We are forced to know that the first element of each row in the game board is a special column.
- * 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.
- ```c#
- public List<GameRow> getFlaggedRows()
- {
- // this list value is pre-populated
- // but for the purposes of this exercise
- // we will just instanciate it here:
- List<GameRow> gameBoard = new List<GameRow>();
- return gameBoard.Where(gameRow => gameRow.IsFlagged == true).ToList();
- }
- public class GameRow
- {
- public bool IsFlagged { get; set; }
- public int[] RowValues { get; set; }
- }
- ```
- 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).
- ```Java
- public List<Cell> getFlaggedCells() {
- List<Cell> flaggedCells = new ArrayList<Cell>();
- for (Cell cell : gameBoard)
- if (cell.isFlagged())
- flaggedCells.add(cell);
- return flaggedCells;
- }
- ```
Add Comment
Please, Sign In to add comment