Page 1 of 2

How to do better programming?

Posted: Wednesday 29 November 2017 8:55
by ensingg
Hi
A few months ago i started with domoticz and lua.
Now i have 1 script that is now over 400 lines with some functions and a lot of if devicechanged statements.

I just read about a script_devicename.lua. Should i split my script in a lot of these scripts?

Probably better because of the total time in the main script?
Will the scripts be fired parallel ?

Please help me in improving the programming quality.

best regards

Re: How to do better programming?

Posted: Wednesday 29 November 2017 9:23
by DutchHans
Hi, it would make sense if you show it...
Regards, Hans

Re: How to do better programming?

Posted: Wednesday 29 November 2017 10:28
by ensingg
I think this is a global thing, not dependent on the code itself.
Is it better to have scripts per device or just have one script with a lot of if devicechanged statements.

Re: How to do better programming?

Posted: Wednesday 29 November 2017 14:22
by dannybloe
First of all, switch to dzVents.. it will significantly reduce the amount of code you write and it's a lot easier and better to read (and documented).

Re: How to do better programming?

Posted: Wednesday 29 November 2017 15:24
by dutchdevil83
dannybloe wrote: Wednesday 29 November 2017 14:22 First of all, switch to dzVents.. it will significantly reduce the amount of code you write and it's a lot easier and better to read (and documented).
But does it execute faster?

I have one "master" script for device and one "master" script for time. In that script i call my own function library that i wrote. Before i had one script for each device or action but i think this will slow things down more then one master script. I was thinking of migrating to dzVents but for now i don`t see why i should.

Re: How to do better programming?

Posted: Wednesday 29 November 2017 15:28
by dannybloe
Yeah.. you asked for how do better programming? Creating one big file doesn't help much in that direction. Better to create small scripts, easier to manage, debug, test and maintain.

Re: How to do better programming?

Posted: Wednesday 29 November 2017 17:11
by jannl
dannybloe wrote:First of all, switch to dzVents.. it will significantly reduce the amount of code you write and it's a lot easier and better to read (and documented).
I totally do not agree to the part it is better readable.

I now use a container script for lua. The issue with lua in Domoticz is the commandarray. If you use that in every script it takes a lot of time to load all the scripts, because at every device change all device scripts are executed. So if you are a coder I would prefer using LUA and optimize it.

Edit: 1 big script could be a problem when you reach the max time a script is allowed to run.

Re: How to do better programming?

Posted: Wednesday 29 November 2017 18:22
by dannybloe
That’s totally not the case with dzVents.

Re: How to do better programming?

Posted: Wednesday 29 November 2017 18:58
by jannl
Obviously we have different opinions.

Re: How to do better programming?

Posted: Wednesday 29 November 2017 19:30
by dannybloe
Clearly... although I'm more interested in facts.

Re: How to do better programming?

Posted: Wednesday 29 November 2017 19:32
by jannl
I don't think readability can be measured.

Re: How to do better programming?

Posted: Wednesday 29 November 2017 19:40
by dannybloe
Ehm, yes you can. You can ask a group of people to read a piece of code in a limited amount of time and test if they understood the code. You can test that fairly objectively. On top of that, there is good amount of knowledge acquired in the field of software engineering in the past 50 years regarding maintainability and lines of code, testability etc etc all based on experience, long term costs calculation.. that sort of things.
But in the end it's indeed up to you. If it works for you then that's fine with me. I'm just responding to the question of this topic and bring in what I have learned in the past 30+ years of software development.

Re: How to do better programming?

Posted: Wednesday 29 November 2017 20:40
by jannl
If peoples opion is involved it will never be objective....and for that matter, since you mention it, my experience is also 30+ years.

But I still think that most people without any programming experience at all more easy understand a small lua program compared to the same code in dzvents.

I looked at it, but rewriting more complex lua code to dzvents is not that easy. May be without programming knowledge dzvents is easier to learn.

But again, my opinion.

Re: How to do better programming?

Posted: Wednesday 29 November 2017 20:54
by dannybloe
Totally baffeled here. So you think responding to events using dozens of scattered Lua tables is easier? Working with piped commandArray structures with cryptic codes that are poorly documented is easier than do something like myDevice.updateSetPoint(24)? That writing a simple script that only executes 'at nighttime on mon,tue,fri' is harder than using many lines of code dealing with Lua's horrible way of working with times and dates? And... that experience programmers find that hard to learn? Seriously??

Totally doesn't make any sense to me. dzVents is primarily written so unexperienced coders can finally write simple, easy to maintain and easy to use domoticz event scripts without having to deal with unnecessary complexity.

Anyway, I give up. Good luck with your code and complex scripts.

Re: How to do better programming?

Posted: Wednesday 29 November 2017 21:26
by jannl
myDevice.updateSetPoint(24) updates which device? That line should look almost the same in lua

Anyway, I really looked at dzvents and for simple scripts it even could be better, especially some time scripts Looking at my more complex lua scripts, it doesn't seem that easy to rewrite them and I don't want to add yet another scriptlanguage.

And as I already said, for unexperienced programmers, dzvents propably is easier to learn/start with.

But the thread started with the question about 1 big lua file. I think that is a bad choice. I should split it in smaller parts with lots of comments to make sure one understands the code after some time. But that also counts for dzvents.


Re: How to do better programming?

Posted: Wednesday 29 November 2017 22:18
by pigloox
Hello,

I am a person who does not have a lot of programming experience.

I discovered the language Lua with domoticz and to be honest I find that between a code lua and a code dzvent, it is the code lua which for me wins the duel of understanding.

In addition, whenever I could not program the desired function in Lua, Google has always been able to help me, even for the manipulation of times and dates. With dzVents, the help is not the same and for me the examples given are not enough to understand an "layer" of a programming language especially for a person who does not have the basics of this language.

Re: How to do better programming?

Posted: Wednesday 29 November 2017 22:42
by dannybloe
dzVents is 100% Lua.

Re: How to do better programming?

Posted: Wednesday 29 November 2017 23:23
by Nautilus
For the original question, please remember that "script_devicename.lua" is only executed for the specific device. In many cases that might not be enough. Also, as mentioned here by others, it will slow down your system a bit (increase CPU usage) the more you have these. I have not tried dZvents(*) but having one master device script that returns the commandArray and then several subscripts that are called from the master has worked the best from performance and usability point of view for me. If you use the internal script editor in the web gui it will be a bit slower than having the scripts directly in the file system. Blockly is the fastest, but you cannot of course do everything with it (it is also missing some features available in the standard framework) and the Domoticz implementation of Blockly has a couple of quite severe bugs.

(*)Disclaimer: I have only a little programming experience but found it quite easy and intuitive to learn what I needed to do with Lua. If I would be in that staring point again, now with dZvents available, I probably would start with that and no doubt it would be easy to learn as well. But at this point I find it as quite a big hurdle to be switching over - although thanks to the good documentation it should go pretty smoothly. For someone who has not spent any time learning it, it is of course not easy to read it, but I think that is the expectation with any this type of thing...:) From performance perspective it would be nice to get some statistics, how does dZvents compare to Lua and Blockly?

Re: How to do better programming?

Posted: Wednesday 29 November 2017 23:33
by dannybloe
dzVents is basically one master script returning one commandArray.

Re: How to do better programming?

Posted: Wednesday 29 November 2017 23:41
by Nautilus
dannybloe wrote: Wednesday 29 November 2017 23:33 dzVents is basically one master script returning one commandArray.
Yes, but if I've understood correctly there are several additional features built on top of it if compared to "traditional" Lua in Domoticz context. How these might affect the performance is a mystery to me, maybe not at all or have a slight impact either way? All in all, I assume it is not a very noticeable change if already using this type of "master script" approach...