Good Coding Practices I - When You SHOULD Make a Comment in Your Code

Hello everyone, Boggarak here!

I’m currently a Software Engineering student, and there are some good pieces of programming advice I could give you that could not only help you with coding your games on Roblox, but also coding elsewhere. This will be a multi-part series that will cover coding advice that you ideally should start using when coding in Roblox, as it will definitely help you when working on your projects and help you become a better programmer.

The first topic I will cover are about comments. If you have programmed on Roblox before, you probably have known or at least seen what a comment is. For those who still don’t know, comments are lines in code that does absolutely nothing, and usually contain text. In Lua, comments are indicated using dashes -- ... and square brackets with dashes --[[ ... --]].

While they can be used for debugging (like disabling certain pieces of code in a given script), their real purpose is for readability, or the ease of understanding what a section of code does. Some programmers add a lot of comments, while some don’t even add any comments. However, the most important thing when writing a comment is when you should write a comment, and when you can get away with not writing one.

I will give you a list of instances of when you should write a comment (and instances where comments are not really preferable). For all of these instances, the key idea is that, if a given piece of code is easily readable, then the programmer should know in seconds what a given section of codes does, and doesn’t require mental gymnastics or a lot of deciphering to understand it.

With that said, here is a list of instances to consider when write a comment:


Writing Documentation Comments

Documentation comments are multi-lined comments that are used to allow other people to understand how to use the code you give them, whether it is a function, a class, or even the entire script or file. The purpose is that people can use your code without caring about how it is implemented (or coded), but instead if it does what they want it to do.

For example, let’s say we made a Lua module that contains functions that one can use to ease the process of creating a minigame-type Roblox game (like Ripull Minigames). In that case, I would have a documentation comment for the entire module, describing briefly on:

  • its purpose (i.e. make it easier to create a minigame-type game)
  • who made it (i.e. you and the other people who have worked on it)
  • the version of the module
  • the date of when the module was first made (depends on the situation)

Then, I would write a documentation comment for each of the (public) functions I want the people using this module to use, since they will need to do so to create their game. For each function, the documentation comment will include:

  • A brief description of what the function does
  • What to put into the function, if applicable
  • What is the output of the function, including what kind of output (i.e. is it a piece of text, a number, etc.) and what the output represents. again, if it’s applicable.

Then that would be it. Don’t write documentation comments within functions, as the person using the module doesn’t care about how the function actually works, but rather, as I said before, if it does what they want it to do.

How to write a documentation comment will vary depending on the programming language you are using, and Lua doesn’t really have a universal style of writing a documentation comment. However, the things I have listed should be sufficient for a decent (or even good) documentation comment.

Let’s do an example of writing a documentation comment. Using the same example as before, let’s say we have the following function in the code

function minigameModule.teleportPlayer(player, toLocation)
    -- Code to teleport player to location (ignore this, as it isn't really important)
end

To write a documentation comment, we will make a multi-lined comment above the function (as it will be the first thing the person will see, since we read code from top to bottom). In this, we will include everything that I mentioned earlier about documentation comments for functions. After which, the code now looks like this:

--[[
    Teleports a given player to a given location
    
    @param player      Player   the player being teleported
    @param toLocation  Vector3  the location the player will be teleported to
--]]
function minigameModule.teleportPlayer(player, toLocation)
    -- Code to teleport player to location (ignore this, as it isn't really important)
end

Note that the function doesn’t return an output, since it doesn’t really need to. If you plan to have a function that does return something, describe what that is in the comment, preferably using the statement like “returns …”.

It is important to mention that you don’t need to write documentation comments for the most part, and when you should depends on several factors that relate to what you are working on. You can get away with not writing them as long as it is clear to other people how to use the code based on just code itself, which leads to my next point:


Best Comments are the names of Variables, Functions, Classes, etc.

I really need to emphasize that you don’t always need to write a comment, as there are alternatives that do a better job in certain instances. The point here is to give you other tools to help make your code more readable, and it is a bad idea to only use one tool when there are others that do just as well (or even better). As a wise person once said, “If you only use a hammer, everything will look like a nail”.

With this said, I will describe ways where this tactic is the most useful. One of these instances is when you have a rather messy if statement like this:

if (dist > 0 and dist <= PieceSize*math.sqrt(2)) then
    -- Do something
end

Tell me, what is the if statement checking? I can tell that some of you might have taken like 10~15 seconds to figure out what is being checked here, more or less, or some of you still don’t even know what is being checked.

Okay, what about this?

isNearby = (dist > 0 and dist <= PieceSize*math.sqrt(2))
if (isNearby) then
    -- Do something
end

Most of you should have known what the statement is checking almost instantly or at least less time than the other if statement. Notice that I didn’t add any comments describing what the if conditional does (although I could if I wanted to), but yet you could clearly get what is being done here, or at least a very good idea.

The kind of variable I used here is called an extract variable, and they are primarily used for making conditionals easier to understand. All extract variables are boolean (i.e. either true or false), since that is the only kind of variable that is allowed to function as a conditional.

However, tactic would be useless if the variable name was like a or conditional since they don’t provide information that describes what the variable means or even is used for. This is why you should have good (i.e. descriptive) names for your variables, functions, and classes, since they can go a very long way in making your code easier to understand.

Note that when I mean descriptive, I don’t mean long. What I mean is that one can clearly get what the variable, function, etc. represents just from its name. As long as it clear in context, a short, but descriptive name should suffice.

There are also extract functions (or extract methods for Object-Oriented Programming), which are used to make a complex function (that does a lot of stuff) easier to understand, but I’ll save that for next time.

I want to mention that you don’t always need to do this, and in most cases its preferable to not do this, especially where performance or memory are crucial (extract variables and functions can waste valuable memory), which, in that case, a comment would be better. However, try to make sure that you choose good names for your variables, functions, and classes so that it is easier to get what is being done. Analyze what kind of requirements you need your program to follow and apply any techniques that are appropriate


Comments are a Last Resort

Comments can very easily waste memory (and the time of the programmer, if they see more comments than code), which is especially bad since they don’t really do anything, so it is important to emphasize that, again, you don’t need to comment if you if it is not really needed.

Instead, see if the code can easily be understood by itself. If not, instead of immediately writing a comment, ask yourself* why* it is not easily understood. Are the names for variables too vague or even meaningless? Is the logic too complex?

If you have meaningless names or names that are too vague (see previous section), change them so that people can easily get what the variable or function represents. See if the name makes sense in the context of the code. If the variable name is still too vague or is way too long, then a comment that adds more of the important details will do the trick, as long as the variable name you chose is good and relates nicely to the context of the code (see the previous section).

If you have logic that is too complex, see if you can simplify it. This can require you to learn about boolean algebra, especially for complex conditionals, which I will cover in a later section, but there are still meaningful ways of reducing complexity of a piece of code. If you can’t simplify it any further and the code is still a mess, a comment describing the function and purpose of the algorithm would be fine.

Ideally, you want to minimize the use of comments in your code, and only include comments that have a very good reason of being there. Usually, if you do need to add a comment, a comment that describes why that piece of code exists or does something in a particular way is usually the best comment to write. Never write comments that simply describes how the code works, as the programmer can get that information from just the code itself. There are exceptions (see section 1), but for the most part, you shouldn’t even bother writing a comment that describes how the code works, as, if the code is well written, then any programmer can get that information from just the code itself.


This is all I have for now. However, if you have any goo
d suggestions on what I can add here, then please comment below (pun intended). I’ll update this if I have more ideas of what to add here (or to include the link to the part of this series).

Otherwise, see you next time when I cover the DRY principle and what it really means when coding.


Resources (Recommended):

Next Post: Good Coding Practices II - The DRY Principle

  1. Source Making - Comments
  2. You’re commenting your code too much (and other controversial thoughts on documentation) (Thanks Vizualizememe for the article).
25 Likes

Something you should have mentioned: you should aim to write code that minimizes the need for such comments. Code should be self-documenting, which means you know what the code does just by looking at the code itself. If you need a comment to explain something, there is probably a more readable approach out there. As for documentation, I’d personally move away from doing it within the source and document on GitHub or something.

As for these:

This is something your version control software (i.e. Git) should tell you – not comments as you would need to update them every time when you can just have your VCS do it for you. Also clutters code a bit.

7 Likes

Very true, but there are instances like for JavaDoc where documentation comments are necessary if you want to make documentation pages for your program for any reason. Other than that, I do agree with you for the most part.

Thank you for the comment!

edit: I modifed my original post to emphasize that documentation comments are not necessary for the most part, and depends on their situation. I should have mentioned this a while ago, but I haven’t since I personally think those kind of comments are useful (for reference, I am primarily taught Java for my degree, so that might have been the reason lol).

2 Likes

What if a user decides to add too much in the way of comments and makes a piece of code hard to look through?

What I typically do is add comments to something that would need a bit of explaining as I go along, not only to give me a way to more easily proofread it when I need to add to the code later on or fix it, but also to allow others to more readily understand what a piece of code does. I’ll skip posting comments on the more syntactical side of the code such as for loops and print statements, and will place comments on the somewhat complex logic. Give yourself a path to follow.

Preconditions and postconditions, what must be true before and after a function executes, are meaningful in writing good code and you explained that well. Can’t wait to hear what you have to say on reusing code through module scripts in your next post; I’ve got a library of module scripts I reuse whenever the need arises.

1 Like

Yeah, too many comments can be a bad thing, especially if they don’t really need to be there or better methods exist.

I just realized that I should probably add another section to this about the situation where the code is too complex and can’t be made any simpler or more readable from the other techniques I have mentioned, which in that case a comment would be needed. I’ll add it later tomorrow since it is like midnight as I am writing this lol.

Again, thank you for the comment.

1 Like
--[[
    Teleports a given player to a given location
    
    Player   player     - the player being teleported
    Vector3  toLocation - the location the player will be teleported to
--]]
function minigameModule.teleportPlayer(player, toLocation)
    -- Code to teleport player to location (ignore this, as it isn't really important)
end

why not just use annotations?

--- Teleports player to given location
---@param player Player the player being teleported
---@param toLocation Vector3 the player being teleported
function minigameModule.teleportPlayer(player, toLocation)
    -- Code to teleport player to location (ignore this, as it isn't really important)
end
4 Likes

And here’s an article describing when you should NOT comment: You’re commenting your code too much (and other controversial thoughts on documentation) | by Bennett Garner | Level Up Coding

Remember that comments aren’t there to help the clueless, unless that is some requirement for some reason. For example in a programming tutorial etc.

If you have code like this:

local sum = 1 + 3;

Then you don’t need to comment that sum is the sum of 1 + 3, because that is blatantly obviously by just looking at the code.

Another thing is that you shouldn’t avoid comments and create redundant variables that really have no purpose. For example here:

A comment would have sufficed because this is when to use them, to explain something that’s happening. If you’re making a performance-critical program you don’t want to do redundant things like this, although the impact is minimal, it’s still bad coding practice because one line of comment would suffice saying “checks if the player is nearby …”.

Alternatively, use better variable names beforehand, that will also help a lot!

4 Likes

I do know that there are good reasons in not using an extract variable (like for memory), but the point is that there are different ways of making code readable, not just writing comments. I should have been more clear on the fact that which technique you use to make your code readable depends on several factors and that there are situations where you wouldn’t use a particular technique.

Thank you for the comment, and I also like the article you recommended. Just now thinking about it, I should provide links that people can go to for more information or get a better understanding of what I am writing about here (I’m not the best explainer in the world, after all).

Are you referring to computer memory here? As in, will having an excess of comments actually cause a script to slow down? I thought they were all removed at runtime anyway, but may be wrong.

Yes, I was referring to computer memory. I am aware that comments get removed when compiled (or ignored when interpreted). This isn’t really a good reason to not have comments (except for specific situations), but my other reasons for why should hopefully make sense.

I updated this post to include the link to the next part of the Good Coding Practices series. If you want to read it: You can find it in this post or click here to go to it.

Instead of

--[[
Something 
--]] 

Can’t you do this?

--[[
Something 
]]

I mean, you could, but the formatting feels off to me personally. If it is the kind of formatting you want to use (or, more importantly, what your team uses), then go ahead, as long as you are consistent.

1 Like