Good C++
These are some of the lessons that I learned during my internship at Tesla Dojo. Many of these things are fairly minor, but it shows the level of standard that software engineers have at these big tech companies.
As someone who cares deeply about just shipping fast, I find it sometimes annoying that these things get in the way. But code quality is still pretty important.
Your number 1 goal is to have your code readable. Kostya: “Boring code is best code”.
Thinking about the right level of abstraction seems to always be the toughest part. Thinking about the intent of the code.
- From gennady: For
if else
blocks, you should put the shorter statement first, rather than the longer one. This is for readability purposes. static constexpr
as opposed tostatic constexpr
- From evgenii: More on performance side, use compile time polymorphism via templates, type traits. Use enums for the types on the templates, ends up being pretty readable. abuse constexpr whenever possible.
- From Kostya: Comment out your fields. I know like too many comments are bad, like code should document itself, but it seems writing comments are still comment. In regards to comment, use periods at the end is the style that I’ve adopted.
- From kostya: Use
std::string_view
whenever possible. - From kostya: Create clean abstractions. Separation of concerns. Don’t introduce too much code that end up not being maintainable.
- from kostik: Think about the interface before designing the code. What will users have to interact with.
- For PRs, use
[topic] Describe the issue
to outline which section of the code the PR is meant to fix. Ex: BAD
if (condition) {
print(help);
print(help);
print(help);
print(help);
print(help);
print(help);
} else {
print(help);
}
GOOD (it’s argued to be more readable)
if (!condition) {
print(help);
} else {
print(help);
print(help);
print(help);
print(help);
print(help);
print(help);
}