user1899020
user1899020

Reputation: 13575

How to avoid duplicate names of class and method?

Currently I use same convention for class names and method names. They have a leading capital letter without prefix. And I don't use "Get" prefix to get an attribute. However, I meet a name conflict problem in the following code

class Material
{};

class Background
{
public:
    Material* Material() const             {return m_material;}  // Name conflict
    void SetMaterial(Material* material)   {m_material = material;}

private:
    Material* m_material;
};

What is the easiest way to solve the problem but keeping or with minimum modification of my conventions? Thanks a lot!

Upvotes: 0

Views: 485

Answers (4)

billz
billz

Reputation: 45470

You are bitten by not following good naming rules.

To name a function, use a strong verb followed by an object A procedure with functional cohesion usually performs an operation on an object. The name should reflect what the procedure does, and an operation on an object implies a verb-plus-object name. GetMaterial(), SetMaterial, PrintDocument(), CalcMonthlyRevenues(), CheckOrderlnfo(), and RepaginateDocument() are samples of good procedure names.

What is the easiest way to solve the problem but keeping or with minimum modification of my conventions?

Simply add an action in front of object as function name:

Material* GetMaterial() const {return m_material;}  

Upvotes: 0

Arun
Arun

Reputation: 20403

What is the easiest way to solve the problem but keeping or with minimum modification of my conventions?

The easiest way might be to slightly modify the convention, such as start method names with lowercase.

BTW, I like that you are not using the word "get" in the accessor method. In the same spirit, you may drop the word "set" in the mutator method (here, SetMaterial()). The signature would be sufficient to distinguish the accessor and mutator.

If you like them, you would reach at:

class Material
{};

class Background
{
public:
    Material* material() const          {return m_material;}
    void material(Material* material)   {m_material = material;}

private:
    Material* m_material;
};

Upvotes: 5

Josef Kufner
Josef Kufner

Reputation: 2989

Take a look at Qt library. It uses almost the same conventions as you and it is the best piece of code I've ever seen. Only difference I see is capital letter at begin of method names. So change yours convention to match theirs and you will be happy.

Upvotes: 1

Alex Chamberlain
Alex Chamberlain

Reputation: 4207

It is unusual to capitalise method names.

Upvotes: 1

Related Questions