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

Ok, so you can just 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 will run better! All the more reason to use this rule.

Ported from unicorn/prefer-math-min-max. Only fires when the ternary is a simple comparison returning one of the two operands. Won’t catch every pattern, but covers the obvious 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);