Better Code Comments: Using Coding Syntax For Your Comments

Better Code Comments: Using Coding Syntax For Your Comments 

Today I learned a cool way to write comments in the codebases.

So instead of writing lots and lots of words to explain what your code does... you can also write comments in the syntax of your code.

So for example:

AVERAGE_SCREEN_TIME = {

  "LOW": {1: 2, 2: 2, 3: 2, 4: 3, 5: 3, 6: 3, 7: 1},

  "AVERAGE": {1: 4, 2: 4, 3: 5, 4: 5, 5: 4, 6: 4, 7: 3},

  "HIGH": {1: 5, 2: 5, 3: 6, 4: 6, 5: 6, 6: 7, 7:6}

}

Could be commented like this in the line above the constant declaration:

# Average screen time per day {usage category: {weekday by number: average daily screen time in hours}}

Like this then we assume that anyone who knows what a dictionary is in Python (or, very similarly, an object in JavaScript) knows that this is a dictionary of dictionaries; we don't need to waste time on explaining to them what a dictionary is.

We just need them to know the functionality of the code. We just need to explain to them what the functionality of the code is.

Like this we can save a lot of time with our comments and a lot of mental load for software engineers as well - a line of code says more than a thousand words, eh? 😉 

So in the case of this code, the dictionary tells us average screen time, and what would be low, and high as well as average - for a specific day of the week - assuming I have specific habits on particular days of the week that I am likely to follow, so as to lead to different patterns of consumption for different days of the week. Thank you!

Red coding images in the background. Foreground reads in dramatic text: Better code comments: try using syntax for comments and replicating pseudocode inside the syntax.

Comments