News for hackers. Stuff that matters.
Re:TerrariaClone – An incomprehensible hellscape of spaghett... by yathern on Thursday October 12, 2017 @04:52PM (#15461233)
Re:TerrariaClone – An incomprehensible hellscape of spaghett... by bcbrown on Thursday October 12, 2017 @05:52PM (#15461606)
Re:TerrariaClone – An incomprehensible hellscape of spaghett... by danbruc on Thursday October 12, 2017 @06:29PM (#15461819)
That is probably (x + 1) % 10 assuming x is non-negative. That kind of code is responsible for quite a bit of the verbosity, the author was obviously not aware of many of the little tricks usually used in this kind of code.
Re:TerrariaClone – An incomprehensible hellscape of spaghett... by raxod502 on Thursday October 12, 2017 @08:53PM (#15462580)
Original author here.

It's actually a method to convert from the index of an inventory hotkey slot into the keystroke used to access it -- there were ten hotkey slots, which were numbered 1–9 and 0 at the end.

But I like all the other interesting interpretations here :P (They all assume way more knowledge than I had at the time.)

Re:TerrariaClone – An incomprehensible hellscape of spaghett... by baddox on Thursday October 12, 2017 @08:29PM (#15462486)
Also assuming that x <= 9.
Re:TerrariaClone – An incomprehensible hellscape of spaghett... by hacking_again on Thursday October 12, 2017 @06:30PM (#15461821)
Well, it's faster than %, but I bet it's premature.
Re:TerrariaClone – An incomprehensible hellscape of spaghett... by CydeWeys on Thursday October 12, 2017 @08:03PM (#15462372)
Why do you say it's faster? It's guaranteed to fail branch prediction one out of ten times. My guess is that'd be a lot slower than using the integer modulo operator, which is not an expensive operation.
Re:TerrariaClone – An incomprehensible hellscape of spaghett... by hacking_again on Thursday October 12, 2017 @08:35PM (#15462513)
On their own, % and / are way slower than +, -, *, <<, >>. Cycle counts depend on your architecture, you can look them up. That mod is so slow and should be avoided is a kind of folklore based in truth - kind of like function calls being slow - but like everything time-sensitive the mistake lies in not profiling before (manual) optimization.

There's an example on SO, I got similar results just now when I replicated it:

https://stackoverflow.com/questions/15596318/is-it-better-to...

It doesn't matter on my machine whether the divisor is 10 or 42 (as in the example), the branching is way faster. Now, maybe if the branching were not in a loop and hence not so easily predicted, it wouldn't make a difference. But if this code is not being used in a loop, optimization may be premature anyway (as indicated in my original comment).

Probably f() has something to do inside the main game loop and gets called on a bunch of objects every frame. I haven't looked at the code enough to know if that's a bottleneck.

Re:TerrariaClone – An incomprehensible hellscape of spaghett... by 35bge57dtjku on Thursday October 12, 2017 @10:10PM (#15462819)
> On their own, % and / are way slower than +, -, *, <<, >>.

And the branch instruction is free??

Re:TerrariaClone – An incomprehensible hellscape of spaghett... by hacking_again on Thursday October 12, 2017 @11:40PM (#15463117)
More or less, assuming you can predict it. But there's a penalty for misprediction. So it boils down to whether using % frequently (either as a native instruction or as a sequence of instructions) is more or less expensive than predicting a branch frequently, given a certain misprediction rate.
Re:TerrariaClone – An incomprehensible hellscape of spaghett... by 35bge57dtjku on Saturday October 14, 2017 @05:18PM (#15474369)
> More or less, assuming you can predict it. But there's a penalty for misprediction.

Yeah, no, that's not free.

Re:TerrariaClone – An incomprehensible hellscape of spaghett... by hacking_again on Saturday October 14, 2017 @07:13PM (#15474851)
Ok, so what changes would you make to my explanations?
Re:TerrariaClone – An incomprehensible hellscape of spaghett... by CUViper on Thursday October 12, 2017 @08:40PM (#15462531)
At least in C++, GCC and Clang will both use a CMOVE: https://godbolt.org/g/W1GWyP
Re:TerrariaClone – An incomprehensible hellscape of spaghett... by sparky_ on Thursday October 12, 2017 @06:04PM (#15461665)
My favorite part is the overloaded print methods: https://github.com/raxod502/TerrariaClone/blob/master/src/Te...
Re:TerrariaClone – An incomprehensible hellscape of spaghett... by js2 on Thursday October 12, 2017 @11:11PM (#15463029)
Maybe those are stubs the author intended to do something else with later?
Re:TerrariaClone – An incomprehensible hellscape of spaghett... by raxod502 on Friday October 13, 2017 @01:49AM (#15463521)
Original author here.

I can confirm that the actual reason is that at the time I thought typing "print" instead of "System.out.println" was a great idea.

Re:TerrariaClone – An incomprehensible hellscape of spaghett... by js2 on Friday October 13, 2017 @03:10AM (#15463744)
In that case, I think you might have been happier in a language other than Java. :-)
Re:TerrariaClone – An incomprehensible hellscape of spaghett... by userbinator on Friday October 13, 2017 @06:30AM (#15464434)
That's actually not such a bad idea, since you seem to have discovered one of the traits of good abstraction: it makes code more concise.
Re:TerrariaClone – An incomprehensible hellscape of spaghett... by bitexploder on Friday October 13, 2017 @02:44AM (#15463677)
It always was rather onnerous.
Re:TerrariaClone – An incomprehensible hellscape of spaghett... by taw55 on Friday October 13, 2017 @12:21PM (#15466996)
It IS a great idea!
Re:TerrariaClone – An incomprehensible hellscape of spaghett... by alexwebb2 on Thursday October 12, 2017 @04:54PM (#15461250)
I'm sure there was a good reason for that.
Re:TerrariaClone – An incomprehensible hellscape of spaghett... by Nition on Thursday October 12, 2017 @06:54PM (#15461986)
I'm not so sure. In the readme he says there are over 500 cases of "unnecessary boxing."
Re:TerrariaClone – An incomprehensible hellscape of spaghett... by Agathos on Thursday October 12, 2017 @09:33PM (#15462707)
Raise your hand if you were excited when you heard Java 1.5 would introduce autoboxing.
Re:TerrariaClone – An incomprehensible hellscape of spaghett... by SuddsMcDuff on Thursday October 12, 2017 @07:04PM (#15462052)
A textbook example of a quasi-class: http://www.idinews.com/quasiClass.pdf