If you've been programming for any period of time, you've probably seen bad code. The kind that you're scared of touching for fear of breaking the program, the kind that employs some trickery that is beyond any documentation found online, the kind that slows you down.

So what makes clean code, and how can we tackle the disease of bad code?

Luckily, you can follow the same principles to write good code and to refactor bad code. Here are some attributes I've noticed in good code:

Good Names

Programming is not a math equation. The vast majority of the programs you write will not be simple. They will have to interact with the rest of the program, forming complex relationships along the way, and therefore have to be read by other programmers. Someone with no idea of your contribution should be able to quickly understand what your code does.

Let's look at an example, which code snippet is easier to grasp?

Bad

vector<pair<int, int>> getThem() {
  vector<pair<int, int>> v;

  for (pair<int, int> x : theList) {
    if (x.second == 4) {
      v.push(x);
    }
  }

  return v;
}

Good

vector<pair<int, int>> findFlaggedCells() {
  vector<pair<int, int>> flaggedCells;

  const int FLAGGED = 4;
  for (pair<int, int> cell : gameBoard) {
    if (cell.second == FLAGGED) {
      flaggedCells.push_back(cell);
    }
  }

  return flaggedCells;
}

Better

vector<Cell> findFlaggedCells() {
  vector<Cell> flaggedCells;

  const int FLAGGED = 4;
  for (Cell cell : gameBoard) {
    if (cell.status == FLAGGED) {
      flaggedCells.push_back(cell);
    }
  }

  return flaggedCells;
}