|
-
February 23rd, 2011, 01:30 PM
#1
C++ constructor and good style of programming
Hi,
I have a class in my project, which is a dictionary. It holds all the declination and stuff for my language. Before I start to use it in program, it must be loaded with data. The nouns, verbs, adverbs and pronouns are loaded from dictionary files into memory, then the object is ready to be used.
For me, the most clear solution is to keep importing data from these four files in object's constructor (precisely: in methods called from constructor; one method per file). This way I know that object after being created is ready to work and I don't have to create a method like "initialise()", which should be explicitly called.
Is this solution following the good programming style rules? I find a constructor as a method which prepares an object to be useful, not just to be safe (like initialising variables, especially pointers).
Big thanks in advance for any help.
-
February 23rd, 2011, 01:40 PM
#2
Re: C++ constructor and good style of programming
The question is what you'll do if loading fails. Normal methods can return a status code; a constructor has only two options: set some internal state indicating the object is invalid, or throw an exception.
-
February 23rd, 2011, 04:15 PM
#3
Re: C++ constructor and good style of programming
 Originally Posted by Horsey
For me, the most clear solution is to keep importing data from these four files in object's constructor (precisely: in methods called from constructor; one method per file). This way I know that object after being created is ready to work and I don't have to create a method like "initialise()", which should be explicitly called.
Is this solution following the good programming style rules? I find a constructor as a method which prepares an object to be useful, not just to be safe (like initialising variables, especially pointers).
It's good to be able to fully initialize an object in a constructor, cause sometimes this allows for better code than two-stage initialization. However, not being able to default construct an object or not being able to change an object after construction is a severe limitation.
- What if you want to load different files into a dictionary, e.g. to separate predefined and user defined words?
- What if you want to load the dictionary in a worker thread to keep your application responsive? If the dictionary is initialized in the constructor, you'll probably need to keep a pointer and dynamically allocate the object.
So, based on what you told, I'd say it's best to have both possibilities: full initialization in the c'tor and a default c'tor with separate 'load' function.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
February 23rd, 2011, 04:28 PM
#4
Re: C++ constructor and good style of programming
The answer is it depends. In this case, for reasons already mentioned, I'd have the loading as a separate function.
-
February 24th, 2011, 02:38 AM
#5
Re: C++ constructor and good style of programming
 Originally Posted by Horsey
It holds all the declination and stuff for my language.
Do you ever plan on having more than 1 dictionary? Would that even make sense? Sounds like what you have is a service, rather than an object you want to instantiate.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
February 25th, 2011, 04:26 AM
#6
Re: C++ constructor and good style of programming
The object is (and should be) a singleton. There is need for only one dictionary in program, loaded at beginning and being used till end of program work.
For user the dictionary is read-only. Any data operation (adding/removing, changing) should happen at program loading, not later.
When loading falls, dictionary is invalid and program exits.
Thanks for your answers, I think I'll leave this as it is right now.
-
February 25th, 2011, 03:55 PM
#7
Re: C++ constructor and good style of programming
 Originally Posted by Horsey
The object is (and should be) a singleton. There is need for only one dictionary in program, loaded at beginning and being used till end of program work.
For user the dictionary is read-only. Any data operation (adding/removing, changing) should happen at program loading, not later.
When loading falls, dictionary is invalid and program exits.
Thanks for your answers, I think I'll leave this as it is right now.
The whole purpose of OO (and design patterns) is to make programs easy to change in the future. Your approach on the contrary seems to be - given the current situation how do I restrict my program maximally so that future change becomes as hard as possible. I'd say you're not following good programming style.
Instead anticipate that in the future your program may have to handle several dictionaries simultaneously, and that not all dictionary data are available at the time of object construction or that the total dictionaries may become so large that the whole of them cannot be stored in memory.
This has two immediate implications: Don't implement the dictionary object as a Singleton and don't "load it" in the constructor. It's bad design because it imposes unnecessary restrictions on future change.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|