Advertisement

Silly question about classes/libraries/headers

Started by January 07, 2015 09:07 AM
5 comments, last by Misantes 10 years ago

Embarrassing question here, but I thought I would sort it out before going further. It's a simple one, but since I don't know the correct term I'm struggling to find an answer. Oddly enough, despite coming across countless examples of them, I haven't found a clear answer.

So, I guess you would call them private libraries, or whathaveyou, but I'm referring to code in a header and .cpp file that don't actually have instances of classes. Say, a physics or math library, something with common commands, shortcuts, etc. Are there any downsides to utilizing these to simplify my code, or anything I should be wary of?

I've been trying to clean up my code structure, and it gets difficult with a lot of boilerplate math/opengl/physics all over the place (even with inheritance, it still seems rather cluttered). Stuffing all my physics or boilerplate opengl functions into their own .cpps, and just calling them when needed would be great. Functionally, this has been working out fine, but I worry I'm overlooking something or creating a bad programming habit. It seems ideal, as much of this stuff doesn't actually seem to belong to the classes I have them in, but I lack a neat place to put them.

Anyhow, I just wanted some input for pitfalls I may be overlooking here before I go refactoring everything.

Beginner here <- please take any opinions with grain of salt

I'm not sure I get what you are asking. Do you believe everything you write has to be in a class for the sole purpose of being in a class? It does not, it should not. Classes are a tool. Free functions are tool. Several other things are a tool. The hallmark of a good programmer is picking the right tool for the right job.

For example std::tuple is a (template) class and there is a very useful free function right next to it: std::make_tuple. Since template argument deduction works on free functions but never on classes that can be extremely convenient.
At the same time free function like std::sort exist as well.
Advertisement

Ah, free function is the term I was looking for. Any advice for best practices with it? It's a tool I haven't actually come across a lot of information on yet, though use rather often. Though, I suppose I can research on my own at this point, knowing the term (thanks for the response :)). I've been avoiding them, but I suppose I ought to research creating templates as well.

Beginner here <- please take any opinions with grain of salt

In general, the rule of thumb for good coding practice is that if a function can be implemented as a "free function" (or "non-member function") then it should be. Free functions, in general, are more flexible then member functions, and also do not violate object encapsulation.

That being said, you shouldn't punch holes in your encapsulation just to make something a free function.

Example: Let's say you have a 3d vector class with X, Y, and Z elements. Should the cross product function be a member or non member? Well, since most vector classes are going to expose their X, Y, and Z elements completely anyway (not very encapsulated) then the cross product function can be non-member, so it should.

The technical term in C++ is "namespace-level function". It's often colloquially known as a free function or a non-member function, both terms implying the normal state for a function is as a member of a class. It is an untrue assumption.

The technical term in C is "function".

The technical term introduced by FORTRAN well over 60 years ago is "subroutiune."

The technical term for moving a namespace-level function into a separate .cpp file is "separate compilation."

Stephen M. Webb
Professional Free Software Developer

Perfectly fine to have these 'helper functions' around if you ask me.
There are always situations where you don't want to link it to a class, a few examples:

- math functions
- loading a specific type of file
- returning a string with an error description

Etc.

Like mentioned above, the trick is to choose right when you are in a situation where you need to pick one.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Advertisement

Thanks for the clarifications all :) I know it was a silly question, just one that had been bothering me for a bit. It appears I've been going about things correctly after all, but felt I should double check.

Beginner here <- please take any opinions with grain of salt

This topic is closed to new replies.

Advertisement