this post was submitted on 22 Oct 2023
600 points (95.6% liked)

Programmer Humor

31800 readers
207 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
top 50 comments
sorted by: hot top controversial new old
[–] [email protected] 131 points 10 months ago (3 children)

Why would you use anything other than Math.max?

[–] [email protected] 80 points 10 months ago

Some of us have trust issues. Or worked with Java.

Which, now that I think about it, comes to the same thing.

[–] [email protected] 17 points 10 months ago* (last edited 10 months ago) (1 children)

Well, the question sort of implies that you're needing to implementing Math.max yourself, for whatever reason. Probably as an exercise. It doesn't make sense to reuse a library that implements the feature if you're explicitly being asked how you would implement it yourself.

[–] [email protected] 14 points 10 months ago (1 children)

This is why I think school and interviews are like a whole different universe from the one where actual work gets done.

[–] [email protected] 7 points 10 months ago (4 children)

In some ways they can be wholly different, but I don't think this is a good example of it.

Any programmer who cannot implement "take two numbers, return the larger one" is clearly not very competent. Even though you're never going to literally need to implement Math.max yourself at work, you are going to need basically the same types of skills. Probably 95% of the work I do day-to-day is stuff you'd learn in your first year at uni, and this just shows that you've got that ability.

In practice, the best interviews I've had usually set a slightly more complicated task as a do-in-your-own-time problem and then go through what you did in the actual interview. Problems like "read a list of names in the form , each name on a separate line, from a text file. Sort the names by last name, then by other names. Output to another text file. Include unit tests." They wouldn't then expect you to re-implement the sorting algorithm itself, but more want to look at the quality of code, extensibility, etc.

More basic questions like the one in the OP, or fizzbuzz, are decent as well, and a big step up from lame questions like "what does SOLID stand for? What does the Liskov substitution principle mean to you?" Even if they're not quite as valuable as a miniature project.

load more comments (4 replies)
load more comments (1 replies)
[–] [email protected] 72 points 10 months ago

TDD

const max12 = (x, y) => {
    if (x === 1 && y === 2) {
        return 2;
    } else if (x === 7 && y === 4) {
        return 7;
    } else {
        return x;
    }
};
[–] [email protected] 61 points 10 months ago (2 children)

Thief. Writing code is for chumps, and the more code you right, the more of a chump you are.

[–] [email protected] 30 points 10 months ago (1 children)

why say many word when few do trick

[–] independantiste 8 points 10 months ago* (last edited 10 months ago)

Why 🗣️📈 word when 😃👍

[–] [email protected] 11 points 10 months ago (1 children)

Writing code is for chumps, and the more code you right, the more of a chump you are.

So you're the one in there wronging up my code?

[–] [email protected] 7 points 10 months ago* (last edited 10 months ago)

It’s too late now to wright my wrongh ¯\_(ツ)_/¯

[–] [email protected] 48 points 10 months ago (1 children)

Where's the Julia programmer that hits every one of these with @benchmark and then works for six hours to shave three nanoseconds off of the fastest one?

(Example: https://discourse.julialang.org/t/faster-bernoulli-sampling/35209)

[–] [email protected] 5 points 10 months ago (1 children)
load more comments (1 replies)
[–] [email protected] 28 points 10 months ago (5 children)

Mathematician 2 kinda blew my mind, kinda obvious, just can't believe I was never taught or thought about it.

load more comments (5 replies)
[–] [email protected] 24 points 10 months ago

Procrastinator + troll.

return x

[–] [email protected] 24 points 10 months ago

Bit hacker 2 is really fascinating. It uses a bit mask of all 1s (-1) or all 0s (0) and takes advantage of the fact that y ^ (x ^ y) = x and y ^ 0 = y

[–] [email protected] 23 points 10 months ago (1 children)
[–] [email protected] 7 points 10 months ago

Thief gang. Why stand on shoulders of giants if you're not using it to your advantage?

[–] [email protected] 21 points 10 months ago* (last edited 10 months ago) (9 children)

wtf kind of cursed programming language is this? JS? it's so ugly, in no universe should a function look like that

but obviously as a rust enjoyer i have to do it like

fn max ⟨T: PartialOrd + Copy⟩(nums: ⁊[T]) -> Option⟨T⟩ {
    let mut greatest: ⁊T = ⁊nums[0];
    match nums.len() {
        0 => None,
        1 => Some(*greatest),
        _ => {
            for num in nums {
                if num > greatest {
                    greatest = num;
                }
            }
            Some(*greatest)
        }
    }
}

edit: lemmy formatting REALLY hates references and generics it seems... time to go back to medieval times

load more comments (9 replies)
[–] [email protected] 19 points 10 months ago (2 children)
#define max(x,y) ( { __auto_type __x = (x); __auto_type __y = (y); __x > __y ? __x : __y; })

GNU C. Also works with Clang. Avoids evaluating the arguments multiple times. The optimizer will convert the branch into a conditional move, if it doesn't I'd replace the ternary with the "bit hacker 2" version.

load more comments (2 replies)
[–] [email protected] 19 points 10 months ago* (last edited 10 months ago) (8 children)

Mathematician 3

Max(x, y) = floor(ln(e^x + e^y))

load more comments (8 replies)
[–] [email protected] 12 points 10 months ago

Master Thief const { Max: max } = Math;

[–] [email protected] 11 points 10 months ago

One, two and nine. Depending on the project, depending on the requirements.

[–] [email protected] 11 points 10 months ago (1 children)

Engineer I guess... Thief is the objectively better enterprise programmer option but I don't know why I always forget about it and just write a ternary ¯⁠\⁠_⁠(⁠ツ⁠)⁠_⁠/⁠¯

[–] mindbleach 11 points 10 months ago

JS instills distrust. Rely on built-in sort? It's alphanumeric. Rely on built-in reverse? It modifies your array. Copy an array? No you didn't.

Yeah no kidding we bang out a kata in a dozen characters. I don't even believe in order of operations at this point. I've been routinely betrayed.

[–] [email protected] 10 points 10 months ago (2 children)

Reminded of how truly little I know about programming despite the time have spent doing it

Ugh. I'll never be any good.

[–] [email protected] 11 points 10 months ago

Listen, in industry programming (and for personal projects if you want to get them done), the thief is the way to go. By all means, challenge yourself to understand each of these functions, but 99% of day to day development will not look like this.

[–] pomodoro_longbreak 6 points 10 months ago (2 children)

How much time have you spent doing it? What part didn't you understand? If it's the bit shifting stuff, don't worry about it - hardly anyone actually knows how that works unless they look it up.

load more comments (2 replies)
[–] DumbAceDragon 10 points 10 months ago

Either engineer or bit hacker, depending on whether or not I'm trying to avoid branching.

[–] [email protected] 9 points 10 months ago* (last edited 10 months ago) (1 children)

And then your customer changes their mind. Instead of two numbers, they will now input three numbers. How easy will it be for you to change your code?

And then the customer changes their mind. Instead of three numbers, they will now input any series of numbers. How easy will it be for you to change your code? And why didn't you already do this is the previous step?

And then the customer changes their mind. Instead of any set of numbers, they will now input numbers and text. How easy will it be to change your code?

And then the customer changes their mind. They now have no idea of what they're sending you or if they're even sending you anything. Nevermind the code now, you already did that in the previous step, right? How easy will it be to explain what you're invoicing them for?

[–] [email protected] 7 points 10 months ago (1 children)

How easy will it be to explain what you're invoicing them for?

By the hour I presume

load more comments (1 replies)
[–] [email protected] 7 points 10 months ago* (last edited 10 months ago)

Otherwise, realistically, I’m prob the worst of all worlds … the procrastinator waiting/hoping to be the pair programmer that has hopefully remembered to just be the thief.

[–] [email protected] 7 points 10 months ago

I'm the first and the last, it depends on my mood

[–] [email protected] 6 points 10 months ago* (last edited 10 months ago)

If thief is actually an option, then thief.

Otherwise probably procreator or engineer 😅😬

Edit: errrrr that was supposed to say "procrastinator" 😬dyac

[–] [email protected] 6 points 10 months ago

I’m number 11.

[–] [email protected] 6 points 10 months ago

max6(1, 2, 3)

Man that's going to cause some headaches...

[–] [email protected] 5 points 10 months ago

I'm in this post and I'm offended.

[–] [email protected] 5 points 10 months ago

Thief, or Procrastinator.

[–] [email protected] 4 points 10 months ago

I use 8, but only when I'm operating on unsigned longs.

load more comments
view more: next ›