How To Design Computer Programs
Tags: oop, programdesign, java
Published: 2018-06-03 22:12:12

---
title: "How To Design Computer Programs"
published: true
description: 
tags: OOP, Program Design, Java
---

## Why Think About Program Design Before Coding?
Give me six hours to chop down a tree and I will spend the first four sharpening the axe, said *Abraham Lincoln*. This
statement also applies to software development. The truth is, in order to develop great software you have to put in a lot of thought
around the design and interactions of the software components. Why spend time on program design? If you put in time to think about
your program design you will spend less time coding. Following Abraham Lincoln's quote, you will spend 33.3% of your time coding. This is because after program design you know all the classes and methods which you have to code and how they will change the state (data) of your program. It will help you think about your program as a group of modules interacting with each other thereby making your program modular. Modularizing your program will ease task assignment during development. It will help you write better code as you will have to think about program decomposition and step wise refinement. All in all, thinking about program design before coding will make you a better software engineer and programmer.

## Why Use Tic-Tac-Toe As Our Case Study?
![Tic Tac Toe](https://vladimirfomene.github.io/assets/img/tic_tac_toe.png)
Tic-Tac-Toe is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game. The following example game is won by the first player, X (En.wikipedia.org, 2018):

![Game Play](https://vladimirfomene.github.io/assets/img/479px-Tic-tac-toe-game-1.svg.png)

Players soon discover that the best play from both parties leads to a draw. Hence, tic-tac-toe is most often played by young children (En.wikipedia.org, 2018).
I choose Tic-Tac-Toe as our case study because it is relatively easy to program and programming it will give me the chance to talk
about some key software engineering techniques. You can find the code at the following link:
[Fork me on Github](https://github.com/vladimirfomene/Tic-Tac-Toe)

## Strategies For Designing Better Programs

### Decomposition
* As computer scientists, we know that to solve a big problem we need to break it into smaller problems and solve these smaller
problems. In software design we do exactly the same thing. To develop the Tic-Tac-Toe game, I decomposed it into the following pieces:

  1. *Initialize the players - Get players' names, assign them drawing symbols and ids.*
  2. *Initialize the game grid - Draw the grid for Tic-Tac-Toe and set the initial turns for Players.*
  3. *Wait for a player to play their turn*
  4. *If a player has played, Check for a win*
  5. *If there is a win, ask the players whether they want to continue playing.*
  6. *If they want to continue playing, reset the game grid and reset players' turns(go back to 2), otherwise declare the winner of the game and exit the program.*
  7. *If there is no win, alternate turns and go back to 3.*

This was my initial strategy for developing this software. I had to change this strategy when I started writing the code and I realized that most of the players' actions in the game were going to be MouseClick events and I will have to handle them by implementing event listeners.

### Programming Paradigm
* When working on a software project, it is advised to pick a programming paradigm to use while writing your code. For Tic-Tac-Toe, I'm using an Object Oriented Programming Paradigm. That means, I designed the whole program around classes and interfaces.

  1. TicTacToe class - This class contains the main method of our program.
  2. GameBoard class - This class implements our gameboard and handling all the interactions on it.
  3. Player class - This class represents a player in the game and also stores that player's attributes.

Aside from these classes, there is a GameConstants interface shared by these classes. This interface specifies all the  constants
needed for the game.

### Good Names and Method Length
* When programing make sure your classes' names, methods' names, variables' and constants' names reflect what the class represents or what they do. Good class, method, variable and constant names will reduce the amount of commenting that you have to do. All my class names reflect what each of these classes either do or stand for.
  * Example of Good Method names - initGrid(initializes the tic-tac-toe game grid), checkForWin(after a player plays check whether the player won), drawCircle(draws circles on a grid cell), drawCross(draws cross on a grid cell) and recordScore(records the score for a particular player).
  * Example of Good Variable names - scoreBoard(an array that keeps track of the players' scores), boardState(an array of Grid cells that keeps track of the state of the board)
  * Example of Good Constant names - NUM_OF_PLAYERS(represents the number of players in the game), NUM_GRID_ROWS(number of rows on the game grid), NUM_GRID_COLUMNS(number of columns on the game grid)


### Version control
Whenever you are working on a project, make sure you start by initializing a repository using *Git* or any other version control tool of your choice.  Whenever you add a new functionality to your code, make sure you stage and commit it these changes. Stage and commit very often, don't wait until you have achieved a major milestone before you stage and commit the changes.


### Code Review
Ask a peer to review your code and tell you how you can make it better. If you want to review my code, send me a request at the email below:

## Contacts:
[Follow me on Twitter](https://twitter.com/VladimirFomene)

[Fork me on Github](https://github.com/vladimirfomene)

[Email Me: vladimirfomene@gmail.com]()