If I have a Store class, I can write
Store starbucks;
Here, the variable name is the name of the object itself.
Alternatively, I can write
Store shop( "starbucks" );
where a name variable inside Store is initialized with "starbucks". Here, the variable name is generic but the object contains the specific store name.
Which is preferable, and when?
I can’t think of a real world use for
Store starbucks;. Any time you wanted to add a new store you would have to write all new code and recompile. Outside of test data, for unit tests and whatnot, this should never be used.For similar reasons, hard coding
Store shop( "starbucks" );is also a bad idea. Again, changing instance data should not cause you to recompile your code.Most code that I’ve written uses a combination of user input and a data store to create instances. This is done something like
Store shop; shop.load();or more likelyStore shop = storeFactory.getStore();In addition, I prefer to use the type of the object as the name of the object. This would make the example
Store store = storeFactory.getStore();. It lowers the cognitive load of the reader, because they don’t have to remember that shop is of type Store.