Programming Habits

Is there a reason?

Page 1 of 1

5 Replies - 933 Views - Last Post: 13 March 2009 - 05:19 PM

#1 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 39
  • View blog
  • Posts: 378
  • Joined: 01-December 08

Programming Habits

Posted 13 March 2009 - 01:00 PM

I've got a few questions about other people's code.

One thing that I've noticed is that some people will put their code in three files, one header file defining the class, one to define the methods, and one to implement them. Others only use two pages, one for the class definition and its methods, and the other page for the implementation. Lastly, some people (including me) just shove everything into one page. Is there an advantage to any of these besides less cluttered code?

Another thing I've noticed is that some people will use this
using std::cout;
using std::endl;


and so on. I find it easier to do this
using namespace std;


Why bother using the first method?

I'm a self-taught programmer, so I'm not sure why people do these things...

This post has been edited by ericr2427: 13 March 2009 - 01:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Programming Habits

#2 Nykc  Icon User is offline

  • Gentleman of Leisure
  • member icon

Reputation: 731
  • View blog
  • Posts: 8,644
  • Joined: 14-September 07

Re: Programming Habits

Posted 13 March 2009 - 01:06 PM

using namespace std; is making use of all the namespace std, where as the above example is only using cout; and endl;

I have also seen programmers just typing
std::cout <<"Hello";
right in their code.

Personal preference I guess for the most part but also certain compilers interpret it differently.
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Programming Habits

Posted 13 March 2009 - 01:07 PM

Moved to Corner Cubical :)
Was This Post Helpful? 0
  • +
  • -

#4 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: Programming Habits

Posted 13 March 2009 - 01:16 PM

The advantage of having your code split into separate files is compile speed. If you have a file with 5000 lines of code in it, and you edit 1 line, it will have to recompile all 5000 lines of code. If you have 2 files, and you only edit 1 file, it will only need to recompile the edited one, thus making compilation faster. Also, if you want to reuse some of your code in another program, it's best to put in a separate file for easy transportation.
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3108
  • View blog
  • Posts: 19,146
  • Joined: 14-September 07

Re: Programming Habits

Posted 13 March 2009 - 02:36 PM

namespace declaration isn't a good example of programming habit. It is most often a case of scope and necessity. In a large program you don't want to pollute the entire namespace.
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Programming Habits

Posted 13 March 2009 - 05:19 PM

The reason it's not a good idea to globally declare the namespace is because there are hundreds of labels inside a namespace. While you are certainly familiar with cout, and not likely to name your own function cout, there are several other labels in the standard namespace with which you may not be familiar, and run the risk of duplicate names. The error messages generated by these collisions are extremely hard to understand, and things are further complicated by the fact that some compilers have differing implementations, and what compiles on one compiler may not compile on another.

The safest method is to explicitly declare scope while using, like std::cout<<"This is an explicit scope declaration."<<std::endl;, the second safest way is to have these explicit declarations at the top of your file, and the most risky is to simply globally declare the namespace.

I once saw someone describe namespaces as the seatbelts of C++...great protection if used properly, but worse than nothing if used improperly. Not 100% accurate, but close enough. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1