Capstone Blog – Clean Code & Code Smells


After reading a GitHub Gist by Robert C. Martin, I am surprised to see that I have been following most of the code conventions for cleaner and nicer looking code. However, one thing that I would like to start doing better is leaving behind more descriptive comments on functions so that when I go back months later I am not overwhelmed and confused. I do leave small comments here and there, but not enough that if I were to hand off my project to someone else, they would have trouble understanding the purpose of my code.

The one thing I do need to get better at is detecting common code smells. I always have a certain way I code, but that doesn’t mean it is best practice or recommended. For instance, I mostly code in Vue making simple web applications, there are many ways you can organize your components and functions. However, one particular aspect of Vue allows you to manipulate the DOM by updating an element’s innerHTML directly through built-in directives. Which we all know can be prone to scripting attacks.

Vue as many ways you can dynamically render HTML. Below I will describe some of the dos and don’ts with inserting HTML. The first code snippet below shows how you should render information on the DOM, the second one shows how you should not.

This way of rendering information on the DOM is safe, but limits how you can style your information to appear. However, you are not prone to scripting attacks as HTML is pre-rendered and not injected.

<div>{{message}}</div>

Here, we inject HTML on the DOM. Which we all know is a big problem as you are more prone to scripting attacks from potential adversaries.

<div v-html="message"></div>

The article mentioned points out key aspects on how to find code smells. I hope to use this information to avoid fewer code smells in the future!

Stay away from those people who try to disparage your ambitions. Small minds will always do that, but great minds will give you a feeling that you can become great too.

Mark Twain

References
[1] Martin, Robert C. “Summary of ‘Clean Code’ by Robert C. Martin.” GitHub Gist, GitHub, https://gist.github.com/wojteklu/73c6914cc446146b8b533c0988cf8d29.

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *