Page 1 of 1
Best practice for selector switch
Posted: Thursday 24 August 2017 13:43
by elmortero
I would like some advice for the best practice in LUA/dzVents for selector switch when there are a lot of levels.
Now I use this
- Spoiler: show
- local ubi = (domoticz.devices('Ubica').rawData[1])
if ubi == '10' then zmw = tostring('08360') end
if ubi == '20' then zmw = tostring('08085') end
if ubi == '30' then zmw = tostring('07510') end
if ubi == '40' then zmw = tostring('07130') end
if ubi == '50' then zmw = tostring('07037') end
if ubi == '60' then zmw = tostring('06431') end
if ubi == '70' then zmw = tostring('06431') end
if ubi == '80' then zmw = tostring('07385') end
but cannot help but thinking there is a nicer/better way for this.
I guess I am looking for a way of avoiding all the if then end
Re: Best practice for selector switch
Posted: Friday 08 September 2017 8:49
by BakSeeDaa
I saw that your post had no answers so I thought I should give it a try to answer to it.
I believe best practice is to code it so that it's readable and self explaining. Your own example is not so bad in that aspect but you can skip one set of parenthesis on the initialization of ubi and you can make elseif's instead of if's following the first if.
Then it could look like this:
Code: Select all
local ubi = domoticz.devices('Ubica').rawData[1]
if ubi == '10' then zmw = tostring('08360')
elseif ubi == '20' then zmw = tostring('08085')
elseif ubi == '30' then zmw = tostring('07510')
elseif ubi == '40' then zmw = tostring('07130')
elseif ubi == '50' then zmw = tostring('07037')
elseif ubi == '60' then zmw = tostring('06431')
elseif ubi == '70' then zmw = tostring('06431')
elseif ubi == '80' then zmw = tostring('07385') end
If your goal is to avoid multiple if statements you could use ternary operators, that is if there was such a thing in Lua. But even though Lua lacks ternary operators you can closely approximate it by combining the and and or binary operators.
That is, you may do an attempt to obfuscate your code like this
Code: Select all
local ubi = tonumber(domoticz.devices('Ubica').rawData[1])
local zmw = ubi==10 and '08360' or ubi==20 and '08085' or ubi==30 and '07510' or ubi==40 and '07130' or ubi==50 and '07037' or ubi==60 and '06431' or ubi==70 and '06431' or ubi==80 and '07385' or '?????'
The question is what you gain on doing it that way.
You will probably have more and better answers if you post your question in the
Lua forum.
Re: Best practice for selector switch
Posted: Friday 08 September 2017 16:02
by BakSeeDaa
elmortero wrote: Thursday 24 August 2017 13:43
I would like some advice for the best practice in LUA/dzVents for selector switch when there are a lot of levels.
Here is a variant of achieving the same result. 2 lines of code and an additional line for logging.
Code: Select all
local t = {[10]='08360', [20]='08085', [30]='07510', [40]='07130', [50]='07037', [60]='06431', [70]='06431', [80]='07385'}
local zmw = t[tonumber(domoticz.devices('Ubica').rawData[1])]
domoticz.log('The new value of zmw is: '..(zmw and zmw or 'nil'), domoticz.LOG_FORCE)
What do you think about that?
Re: Best practice for selector switch
Posted: Friday 08 September 2017 16:17
by elmortero
BakSeeDaa wrote: Friday 08 September 2017 16:02
Here is a variant of achieving the same result. 2 lines of code and an additional line for logging.
- Spoiler: show
-
local t = {[10]='08360', [20]='08085', [30]='07510', [40]='07130', [50]='07037', [60]='06431', [70]='06431', [80]='07385'}
local zmw = t[tonumber(domoticz.devices('Ubica').rawData[1])]
domoticz.log('The new value of zmw is: '..(zmw and zmw or 'nil'), domoticz.LOG_FORCE)
What do you think about that?
This is a very elegant way of doing it.
Thank you very much for digging into that.