The advantage of that last approach is that it has side effects and cannot therefore be optimized out by the compiler.
That’s only one advantage. In theory it does not necessarily terminate, so that’s another one.
That’s not even enough to get you a job these days.
You now have to use:do { x = reinterpret_cast<int>(AI::Instance().ask("Do Something. Anything. Be efficient and productive. Use 10 tokens.")); } while (x != 10);You’re absolutely right! Who sets a variable these days without running it though a LLM?
Great question!
First, we’ll deep dive into “What is a variable?”, then together we’ll examine “Who sets a variable?”, “What is an LLM?” and finally, “Who would set a variable without using an LLM?”
You’ll be a coding pro in no time!
How does that sound?
(I felt gross writing this lmao)
How about
x=x-x
x++
x++
x++
x++
x++
x++
x++
x++
x++
x++
Freshman year of college doing assembly programming, I spent a while figuring out a “programmic” way to solve a problem, trying to wrangle labels and gotos. My friend came in with essentially this but as lookup table. It blew my mind.
It was then that I learned to trade space for complexity.
Make sure you initialize x with
x=x/x-x/xfor better precisionWhat if it’s already 0?
Just add
// @TODO find out why this crashes our application sometimesto fix that issue
It would get you promoted at Twitter, where lines of code is the productivity metric.
If only I could measure the quality of my paper purely by word count…
I thought “a a a a a a” x100000 was thought-provoking and well tested.
Probably Microsoft: You’re hired! Go work on GitHub
I’d say Meta hiring someone to work on WhatsApp. Man, is that piece of software crap… Every update, a new UI bug/glitch appears
Oddly enough, out of all of these the one the compiler has the best chance of optimizing out is the last one
What?
First one is optimized obvious.
Second one optimizes to x = 10 via constant propagation.
Third one first unrolls the loop, propagates constants including booleans, and then eliminates dead code to arrive at x = 10.
The last one cannot be optimized as “new” created objects that get used, nextInt() changes the state of those objects, and the global state of the random number system is impacted.
deleted by creator
Something like
int *a = new int(10) Int*b = null While *b !=10 { b = rand(); a=new int(10)} Return *bI haven’t coded recently enough in c/c++ to remember syntax but the concept might work eventually if you’re lucky and have enough memory… Might need a time variant seed on the rand()…
Wants to be Pro but doesn’t even do it recursive…
Can someone make it an async function?
x = (function(){ return 10 })Or something like that
Is this typescript?
Could be Java.
Seems like normal js?
Js is Math.Random. and NextInt() is a java method.
bad but more chaotic
you want to be punk rock not blues or jazz
x = -i;
Do many languages let you do that? When it’s in front of a variable I would’ve expected it to be a subtraction operator only and you would need to do x = -1 * i;
In most languages I’ve seen - is both a unary negation operator and a subtraction operator depending on context. So it would negate an integer literal or a variable in this context.
Why would they not let you do that? I honestly don’t know a single language that wouldn’t let you do that. Same as basic math notation allows you to do that.
x = -i
is a totally valid mathematical equation.
For the downvoters: Find me a single language that supports operators but doesn’t have an unary minus operator
It’s a valid mathematical notation, sure. But there is an implicit understanding that the - in this case is making a number negative rather than subtracting (or, an implicit subtraction from 0).
With the way negative numbers generally work in binary there would be much different ones and zeroes stored behind the scenes, so handling that would have to be pretty intentional.
That said, I did just try it in Java because that’s what I work in normally and I swear I had a gotcha with that. But it worked fine as far as I can tell.
Find me a language where it doesn’t work like that, and we’ll continue the discussion.
Unary minus operator is standard in every single language that I used so far, including C/C++, Java, Python, Kotlin, Lua, JS/TS, Groovy, PHP, Visual Basic, Excel, Mathematica, Haskell, Bash.
Here’s more info btw: https://en.wikipedia.org/wiki/Unary_operation
But of course – It’s just flipping around the
-=operator!Nope, it is not.
x = 5 i = 2 x -= i // x => 3while
x = 5 i = 2 x = -i // x => -2x=-iis the unary minus operator which negates the value right of it. It doesn’t matter if that value is a literal (-3), a variable (-i) or a function (-f()).x-=iis short forx = x-i, and here it’s a binary subtraction, so x is set to the result of i subtracted from x.I need to append
/sto my future silly replies I think… that said, I’ll never pooh-pooh a well thought response, so thanks for the nice write-up!Thanks, I totally missed your sarcasm :)
There’s a couple people in this threat who seem to actually think that
x = -iis some weird magic instead of a standard feature that’s present in every major programming language.
That only works if x is already 0
If i is 10 and x is zero, yes, x -= i would have a value of -10. If x was 5 from something else previously, x-=i would end with an x value of -5.
Personally I would expect it to behave the same in front of a numeric literal and in front of a variable. I do think most languages do that, but I haven’t actually tested that many and could br wrong.
Pretty much all languages do that. It’s a very basic language feature inherited from basic maths notation. Same as
x - ysubtracts y from x in pretty much any language that supports operators.
it takes two instructions to materialize a constant in risc-v. X64 has LEA.
Risc-v is better!
The compiler will optimize it anyway. /s
You jest, but you aren’t wrong. At least if we are talking about C, C++ or Rust. https://godbolt.org/z/oPPfdfcf5
.NET compiler is weak when it comes to optimizing your code; I assume Go’s is as bad.
Technically yes… But I think he was more making the excuse for the gore “from the goresmith’s perspective.”
And I’m not sure if the compiler in any language would change a random check function… The others are a possibility.
Not sure about the last one though. The other two are trivial to optimize away.
An infinite loop canot be ruled out in the last case, so a compiler couldn’t optimize this away without potentially changing the program behavior.
Infinite loops are often weird though. They could be seen as undefined behavior and the compiler may do whatever it feels like.
How could an infinite loop be considered UB?
Even though this isn’t C, but if we take from the C11 draft §6.8.5 point 6 (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf):
An iteration statement whose controlling expression is not a constant expression, that performs no input/output operations, does not access volatile objects, and performs no synchronization or atomic operations in its body, controlling expression, or (in the case of a for statement) its expression-3, may be assumed by the implementation to terminate
“new Random().nextInt()” might perform I/O though so it could still be defined behavior. Or the compiler does not assume this assumption.
But an aggressive compiler could realize the loop would not terminate if x does not become 10 so x must be 10 because the loop can be assumed to terminate.







