Question: When to use multiple CPP files (Good Coding Practice)

Question

When to use multiple CPP files (Good Coding Practice)

Answers 2
Added at 2016-12-19 02:12
Tags
Question

When writing a C++ program what is considered to be good practice in regards to using multiple cpp files?

IE If you are writing a rather large (lines of code based) object it would generally be easier for comprehension purposes to divide the program in multiple sections.

Lets say you have a thousand lines of just constructors and then a 1000 lines of foo and another of bar. Generally it would be easier for one to read it if they understood they are just viewing all the variations of foo, bar, constructors or any other methods.

What do you consider to be "good practices?" Do you base it on lines of code? Do you do these partitions even in smaller programs (sub 1000 lines total).

I understand this is subjective but what are the rules of the thumbs for dividing your program/object/etc amongst multiple cpp files?

Answers to

When to use multiple CPP files (Good Coding Practice)

nr: #1 dodano: 2016-12-19 02:12

Try to limit yourself to one class per file. If that class is extremely long you could try to break it into multiple files, but this may only complicate things. Be sure to leave enough blank lines to make the code more readable and to leave enough comments so that you and others can figure out what the code does in the future.

nr: #2 dodano: 2016-12-19 03:12

Most people typically break up their files by a loosly defined category that makes sense to the programmers on the project.

For example, if you are coding a calculator, you might have a main cpp for input, then a cpp for error handling, another cpp for binary operators, another for unary, ... or you can split it up by having a main cpp for input, output, and error handling, and a cpp for all possible math functions. It really depends on how structured you want to be and how you define a category or section.

Here is a good quote from this website:

As programs get larger, it is not uncommon to split them into multiple files for organizational or reusability purposes. One advantage of working with an IDE is they make working with multiple files much easier. You already know how to create and compile single-file projects. Adding new files to existing projects is very easy.

Source Show
◀ Wstecz