Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy the C++ style guide into the wiki #47

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

warp-core
Copy link
Contributor

New content

Summary

It has been suggested on occasion that the C++ style guide (available on the website) should be inside the wiki.
This PR copies and reformats that content into a markdown file within the wiki.

@TomGoodIdea
Copy link
Member

I'd prefer the website redirecting here, or having some method to sync automatically (but there's format difference). Hosting it on both places makes it harder to modify.

@TomGoodIdea TomGoodIdea added the standalone A PR that doesn't require an endless-sky PR to be merged. label Sep 16, 2024
Copy link
Contributor

@mOctave mOctave left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite a few comments on this.

Most of my comments have to do with changes to the styleguide itself (or changes to our current practices), so may be best dealt with in a separate PR. The biggest ones here stem from the fact that this style guide hasn't been updated in a decade or so, and I think Endless Sky switched which platform it was hosted on two or three times in that period?

The other two things I've noticed is that, since markdown has this, we should probably use it instead of MZ's "double-quoted code," and that I think code blocks might need an extra line after their preceding paragraph in order to work possibly.

Also, we might want to make edits now that this isn't a list where you click to expand points. It seems to mostly work as-is, though.

Finally, I would very much support moving this over to only be on the wiki. I think the fact that this is so very outdated emphasizes how little people actually use the website, especially when writing code (or making sweeping changes to little things like the version of C++ ES uses!)

this program. If not, see <https://www.gnu.org/licenses/>.
*/
```
If you make substantial additions or changes to a file, please add your name to the copyright list.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, very funny MZ. This should probably either be removed, or we should go back and actually start doing copyright headers properly in the first place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate on what it would mean to do them properly?
As I understand it, the current approach is indeed for people who make substantial changes to a file, or create new code in a new file, to add their name to the copyright at the beginning, though this is perhaps more common in relation to data files than it is to the source code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I'm aware, people's names only get put in the copyright header when they're the one who makes it, for ES. At least, that's the rules I've been playing by. I think, according to the GPL license, every single contributor to the file should have their name in the header. We definitely aren't doing that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know for certain that people who have made significant additions to data files have been added to the copyright header.
I don't know about the C++ files, but it's possible that no one has made sufficient changes to a single file that they did not create to get to that point, at least not in one go, or they just didn't want to put their name there, or I'm just forgetting instances where it has happened.
I can't see where the GPL says that. It certainly references "authors". I don't think every contribution to a file constitutes "authorship".


Within a .h file, the #includes should be broken into "paragraphs" each separated by a single blank line:
1. Classes that this file's class inherits from.
2. Other files in the "endless-sky" code base.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why this is "endless-sky", rather than Endless Sky or even just endless-sky?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why it was originally written that way. As mentioned, this PR just copies the content of the existing style guide and reformats it from xml.
Do you think there is something wrong with the way it is right now?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not particularly anything wrong, this could just be something to change in a follow-up PR.

Or, it could be formatted as

`endless-sky`

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
2. Other files in the "endless-sky" code base.
2. Other files in the `endless-sky` code base.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would this change the appearance and why do you think it would be an improvement?

class Angle;
class Sprite;
```
It is helpful but not required to alphabetize the lines within each "paragraph" of #includes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should require this? Is there any place where we don't do this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In practice, we do require it. This can be changed.

Comment on lines 112 to 132
## Order of #includes in a .cpp file

Within a .cpp file, the #includes should be broken into "paragraphs" each separated by a single blank line:
1. This file's corresponding .h.
2. Other files in the "endless-sky" code base.
3. Non-standard third-party libraries.
4. Standard libraries.

For example, a .cpp file might contain the following #includes:
```c++
#include "MyClass.h"

#include "Angle.h"
#include "Sprite.h"

#include &lt;SDL/SDL.h&gt;

#include &lt;set&gt;
```
It is helpful but not required to alphabetize the lines within each "paragraph" of #includes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't help but feel like this could be combined with the section above...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could, but would that be an improvement?

}
```

## Function definitions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blue text again


## No compiler warnings

Code should compile with -Wall without any warnings.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already get compilation warnings every time I build for MacOS. Are we still following this guideline, or can we safely ignore specific compilation warnings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, no warnings are produced in the CI/CD MacOS builds.
Building using supported tools should not produce warnings with -Wall.
Consider opening an issue providing more details if this is not consistent with your experience.




# C++11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should maybe have a section for later versions of C++, too?

@warp-core
Copy link
Contributor Author

In general, I'm not inclined to change the content of the style guide in this PR, only the formatting.
You can already propose changes to it in the repository for the website at https://github.com/endless-sky/endless-sky.github.io.

Co-authored-by: mOctave <73318970+mOctave@users.noreply.github.com>
@mOctave
Copy link
Contributor

mOctave commented Sep 17, 2024

Yeah, that makes sense. I still would suggest taking it off the website, though, since the website in general is the last place anyone looks for anything.

Also, I'm pretty sure I didn't catch all the quoted text that should be changed. I can go through it again and find some more if you'd like.

Copy link
Contributor

@mOctave mOctave left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is all of them, now.


## File extensions

All C++ files should use ".h" and ".cpp" for their extensions.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
All C++ files should use ".h" and ".cpp" for their extensions.
All C++ files should use `.h` and `.cpp` for their extensions.


## One class per file

Each class "MyClass" should have its own .h and .cpp files, and they should be named "MyClass.h" and "MyClass.cpp".
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Each class "MyClass" should have its own .h and .cpp files, and they should be named "MyClass.h" and "MyClass.cpp".
Each class `MyClass` should have its own .h and .cpp files, and they should be named `MyClass.h` and `MyClass.cpp`.


## Names for files with no classes

If a file does not contain any classes (such as "main.cpp" or "shift.h"), its name should be lower-case.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If a file does not contain any classes (such as "main.cpp" or "shift.h"), its name should be lower-case.
If a file does not contain any classes (such as `main.cpp` or `shift.h`), its name should be lower-case.


## Header file guards

All header files should have #define guards, and they should be in the format "#ifndef MY_CLASS_H_".
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
All header files should have #define guards, and they should be in the format "#ifndef MY_CLASS_H_".
All header files should have #define guards, and they should be in the format `#ifndef MY_CLASS_H_`.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to update lines about header guards anyway, so this will be removed.

Co-authored-by: mOctave <73318970+mOctave@users.noreply.github.com>
@warp-core
Copy link
Contributor Author

I'm not so sure about putting things that aren't code (file names, for example) in in-line code blocks instead of quotes.

Copy link
Member

@TomGoodIdea TomGoodIdea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just finish the migration for now. Any updates to the guide contents themselves that aren't already present in the old version should be properly discussed in their own separate PRs after the migration is complete.

Comment on lines +57 to +58
All header files should have #define guards, and they should be in the format "#ifndef MY_CLASS_H_".
The C++ standard technically does not allow source code files to define macros that start with an underscore or contain a double underscore.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

endless-sky/endless-sky.github.io@a4afddd

Suggested change
All header files should have #define guards, and they should be in the format "#ifndef MY_CLASS_H_".
The C++ standard technically does not allow source code files to define macros that start with an underscore or contain a double underscore.
All header files should have #pragma once guards.
The C++ standard technically does not allow #pragma once, but all major compilers support it. It is also much easier to use than regular header guards.

Comment on lines +181 to +183
8. private variables
Anything not listed here (e.g. public non-constant variables, protected variables, etc.) should not be used.
Here is an example class definition, with extra comments added to point out aspects of the formatting:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
8. private variables
Anything not listed here (e.g. public non-constant variables, protected variables, etc.) should not be used.
Here is an example class definition, with extra comments added to point out aspects of the formatting:
8. private variables
Anything not listed here (e.g. public non-constant variables, protected variables, etc.) should not be used.
Here is an example class definition, with extra comments added to point out aspects of the formatting:


## Function names

Function names should be CapitalizedCamelCase
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Function names should be CapitalizedCamelCase
Function names should be CapitalizedCamelCase.


## Single-line loop bodies

You do not need to use braces if a loop or conditional body is a single expression.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

endless-sky/endless-sky.github.io@c541362

Suggested change
You do not need to use braces if a loop or conditional body is a single expression.
You should not use braces if a loop or conditional body is a single expression.

if(theThing != done)
DoTheThing();
```
You can also forgo the braces for something like this:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
You can also forgo the braces for something like this:
You should also forgo the braces for something like this:

if(vec[i])
sum += i;
```
And, you can also do this:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
And, you can also do this:
And, you should also do this:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
standalone A PR that doesn't require an endless-sky PR to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants