5 Easy ways to write better code
Your colleagues will like you more.
1. Segregation of concerns
This basically means that one function should do one thing, it’s common sense, but when you’re building something it usually goes overlooked. If you write a database query, that needs to be a function all to itself. More than that if you have a bunch of code related to the piece of functionality, put that in a class. Then if a database table changes, the related queries only changes in one spot. It’s a good habit because it increases your readability and makes everything more modular and easier to maintain.
Having to locate rogue code that does the same thing as some other code is one of the most counterproductive things anyone has to do. If it’s in a logical, segregated place then finding what code does what is simple.
2. Good naming
for element in list: # Bad namingfor element in articles_list: # OK namingfor article in news_articles: # Good naming
These examples should be pretty self explanatory but you should know what you’re looking at before inspecting the code.
In most languages just trying to read things as english should be a good indicator as to if your code is readable or not.
3. Not shit comments
/** Loops over list**/
for article in news_articles:
As far as I’m concerned this is a waste of a comment. There’s no new information added in this comment to help anyone (or yourself) in the future.
/** list of type news_article **/
news_articles = get_news_articles()
for article in news_articles:
This comment is good because it lets people know what the type of the array is, near when it’s getting used.
4. Have style
- Be consistent, use camelCase of underscore_notation for everything. If there’s a standard for the codebase you’re working on, keep using it.
- Make it readable. This is a combination of good naming, consistent looking code and following style guides of that language or framework
- eg// java curl placements, python arg, kwarg useage
// GOOD CODE
// a class to demonstrate the toString method
public class toStringExampleClass
{
private String message;
private boolean answer = false;
public toStringExampleClass(String input)
{
message = "Why, " + input + " Isn't this something?";
}
public String toString()
{
return message;
}
}// BAD CODE <<--DONT DO THIS
// a class to print stuff <<--BAD COMMENT
public class toStringExampleClass{ // <<--NON-STANDARD BRACE POSITION
private String message;
private boolean answer = false;
public toStringExampleClass(String input)
{ // <<--INCONSISTENT BRACE POSITION AND ALIGNMENT
message = "Why, " + input + " Isn't this something?";
}
public String toString()
{return message;} <<-- NON STANDARD RETURN POSITION AND BRACES AND ALIGNMENT
}
5. Unit/Integration tests
Easily the best thing you can do for long term prosperity of your code. If there’s anything you take away from this article it’s write tests. If you can’t write a small test for it, then you wrote it wrong.
If you care about easy updates, reducing headaches and not allowing shitty code in your code base you’ll write unit tests and won’t complain about it.
Whenever you write something that people are paying for, or a key component that gets used everywhere (like DB accesses) you need a unit test.
This example is from the python standard unit tests for the string class
# Python code from https://docs.python.org/2/library/unittest.htmlimport unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
if __name__ == '__main__':
unittest.main()
TDLR;
- Abstract code so it’s segregated and modular
- Name your variables and functions well
- Write useful comments
- Follow style guides of the project or framework
- Write unit tests