Jump to content

  • Log In with Google      Sign In   
  • Create Account

A question on style regarding writing a small library (C++)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Shaquil   Members   -  Reputation: 815

Like
0Likes
Like

Posted 29 March 2013 - 10:55 AM

I'm writing a small library of functions that I intend other people to use. In my experience writing stuff intended just for personal use, I generally will break up classes into their own separate files. The "Engine" class for example is in Engine.h and has Engine.cpp. But now that I'm writing something that other human beings will actually have to fiddle with, I'm just wondering what the general consensus is on breaking up classes that way.

 

I mean, if there are going to be about 10 different classes, would other people prefer them all to be in one Library.h with an equally all-inclusive Library.cpp? Or would it be better to just break them out into their own files? My assumption is that if I did that, the user could throw all the files in one folder and just add that folder to the "Additional Includes" option in their IDE, but to be honest I don't know if that'd work.



Sponsor:

#2 n0name   Members   -  Reputation: 225

Like
1Likes
Like

Posted 29 March 2013 - 11:44 AM

Hi

 

If I am to use your library I'd much rather the classes to be in separate .h/.cpp files.. it makes it much more readable...

 

After that if you want u can always add one single .h file that includes all classes at once but still keeping the actual class code in separate files.



#3 swiftcoder   Senior Moderators   -  Reputation: 10364

Like
0Likes
Like

Posted 29 March 2013 - 12:02 PM

If I am to use your library I'd much rather the classes to be in separate .h/.cpp files.. it makes it much more readable...
 
After that if you want u can always add one single .h file that includes all classes at once but still keeping the actual class code in separate files.

QFE.

 

One header + cpp file per class (though small utility classes can be grouped together if related), and one <library-name>.h header file that includes everything I need to use the library.


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#4 AllEightUp   Moderators   -  Reputation: 4267

Like
0Likes
Like

Posted 29 March 2013 - 12:53 PM

I agree with the header/cpp per class but I don't much care for the one header to rule them all solution.  It's usable of course but unfortunately too often abused and problematic.  The abuse comment comes from certain codebases which took 30+ minutes to build almost completely due to a single header including everything and the kitchen sink.  The problem comment comes in later when trying to refactor/change/move code which has become so complex in how the includes are processed that moving one include brings the whole house of cards down.  As much as you can discuss this though, it's really personal preference.

 

My "preference" is definitely not to have all encompassing headers.  I work on a conceptual basis for my code.  An example from my math library is that yes it has a "Math.hpp" file you can include and it takes care of a lot of things.  It defines any primitive types used, includes other concepts if required etc.  So, for instance, using my math library I can do the following:

 

some.hpp

 

#include <Math/Math.hpp>
 
class Blah
{
public:
  Blah( Math::Vector3fv& position );
};

 

That compiles no problem.  What won't compile is:

 

 

#include <Math/Math.hpp>
 
class Blah
{
public:
  Blah( Math::Vector3fv& position );
 
private:
  Math::Vector3fv   mPosition;
};

 

The general header only includes the forward reference, not the class.  As such you have to explicitly add an include, but hopefully I make it very easy:

 

 

#include <Math/Math.hpp>
#include <Math/Vector3fv.hpp>
 
class Blah
{
public:
  Blah( Math::Vector3fv& position );
 
private:
  Math::Vector3fv   mPosition;
};

 

That fixes it for this file.  I'm very explicit in making this easy to figure out since namespace == the directory name, class == file name and as such just replace :: with slash and append hpp and you are done.

 

In the classes themselves, if Vector3fv absolutely must include Matrix or something else to function, it includes it.  I don't leave it up to the user of the code to go figure out implied dependencies...

 

 

This is of course one way to do things, I like it and have seen it in other code bases.  Is it for you, that's your choice. smile.png

 

<Sorry if you saw this cut in half, GD didn't seem to save the whole thing the first time.>


Edited by AllEightUp, 29 March 2013 - 01:02 PM.


#5 swiftcoder   Senior Moderators   -  Reputation: 10364

Like
0Likes
Like

Posted 29 March 2013 - 02:02 PM

I agree with the header/cpp per class but I don't much care for the one header to rule them all solution.  It's usable of course but unfortunately too often abused and problematic.  The abuse comment comes from certain codebases which took 30+ minutes to build almost completely due to a single header including everything and the kitchen sink.  The problem comment comes in later when trying to refactor/change/move code which has become so complex in how the includes are processed that moving one include brings the whole house of cards down.  As much as you can discuss this though, it's really personal preference.

To be absolutely and 100% clear, because I've discovered lately that a lot of people missed this class:

 

The single, global header is only to be used by clients of your library.

 

If you globally include <library-name>.h within your library, I will hunt you down, and lecture you on compile times tongue.png


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#6 AllEightUp   Moderators   -  Reputation: 4267

Like
0Likes
Like

Posted 29 March 2013 - 02:09 PM

I agree with the header/cpp per class but I don't much care for the one header to rule them all solution.  It's usable of course but unfortunately too often abused and problematic.  The abuse comment comes from certain codebases which took 30+ minutes to build almost completely due to a single header including everything and the kitchen sink.  The problem comment comes in later when trying to refactor/change/move code which has become so complex in how the includes are processed that moving one include brings the whole house of cards down.  As much as you can discuss this though, it's really personal preference.

To be absolutely and 100% clear, because I've discovered lately that a lot of people missed this class:

 

The single, global header is only to be used by clients of your library.

 

If you globally include <library-name>.h within your library, I will hunt you down, and lecture you on compile times tongue.png

 

Err, oops.  <Extracts foot from mouth!>  smile.png

 

Though to be honest, I still tend to leave the separated headers in the libraries as personal preference.  I still think it a bit too likely that someone will link another big file to that one and end up with the chain of massive includes.  I guess if you are willing to stand on folks heads and ask: "You will NEVER ever include big includes from other big includes again, will you????", then it works out. smile.png


Edited by AllEightUp, 29 March 2013 - 02:11 PM.


#7 metsfan   Members   -  Reputation: 654

Like
0Likes
Like

Posted 29 March 2013 - 06:25 PM

When I break things into .h/.cpp files, I like to break them up based on their "idea".  if it were java, I would make 1 .java file per class, no matter how big or small.  But with C++ if i have a class that relies on some small struct, I will normally include the struct in the same .h file and inline any methods associated with that struct.  This is simply because in C++, unlike java, an additional include file really DOES matter a lot for compile time, and also, we get the added benefit of the methods being inlined.  However, as soon as I see myself forming a new idea with either new classes, or structs that go beyond that "POD" state, I do branch out into a new .h/.cpp combo.

 

As for the library itself...it depends what you're making.  If you're making a library that is meant to be included as a binary, I think having a "Library.h" file that includes your library itself or many basic types, that makes sense.   The only downside is that if the user upgrades the library, their project will need to recompile all sources that include anything from your library, but let's be frank: if they upgrade a library and don't expect that, what planet are they living on?


Edited by metsfan, 29 March 2013 - 06:26 PM.


#8 Shaquil   Members   -  Reputation: 815

Like
0Likes
Like

Posted 30 March 2013 - 11:31 AM

One problem I'm running into with breaking things up is that a lot of the classes use STL containers and <string>, which I understand to be a typedef of a template class. I only vaguely understand from experience that you can't quite forward declare template classes, so I'm stuck adding "#include otherheader.h" to any headers that define a class that depends on another one. Maybe a little research will help me out of this problem, but I just thought I'd point it out to see if anyone had any opinions on the problem here.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS