You can be doing something straightforward. Say, listing some items from the database and giving them an
You're using them for URL's too, so it makes sense. Simplifies Backbone routing code.
At this point experienced developers get the heebie jeebies. A tiny little voice screaming "Watch out!" in the back of their head.
I couldn't remember why using
name felt funny, so I ignored the little voice and barged on. I deployed.
Bam. Didn't work.
After hours of debugging I finally traced the issue down to spaces in item names. Turns out
id="a word" doesn't work. Who'd've thought? (hint: everyone)
But that's an easy fix right? Just do something like:
id="<%= item.name.gsub(' ', '-') %>"
Or add a function to the model. Whatever. The point is, all you have to do is replace spaces with dashes. Simple.
But you're wrong. You are creating a slug. It looks easy, but is actually very complicated. Luckily others have solved the problem already. Better than you.
A quick Google search reveals a bunch of solutions do in fact exist, but Stringex comes out as the best solution for Ruby. There should be plenty for any language you love.
Why spend an hour looking for and learning how to use a gem instead of solving the problem in two minutes of coding?
Because -> from Stringex's documentation:
# A simple prelude"simple English".to_url => "simple-english""it's nothing at all".to_url => "its-nothing-at-all""rock & roll".to_url => "rock-and-roll"# Let's show off"$12 worth of Ruby power".to_url => "12-dollars-worth-of-ruby-power""10% off if you act now".to_url => "10-percent-off-if-you-act-now"# You don't even wanna trust Iconv for this next part"kick it en Français".to_url => "kick-it-en-francais""rock it Español style".to_url => "rock-it-espanol-style""tell your readers 你好".to_url => "tell-your-readers-ni-hao"
Impressive to say the least.
There's the simple space to dash conversion, but Stringex handles various symbols and common substrings as well. Converting
12-dollars strikes me as something particularly useful I'd never come up with.
Truth be told, the most advanced slug generation function I've ever come up with (and I've written many) only stripped non-alphanumeric characters from the string before changing spaces to dashes.
When you find yourself solving a simple problem, look for a gem. Focus your attention to the problem at hand and let others take care of the externalities. You won't be sorry.
Learned something new?
Read more Software Engineering Lessons from Production
I write articles with real insight into the career and skills of a modern software engineer. "Raw and honest from the heart!" as one reader described them. Fueled by lessons learned over 20 years of building production code for side-projects, small businesses, and hyper growth startups. Both successful and not.
Subscribe below 👇
Software Engineering Lessons from Production
Join Swizec's Newsletter and get insightful emails 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.
"Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"
Senior Mindset Book
Get promoted, earn a bigger salary, work for top companiesLearn more
Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.
Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.
Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.
Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev
Want to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz React components your whole team can understand with React for Data Visualization
Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog
By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️