I give up, min / max just doesn't fit my brain

Discuss things related to game programming and programming languages
Post Reply
User avatar
celes
Posts: 69
Joined: Sun Feb 15, 2026 8:09 pm
Location: The offices at Carrot Corp
Pronouns: she/they

I give up, min / max just doesn't fit my brain

Post by celes »

It's very common when writing game code to want to express the idea that this variable saturates at a certain value, for example:

Code: Select all

this.x += Time.deltaTime;
if (this.X > maxValue) {
    this.X = maxValue;
}
Now you see how my brain likes to think in terms of the "maximum value" this variable is allowed to have, so trying to get more concise code, I reach for the aptly named....

Code: Select all

this.x = Min(this.x + Time.deltaTime, maxValue)
Min, :akko_nope: yes, I have to use min to set a max value and max to set a min value. This breaks my brain, I've known for years, and I still have to stop and think about it every time.

I give up, I'm throwing the towel, I have defined these functions and will be using them from now on

Code: Select all

float AtMost(float a, float b) => Min(a, b);
float AtLeast(float a, float b) => Max(a, b);
 

POSTREACT(ions) SUMMARY

User avatar
Kampffrosch
Posts: 7
Joined: Fri Feb 20, 2026 12:56 pm

Re: I give up, min / max just doesn't fit my brain

Post by Kampffrosch »

I can deal with

Code: Select all

min(a,b)
but I always read

Code: Select all

a.min(b)
as "a should be at least b"
 

POSTREACT(ions) SUMMARY

User avatar
celes
Posts: 69
Joined: Sun Feb 15, 2026 8:09 pm
Location: The offices at Carrot Corp
Pronouns: she/they

Re: I give up, min / max just doesn't fit my brain

Post by celes »

Kampffrosch wrote: Wed May 13, 2026 12:36 pm I always read

Code: Select all

a.min(b)
as "a should be at least b"
Yes, the method syntax makes min/max far worse. But at_least / at_most would read so nice in method syntax... I'm gonna add this and see if I end up using it! :nkoThink:
 

POSTREACT(ions) SUMMARY

User avatar
Sugui
Posts: 19
Joined: Tue Feb 17, 2026 12:50 pm
Location: Europe
Pronouns: she/her

Re: I give up, min / max just doesn't fit my brain

Post by Sugui »

I have another suggestion!

Code: Select all

clampMax(x, to) = min(x, to)
clampMin(x, to) = max(x, to)
... or even :blobcatgiggle:

Code: Select all

clamp(x, min=Float.MIN_VALUE, max=Float.MAX_VALUE) = clamp(x, min, max)
so you can write

Code: Select all

this.x = clamp(this.x + Time.deltaTime, max=maxValue)
 

POSTREACT(ions) SUMMARY

User avatar
celes
Posts: 69
Joined: Sun Feb 15, 2026 8:09 pm
Location: The offices at Carrot Corp
Pronouns: she/they

Re: I give up, min / max just doesn't fit my brain

Post by celes »

Sugui wrote: Wed May 13, 2026 1:33 pm ... or even :blobcatgiggle:

Code: Select all

clamp(x, min=Float.MIN_VALUE, max=Float.MAX_VALUE) = clamp(x, min, max)
so you can write

Code: Select all

this.x = clamp(this.x + Time.deltaTime, max=maxValue)
Ohhhh I read this before during the stream and didn't realize what you meant. You mean clampMax for min and clampMin for max! Yeah that's cool :hammyeyes:
 

POSTREACT(ions) SUMMARY

Post Reply