ingredient_15939
ingredient_15939

Reputation: 3134

Naming - Should I avoid giving Objects the same name as their Classes?

Is it common practice to instantiate a class called, for example, PDFWriter as an object also named PDFWriter, or should that be avoided? Are there any situations where it would be a problem, and if so, is there a standard naming convention to distinguish between classes and objects?

Edit: How about in VB.net, where the IDE does not allow differentiation by case?

Upvotes: 0

Views: 107

Answers (3)

Lee Meador
Lee Meador

Reputation: 12985

Back in the beginning of Apple Mac programming, it was all the rage to name things like:

PDFWriter aPdfWriter;

or

PDFWriter thePdfWriter;

which is better for languages that prefer the capital on the variable:

PDFWriter ThePdfWriter;

Nowdays, thats not so much the fashion.

Sometimes you don't care because the type of writer is irrelevant so you just use:

PDFWriter writer;

or

PDFWriter Writer;

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172398

There is as such no standard for the naming convention of objects and classes but it is considered a good practice to take the name of the objects which:-

1) indicate the intent of its use.

2) uniformly structured.

From here:-

Names are vitally important, for the same reason they are important in non-OO languages, but also because of the anthropomorphic nature of OO programming.

It is common in OO languages to name things with the name of the class and a definite or indefinite article (Control, aController; View, theView). These names are fine when something more appropriate can’t be found (List, employees).

Upvotes: 0

Joshua Swank
Joshua Swank

Reputation: 199

I don't think it's a problem as long as you are using a case-sensitive language where type names and variable names use different case practices.

For example, it should be fine to have a variable called pdfWriter or pdf_writer of the type PDFWriter.

Upvotes: 2

Related Questions