Build Your Own Code Comment Remover
This challenge is to build a tool to remove comments from source code.
It’s not a tool I’ve seen much demand for in the past, but I’ve seen it mentioned a few times now with the advent of AI coding assistants. Apparently some people find they generate too many low value comments so they want to be able to remove them.
It’s an interesting use-case that perhaps not all of us face, but there are other tools that remove comments, for example, interpreters, compilers, minifiers, obfuscators and prettiers so this coding challenge has scope to solve the AI use case and be the building block for a bigger project that you can learn a lot from!
The Challenge - Building A Comment Removal Tool
In this coding coding challenge we’ll be building a tool to remove code comments from source code.
We’ll look at several different programming languages, each of which introduces a new aspect to consider.
There are different ways you can tackle this Coding Challenge. Which you pick will depend on what you want to learn and how far you want to take the project. At one extreme you can use some relatively simple regular expressions, at the other you could build a lexical analyser (aka lexer or scanner) and then convert the tokens back to source to emit the source code minus the comments.
The second approach gives you the initial building block of an interpreters, compiler, minifier, obfuscator or prettier - if you think you might like to take the project further.
Step Zero
Like all good software engineers, here at Coding Challenges we’re zero indexed! In this step you’re going to set your environment up ready to begin developing and testing your solution.
I’ll leave you to setup your IDE / editor of choice and programming language of choice. I suggest picking one that you’ll be comfortable building a CLI tool in.
Step 1
In this step your goal is to remove Python comments. Python comments are from the #
character to the end of the line. For example:
# this is a comment
l = [x for x in range(10)] # this is also a comment
I suggest you find some open source code to test against as well as your own examples. You could use the Coding Challenges Shared Solutions Github repo to find some open source Python that’s been used to solve previous Coding Challenges.
Step 2
In this step your goal is to remove C and C++ style comments from JavaScript source code. For those not familiar that means turning this JavaScript:
/*
* Calculate the factorial of n
*/
function factorial(n) {
// check n is valid
if (typeof n !== 'number' || !Number.isInteger(n) || n < 0) {
throw new Error('n must be a non-negative integer');
}
// base case
if ((n === 1) || (n === 0)) {
return 1;
}
// other cases
return n * factorial(n - 1)
}
Into this:
function factorial(n) {
if (typeof n !== 'number' || !Number.isInteger(n) || n < 0) {
throw new Error('n must be a non-negative integer');
}
if ((n === 1) || (n === 0)) {
return 1;
}
return n * factorial(n - 1)
}
Step 3
In this step your goal is to remove the comments from C and C++ code. For example:
/*
* Calculate the factorial of n
*/
int factorial(int n) {
// check validity
assert(n >= 0);
// base case
if ((n == 1) || (n == 0)) {
return 1;
}
// other cases
return n * factorial(n - 1);
}
Becomes:
int factorial(int n) {
assert(n >= 0);
if ((n == 1) || (n == 0)) {
return 1;
}
return n * factorial(n - 1);
}
Step 4
In this step your goal is to support Go, leaving in Go doc comments. These are the comments that can be extracted from Go source code to create documentation
Step 5
In this step your goal is to leave in Python and Go comments related to configuration of tools and linters. For example:
example = lambda: 'example' # noqa: E731
Or for Go, comments such as:
//go:generate
//go:build ignore
These comments should be left in.
Going Further
how to take this further:
- Add support for more programming languages.
- Extend your solution to minify a scripting language.
- Extend your solution to obfuscate source code, by removing whitespace and creating random and short replacement names for all variables and functions.
Help Others by Sharing Your Solutions!
If you think your solution is an example other developers can learn from please share it, put it on GitHub, GitLab or elsewhere. Then let me know - ping me a message on the Discord Server, via Twitter or LinkedIn or just post about it there and tag me. Alternately please add a link to it in the Coding Challenges Shared Solutions Github repo.
Get The Challenges By Email
If you would like to receive the coding challenges by email, you can subscribe to the weekly newsletter on SubStack here: