#ShitcodeWednesday Ep.2

Marina Lohova
n3rdiii blog
Published in
4 min readDec 23, 2016

It’s Friday. That is why I decided to publish the new installment of my Shitcode Wednesday series. I only want to show you, Dear Reader, that nothing in the world of bad code makes sense. Check out Ep.1 if you dare.

Today we shall continue on our rocky path of enlightenment.

‘I was sick that day’

‘My balls hurt’

‘My girlfriend had PMS and yelled at me’

‘It was ________’

— Demo

— Release

— Production deployment

‘I worked ________’

— Till 3am at night

— Weekends

— From the hospital bed

— From the death bed

And you definitely did not forget to send an email ‘to All’ at 4:45am about some crap nobody cares about to make sure everyone knows how hard you worked your butt cheeks off that night.

I’ve heard all of these, and all of these are just mother-fugging excuses.

Sorry not sorry.

Just remember, hearing excuses is #1 sign that you are dealing with a Bad Programmer, and it’s you, my Brave Reader, who will be asked to come in and save the day when everyone realizes that your fellow brogrammer’s whining is not getting you guys any closer to the actual Production Ready Code™️.

That being said, on my Shitcode Cam today*:

* all shit code is posted without modifications as discovered by the author in the shit code dungeons of America

json.records(@carts) do |cart|
json.id cart.cart_id || cart.smart_cart_id
json.name cart.cart_name
if cart.smart_cart_id.blank?
json.add_link influencers_path(id:cart.cart_id, smart:false)
else
json.add_link influencer_path(id:cart.smart_cart_id, smart:true)
end
json.show_link cart.smart_cart_id.blank? ? group_path(cart.cart_id) : smart_group_path(cart.smart_cart_id)
json.influencers_count number_to_human(cart.influencers_count, precision:3)
json.description cart.cart_description
json.created_at cart.created.to_datetime.strftime("%A, %b %d %Y @ %l:%M %p")
json.is_deleted false
end

Essentially, a simple JSON object is generated on the backend in Ruby and subsequently returned to the ReactJS frontend.

Now let’s see what is exactly wrong with this piece of shitcode.

This snippet here:

if cart.smart_cart_id.blank?
json.add_link influencers_path(id:cart.cart_id, smart:false)
else
json.add_link influencer_path(id:cart.smart_cart_id, smart:true)
end

*** In the world of social media marketing where I work, Users are called Influencers. Companies are essentially buying Internet personalities to participate in their ad campaigns and promote their products via Social Media, hence ‘adding Influencers to the Cart’.

And this one:

json.show_link cart.smart_cart_id.blank? ? group_path(cart.cart_id) : smart_group_path(cart.smart_cart_id)

Just like you, I’m completely in the dark in regards to what Smart Cart actually is. Let’s just assume it’s some specific type of cart with superpowers.

Both snippets generate a link. A link as in the actual piece of HTML. In a better world it would have been <a href=“http://blog.nerdiii.com”>n3rdiii blog</a>. Instead it’s a link to the page where you can add Influencers to or view the mysterious Smart Cart.

Dear Creator of this,

It’s almost like all those smart people who worked their asses off to make web development better for all of us through the “separation of UI and data” and “MVC (Model-View-Controller)” pattern, only did so so that you could walk in and slam the door shut on all those years of knowledge and improvement.

Why do we put the typical frontend entity —a link — in your fucking API response? Why do we generate it on your fucking server? Are we really wasting an HTTP request, network bandwidth, response time, and the fucking processing power of your fucking servers to fucking build your fucking link?

FUCK YOU.

All of this can be done with the plain String concatenation in your fucking ReactJS.

Is it one too many fucks yet?

FUCK

Oh, I get it, you want to re-use your Rails router so the links on the frontend would always match the backend routes… Because your fucking RESTful API changes ‘bout ten times a day. Please.

MVC pattern. Read about it!

Model–view–controller (MVC) is a software design pattern for implementing user interfaces on computers. It divides a given software application into three interconnected parts, so as to separate internal representations of information from the ways that information is presented to or accepted from the user.

Thin server architecture. Read about it. This is what you are attempting here.

A Single Page Application (SPA) moves logic from the server to the client. This results in the role of the web server evolving into a pure data API or web service. This architectural shift has, in some circles, been coined “Thin Server Architecture” to highlight that complexity has been moved from the server to the client, with the argument that this ultimately reduces overall complexity of the system.

This results in the role of the web server evolving into a pure data API or web service.

This.

Remember this:

Your data API response must only have DATA. Period.

Dear Reader, hope you had fun and/or learned something useful today. Happy Holidays and,

TTYL

Subscribe to read more awesome articles about web development. If you like what you just read check other fun pieces in this publication too:
#Shitcode Wednesday Ep.1
How to Freeze Your Enemy’s ReactJS App
Or tweet me at @ftert.

--

--