1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | class CartesianDistance { public: void SetX(u32 x) { _x = x; ComputeDistance(); } void SetY(u32 y) { _y = y; ComputeDistance(); } u32 GetDistance() { return _dist; } private: void ComputeDistance() { _dist = _x + _y; } u32 _x; u32 _y; u32 _dist; }; // usage code // read some data from a file CartesianDistance* cd = manager->CreateCartesianDistance(); cd->SetX(readX); cd->SetY(readY); // later on void MapGenerator::Generate() { foreach(...) { u32 dist = manager->GetCartesianDistance(...)->GetDistance() // do something with dist } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | u32 CartesianDistance(u32 x, u32 y) { u32 result = x + y; return result; } // usage code // read some data from a file void GenerateMap(void) { foreach(...) { u32 dist = CartesianDistance(distances[idx].x, distances[idx].y); // do something with dist } } |
Delix
In my experience, the above process never really results in accessors/mutators, data-hiding, or member functions--it also produces code that is really easy to reason about and use.
NelsonMandella
Not everyone is the same and something that's intuitive to reason about for you may not be for someone other than you. There is no single approach or set of approaches when it comes to this stuff, people's brains often work radically differently from one another.
Delix
I completely agree with this, which is why I think introducing a greater number of abstractions from the hardware is a problem for understandable code. The common language for all programmers, irrespective of how one might mentally model a particular problem, is hardware. Failing to understand the hardware, then, is detrimental to the ability of multiple programmers to communicate--it is the common ground for all mental models.
When code is framed around objects/class hierarchies/"real world representations", that common language ceases to exist--all subjective interpretations are equally valid. This is why I claim that writing something that is as simple as possible and that is purely dictated by the computational realities of the problem is best for understandable code.
Delix
To respond more directly to your response, NelsonMandella, I simply don't see the justification behind the argument that a software development problem should ever deviate from the problem at hand. An API inside of a codebase should provide a simple way to do its part in the solution of a larger problem. Complexity will increase as projects get larger, of course, but introducing new things to the problem isn't going to reduce that complexity.
NelsonMandella
Within the context of a large project, beelining for the simplest most elegant solution to a given problem will not necessarily have favorable implications for your overall projects architecture. Packaging the module, defining the interface and encapsulating its data are largely architectural decisions that may add complexity to the module itself while at the same time reducing the complexity of the overall program.
Within the context of a large project, beelining for the simplest most elegant solution to a given problem will not necessarily have favorable implications for your overall projects architecture. Packaging the module, defining the interface and encapsulating its data are largely architectural decisions that may add complexity to the module itself while at the same time reducing the complexity of the overall program.
boagz57
It seems, from what I can gather and please correct me if I'm wrong, the larger and more complex your project gets the more important encapsulation and well defined interfaces can be. Is this because at a certain point it becomes less costly to have slightly more complicated and harder to work with class hierarchies with higher encapsulation than having less complicated and more flexible classes which are less encapsulated?
boagz57
If so, is this due to the fact that more publicly accessible, flexible code can make it harder to reason about the state of a program at any given point and thus makes it harder to debug your code generally speaking?
boagz57
Well the main reason I was struggling a bit with it's use was the fact that I was always taught to reduce the scope of variables/data when possible, for the same reasons you don't want global variables. Having many possible places where code can be accessed can make it harder to reason about the state of your program at any given point, which becomes especially troublesome when trying to debug a particular issue that has arisen. Though I don't think public variables are quite as bad as global variables, given most of your objects you create will be scoped in some way. I guess what I'm trying to wrap my head around is while I know even global variables have their uses, the general consensus is you very much want to limit them so I'm wondering why the same logic wouldn't be applied to having most of your class variables public by default?