I define my event structure in a class inherited from TNamed. I then save many events in a tree and browse the tree in a TBrowser. All private data members of my “Event” class are shown in the browser. Many users write their scripts using fAPrivateDataMember instead of GetADataMember() Does it violate encapsulation of classes? I don’t know if it is correct or not, but somebody says that calling variable in a script is faster than calling functions. So this behavior does have its advantage. Shall I simply declare all data members in my “Event” class public and get rid of all setters and getters? Or shall I force users to use functions in their scripts?
Are you writing code for yourself only, or for sharing with others? Encapsulation & access levels are mostly a matter of policy.
Since I know my code will only be used by me, I just make all my class members public. Even if I needed to share the code, I am not making any guarantees or trying to hide anything. In the world of Python this is attitude is known as “we’re all adults here.”
An overly-encapsulated class can often be a barrier to productive work. I have sometimes wanted to access in CINT to the private members of a ROOT class (one that didn’t provide access methods), but because of an arbitrary policy and attitude about what OOP means, I wasn’t allowed. It’s not clear to be what is the benefit of private members in a scientific computing context. In a closed-source corporate environment I can believe there are reasons, but not in an open context like ROOT.
Thank you for pointing out that it is just a matter of policy. Then the question would be, how do I decide my policy based on my specific situation?
Let me introduce my situation. My code will be used by many people. They know how to open a ROOT file containing a tree, how to browse the contents of a tree in a TBrowser, and how to call tree->Draw("x") That’s it. Is it better to make every thing public? If yes, what is the necessity to put everything in a class? A class in my mind is only necessary if one wants to hide some calculation details others don’t care. Shall I simply use c structure?
Very interesting. But I do not fully agree. Let’s say, I am a policeman and I get sick and go to see a doctor. I am an adult, but it does not mean that I have to know how the doctor works. I just follow his suggestions, that’s it. I do my job, he does his. We work for each other, and trust each other’s work so that we don’t need to spend time on everything. Classes should work in a similar way. The idea behind it is that I don’t mind how you do it, just give me what I want and I am fine. What do you think?
I fully agree with the first sentance. But if you are an expert, nothing can stop you doing what you want. You can always find a way to break the barrier. For example, you can check out the source of ROOT from git and modify what ever you want. It might be too much for an average user to see all private members. He does not know what to do with them. It only confuses him. For example, my class inherits from TObject, which has its own private members. They are not used in the analysis but also get shown in a TBrowser together with my variables. Users often ask me what they are.