this post was submitted on 22 Oct 2023
632 points (95.8% liked)

Programmer Humor

32558 readers
564 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] 135 points 1 year ago (3 children)

Why would you use anything other than Math.max?

[–] [email protected] 82 points 1 year ago (1 children)

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

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

load more comments (1 replies)
[–] [email protected] 17 points 1 year ago* (last edited 1 year 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] 15 points 1 year 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] 8 points 1 year 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] 93 points 1 year ago (2 children)

Thief way is actually the best among all of these imo, in terms of readability and efficiency.

[–] [email protected] 93 points 1 year ago (12 children)

Not using thief is professional incompetence unless you're doing something deeply cursed

[–] [email protected] 22 points 1 year ago

Like pair programming.

load more comments (11 replies)
[–] snowe 21 points 1 year ago (2 children)

They’re setting a variable to a function. Just use the original function. All thief does is obfuscate for literally no gain except character count.

[–] [email protected] 14 points 1 year ago (2 children)

I presumed it to be a standin for just directly using Math.max, since there's no nice way to show that in a valid code snippet

[–] snowe 7 points 1 year ago

well it's called Thief. They're stealing the function and making it look like they wrote it. hence max1.

load more comments (1 replies)
load more comments (1 replies)
[–] [email protected] 77 points 1 year 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] 70 points 1 year ago (2 children)

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

[–] [email protected] 32 points 1 year ago (1 children)

why say many word when few do trick

[–] [email protected] 8 points 1 year ago* (last edited 1 year ago)

Why 🗣️📈 word when 😃👍

[–] [email protected] 13 points 1 year 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] 9 points 1 year ago* (last edited 1 year ago)

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

[–] [email protected] 49 points 1 year ago (2 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)

load more comments (2 replies)
[–] [email protected] 31 points 1 year 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] 28 points 1 year ago (1 children)
[–] [email protected] 8 points 1 year ago

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

[–] [email protected] 24 points 1 year ago

Procrastinator + troll.

return x

[–] [email protected] 24 points 1 year 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] 22 points 1 year 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] 22 points 1 year ago* (last edited 1 year 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] 20 points 1 year ago* (last edited 1 year ago) (8 children)

Mathematician 3

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

load more comments (8 replies)
[–] [email protected] 16 points 1 year ago

Procrastinator.

Okay, but seriously: "Thief". Why reimplement it if it’s already available in the language?

[–] [email protected] 14 points 1 year ago (1 children)
load more comments (1 replies)
[–] [email protected] 13 points 1 year ago

Master Thief const { Max: max } = Math;

[–] [email protected] 12 points 1 year 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 ¯⁠\⁠_⁠(⁠ツ⁠)⁠_⁠/⁠¯

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

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.

load more comments (1 replies)
[–] [email protected] 11 points 1 year ago

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

[–] [email protected] 11 points 1 year ago

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

[–] [email protected] 10 points 1 year ago* (last edited 1 year 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] 8 points 1 year 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] 10 points 1 year 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 1 year 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.

[–] [email protected] 6 points 1 year 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)
[–] drew_belloc 10 points 1 year ago

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

[–] [email protected] 9 points 1 year ago* (last edited 1 year 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] 8 points 1 year ago* (last edited 1 year 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] 6 points 1 year ago

I’m number 11.

[–] peyotecosmico 6 points 1 year ago

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

[–] [email protected] 6 points 1 year ago

max6(1, 2, 3)

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

[–] [email protected] 5 points 1 year ago (1 children)

@coja I am the engineer because I forget about Math.max existence

load more comments (1 replies)
[–] [email protected] 5 points 1 year ago

Max11 is all my code. Why doesn't it work????🤔

[–] [email protected] 5 points 1 year ago (1 children)

Last one should be // still a student

load more comments (1 replies)
load more comments
view more: next ›