Recently, I was tasked with creating a redirect from my company's Rails app to an external webpage with a rather long URL.
First, I created an action in the page's controller:
Next, I added a route for said action:
Then I called the route in the page:
Upon code review, my teammate suggested that I create a helper method which returns the URL, then call the helper in the view:
The controller action was unnecessary, superfluous, cumbersome.
Implementing the helper method was so simple, uncluttered, direct.
That some code works never exempts that code from being improved upon.
Top comments (9)
Absolutely agree with this as I know I'm very guilty of hashing stuff together sometimes to just get it working.
In this case there are differences between the two implementations. The controller action and route allows someone to visit your site at '/awesome' and be redirected to the very long URL. The helper means that you have to render the link on one of your pages, so a user could be taken to your site before having to click another link to get to the awesome page. Both fine, but have different implications for your traffic, usability, analytics, etc. So much of programming is understanding when you should and should not introduce complexity and the compromises you make between two different solutions.
There is a person in our company who 90% of the time writes WAY more code than is needed to get the job done. You can get 20 levels deep in it and still not know what it's doing. Royal Pain and time consuming to debug.
Many times I have taken huge amounts of their code and reduced it by at least 50% most times more.
I honestly think he does it on purpose so he is the only one who even semi understands it. Do when it breaks they look like the hero when it breaks and somebody or multiple somebody's have tried to fix it he can step in and look like a hero and fix it.
Their biggest skill is taking some thing that could be 50 inline lines of code and turning it into 500 and 3 APIs or external calls to other functions/module's
π€£π€£
Spot on! I tell my developers to write code so if I had to make a change to it 2am in the morning I would not have to drink coffee first.
Every developer is tempted to write things in clever ways and most of the time the benefits are not as valuable, because the next person that sees the code will most likely not know what you were going for and not follow the same cleverness. Simple code that is easy to understand always wins on my books!
"Always code as if the guy who ends up maintaining your code will be a violent psycopath who knows where you live"
It's a challenge to keep code simple. I've noticed if I'm writing a feature, I'll mimic the pattern that works elsewhere. I'll take bits and pieces from other code I've written to get the feature up and running faster, but it might be overkill for what I'm actually trying to do. You're right, there's always room for improvement!
you could have simply created a global constant and used the constant at the link. it was the simplest approach I guess...
anyway, nice article...
Good observation Mohd but my team actually didn't want to create a global constant in this case βΊοΈ
now what can be done when this team thing comes in the scene. I can understand...