473,563 Members | 2,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++ Programming Style Guidelines

C++ Programming Style Guidelines

http://geosoft.no/development/cppstyle.html

I think that these guidelines are almost *all* wrong.
For example:

11. Private class variables should have underscore suffix.
class SomeClass {
private:
int length_;
}

12. Generic variables should have the same name as their type.
void setTopic (Topic *topic)
// NOT: void setTopic (Topic *value)
// NOT: void setTopic (Topic *aTopic)
// NOT: void setTopic (Topic *x)

void connect (Database *database)
// NOT: void connect (Database *db)
// NOT: void connect (Database *oracleDB)

7. The terms get/set must be used
where an attribute is accessed directly.
employee.getNam e(); matrix.getEleme nt (2, 4);
employee.setNam e (name); matrix.setEleme nt (2, 4, value);
Jul 23 '05 #1
16 2386

"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
news:d8******** **@nntp1.jpl.na sa.gov...
C++ Programming Style Guidelines

http://geosoft.no/development/cppstyle.html

I think that these guidelines are almost *all* wrong.
For example:

11. Private class variables should have underscore suffix.
class SomeClass {
private:
int length_;
}

12. Generic variables should have the same name as their type.
void setTopic (Topic *topic)
// NOT: void setTopic (Topic *value)
// NOT: void setTopic (Topic *aTopic)
// NOT: void setTopic (Topic *x)

void connect (Database *database)
// NOT: void connect (Database *db)
// NOT: void connect (Database *oracleDB)

7. The terms get/set must be used
where an attribute is accessed directly.
employee.getNam e(); matrix.getEleme nt (2, 4);
employee.setNam e (name); matrix.setEleme nt (2, 4, value);


I definitely don't agree with #7. I may be new to this, but I'm pretty sure
this violates one of the basic principles of OOD: polymorphism. Why use two
names for a function when you can overload it?

employee.Name() ; matrix.Element( 2, 4);
employee.Name(n ame); matrix.Element( 2, 4, value);

Dave
Jul 23 '05 #2

"E. Robert Tisdale"
C++ Programming Style Guidelines

http://geosoft.no/development/cppstyle.html

I think that these guidelines are almost *all* wrong.
For example:

11. Private class variables should have underscore suffix.
class SomeClass {
private:
int length_;
}
It doesn't say why not to do this with all class members and I don't know a
reason.
7. The terms get/set must be used
where an attribute is accessed directly.
employee.getNam e(); matrix.getEleme nt (2, 4);
employee.setNam e (name); matrix.setEleme nt (2, 4, value);


The functions have different purposes so I agree with giving them different
names.

Fraser.
Jul 23 '05 #3

"David Lee Conley" <co********@ear thlink.net> wrote in message
news:ti******** ********@newsre ad3.news.atl.ea rthlink.net...

7. The terms get/set must be used
where an attribute is accessed directly.
employee.getNam e(); matrix.getEleme nt (2, 4);
employee.setNam e (name); matrix.setEleme nt (2, 4, value);


I definitely don't agree with #7. I may be new to this, but I'm pretty
sure this violates one of the basic principles of OOD: polymorphism. Why
use two names for a function when you can overload it?

employee.Name() ; matrix.Element( 2, 4);
employee.Name(n ame); matrix.Element( 2, 4, value);

Dave


I disagree. Polymorphism has to do with objects of different types
accomplishing the same task with their own methods, such as a Rectangle and
a Circle both performing a Draw function. The task is to"draw". How they
do it is what differs. And that's accomplished by overriding virtual
functions, not by overloading.

(That's run-time polymorphism, anyway. There's also compile-time
polymorphism, using templates, but again, that has to do with working with
different types, not making one name stand for different actions on a single
type.)

A function called "Name" tells you nothing... it's not a verb, which is what
a function name should usually be. It should tell you what it does.

Also, suppose you want to have a default parameter for your function? How
would you accomplish that in your scenario? You can't, because leaving out
the name (or value) parameter suddenly changes the meaning of the function
call from a setter to a getter.

If you use accessor functions, then calling them getWhatever and setWhatever
sound like perfectly reasonable requirements to me. Giving them just the
name of the member variable does not.

-Howard


Jul 23 '05 #4

"Howard" <al*****@hotmai l.com> wrote in message
news:10******** *************@b gtnsc04-news.ops.worldn et.att.net...

"David Lee Conley" <co********@ear thlink.net> wrote in message
news:ti******** ********@newsre ad3.news.atl.ea rthlink.net...

7. The terms get/set must be used
where an attribute is accessed directly.
employee.getNam e(); matrix.getEleme nt (2, 4);
employee.setNam e (name); matrix.setEleme nt (2, 4, value);


I definitely don't agree with #7. I may be new to this, but I'm pretty
sure this violates one of the basic principles of OOD: polymorphism. Why
use two names for a function when you can overload it?

employee.Name() ; matrix.Element( 2, 4);
employee.Name(n ame); matrix.Element( 2, 4, value);

Dave


I disagree. Polymorphism has to do with objects of different types
accomplishing the same task with their own methods, such as a Rectangle
and a Circle both performing a Draw function. The task is to"draw". How
they do it is what differs. And that's accomplished by overriding virtual
functions, not by overloading.

(That's run-time polymorphism, anyway. There's also compile-time
polymorphism, using templates, but again, that has to do with working with
different types, not making one name stand for different actions on a
single type.)

A function called "Name" tells you nothing... it's not a verb, which is
what a function name should usually be. It should tell you what it does.

Also, suppose you want to have a default parameter for your function? How
would you accomplish that in your scenario? You can't, because leaving
out the name (or value) parameter suddenly changes the meaning of the
function call from a setter to a getter.

If you use accessor functions, then calling them getWhatever and
setWhatever sound like perfectly reasonable requirements to me. Giving
them just the name of the member variable does not.

-Howard


You're right. I should've put more thought into it. My bad.

Dave
Jul 23 '05 #5
E. Robert Tisdale wrote:
I think that these guidelines are almost *all* wrong.
I agree, and here's one I particularly hate:
49. [snip] Note that structs are kept in C++ for compatibility with C only,
and avoiding them increases the readability of the code by reducing the number of constructs used. Use a class instead.


This implies that it's never correct to define a datatype that is just
a bag of bits, such as:

struct Name
{
std::string m_sFirst, m_sLast;
};

This implication is nonsensical and overly restrictive. The fact that
C++ gives the programmer many ways to accomplish a given task is a
*good* thing, not a bad thing. Everything must not necesarrily be a
full-fledged object.

Jul 23 '05 #6
John Dibling wrote:
E. Robert Tisdale wrote:
I think that these guidelines are almost *all* wrong.


I agree, and here's one I particularly hate:
49. [snip] Note that structs are kept in C++ for compatibility with C only,
and avoiding them increases the readability of the code by reducing the number of constructs used. Use a class instead.


This implies that it's never correct to define a datatype that is just
a bag of bits, such as:

struct Name
{
std::string m_sFirst, m_sLast;
};

This implication is nonsensical and overly restrictive. The fact that
C++ gives the programmer many ways to accomplish a given task is a
*good* thing, not a bad thing. Everything must not necesarrily be a
full-fledged object.

You guys should read the "disclaimer " at the beginning, goes something
like this:

"The attempt is to make a guideline, not to force a particular coding
style onto individuals."

The intent of this document is just to serve as a guideline to make
code across an organisation "look" the same. Obviously none of these
guidelines will be important if you're coding a simple 200 line
application.

But when you are creating Large Scale C++ Software, it *does* help to
follow some sort of guideline (and the guidelines in this document seem
mostly reasonable to me). Think 20 years down the line.

hth,
-shez-

Jul 23 '05 #7

"John Dibling" <jd******@gmail .com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
E. Robert Tisdale wrote:
I think that these guidelines are almost *all* wrong.


I agree, and here's one I particularly hate:
49. [snip] Note that structs are kept in C++ for compatibility with C
only,
and avoiding them increases the readability of the code by reducing the
number of constructs used. Use a class instead.


This implies that it's never correct to define a datatype that is just
a bag of bits, such as:

struct Name
{
std::string m_sFirst, m_sLast;
};

This implication is nonsensical and overly restrictive. The fact that
C++ gives the programmer many ways to accomplish a given task is a
*good* thing, not a bad thing. Everything must not necesarrily be a
full-fledged object.


What are you talking about? How is a struct not a "full-fledged object",
but "just a bag of bits"? The only differences between structs and classes
is the default visibility and default inheritance. The above sample is the
same as this:

class Name
{
public:
std::string m_sFirst, m_sLast;
};

I seem to recall that some early versions of C++ (such as Turbo C++, I
believe) defined struct and class differently, where a struct was just a
data holder (i.e., POD) and a class could include methods. But that is not
the case any more at all (and I don't know if it was ever the standard).

I see many people still using struct when all they want is a data holder. I
suspect that's for a few reasons. One, they used to do it that way, back
when their compiler required it. Two, they like the convenience of the
default public visibility for struct members (avoiding typing "public:"
every time). And three, it gives a quick visual clue that to them (and to
others who are used to this style) as to whether they're looking at an
object which simply holds some data, or one which has functionality of its
own.

But there's no requirement as to which you should use. You could use struct
everywhere and class nowhere, and as long as you specified the desired
inheritance and visibility, they'll accomplish the very same task.

One more thing: when you don't specify a constructor, destructor, etc. for
a struct or class, the compiler will generate a default one for you. So
really, even a "plain old struct" has *some* functionality.

My personal preference is to use struct only for POD data, and make
everything else a class. (Why? For the three reasons I gave earlier.)

Hmm... I guess that means I disagree with that rule, also. I just disagree
with your argument as to why struct should be kept. I want to keep it
because I like to use it, that's all. It's certainly not needed.

-Howard

Jul 23 '05 #8
Shezan Baig wrote:
The intent of this document is just to serve as a guideline to make
code across an organisation "look" the same. [snip] when you are
creating Large Scale C++ Software, it *does* help to follow some sort
of guideline (and the guidelines in this document seem
mostly reasonable to me). Think 20 years down the line.
I agree, coding guidelines are a good thing. But I believe that coding
guidelines should encourage programmers to generate better code, not
better *looking* code.

Here's an example. The guidelines state:
75. The if-else class of statements should have the following form:
if (condition) {
statements;
}

if (condition) {
statements;
}
else {
statements;
}


I see no benefit whatsoever from this recommendation, and don't think
that this kind of thing should appear in any coding guideline document,
even if it's just a "should." It simply can't help Joe Programmer to
write better code. The first if block is identical to:

if( condition )
{
statements;
}

....which is how I personally write such blocks. There's no difference!
In fact, if a coding guideline tells me I can't write my if blocks the
way I naturally do, I'll end up taking much longer to write code
becasue I'll have to correct what naturally falls out of my brain to
fit some pedantic mandate.

Moreover, a professional programmer should be expected to be able to
maintain other peoples' code just as easily as thier own, even if it
doesn't look like thier own, so long as the code isn't insanly
convoluted.

Instead of these sorts of style recommendations , I believe coding
guidelines should include things like:

"Prefer std::string to dynamically-allocated char buffers."

....or...

"Check all incoming pointer paraaters for validity before using them."

Take care,

John Dibling

Jul 23 '05 #9
because .Name() != .Name(name)

one is a "getter" the other a "setter"

it is definately NOT good OO style to do it the way you are
recommending since everyone else has already agreed to do it the other
( more readable ) way.

Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
2199
by: beliavsky | last post by:
Is there a more recent set of Python style guidelines than PEP 8, "Style Guide for Python Code", by van Rossum and Warsaw, at http://www.python.org/peps/pep-0008.html , which is dated July 5, 2001?
1
1399
by: Ioannis Vranos | last post by:
Check these. http://www.mozilla.org/hacking/mozilla-style-guide.html#General http://www.mozilla.org/hacking/portable-cpp.html#portability_rules I wonder, why they use such obsolete guidelines?
15
1983
by: Earl Higgins | last post by:
The company where I work as a Senior Software Engineer is currently revamping their (1991 era) "Programming and Style Guidelines", and I'm on the committee. The company, in business for over 20 years, writes medical software, which must ultimately pass FDA approval and ISO 9000 certification. The product the document applies to is a large...
39
3478
by: Patrick | last post by:
The c# code style guide that I follow suggests that class variables (fields) be coded with camel casing, like this: int recordId; string name; It also suggests that variables within methods and method parameters use camel casing, like this: void SetName(int id, string newName)
7
4934
by: Robert Seacord | last post by:
The CERT/CC has just deployed a new web site dedicated to developing secure coding standards for the C programming language, C++, and eventually other programming language. We have already developed significant content for the C programming language that is available at: https://www.securecoding.cert.org/ by clicking on the "CERT C...
0
2614
by: Wim Vanhoof | last post by:
----------------------------------------------------------- WLPE' 07 - CALL FOR PAPERS Workshop on Logic-based Methods in Programming Environments (satellite workshop of ICLP'07) September 13, 2007
56
5689
by: valentin tihomirov | last post by:
{ int i = 2; } int i = 1; There is no 'i' defined in the 'parent' context from the moment of declaration on. So what is the problem? They tell us they pursue language simplicity. The rule "do not define a variable more than once in the same context" is natural, and simplest therefore. All normal languages obey it therefore....
13
1058
by: MartinRinehart | last post by:
There's a lot of dumb stuff out there. "Algorithms should be coded efficiently ..." Thanks, I'll keep that in mind. van Rossum's guidelines tend toward "pick something and stick to it" which is OK if you have enough experience to pick something Pythonic. I'm a relative newbie, not qualified to pick. Anything written somewhere that's...
14
1826
by: Astley Le Jasper | last post by:
I'm still learning python and would like to know what's a good way of organizing code. I am writing some scripts to scrape a number of different website that hold similar information and then collating it all together. Obviously each site needs to be handled differently, but once the information is collected then more generic functions can...
0
7664
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7638
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7948
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6250
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
923
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.