Coding Challenge #92 - LOC Counter
Coding Challenge #92 - Line of Code Counter
This challenge is to build your own version of the tools cloc, sloc and scc. These tools count lines of code and produce statistic on the number of lines in the source code, the lines of code, the lines of comments, the empty lines and so on.
Some also calculate the COCOMO 81 and COCOMO II estimates for the software being analysed. If you’re not familiar with it, the COCOMO model was developed by Barry W. Boehm to estimate the effort, cost and schedule for software projects. I wouldn’t rely on these numbers to plan a software project, but they’re an interesting tool to compare existing projects and get a feel for the size and scope of them.
Counting the lines of code in a software project sounds trivial and quite honestly seems like something you could do in a short bash command, i.e.:
However if you want to do it accurately and fast, you can get into some interesting computer science challenges. And when it comes to scc, I mean blazingly fast!
But Why Count Lines Of Code?
TL/DR: It’s useful as a gauge of the size and complexity of a project, but if you want much more detail Ben Boyter, the author of scc wrote a blog post explaining why he put so much effort into building a tool to count lines of code.
📌 Next Systems Programming (Redis) Course Starts 14th July
Would you like to build a network server from scratch with me?
Learning about network programming, concurrency, testing, and systems software development?
If so check out my course: Build A Redis Server Clone: Master Systems Programming Through Practice.
It is designed to be intense! It’s 11 hours of instructor time over two weeks. With the goal of having you build a clone of the original Redis server by the end of the two weeks.
If you sign up before 30th June you can get $100 off using the code: EARLYBLIJ
The Challenge - Building A Tool To Count Lines Of Code
In this project, we’re going to build a tool to count likes of code in each file that is in a directory or subdirectories of that directory.
The tool should identify blank, comment and code lines and be able to provide a value per file and a summary of the whole project with output in text or JSON.
For example, here’s the output of SCC on the Go version of the Redis clone I use in my Build A Redis Server Master Systems Programming Through Practice course:
Interesting to see that it estimates the cost of building the software as 57k USD!
Step Zero
Like all C based programming languages we’re zero indexed at Coding Challenges! 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. While you’re doing that give some thought to the programming language or languages that you might find it useful to be able count the lines of code for.
Step 1
In this step your goal is to scan a file system from a starting directory and identify all the source files in it.
Your program should accept a single argument for the starting directory. For this step I suggest you list out all the matching files. To check your code works you could pipe the output to a file and compare to the bash command:
Replacing with the extension of the programming language your going to focus on counting the code for.
Continued...
You can read the rest of this challenge on the Coding Challenges substack.
Full Stack Developer | Building Scalable Web Applications with React, Node.js & Python | Passionate About Clean Code & User-Centric Solutions
1moThese challenges are pure gold for leveling up coding skills. Definitely going to give the line count tool a go and maybe a few others on that epic list!
Enterpreneur
2moJohn Crickett, you're right about the hands-on approach. I've seen too many developers who can ace coding interviews but struggle when it comes to actually shipping something. These challenges look like they'd bridge that gap nicely.
Senior Software Development Engineer
2moThanks for sharing, Johnny’s