LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-13-2007, 07:46 PM   #1
JMJ_coder
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 478

Rep: Reputation: 30
Questions on C++ style


Hello,

I am taking a beginning programming course at the University this fall and I have a couple questions on proper programming style - don't worry I won't ask any test or lab questions.

The first question is on includes. My professor is using a different convention than used by the book I have (Stroustrup 2000). To illustrate the difference imagine a simple hello world program:

The way the professor writes it is:

Code:
#include <iostream>
using namespaces std;

main ()
{
     cout << "Hello World!"
          << '\n';
}
The way the book presents it is:

Code:
#include <iostream>

main ()
{
     std::cout << "Hello World!\n";
}
Both must be correct because they both compile. But which one is the more common or better approach to programming style?



The second question is with comments. My professor uses:

Code:
/* */
in the form of comment boxes he wants us to use - such as:
Code:
/*---------------------------------------------------*/
/*                                                   */
/* Comment goes here                                 */
/*                                                   */
/*---------------------------------------------------*/
But in the book, the convention used is a double forward slash:

Code:
//
and there is no corresponding ending forward slashes:

Code:
// comment goes here
Aside from the 'program in the style your professor wants' argument (obviously I'll try to program in the style the professor wants for this class), what is the more common and better style of programming?



Thanks in advance.
 
Old 09-13-2007, 07:59 PM   #2
JoeyAdams
Member
 
Registered: Jun 2006
Distribution: Kubuntu Hardy
Posts: 94

Rep: Reputation: 15
This could be an opinion-loaded subject.

I would recommend you use the "using namespace std;" instead of prefixing every name with std:: because it's a bit more concise. As long as you avoid function names like "string" or "open", you should be alright.

As for comments, if you are starting your own project, I would recommend using // for single line comments and /* */ for long block comments. Modern compilers support both comment styles in C mode. However, if you are contributing source to an existing project, try to conform to their style.

Last edited by JoeyAdams; 09-13-2007 at 08:03 PM. Reason: Added paragraph about namespace
 
Old 09-15-2007, 03:07 PM   #3
JMJ_coder
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 478

Original Poster
Rep: Reputation: 30
Hello,

Quote:
Originally Posted by JoeyAdams View Post
This could be an opinion-loaded subject.
WOW! I am just starting to realize this. There seem to be about at least 10 different ways to accomplish anything. For instance, for the includes, I have seen at least four different ways to write them:

#include <iostream>

#include <iostream.h>

#include "iostream"

#include "iostream.h"
 
Old 09-15-2007, 03:09 PM   #4
JMJ_coder
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 478

Original Poster
Rep: Reputation: 30
Hello,

Quote:
Originally Posted by JoeyAdams View Post
As for comments, if you are starting your own project, I would recommend using // for single line comments and /* */ for long block comments. Modern compilers support both comment styles in C mode. However, if you are contributing source to an existing project, try to conform to their style.
I read some of the history of the language, and from what I have gathered, the /* ... */ convention comes from C and the // ... convention is re-emerged from BCPL. And I have seen some good examples of when to use one over the other - other times it seems to be a personal preference.
 
Old 09-15-2007, 05:07 PM   #5
JMJ_coder
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 478

Original Poster
Rep: Reputation: 30
Hello,

Quote:
Originally Posted by JoeyAdams View Post
I would recommend you use the "using namespace std;" instead of prefixing every name with std:: because it's a bit more concise. As long as you avoid function names like "string" or "open", you should be alright.
I read a little further into the book where the Standard Library is introduced and discussed and the author wrote this:

Quote:
Originally Posted by Stroustrup 2000
3.3 The Standard Library Namespace

The standard library is defined in a namespace (§2.4, §8.2) called std. That is why I wrote std::cout rather than plain cout.

Every standard library facility is provided through some standard header similar to <iostream>. For example:

#include <string>
#include <list>


This makes the standard string and list available. To use them, the std:: prefix can be used:

std::string s = "Four legs Good; two legs Baaad!";
std::list<std::string> slogans;


For simplicity, I will rarely use the std:: prefix explicity in examples. Neither will I always #include the necessary headers explicitly. To compile and run program fragments here, you must #include the appropriate headers (as listed in §3.7.5, §3.8.6, and Chapter 16). In addition, you must either use the std:: prefix or make every name from std global (§8.2.3). For example:

#include<string> // make the standard string facilities accessible
using namespace std: // make std names available without std:: prefix

string s = "Ignorance is bliss!"; // ok: string is std::string


It is generally in poor taste to dump every name from a namespace into the global namespace. However, to keep short the program fragments used to illustrate language and library features, I omit repetitive #includes and std:: qualifications. In this book, I use the standard library almost exclusively, so if a name from the standard library is used, it either is a use of what the standard offers or part of an explanation of how the standard facility might be defined.
What I take from this is - for educational purposes and for very small projects where only the standard library will be used, it is acceptable to make the standard namespace global. But, in general programming - to conform with good programming style, go with the std:: prefix.
 
Old 09-15-2007, 11:58 PM   #6
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
for the includes, if you see <>'s it is probably for the standard header files of the language. when you use ""s it is usually for any other header, ie a header file you wrote yourself. in C people will include the .h, in C++ the .h is left out, at least for the standard library headers.

for the comments, i dont know the story behind them, but i agree with JoeyAdams. use //'s for single-line comments, and use /**/'s when you have a large section of comments.
 
Old 09-16-2007, 09:45 AM   #7
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Quote:
Originally Posted by JMJ_coder View Post
Hello,

I read a little further into the book where the Standard Library is introduced and discussed and the author wrote this:



What I take from this is - for educational purposes and for very small projects where only the standard library will be used, it is acceptable to make the standard namespace global. But, in general programming - to conform with good programming style, go with the std:: prefix.
This isn't necessarily the case. It is bad form to include an ENTIRE namespace. It is OK, however, to try and shorten your code by including one class or other declaration, so you don't have to continuously prefix stuff w/ std. Say you have a class that uses std::string A LOT, it would be fine to do something like this:

[code]
using std::string;

// ... code goes here ...
string s = "Hello there";
// ... blah blah ...
[code]

The problem with including a namespace, is that most people can't remember the names of everything declared in one namespace, and it is silly to try and avoid naming conflicts with other namespaces (since that is whay namespaces were invented in the first place!!). However, using just one declaration is OK, since anyone reading your file will CLEARLY see that string (for example) is from std.

Edit:
If you are more comfortable with Java, you can think of using as Java's import. Using an entire namespace is equivalent to importing an entire package in Java (import java.util.* for instance). This is seen as bad form in Java as well. Importing 1 class (or interface or enum or whatever), however, is perfectly fine. Since it is made clear to whoever is editing that file where the classes are coming from.

Last edited by 95se; 09-16-2007 at 09:48 AM.
 
Old 09-16-2007, 11:38 AM   #8
JMJ_coder
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 478

Original Poster
Rep: Reputation: 30
Hello,

Quote:
Originally Posted by nadroj View Post
for the includes, if you see <>'s it is probably for the standard header files of the language. when you use ""s it is usually for any other header, ie a header file you wrote yourself. in C people will include the .h, in C++ the .h is left out, at least for the standard library headers.
So for the standard library use:

#include <library name>
#include <library name.h>

and for user-defined libraries use:

#include "library name"
#include "library name.h"

?

Is this defined in the language or from rules of good programming style or just personal preferences?
 
Old 09-16-2007, 11:42 AM   #9
JMJ_coder
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 478

Original Poster
Rep: Reputation: 30
Hello,

Quote:
Originally Posted by 95se View Post
This isn't necessarily the case. It is bad form to include an ENTIRE namespace. It is OK, however, to try and shorten your code by including one class or other declaration, so you don't have to continuously prefix stuff w/ std. Say you have a class that uses std::string A LOT, it would be fine to do something like this:

[code]
using std::string;

// ... code goes here ...
string s = "Hello there";
// ... blah blah ...
[code]

The problem with including a namespace, is that most people can't remember the names of everything declared in one namespace, and it is silly to try and avoid naming conflicts with other namespaces (since that is whay namespaces were invented in the first place!!). However, using just one declaration is OK, since anyone reading your file will CLEARLY see that string (for example) is from std.
Thanks for this. That is very interesting to know.



Quote:
Originally Posted by 95se View Post
Edit:
If you are more comfortable with Java, you can think of using as Java's import. Using an entire namespace is equivalent to importing an entire package in Java (import java.util.* for instance). This is seen as bad form in Java as well. Importing 1 class (or interface or enum or whatever), however, is perfectly fine. Since it is made clear to whoever is editing that file where the classes are coming from.
I have no personal experience in programming with Java, but it has made a very bad impression on me - mostly from the side of a user. I dislike Java programs very much. And if I as a user can't stand Java programs, I can't imagine it would be fun for me to program them nor would I feel comfortable creating more of these programs to inflict upon other users.
 
Old 09-16-2007, 02:26 PM   #10
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Quote:
Originally Posted by JMJ_coder View Post
I have no personal experience in programming with Java, but it has made a very bad impression on me - mostly from the side of a user. I dislike Java programs very much. And if I as a user can't stand Java programs, I can't imagine it would be fun for me to program them nor would I feel comfortable creating more of these programs to inflict upon other users.
Haha, yes, Java programs are a PAIN to install (and use in some cases), unless your using a package manager or something. However, I have a lot of respect for Java. It keeps it simple, logical, and follows proper OO design patterns (for the most part). Some other languages, try to allow programmers to do very "clever" things. This can result in code that is not very readable and does not make much sense to someone who is not well versed in that language (or even that application). I would definitely recommend Java to anyone wanting to learn programming.

As for the installation problem, they are working to fix these types of issues: http://jcp.org/en/jsr/detail?id=277... but as for when or if that will happen... ugh.

Last edited by 95se; 09-16-2007 at 02:29 PM.
 
Old 09-16-2007, 08:33 PM   #11
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by JMJ_coder View Post
For instance, for the includes, I have seen at least four different ways to write them:

#include <iostream>

#include <iostream.h>

#include "iostream"

#include "iostream.h"
This, as you may now know, is not strictly a matter of style. I won’t dwell on the difference between “<iostream>” and “"iostream"” (since others have already pointed it out, and since it is implementation-defined). There is a greater difference, however, between the standard library headers with and without the “.h” suffix. The old-style headers include the “.h” but have no support for the standard namespace. The new-style headers have no such suffix, but require reference to namespace std, either by a using declaration or by explicit qualification.

So what I’m trying to say is that this is a conforming application:
Code:
#include <iostream.h>

int main(int argc, char *argv[]) {
     cout << "Hello World!\n";
}
While this is not:
Code:
#include <iostream>

int main(int argc, char *argv[]) {
     cout << "Hello World!\n";
}
As tempting as it may look, don’t use the first one (even though it’s standards-conforming, it’s also deprecated—meaning its support may be removed at any time).
 
Old 09-16-2007, 08:39 PM   #12
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
osor i think you meant to say your first application is not conforming.
 
Old 09-16-2007, 09:08 PM   #13
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by nadroj View Post
osor i think you meant to say your first application is not conforming.
No. I assert that the first one is conforming (although it uses deprecated features). I am looking at §17.4.1.2, specifically the footnote of clause 7:
Quote:
Originally Posted by §17.4.1.2
The “.h” headers dump all their names into the global namespace, whereas the newer forms keep their names in namespace std.
Here is some empirical proof of the matter (but, in general, don’t take positive compilation with gcc as proof of conformance):
Code:
$ g++ -x c++ -o conform -
#include <iostream.h>

int main(int argc, char *argv[]) {
     cout << "Hello World!\n";
}
^D
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4/backward/iostream.h:31,
                 from <stdin>:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.1.2/include/g++-v4/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
$ ls -l conform
-rwxr-xr-x 1 osor users 9556 2007-09-16 22:06 conform
$ g++ -x c++ -o nonconform -
#include <iostream>

int main(int argc, char *argv[]) {
     cout << "Hello World!\n";
}
^D
<stdin>: In function ‘int main(int, char**)’:
<stdin>:4: error: ‘cout’ was not declared in this scope

$ ls -l nonconform
ls: cannot access nonconform: No such file or directory
 
Old 09-16-2007, 09:11 PM   #14
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
oh sorry, im always used to seeing 'using namespace std;' so i missed that error (of not having it).
 
Old 09-16-2007, 10:50 PM   #15
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
I have a job as a C++ programmer. The coding standards at our company require:
  • using namespace std;
  • Comments with // instead of /* ... */

Other places have other standards, of course.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
new [quote] style Ynot Irucrem LQ Suggestions & Feedback 9 11-09-2005 09:25 PM
How to know if I am using mbox-style or maildir-style? Niceman2005 Linux - General 1 09-23-2005 12:08 PM
VIM-style wrapping to OpenOffice style schmmd Linux - Software 1 12-21-2004 06:50 PM
how to change mandrake style menu to kde style menu msalimane Mandriva 1 12-07-2004 01:16 PM
Fluxbox Style oulevon Linux - Software 3 12-24-2002 10:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration