Should I use private classes in private projects?
I'm facing writing a reasonably large program for a research project that will probably b开发者_Go百科e used only by me, or possibly also by a small number of people that might take it over in the future. The point is - it's not a commercial application, and won't be publicly available. And my question - in such a case are there really any good arguments behind defining all my classes private rather than public? The whole code is meant to be a job sequence scheduler. So, for example, I have a basic job class that looks like:
class Job {
private: // should it be?
int Jobid;
int stack_row ; // horizontal positon in a stack
int row_height ; // position in a column of containers
float ArrivalTime;
float FinishTime;
float GantryTime;
float WaitingTime; // Job Start - Job Arrival
float ReachableTime; // later time of Vehicle or YC arrival to job location
float DueDate;
float Tardiness; // max{0, Ci-di} Ci, Completion time = Finish time; di, DueDate
float SlackTime;
int type; // 1, Loading; 2, Unloading
} ;
Then there's a class for a sequence of jobs, simulation data, machines involved, and a number of other things. The point is, I end up with lot of classes, and even more components for them. All of them will be used either only by me, or a small number of other people. I could define all classes private, with set_whatever()
, get_whatever()
functions for all private components that need to be set or read, but is there really a point to it? A - it takes time. B - it doesn't make for a very legible code when I write
job_schedule.job_list[i].set_finish_time( job_schedule.job_list[i].get_ArrivaTime() + job_schedule.job_list[i].get_ProcessingTime() ) ;
instead of
job_schedule.job_list[i] = job_schedule.job_list[i].Finish_Time + job_schedule.job_list[i].Processing_Time ;
So my question is - is there a really good reason I would stick to private classes in this case? Or maybe there is a more elegant way to do
job_schedule.job_list[i].set_finish_time( job_schedule.job_list[i].get_ArrivaTime() + job_schedule.job_list[i].get_ProcessingTime() ) ;
while remaining private?
There is more to private
than dependency management.
Information hiding is good as it lessens maintenance, but what's the point of having a class if you just treat it as a tuple of data ?
You should think about the interface of your class, and instead of providing a bunch of getters/setters so that anybody can just manipulate anything, you should strive to define meaningful methods.
Remember than source code should be readable first and foremost.
If your program is meant to be maintained for some time and it's not a one time project I strongly recommend using information hiding, i.e. private member variables and public accessors (setters and getters). Because you may never know when you will want to add a functionality when some propery changes. At that time you will need a setter function that does more than setting the member variable but you'll have to refactor all previously written code.
So I'd say go with the better design. It's a little harder to type but a typical piece of code is typed only once and read many more times.
In C++ there is no such thing as private or public classes. You have private, protected or public members or classes.
As to writing a lot of classes, using inheritance and encapsulation (information hiding) - I would strongly suggest that you do. This will ensure that the code that you develop is more likely to be free from errors and in the long run easier to understand, maintain and modify.
Create an interface for the classes but keep the data private.
Why not job_schedule.set_finish_time(i, now());
or similar?
精彩评论