New year, new projects
After Advent of Code has ended I’ve started looking for another challenge and found weirdtext. It’s a recruitment test, but as I’ve found it quite interesting I’ve decided to do it and have coded it in Go.
To make initial version of encoder and decoder was quite easy, but then I’ve started to add other cases to my tests and made some improvements, and also fixed some bugs. I’m not big fan of TDD, but I’m fanboy of unit tests. They allow me to test edge cases and keep me confident while improving my code that I don’t break anything. I may not be very good at writing tests, yet, but I’m always trying to improve.
First I was reminded that indeed arrays are copied when passed to function, but []string isn’t an array. It’s a slice and slices are passed by reference, so I was destroying my data. Another problem occurred when I’ve added some unicode to my tests. I’ve learned that I should get length of my encoded word after it’s been converted to slice of runes, not when it’s still a string. Version with string works for latin characters, but not for ones which occupy more than one byte. I’ve also screwed one of my ifs, but it’s typical for me and that’s why I’m using unit tests. These are not advanced concepts, but ones which are easy to overlook/forget, at least for me. And that’s why you should have tests.
You also need to be careful with tests. At first I was testing my EncodeText function using static string as comparison to the value returned by EncodeText. But, as EncodeText uses rand.Shuffle the output is, surprise, random. So after a while my tests started to fail. I’ve quickly switched to testing DecodeText on the output of EncodeText and comparing if the decoded text is the same as the what was originally encoded.
I’m still using IRC as my main communicator and one day somebody has mentioned that they’re planning to use YOURLS/YOURLS as URL shortener of choice. As this person is way more experienced than I am, I’ve started to wonder what’s so hard in writing your own. And so I’ve created shorty. My very own URL shortener. While writing it I’ve tried to keep it as simple as possible, so it uses text file as storage and hash is just crc32. But I’ve made it easy to be changed in future, or so I hope. Oh, and to answer my own question, it’s not hard to write URL shortener. Actually it was quite easy, so, in my paranoia, I’ve started to wonder if maybe I’ve screwed something up.
I’ve decided to use sync.Map as a type to store my shortened (and original) URLs. But I want to mention that I’ve also found orcaman/concurrent-map which is a nice project. And probably it would perform better in shorty, but as I’ve wanted to keep it simple I also didn’t want to use any external modules. However I may change my mind in future, if I’ll keep developing it. I’ve also made a little contribution to concurrent-map, so I won’t forget it, for sure.
Later somebody mentioned shorty on IRC and in conversation it turned out that some people would like to have both URL shortener and pastebin as one service. I don’t believe in mixing up services. But it reminded me of solusipse/fiche, pastebin where you can post your code using nc command, so directly from terminal. I like it a lot and am hosting it on my server, but I also like syntax highlighting and line numbering when reading code. fiche’s author provides his own solution for it. But, I don’t like either Python or Flask (however I don’t know this one too well). And it’s not very performant as it’s generating HTML from TXT paste with every request, plus it’s Python. I know that for my own usage and with current servers it probably wouldn’t be a problem, ever. But as I’m moving from hosting to hosting quite a lot and like to keep it as smooth as possible. I’ve decided to write my own solution, so behold ficheSyntaxHighlight. It’s using alecthomas/chroma for generating HTML files with syntax highlighting, but language detection is my own creation as chroma’s one doesn’t detect what I want. And it should be a bit faster than chroma’s lexers.Analyse which is searching through entire paste, more than once. Still, it’s nowhere close to guess_lexer from pygments.lexers, used by fiche’s author.
I’ve also discovered deepsource and have added it to almost all of my Go projects. It’s shown how messy my TM Search is. It’s no excuse that I’ve written it while learning Go. It needs complete refactor, because it’s a little embarrassing.
As you can see beginning of the year turned out to be quite creative for me. And I hope entire year would be like that as I love my hobby projects. They allow me to code and to learn. And it’s always better if I can use what I’ve learnt solving real problem, rather than only reading about it and doing exercises.