#Clean code advice

46 messages · Page 1 of 1 (latest)

distant apex
#

Why do we make headers and cpp files and then include them in main.cpp like file.h and file.cpp and then #include "file.h" in the main.cpp, and if I'm creating a class should I do the same thing whats best practice in this case ?

viral grottoBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

burnt surge
#

headers and cpp files dont necessarily related to using classes or not

#

its about functions

#

(classes can have member functions but theyre treated similarly with regards to linking and ODR)

distant apex
#

So classes don't really need to be in individual header files same goes for the member functions inside of them, and we cpp #include "file.h" in the main.cpp not the cpp file.

burnt surge
#
void foo() {
  // ...
}
```this is a function definition. The c++ standard is always using weird termonology so im just going to say lets call `foo` an "entity"

C++ has something called the one definition rule (ODR) which means that every entity can have at most 1 definition. You can't have a definition for `foo` in 1 cpp file and another definition for `foo` in another cpp file because then how would c++ know which is the correct `foo`

If you put a definition in a header and then include that header in multiple places you create multiple definitions which violates ODR

This is true for every entity. So if you have a class `C` with functions `a` and `b` like
```cpp
class C {
  void a();
  static void b();
};
```then there can only be 1 definition of `C::a` and `C::b`. This is where a cpp file would come in handy. You would define these as
```cpp
void C::a() {
  // ...
}

void C::b() {
  // ...
}
```in a cpp file and not in the header (since every file that included that header would be making a duplicate definition)
bitter flame
burnt surge
#

its not for speeding up the build process

#

what

bitter flame
burnt surge
#

the concepts of headers and cpp files makes the build process slower

bitter flame
#

when compiling?

burnt surge
#

it was designed that way because of limited resources

#

its not for speed

#

its actually a terrible way to compile

#

which is why everything nowerdays uses modules-like systems

bitter flame
burnt surge
#

thats a happy side effect

#

and also not always true

bitter flame
burnt surge
#

no

#

its not the objective

#

as I said

#

its not why it was designed that way

#

its designed that way because early pcs were very memory constrained

#

and splitting the build into multiple steps was the only way to do it and actually have a functioning compiler

burnt surge
bitter flame
distant apex
burnt surge
bitter flame
burnt surge
#

usually we reserve main for the program's user interface/startup code

#

or higher levels of abstraction

distant apex
#

Perfect understood, thank you guys for your assistance ❤️

viral grottoBOT
#

@distant apex Has your question been resolved? If so, type !solved :)

distant apex
#

!solved

viral grottoBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

hoary skiff
# distant apex !solved

Hey i think you and i are on same page beginner right! Can you tell me from where you're studying

hoary skiff
bitter flame
distant apex