# How to simplify this code?

**URL:** <https://discourse.julialang.org/t/how-to-simplify-this-code/81734>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 26, 2022, 5:20pm UTC](https://discourse.julialang.org/t/how-to-simplify-this-code/81734 "2022-05-26T17:20:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [May 26, 2022, 5:20pm UTC](https://discourse.julialang.org/t/how-to-simplify-this-code/81734/1 "2022-05-26T17:20:29Z")

</div>

I have the following type:

```julia
mutable struct JSButtonState
    btn1::Observable{Bool}
    btn2::Observable{Bool}
    btn3::Observable{Bool}
    btn4::Observable{Bool}
    btn5::Observable{Bool}
    btn6::Observable{Bool}
    btn7::Observable{Bool}
    btn8::Observable{Bool}
    btn9::Observable{Bool}
    btn10::Observable{Bool}
    btn11::Observable{Bool}
    btn12::Observable{Bool}
end

```

And use the following code to set the Observables from the array `buttons`, which is an array of UInt8 values that are zero or one:

```julia
            if ! isnothing(jsbuttons)
                buttons = GLFW.GetJoystickButtons(js.device)
                if js.button_count >= 1  
                    if jsbuttons.btn1[] != (buttons[1] != 0)
                        jsbuttons.btn1[] = (buttons[1] != 0)
                    end
                end
                if js.button_count >= 2  
                    if jsbuttons.btn2[] != (buttons[2] != 0)
                        jsbuttons.btn2[] = (buttons[2] != 0)
                    end
                end
                if js.button_count >= 3  
                    if jsbuttons.btn3[] != (buttons[3] != 0)
                        jsbuttons.btn3[] = (buttons[3] != 0)
                    end
                end
                if js.button_count >= 4  
                    if jsbuttons.btn4[] != (buttons[4] != 0)
                        jsbuttons.btn4[] = (buttons[4] != 0)
                    end
                end
                ... # there are 12 buttons or more 
           end

```

This are lots of lines of code…

Any idea how to simplify the code without changing the type JSButtonState ?

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [May 26, 2022, 5:28pm UTC](https://discourse.julialang.org/t/how-to-simplify-this-code/81734/2 "2022-05-26T17:28:56Z")

</div>

> [@ufechner7](#):
>
> without changing the type JSButtonState ?

Would be simpler and extensible to put them in a vector.

Macro to generate the if blocks?

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [May 26, 2022, 5:30pm UTC](https://discourse.julialang.org/t/how-to-simplify-this-code/81734/3 "2022-05-26T17:30:53Z")

</div>

How about this:

```julia
for (i, b) in Iterators.take(enumerate(buttons), fieldcount(JSButtonState))
    field = Symbol("btn$(i)")
    obs = getfield(jsbuttons, field)
    new = (b != 0)
    if obs.val != new
        obs[] = new
    end
end

```

Also, for this code, there is no need for JSButtons to be a mutable struct, you are not changing out the Observable just modifying its stored value. Inner Mutability does not require a mutable struct.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [May 26, 2022, 5:52pm UTC](https://discourse.julialang.org/t/how-to-simplify-this-code/81734/4 "2022-05-26T17:52:28Z")

</div>

@contradict I will try your suggestion! Thanks a lot! 😃

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [May 26, 2022, 6:34pm UTC](https://discourse.julialang.org/t/how-to-simplify-this-code/81734/5 "2022-05-26T18:34:23Z")

</div>

> [@contradict](#):
>
> `for (i, b) in Iterators.take(enumerate(buttons), fieldcount(JSButtonState))`

Could you please explain a bit what this does?

Without totally understanding it, would be tempted to replace it by:  
`for (i, b) in pairs(buttons)`

Thank you.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [May 26, 2022, 6:48pm UTC](https://discourse.julialang.org/t/how-to-simplify-this-code/81734/6 "2022-05-26T18:48:37Z")

</div>

I think a straightforward translation would be “Construct an (ordinal index, value) tuple for each entry in buttons and return at most the same number of these as their are fields in `JSButtonState`”

`pairs` and `enumerate` do the same thing for an array, so that would be a valid substitution. I wanted to ensure a number since the field name construction needs a number, so `enumerate` seemed more natural. `take` is there to prevent generating more field names than exist in `JSButtonState` . Another way to do this would be to add a test and break to the loop.
