Thanks. I was searching the inspection profile for "constant" or "math" but could not find one. But clicking Alt+Enter on the operator shows the quickfix - weird.
It is not a quick fix but an intention. You can find it in the intention setings.
Bas
Tom wrote:
Thanks. I was searching the inspection profile for "constant" or "math" but could not find one. But clicking Alt+Enter on the operator shows the quickfix - weird.
Yes! You can do this quickly with JavaScript or a small script in Python that evaluates simple arithmetic expressions inside function calls. Basically, you parse the arguments, evaluate them, and rebuild the call.
I don't think any exists, but if you compute the value like this, I believe it's better to keep the computation in place (for clarity purposes).
What I would do is create constants
public static int MY_CONSTANT = 1+2;
and use those in your code, this way you have both the explanation of the value, and the optimization (not computing it every time)
Hello Tom,
AFAIK, there's a "Compute constant value" intention available on each operator sign.
Sascha
Thanks. I was searching the inspection profile for "constant" or "math" but
could not find one. But clicking Alt+Enter on the operator shows the
quickfix - weird.
Tom
I've inlined a constant ...
It is not a quick fix but an intention. You can find it in the intention
setings.
Bas
Tom wrote:
Thanks (again). Did not know intentions and inspections were separate things.
Tom
Yes! You can do this quickly with JavaScript or a small script in Python that evaluates simple arithmetic expressions inside function calls. Basically, you parse the arguments, evaluate them, and rebuild the call.
Here’s a JavaScript quickfix:
function quickEvalArgs(str) { return str.replace(/\(([^()]+)\)/g, (match, args) => { // split by commas, trim, evaluate each const evaluated = args .split(',') .map(arg => eval(arg.trim())) .join(', '); return `(${evaluated})`; }); } const input = "foo(1 + 2, 3 + 4 * 5);"; const output = quickEvalArgs(input); console.log(output); // foo(3, 23);