How to do better programming?

Moderator: leecollings

ensingg
Posts: 65
Joined: Saturday 22 April 2017 17:35
Target OS: Windows
Domoticz version:
Contact:

How to do better programming?

Post 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
DutchHans
Posts: 230
Joined: Friday 03 April 2015 20:44
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Germany (near dutch border)
Contact:

Re: How to do better programming?

Post by DutchHans »

Hi, it would make sense if you show it...
Regards, Hans
ensingg
Posts: 65
Joined: Saturday 22 April 2017 17:35
Target OS: Windows
Domoticz version:
Contact:

Re: How to do better programming?

Post 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.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: How to do better programming?

Post 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).
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
dutchdevil83
Posts: 130
Joined: Monday 31 October 2016 19:34
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Netherlands
Contact:

Re: How to do better programming?

Post 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.
Hardware
1x Raspberry Pi
1x Razberry Z-Wave
1x RFXtrx433E
1x Toon
1x GoodWe Solarpanels
2x FGSD-002 Smoke Detector
1x FGBS-001 Binaire Sensor (RFID lezer)
7x FGMS-001 Motion Sensor
3x ZW089 Verzonken Deursensor
7x NC Wallplug
&lots of KaKu stuff
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: How to do better programming?

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
jannl
Posts: 673
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: How to do better programming?

Post 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.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: How to do better programming?

Post by dannybloe »

That’s totally not the case with dzVents.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
jannl
Posts: 673
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: How to do better programming?

Post by jannl »

Obviously we have different opinions.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: How to do better programming?

Post by dannybloe »

Clearly... although I'm more interested in facts.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
jannl
Posts: 673
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: How to do better programming?

Post by jannl »

I don't think readability can be measured.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: How to do better programming?

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
jannl
Posts: 673
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: How to do better programming?

Post 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.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: How to do better programming?

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
jannl
Posts: 673
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: How to do better programming?

Post 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.

pigloox
Posts: 22
Joined: Tuesday 09 February 2016 23:02
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: France
Contact:

Re: How to do better programming?

Post 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.
RPI3 Stretch (Hard disk)
-RFXtrx433 USB
-OpenZwave USB
-Teleinfo USB
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: How to do better programming?

Post by dannybloe »

dzVents is 100% Lua.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: How to do better programming?

Post 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?
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: How to do better programming?

Post by dannybloe »

dzVents is basically one master script returning one commandArray.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: How to do better programming?

Post 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...
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest