Sponsored By

Learning and #include

A bit about what helps to learn how to program.

Ofer Rubinstein, Blogger

December 11, 2009

3 Min Read

From A Development and Gamers Blog 

In C++ you can roughly divide code files into two types. Implementation files(.cpp) and header\definitions files (.h). The header files can include other header files, which mean you have some sort of a graph depicting the includes "web" of the headers. You cannot have cyclic inclusion of headers. The header files are used to make class and function definitions, which allow other files(.h or .cpp) to use these classes/functions as well.
I want to ask you some questions, and I want you to think of the answers before you keep on reading. Even if you don't know C++, try to think of answers. There is no penalty for giving a "wrong" answer, just try to honestly answer these questions.

  1. What is the potential problem of having a header include many other headers?

  2. Why it can be good to include directly all the necessary headers instead of including a header that include the necessary headers?

  3. What is the potential problem of having many headers include the same header?

My answers to these questions are not the "correct" answers, they are also not the only answers. These are just things I have learned from experience, and they were right for me at least at one moment and situation.
The potential problem with having a header include many headers is because of compilation. If you include many headers, each time you change one of those headers, you will need to recompile everything that include this header. Which means almost every time you change the program, you will need to compile a lot of things.
Including the necessarily files directly is good when you want to remove a specific header file from your project. Because otherwise you might need to start figuring out what more you need to include in a file that included the header you want to remove.
When a lot of files include a specific header, removing that header might require removing all those file that include it. This make sense and is how things work. But things might get problematic when a header file contains two "subjects", one that you want to remove and the other that you don't want to remove. In this case you need to split up this file.

What does all this got to do with learning? I just recently completed and released Labyrinthica: The quest of lima, and started to work on a new project. I wanted to use some of the files from Labyrinthica in the new project, but I wanted to remove some files which were not relevant to this project. This made me encounter some of the problems above, and made me learn by example or learn from mistake. People remember much better when they learn from a mistake
This is backed up by studies and research. I have already been told by other developers about good practices with header files, a million of times. But only when I experienced the problem myself, I got an in depth understanding of the problem.
Programming is a bit different than giving an incorrect answer. Because programming is not 100% formal, problems in programming can have many different solutions and no one correct answer. But still, you learn how to program by sitting down and trying to code. Some people might try to find the perfect solution to a problem they never solved before. They end up thinking about it for months, But never trying to actually sit and code. Sometimes you need to look for the simplest solution, without knowing if it will really succeed or if this solution won't really solve the problem. If this solution works, good. If it doesn't, then you will have better understanding of the problem you tried to solve, and in the next attempt you are more likely to solve it.

And lastely, here is a research I have found by looking at Yahoo news. Link.

Read more about:

Blogs
Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like