What do novice programmers do that professional programmers find cringe-y?

Mudassir Ali
Feb 10, 2020 05:33 AM 0 Answers
Member Since Dec 2019
Subscribed Subscribe Not subscribe
Mudassir Ali
- Feb 10, 2020 05:33 AM

Scenario: a novice programmer is asked to write the checkout logic for an online store.

So, he creates a method called checkout(), which:

Totals up the prices of all the items in the cart.
Calculates tax on those items.
Applies any relevant discounts or coupons.
Calls the merchant service to charge the user’s card.
Saves the payment information to the database.
Saves the shipping and billing address to the database.
Saves the order to the database.
Emails the invoice for the order to the fulfillment center.
Emails the receipt for the order to the user.
If all that worked, outputs all the HTML, etc. for the success page.
If it didn’t, outputs all the HTML, etc. for an error message and emails the system administrator.
No. No. No. No. For the love of pasta and all that is holy, NO.

When I, the senior developer, review his code, what I see is pages and pages of undifferentiated logic, no part of which relates to any other part. Yes, technically it may work, but maintaining that code is going to be a nightmare, particularly since in any realistic scenario there’s going to be a godawful amount of conditional logic — if this, then do this step this way, or if not, do it this other way — that the next developer will have to painstakingly trace through just to figure out how the hell a particular thing is being done.

And the frustrating thing is that many novice developers (1) don’t get why this is a problem, and (2) can’t comprehend how to do it any other way.

Here’s the right way to do it:

Make a method called cart.total() that returns the total of the items in the cart.
Make a method called cart.tax() that returns the tax on said items.
Make a method called cart.discount() that calculates any applicable discount, and if there are multiple types of discount that can apply, make a separate inherited class for each discount type that has a method for calculating just that type of discount.
And so on and so forth. The point is, any discrete, self-contained piece of logic should get its own method. Every method should do precisely one thing and do that thing right. Every conceptual unit of logic and program state (e.g., a VolumeDiscount) should have its own class. Related classes should all do things the same way (e.g., don’t make a VolumeDiscount class with a calculate() method and a CouponDiscount class with an applyCoupon() method). Separate output and logic (e.g., outputting HTML or sending an email should happen in a view, not in your method).

That is how you make readable, maintainable code. I should know at a glance exactly what your code is trying to do, and why, and where to find any given piece of logic. If I have to hunt and pick through your code to figure out the flow of logic, you did it wrong and I’m going to yell at you. Nicely.

ETA: I’m not a huge fan of functional programming unless you’re doing something like massive parallel processing — I think it complicates certain things unnecessarily — but I will say this for it: it forces people to learn about separating their logic into logically distinct operations that do precisely one thing and do that thing right. That’s a good lesson to learn.

Reply on This
Replying as Submit
0 Subscribers
Submit Answer
Please login to submit answer.
0 Answers