DEV Community

Neil Gall
Neil Gall

Posted on

Pirrigator, part 3. Being a newbie and writing bad code.

I procrastinated a bit this week. There were other things to do - I received the darlington transistors and got the water valve working under Raspberry Pi control with the sysfs interface to GPIO. I also received the MCP3008 ADC and got some Python code reading the analog output from my moisture sensors.

But I find the hardest thing about learning a new language is the feeling you're doing it wrong. I don't mind the feeling of being less productive, having to look everything up or having a very short and frequent edit-compile cycle to catch all the silly and syntactic mistakes. I've been writing code in many languages for over three decades and I like to think I have a good sense of what the right and wrong way to do things is, but entering the new world of Rust feels like I've lost this sense. I've learned enough to be able to write stuff that works, but is it the right way to do it in Rust? I found myself saying I'll wait until I've read more of the book, or find someone else's code which does something similar, then I'll write my own code. But I had to fight this urge. Writing anything, even if it's terrible, is progress. The wonderful thing about software is you can always rewrite it again later.

So I have produced my first Rust code! It's hideous, probably non-idiomatic in countless ways, but it's Rust code which compiles and works. I have built an ARMv6 native binary for Raspberry Pi which reads the temperature/pressure/humidity sensor (I think I'll just call this the weather sensor from now on) and prints the current values to the console. I will still wait to learn a little more Rust before publishing on crates.io, as I don't think it's ready for "production" yet but its beautiful functionality and straight-port-from-C ugliness is available for all on GitHub.

I've also been working on a module to read the moisture sensors, which have an SPI interface. I really wanted to have everything on I²C but SPI is only a few more wires, and a bit more code. RustpiIO is looking pretty promising as a base to build this on.

So today's message is to push on when you're learning and write that bad code. The worst you've ever written. Awful nonsense that reinvents the standard library or misses language features which would make it more concise or robust. Throwaway garbage that other people will laugh at. One day you'll be the other people laughing at it, because the bad code will have taught you more than all the examples of perfection you can find.

Top comments (1)

Collapse
 
neilgall profile image
Neil Gall

Update: I got the SPI stuff working in Rust and can now read my moisture sensors too. github.com/neilgall/rust-mcp3xxx