Well, it's holiday season in the US and I've been gradually becoming less productive. I'd like to say it's because I have family over and that I haven't had enough sleep lately, but that's only part of the equation. To combat feeling like I've been doing less, though, I decided to work on some smaller tasks and clean up some code that I wrote previously.
Anyway, I ran into a small error when cleaning up the database seed file -- basically a file that you run to create dummy data for development -- and decided to fix it right then and there. No better time than now, right?
To give some background, our tags have two columns, bg_color_hex
and text_color_hex
. They're supposed to take in a hex code, and we properly restrict it to alphanumeric characters and a limit of 6 of those in a row -- well, that's what I think it is since it's a regex validator and I didn't want to translate it 🙃.
There is, however, an annoying edge case where we can input it a hex code without the pound sign/hashtag/whatever-people-call-it-nowadays in front of the 6 characters, and the validator will throw us an error. There's no reason for that to not be automated, so I went for it.
As I'm writing the method out, I suddenly had a moment of glorious inspiration. In our quiet office, I smiled to myself at my extreme cleverness.
So, in great joy I wrote out a method that puts the pound sign in front of the color attributes if necessary:
def pound_it
text_color_hex&.prepend("#") unless text_color_hex&.starts_with?("#")
bg_color_hex&.prepend("#") unless bg_color_hex&.starts_with?("#")
end
Now, the code could probably be refactored, but the beauty of course lies in the name. This happens every time a tag is validated, so know that you are indeed "pounding it" every time you hit the Preview or Save button for your posts.
Hopefully, this end-of-year post will inspire you to do something similar. What are some funny bits of code that you've run into? Have you written anything in your code and gotten it through all the red tape of your company? Would love to hear your stories!
Top comments (12)
aka the octothorpe...but octothorpe it is not nearly as funny as your version ;-)
Wow, I have never heard of that until now. What a name!
Can't think of anything at the moment, but I've seen some cheeky comments from linters that make me double take.
My humble contribution to silly codes.
Also, Inserted this to a script header
I recently put this comment
// put hash in cookie
in my code.In South Africa we have power issues which the government mitigates with "load shedding" - turning off parts of the grid during high load times.
Our app suffered similar high load call stacks causing jank, so I created a load shedding service that will spread computation across animation frames or ticks in order to keep frame rate at 60fps.
In a legacy Java codebase I was immersed in in my first programming job, I found this comment:
Whatever code it referred to was no longer there.
I'll definitely start thinking about it now. Though I work with a lot of non-native English speakers, so I'll have to be careful.
This question reminded me of this: stackoverflow.com/questions/184618...
:)