What is good or bad here?

Hello,

Below I will show you two sample codes.
I heard there is bad habits and good habits of coding, which of these below are good habits, and bad habits.

Code Example Nr. 1

1
2
3
4
5
6
7
8
9
10
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello World!" endl;

    return 0;
}


Code Example Nr. 2

1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
    std::cout << "Hello world!" << std::endl;

    return 0;
}


What is right or wrong? I'm pretty confused, and do not want to learn bad habits etc.

Dario
second is better:
* No chance of name collision (std::distance, I looking at you)
* Everybody who will read your code will know that you are referring to standard library, not self-written one.

Compare:
1
2
3
#include "everything_needed.h"
using namespace std;
x = distance(start, finish);

and
1
2
3
#include "everything_needed.h"
x = distance(start, finish);
y = std::distance(x.begin(),x.end());

In first case you cannot be sure that it is or isn't a standard function. Worse: if you using self-written function and will sometime add <algorithm> header, you will have to deal with name collision, which is nasty.
Last edited on
Thank you for the reply, I will now use as you recommend :)

Dario
Topic archived. No new replies allowed.