#Clean code advice
46 messages · Page 1 of 1 (latest)
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.
- don't
#include "file.cpp", you should include headers not source files (except in very specific circumstances) - you don't necessarily need a unique header file and source file for every class, there isnt a best practice on this its just whatever coding style your project uses. I often have multiple types + functions per header (and then implement them in an equivalent source file)
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)
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.
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)
we split them in separate files to speed up the build process, not for code quality lmao
Oh
avoiding re-building unchanged source files? obviously?
the concepts of headers and cpp files makes the build process slower
when compiling?
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
so you are telling me that in a small project with 50 cpp files, when I edit only one of these, I should recompile the remaining 49? are you serious? of course you won't recompile the other 49
no but thats not why it was desigend that way
thats a happy side effect
and also not always true
a.k.a. the objective
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
you also don't need to recompile source files in module-based systems if you can prove none of the related modules changed. its not a different idea
okay, the compiler consumes loads of RAM, so you split the work, I get that part
I perfectly understood this but I have no clue what you guys are saying, so what is the best-practice should I do different cpp and header files or put them all in the main.cpp
you should likely use different cpp files for your implementations than main
that's fine, careful not to hit cyclical includes, they need forward declaration magic to resolve them
usually we reserve main for the program's user interface/startup code
or higher levels of abstraction
Perfect understood, thank you guys for your assistance ❤️
@distant apex Has your question been resolved? If so, type !solved :)
!solved
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
Hey i think you and i are on same page beginner right! Can you tell me from where you're studying
Any server for that
mostly from learncpp.com
To be honest not a single place I just keep doing projects and whenever I'm stuck with something I search google then as my last solution I come ask people here, learncpp.com is good and also if you're an absolute beginner CodingWithMosh on youtube is good aswell thats where I started.
Thanks I'm also a newbie