Coding Challenge #87 - Code Comment Remover
This coding challenge is to build your code comment removal tool.
Hi this is John with this week’s Coding Challenge.
🙏 Thank you for being one of the 80,132 software developers who have subscribed, I’m honoured to have you as a reader. 🎉
If there is a Coding Challenge you’d like to see, please let me know by replying to this email📧
Coding Challenge #87 - 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!
Heads up! I’m Opening Up Some Paid Mentoring Slots
I’m opening up a couple of slots per week to provide mentoring.
If you have a challenge that you think I can help you with and you’d like me to mentor you, please register your interest by completing this form.
If You Enjoy Coding Challenges Here Are Three Ways You Can Help Support It
Refer a friend or colleague to the newsletter. 🙏
Sign up for a paid subscription - think of it as buying me a coffee ☕️ twice a month, with the bonus that you also get 20% off any of my courses.
Buy one of my courses that walk you through a Coding Challenge.
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
“Doc comments” are comments that appear immediately before top-level package, const, func, type, and var declarations with no intervening newlines.
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.
Two Other Ways I Can Help You:
I write another newsletter Developing Skills that helps you level up the other skills you need to be a great software developer.
I have a YouTube channel sharing advice on software engineering.
Share 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 via Bluesky 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
Request for Feedback
I’m writing these challenges to help you develop your skills as a software engineer based on how I’ve approached my own personal learning and development. What works for me, might not be the best way for you - so if you have suggestions for how I can make these challenges more useful to you and others, please get in touch and let me know. All feedback greatly appreciated.
You can reach me on Bluesky, LinkedIn or through SubStack
Thanks and happy coding!
John