These days, we have lots of frameworks, libraries and languages and everybody can code, but not everybody has a taste for it. Just look on Stack Overflow and you can see a huge amount of really bad code.
The worst thing is that sometimes coders think they are professional software engineers, when in fact, they just copy paste bad code into their projects making the code base utterly difficult to maintain. The concept of good taste code is not something I invented. I heard the term from Linus at his TED talk at 14:24. I don’t even think that we can establish precisely what is good taste, but we definitely can tell what’s bad. For me, it’s bad to write incorrect, unmaintainable, redundant, slow code. Exactly in this order.
For example, below is an algorithm to initialize the points along the edge of a grid, which is represented as a multidimensional array: grid[GRID_SIZE][GRID_SIZE].
for (i = 0; i < GRID_SIZE; ++i) { for (j = 0; j < GRID_SIZE; ++j) { // top edge if (i == 0) { grid[i][j] = 0; } // left edge if (j == 0) { grid[i][j] = 0; } // right edge if (j == GRID_SIZE - 1) { grid[i][j] = 0; } // bottom edge if (i == GRID_SIZE - 1) { grid[i][j] = 0; } } }
The bad things about this code are the two loops and the if statements. It is bad taste code not because it’s incorrect, but because it has a lot of unnecessary conditionals and loops. We can rewrite the code as follows:
for (i = 0; i < GRID_SIZE; ++i) { // top edge grid[0][i] = 0; // bottom edge grid[GRID_SIZE - 1][i] = 0; // left edge grid[i][0] = 0; // right edge grid[i][GRID_SIZE - 1] = 0; }
You see? The code is faster, we don’t have conditionals, it is easier to read and to maintain, but the code requires a little bit more thinking ahead before writing it. Let’s take another example:
ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("weekly"); arrayList.add("monthly"); arrayList.add("yearly"); return arrayList;
The code is OK, but it’s verbose. If we read the Java documentation we find that in Java we have a way to initialize the list directly:
return Arrays.asList("weekly", "monthly", "yearly")
The second version looks better because it uses the language specific tools. The developer actually took some time to read about the new Array APIs in Java. I don’t think that this is a general rule, but usually people who think before writing code, who care about how the “code looks”, have smaller chances to write bad taste code. I know it sounds obvious, but sadly many developers don’t follow this simple principle. 🙂