04-21-2023 04:14 AM
@thols wrote:
A class attribute is essentially a global variable shared by all objects in the class. In my opinion, there is always a better design pattern than to use them.
But there's worse also. 🙂 I'm trying to think of when it'd be a good/decent solution, but it's hard. Maybe if you have several instances accessing the same hardware it can be good to have the resource at a class level, since you want all to use the same?
04-21-2023 05:42 AM - edited 04-21-2023 05:48 AM
@Yamaeda wrote:
@thols wrote:
A class attribute is essentially a global variable shared by all objects in the class. In my opinion, there is always a better design pattern than to use them.
But there's worse also. 🙂 I'm trying to think of when it'd be a good/decent solution, but it's hard. Maybe if you have several instances accessing the same hardware it can be good to have the resource at a class level, since you want all to use the same?
All code with 'side effects' (either state or external effect, incl everything reference) will be harder to debug and (unit) test.
VIs that produce the same output values with the same input values are just easier to understand, to debug and to test. Globals, references and state make this hard(er).
Grokking Simplicity - A book on Functional Programming explains this very well. It starts by explaining that FP isn't mutually exclusive with OO. Then it explains FP can also be applied to languages (like LV) that don't support every FP aspect. That takes case of the elephants in the room.
Esp. when starting programming, but with some experience, reading this book will be very beneficial.
At parts of the book it's a bit too easy (if there's such a thing), but it makes very, very good points about things I intuitively knew but couldn't explain (well). Like why Globals makes things harder in the long run.
I'm sure the book explains globals\global resources\side effects are actually required (try to make a program that doesn't have any IO or UI) and the book also how to use them in the least painful way.