[m-users.] Question about any difference in efficiency on this code...

Richard O'Keefe raoknz at gmail.com
Tue Aug 29 11:16:45 AEST 2023


It doesn't answer your question, but if your game is any good,
some day it will need maintaining.  I have recently done some
maintenance work on code I wrote 20 years ago, and it was as
if it had been written by someone else.  (I guess I'm just not
smart to keep the structure and justification of several hundred
thousand lines of code in my head for decades.)  So readability
is an issue, even if you never expect anyone else to read it.
And for me, the first version is just so much more readable than
the second version that I'd prefer it even if it was slower.
You can tell at a glance what is the same every time and what
is not the same.

There's a rule of thumb I picked up from the Smalltalk
community: if all the branches of a conditional have a
common pattern, move it out so that you can see it and
make sure it *has* to be the same.  It's a rule of thumb.
There can be reasons to violate it.  But I find that it
generally makes my code better.  (And boy do I need help
with that.)


On Sat, 26 Aug 2023 at 22:13, Sean Charles (emacstheviking) <
objitsu at gmail.com> wrote:

> I started out with this:
>
>     get_random_value(0, 2, V, !IO),
>     ( if V = 0 then
>         Speed = 0.25, Color = color(gray)
>     else if V = 1 then
>         Speed = 0.75, Color = color(skyblue)
>     else
>         Speed = 1.25, Color = color(beige)
>     ),
>     Star = star(X, StarY, Speed, to_rgba(Color)).
>
> end then, for some half0baked reason regarding not creating Speed and
> Color but instead directly returning Star...
>
>     get_random_value(0, 2, V, !IO),
>     ( if V = 0 then
>         Star = star(X, StarY, 0.25, to_rgba(color(gray)))
>     else if V = 1 then
>         Star = star(X, StarY, 0.75, to_rgba(color(skyblue)))
>     else
>         Star = star(X, StarY, 1.25, to_rgba(color(beige)))
>     ).
>
>
> So, is there any real difference, or did I do something good / bad /
> indifferent at best?
> Yes, I know it's a nit picking question but I am curious to know the
> relative speed / efficiencies , I am writing a game after all so being
> 1980-s paranoid, every cycle counts. I have '-O4' level optimisation in my
> build.
>
> :D
>
> Thanks
> Sean.
>
> _______________________________________________
> users mailing list
> users at lists.mercurylang.org
> https://lists.mercurylang.org/listinfo/users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20230829/fd00079f/attachment-0001.html>


More information about the users mailing list