function prefixes... are they needed?

M

mscava

Recently I've came to a small problem... I've read few coding
guidelines, to improve and standardize my style. Most of them advised
me to use function prefixes such as Get, Set, Compute etc..

I understand that avoiding Set prefix might be very confusing and I
feel that one is right.

But what about others?

class Rectangle
{
public:

inline int ComputeArea() const { return w_ * h_; }

inline int GetX() const { return x_; }
inline int GetY() const { return y_; }

inline int GetWidth() const { return w_; }
inline int GetHeight() const { return h_; }

private:

int x_, y_;
int w_, h_;
};

I feel like it's just too verbose... what about:

class Rectangle
{
public:

inline int Area() const { return w_ * h_; }

inline int X() const { return x_; }
inline int Y() const { return y_; }

inline int Width() const { return w_; }
inline int Height() const { return h_; }

private:

int x_, y_;
int w_, h_;
};

Can anyone advise me one of these? Or at least show me some pros &
cons?

Thanks
 
A

Alf P. Steinbach

* (e-mail address removed):
Recently I've came to a small problem... I've read few coding
guidelines, to improve and standardize my style. Most of them advised
me to use function prefixes such as Get, Set, Compute etc..

I understand that avoiding Set prefix might be very confusing and I
feel that one is right.

But what about others?

class Rectangle
{
public:

inline int ComputeArea() const { return w_ * h_; }

inline int GetX() const { return x_; }
inline int GetY() const { return y_; }

inline int GetWidth() const { return w_; }
inline int GetHeight() const { return h_; }

private:

int x_, y_;
int w_, h_;
};

I feel like it's just too verbose... what about:

class Rectangle
{
public:

inline int Area() const { return w_ * h_; }

inline int X() const { return x_; }
inline int Y() const { return y_; }

inline int Width() const { return w_; }
inline int Height() const { return h_; }

private:

int x_, y_;
int w_, h_;
};

Can anyone advise me one of these? Or at least show me some pros &
cons?

The first is Java, the latter starts to look like C++. The word
"inline" is superflous and should be removed. Otherwise style is
subjective, so even though I have other preferences, if you like the
second example's style, go for it.
 
J

Jim Langston

Recently I've came to a small problem... I've read few coding
guidelines, to improve and standardize my style. Most of them advised
me to use function prefixes such as Get, Set, Compute etc..

I understand that avoiding Set prefix might be very confusing and I
feel that one is right.

But what about others?

class Rectangle
{
public:

inline int ComputeArea() const { return w_ * h_; }

inline int GetX() const { return x_; }
inline int GetY() const { return y_; }

inline int GetWidth() const { return w_; }
inline int GetHeight() const { return h_; }

private:

int x_, y_;
int w_, h_;
};

I feel like it's just too verbose... what about:

class Rectangle
{
public:

inline int Area() const { return w_ * h_; }

inline int X() const { return x_; }
inline int Y() const { return y_; }

inline int Width() const { return w_; }
inline int Height() const { return h_; }

private:

int x_, y_;
int w_, h_;
};

Can anyone advise me one of these? Or at least show me some pros &
cons?

I used to use SetX and GetX, but after a conversation here on the newsgroup
about if I'm using setters and getters, why not just make it public, etc...
I decided that it made more sense not to use them.

I use youre second method, although without inline.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top