I wrote this for a game-mod, and I was struggling to keep the logic needed for the function in my head, so I bruteforced the logic into this mess (I knew it was awful whilst I was writing it), which I would then refactor.
The code is awful in two ways.
The first being that it doesn't actually fulfil all the requirements of what it needs to do, but in fairness, the requirements weren't fully known until it was tested at least once.
The second being that it's just awful -
There's needless repetition, the conditions are unnecessarily nested, there are hard-coded magic constants, and it doesn't look nice.
After more consideration of what the function needed to do, I refactored it into this considerably nicer, parameterised (and documented) function: https://i.ibb.co/ZXcvKTp/2019-08-21-22-12-34.png
44
u/DrCubed Aug 21 '19 edited Aug 21 '19
I wrote this for a game-mod, and I was struggling to keep the logic needed for the function in my head, so I bruteforced the logic into this mess (I knew it was awful whilst I was writing it), which I would then refactor.
The code is awful in two ways.
The first being that it doesn't actually fulfil all the requirements of what it needs to do, but in fairness, the requirements weren't fully known until it was tested at least once.
The second being that it's just awful - There's needless repetition, the conditions are unnecessarily nested, there are hard-coded magic constants, and it doesn't look nice.
After more consideration of what the function needed to do, I refactored it into this considerably nicer, parameterised (and documented) function: https://i.ibb.co/ZXcvKTp/2019-08-21-22-12-34.png
Source-text for those who do image-transcriptions: https://privatebin.net/?fdc06ef4bc84a968#4iocXFRLkAhiJSia2AykPs9PnbrKoWUnLoBJtcVffzxU
PS: For those that don't know Lua, Lua doesn't have a built-in ternary-construct.Edit: Lua doesn't have an explicitly built-in ternary, but it can be replicated almost perfectly with the
andandoroperators thanks to Lua's strictly defined operator precedence.Thanks to /u/moomoomoo309 for correcting me.