BlogProjectsVideosMusic
|
Cleanup­CLI
ditch the file explorer for good
2024/08/04
link

So basically, I feel like I've been funneling myself into front-end development and staying away from the backend and lower-level languages for the past 3 years, and I was looking for a change. I'd done a couple of courses in Uni that covered the basics of C and C++ but they had their own limitations that prevented me for looking into them further.

I hated C++ and its header files. I could not fathom having to write out what is essentially an abstract class for every single class or function definition. C felt a little too restricted, not verbose enough in terms of error messaging - I remember having to search and search for a segfault. This led me down towards 3 options, Rust, Go, and Zig. The deciding factor was the fact that an up-and-coming JavaScript runtime that I'd been using was written completely in Zig. I'd been using the Bun runtime in tandem with Vite and React in one of my other in-progress projects, and it has genuinely been such a good experience. So damn fast.

After running through the Ziglings tutorials I decided that it was time to try build something for real this time; and the obvious choice was a CLI tool that interacted with the filesystem somehow. It's about as standard as a first CLI tool gets.

So armed with a rudimentary understanding of Zig, I set off to make a compiled binary that I could add to my PATH variable and may actually use every once in a while. I have discovered that Zig is really fun to write. The error messages are so verbose, that once you learn what it's telling you is wrong just once, it's really easy to remember and apply change when you run into this error in the future.

Given that there was a lot of string manipulation, I went through a lot of iterations of how to manage this bullshit. Ended up using []const u8 for almost everything - which is an array of constant pointers to u8 "chars" (in quotes because its really just an unsigned int). A concept in Zig that doesn't necessarily agree with me just yet is the whole business about slices. I'm sure it makes sense to smarter people and those who put in the time to actually learn it, but as of writing this it's still kind of a mystery to me.

Zig is a massive driver of TDD (Test-Driven Development). I'd had a limited experience of TDD before, namely using Python and its general unit-testing ability, but in this case it felt like a first-class citizen. Given that I was learning the whole damn thing from scratch, I was testing random little implementations all the time; from rolling my own case-insensitive string comparison function to finding out there was absolutely a standard library function for that exact thing (this happened way more than I'd like to admit). I was rewriting large sections of my main() function over and over after trying new approaches in the test blocks. This iterative process was incredibly useful in getting me up to speed with the language and all the fun little 'isms it includes.

Don't even get me started on error handling.

Truth be told I had a lot of fun learning something completely different to my regular React and TypeScript situation that I've been cultivating. If you'd like to view the source code or make fun of my programming abilities lol you can find it here: https://github.com/liamdoka/cleanup-cli

More projects