Skip to content

Prefer math.min / math.max

SuggestionAuto-fixableRoblox
small-rules/prefer-math-min-max

Prefer math.min() and math.max() over simple clamp-like ternaries.

Rationale

You can use math.min() and math.max() instead of ternary expressions for clamping values you know. It’ll be more readable, and with Luau’s optimizations, it runs better. All the more reason to use this rule.

Ported from unicorn/prefer-math-min-max. Only fires when the ternary is a plain comparison returning one of the two operands. Won’t catch every pattern, but covers the common ones.

Diagnostic Messages

preferMathMethod
Use `math.min()` or `math.max()` instead of this ternary comparison.

Configuration

This rule does not accept options.

{
"jsPlugins": [
"@pobammer-ts/small-rules"
],
"rules": {
"small-rules/prefer-math-min-max": "error"
}
}

Examples

ternary minimum expression
height > 50 ? 50 : height;

After auto-fix

math.min(height, 50);
math minimum call
math.min(height, 50);