r/programminghorror • • Feb 28 '26

Do you like my FizzBuzz implementation

Post image
1.3k Upvotes

218 comments sorted by

View all comments

178

u/ababcock1 Feb 28 '26 edited Feb 28 '26

Can you explain the horror? I'm not a python dev but this looks prettyreasonable. Could it be slightly better, sure. But not quite horror levels.

Edit: In my replies are the following... People who think throwaway code from a first year uni class needs to be perfect. People who think every piece of code needs to be a work of art. People who don't understand YAGNI. People who think "I wouldn't have written it that way" is equivalent to horror.

It's fizzbuzz guys. It's not enterprise world class Web 4.0 Scale software now with AI (tm) and it doesn't have to be.

11

u/418_TheTeapot Mar 01 '26

Checked the comments to find the horror. It’s in the discussions this post spawns…

21

u/[deleted] Feb 28 '26

Yeah, like it’s horror in where it’s going. Another case and we need 8 switches, another and we need 16, etc., but wouldn’t technically call it horror yet until it had more cases. In its current state, it makes me wince but not cry.

I’d leave a review comment saying this makes my eyes hurt and rewrite x way if it was in a PR, but wouldn’t consider it horror.

If it was an interview I’d just ask them how they’d approach adding another case, and make sure that they recognize their current pattern isn’t scalable

27

u/JollyJuniper1993 Feb 28 '26

I don’t think FizzBuzz requires scalability

9

u/ACoderGirl Mar 01 '26

Maybe your FizzBuzz doesn't, but mine needs to be able to handle 20M QPS.

1

u/california_snowhare Mar 03 '26 edited Mar 03 '26

Here you go. Tested to run a bit more than 140MQPS on an Apple M2 Ultra and more than 50MQPS on a Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz.

#include <string>
#include <vector>
using namespace std;

class Solution {
private:
    vector<string> lookup = {
        "FizzBuzz",
        "", "", "Fizz", "", "Buzz", "Fizz", "",
        "", "Fizz", "Buzz", "", "Fizz", "", ""};
public:
    vector<string> fizzBuzz(int n) {
        vector<string> ans;
        ans.reserve(n);
        int mod15_counter = 1;
        for(int i=1; i<=n; i++) {
            ans.push_back(lookup[mod15_counter].length() == 0 ? to_string(i) : lookup[mod15_counter]);
            mod15_counter += mod15_counter == 14 ? -14 : 1;
        }

        return ans;
    }
};

-4

u/[deleted] Feb 28 '26

A problem of this type may, hence why I would ensure they understand the limitation in an interview, and accept their answer if they show that they do.

If this was a code base, it can simply be written cleaner. But, you wouldn’t be PRing FizzBuzz. You’d be PR’ing a problem of this type

8

u/JollyJuniper1993 Mar 01 '26

A decent developer will know when something is a waste of time

1

u/[deleted] Mar 01 '26

So which one do you disagree with, that this is not horror, but could be cleaner if in PR or that this would be acceptable in an interview if candidate was able to identify that this pattern doesn’t scale when I ask?

1

u/Jamie_1318 Mar 01 '26

You say it wouldn't scale well, but it isn't really any worse when adding additional constraints than the traditional if elseif elseif else implementation.

If I were reviewing this I probably wouldn't complain, at worst it would be a nit to name the conditionals or something. With a dict to name the conditions it can scale as far as code realistically should before talking about data-driven implementations or a dedicated class.

Realistically it's a short enough piece of code that it can easily be rewritten later if necessary, and it isn't hairy enough that it would cause a problem to do so.

26

u/csabinho Feb 28 '26

But FizzBuzz is defined as such. And this is a concise version of FizzBuzz.

→ More replies (10)

2

u/le_ramequin Mar 01 '26

stuff can always be refined later when needing to scale

1

u/-TRlNlTY- Mar 02 '26

There is nothing to scale here

1

u/krutsik Mar 01 '26

It's not enterprise

I'm not even sure you cloud write FizzBuzzEnterpriseEdition in python and not still have it be pretty reasonable.

1

u/Mistigri70 Mar 04 '26

to me the problem is using True and False in a match statement when a simple if statement is more readable

-2

u/[deleted] Mar 01 '26

On your edit, nobody is talking like that lmao. You asked to explain how it could be horror so we did.

Saying I’d probably leave a PR comment (which can be non-blocking to merge depending on specific case)?

Or making sure an intern (only person who I might actually start with FizzBuzz in an interview) can answer a valid follow up question to test their pattern recognition?

You can say you don’t mind the switch case, who cares.😂 It’s ridiculous to get so bent up over the above two statements though

6

u/ababcock1 Mar 01 '26

I asked for horror and you gave me made up requirements and nitpicking. You call me "bent up" after you spent your entire Saturday arguing over it.

0

u/[deleted] Mar 01 '26

Reply before deleting comment:

“I asked you to give me horror and you give me nit picks and made up requirements. You say I’m “bent up” but spent your Saturday talking about it.”

Read more carefully. I said it isn’t horror in current state. That if it were extended it would be. That if I were in an interview I would ask them a follow up.

That I would leave:

nit: Can reduce with: insert code

In a PR. On the Saturday thing, that’s a good one, but I’m over here laughing. This was more passive amusement. I still don’t understand what y’all are really disagreeing with. Is this how you act at work if someone leaves a nit in your PR? Yeesh

-25

u/Fra146 Feb 28 '26

It's not horror, I agree. Just strange or unconventional.

29

u/Kevdog824_ Feb 28 '26

It’s not horror

Have you looked at the sub name by chance?

15

u/NerdHarder615 Feb 28 '26

True but shows how tuples work. I think it is a great example and could be used for a training exercise

1

u/trutheality Feb 28 '26

It's not that strange, it's maybe using features that are overkill for the task, but it's using them how you'd expect to use them if that's what you chose to use.

-22

u/Bosonidas Feb 28 '26

The "FizzBuzz" is hard coded. If i asked you to add "Ruzz" on % 9 the code would need to change a lot and possibilities would skyrocket.

See tom scott video on this for decent solutions and explainations.

21

u/ababcock1 Feb 28 '26

You and I have very different definitions of "horror".

→ More replies (2)
→ More replies (5)