Why would you use anything other than Math.max?
Programmer Humor
Post funny things about programming here! (Or just rant about your favourite programming language.)
Rules:
- Posts must be relevant to programming, programmers, or computer science.
- No NSFW content.
- Jokes must be in good taste. No hate speech, bigotry, etc.
Some of us have trust issues. Or worked with Java.
Which, now that I think about it, comes to the same thing.
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.
This is why I think school and interviews are like a whole different universe from the one where actual work gets done.
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.
Thief way is actually the best among all of these imo, in terms of readability and efficiency.
Not using thief is professional incompetence unless you're doing something deeply cursed
Like pair programming.
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.
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
well it's called Thief. They're stealing the function and making it look like they wrote it. hence max1
.
TDD
const max12 = (x, y) => {
if (x === 1 && y === 2) {
return 2;
} else if (x === 7 && y === 4) {
return 7;
} else {
return x;
}
};
Thief. Writing code is for chumps, and the more code you right, the more of a chump you are.
why say many word when few do trick
Why 🗣️📈 word when 😃👍
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?
It’s too late now to wright my wrongh ¯\_(ツ)_/¯
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)
Mathematician 2 kinda blew my mind, kinda obvious, just can't believe I was never taught or thought about it.
Thief no doubt
Thief gang. Why stand on shoulders of giants if you're not using it to your advantage?
Procrastinator + troll.
return x
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
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
#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.
Procrastinator.
Okay, but seriously: "Thief". Why reimplement it if it’s already available in the language?
Master Thief const { Max: max } = Math;
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 ¯\_(ツ)_/¯
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.
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?
How easy will it be to explain what you're invoicing them for?
By the hour I presume
One, two and nine. Depending on the project, depending on the requirements.
Either engineer or bit hacker, depending on whether or not I'm trying to avoid branching.
Reminded of how truly little I know about programming despite the time have spent doing it
Ugh. I'll never be any good.
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.
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.
If thief is actually an option, then thief.
Otherwise probably procreator or engineer 😅😬
Edit: errrrr that was supposed to say "procrastinator" 😬dyac
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.
I'm the first and the last, it depends on my mood
I'm in this post and I'm offended.
max6(1, 2, 3)
Man that's going to cause some headaches...
I’m number 11.