Why writing simple, clear and debuggable codes is so underrated ?
I often hear programmers boasting how intelligent their codes are. Indeed, many resemble literature. However, the reality is different.
Consider the following lines in Javascript:
const isExtensionValid = this.allowedExtensions.indexOf(files[i].type) !== -1;if (isExtensionValid) {
// do something
} else {
// error
break;
}
Some programmers would prefer writing the isExtensionValid logic in the if statement itself. Sounds like the right thing to do. During my initial years of professional programming, I would do the same thing. Less variables, less complexity. Appears easier to read.
However, experience taught me otherwise.
First, giving the logic a name where possible makes it easier to understand the concept here. It’s more descriptive. Imagine the following :
if (this.allowedExtensions.indexOf(files[i].type) !== -1) {
Each time I have to debug this part or read the code again, I have to try to understand it. It’s initially not an issue. However, imagine reading the same codes after 6 months during a hectic schedule. Having a constant or variable named isExtensionValid makes it abstract enough for me focus more on the overall behaviour instead of having to drill in too much details.
Second, let’s assume, I have an issue with my logic. How do I proceed to debug this code ?
Right now, I can add a debugger or console.log(isExtensionValid) below the constant declaration. Then by running the app, I can see the value. Of course, I can do the same thing if the logic is directly in the if statement. But I would have to cover for both cases as I am not sure which condition is acceptable. Imagine if there are more, e.g. 5. Either I add them in each statement or I copy the logic in a constant separately to see the value. Seems like an overkill when I can do it when writing or refactoring the code.
I usually try to write dumb looking codes that are descriptive enough but easy to debug. Programming is not only about writing prose. Software contains bugs. At one point, I have to debug them. So I try to make it easier for me.