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

Write the coding-style guide for Modmesh #362

Closed
wants to merge 7 commits into from

Conversation

ThreeMonth03
Copy link
Collaborator

I add the draft about the coding-style.
There are some detail need to discuss.

ThreeMonth03 and others added 2 commits June 10, 2024 13:39
Copy link
Collaborator Author

@ThreeMonth03 ThreeMonth03 left a comment

Choose a reason for hiding this comment

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

I draft a coding style, and I have some question.

3. The QT-related function name uses Camel case,
whereas non-QT-related function name uses snake case.
4. The name of the class member starts with ```m_```.
5. The name of [macros](https://en.cppreference.com/w/cpp/preprocessor/replace) starts with ```DECL_MM_```.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

According to @tigercosmos and my suggestion,
here is the brief and rough conclusion.

However, it seems that the naming of the macros is inconsistent.
image
Is there any naming rule about macros?

Copy link
Collaborator

Choose a reason for hiding this comment

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

interesting, @yungyuc could you decide a prefix name and let's change all the other?

Copy link
Member

Choose a reason for hiding this comment

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

Let's use MM_DECL*.

Comment on lines 34 to 43
### Formatting
1. It is necessary to obey the [clang-format](https://clang.llvm.org/docs/index.html), or you must fail the github action.
2. There exists a space between the definition of the classes/functions.
3. A long line should be devided to several shorter lines.

### Class
1. The class should follow [the rule of five](https://en.cppreference.com/w/cpp/language/rule_of_three).

### Functions
1. The recursive function should be replaced with the iterative function, unless it is inevitable to be used.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Should I add more concrete example or basic suggestion(like const member function/ copy by const reference) for the items?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't know we have such a rule.

Copy link
Member

Choose a reason for hiding this comment

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

This does not look like a coding style. It is a performance (HPC) concern. It should not be included in this file.

@ThreeMonth03
Copy link
Collaborator Author

I add the draft about the coding-style. There are some detail need to discuss.

@yungyuc If you are availabe, may you start a review?

* [Functions](#functions)

### Naming
1. The class names use Camel case, such as ```class R3DWidget;```.
Copy link
Collaborator

Choose a reason for hiding this comment

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

R3DWidget is not a good example. QT related and non-QT can be different.
Also, let's clarify if we want HTTPRequest or HttpReuqest.

Copy link
Member

Choose a reason for hiding this comment

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

Correct. Please distinguish Qt and non-Qt classes. If not mentioned, the style refers to non-Qt classes, and you should take non-Qt classes for example.

Copy link
Collaborator Author

@ThreeMonth03 ThreeMonth03 Jun 16, 2024

Choose a reason for hiding this comment

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

R3DWidget is not a good example. QT related and non-QT can be different. Also, let's clarify if we want HTTPRequest or HttpReuqest.

Sorry, I don't get it.
How does the cpp program call the HTTPRequest or HttpReuqest function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

R3DWidget is not a good example. QT related and non-QT can be different. Also, let's clarify if we want HTTPRequest or HttpReuqest.

By the way, do you mean that if the class use HTTPRequest or HttpReuqest,
the class belongs to a QT-related classes?

Copy link
Member

Choose a reason for hiding this comment

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

R* classes are Qt classes in modmesh. I name them with the leading R because it's the next letter of Q. I do not want to give them the leading letter Q which is already used by Qt itself.

R3DWidget is a modmesh-made Qt derived class. The CamelCase rule is ambiguous for it because "3D" is an acronym for "three-dimensional" but "3" cannot be capitalized. Both R3DWidget and R3dWidget are CamelCased.

@tigercosmos used HTTPRequest and HttpRequest to explain that you need to consider how to deal with acronyms. The former keeps each of the letter in the acronym "HTTP" in upper-case. The latter treats the whole acronym as a word, and makes it "Http".

Treating an acronym as a word will make naming more consistent. So for acronyms, let's go with HttpRequest.

1. The class names use Camel case, such as ```class R3DWidget;```.
2. The name of the variable / using-declarations uses snake case,
such as ```R3DWidget * viewer;``` and ```using size_type = std::size_t;```.
3. The QT-related function name uses Camel case,
Copy link
Collaborator

Choose a reason for hiding this comment

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

what's the difference with point 1.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I notice that whether the class is related QT, the naming of the class is Camel case.

Copy link
Member

Choose a reason for hiding this comment

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

Class names are mostly CamelCase, although exceptions are not rare when it comes to a helper class.

Copy link
Collaborator Author

@ThreeMonth03 ThreeMonth03 Jun 16, 2024

Choose a reason for hiding this comment

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

Class names are mostly CamelCase, although exceptions are not rare when it comes to a helper class.

What is the style of the helper class?
Should I mention the helper class in this manual?

Copy link
Member

Choose a reason for hiding this comment

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

It's complicated

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's complicated

Ok, we may skip the helper class temporaily.

such as ```R3DWidget * viewer;``` and ```using size_type = std::size_t;```.
3. The QT-related function name uses Camel case,
whereas non-QT-related function name uses snake case.
4. The name of the class member starts with ```m_```.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
4. The name of the class member starts with ```m_```.
4. The name of the class member starts with `m_`.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What is the difference between ` and ```?

Copy link
Collaborator

Choose a reason for hiding this comment

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

` is for inline code, ``` is for code block

Copy link
Member

Choose a reason for hiding this comment

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

whereas non-QT-related function name uses snake case.
4. The name of the class member starts with ```m_```.
5. The name of [macros](https://en.cppreference.com/w/cpp/preprocessor/replace) starts with ```DECL_MM_```.
6. The [using-declarations](https://en.cppreference.com/w/cpp/language/using_declaration) ends with ```_type```.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
6. The [using-declarations](https://en.cppreference.com/w/cpp/language/using_declaration) ends with ```_type```.
6. The [using-declarations](https://en.cppreference.com/w/cpp/language/using_declaration) ends with `_type`.

### Formatting
1. It is necessary to obey the [clang-format](https://clang.llvm.org/docs/index.html), or you must fail the github action.
2. There exists a space between the definition of the classes/functions.
3. A long line should be devided to several shorter lines.
Copy link
Collaborator

Choose a reason for hiding this comment

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

what's the recommended length? 120?

Copy link
Member

Choose a reason for hiding this comment

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

No, we have not been able to decide what is a proper length. @ThreeMonth03 , could you please make it explicit that we do not yet have a set limitation on line width?

Copy link
Collaborator

Choose a reason for hiding this comment

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

note that for python code, the length limit is 80 for sure.

@tigercosmos
Copy link
Collaborator

@ThreeMonth03 you should mention the issue number when you raise the PR.

@yungyuc
Copy link
Member

yungyuc commented Jun 10, 2024

This PR references #350

@yungyuc yungyuc added the documentation Improvements or additions to documentation label Jun 10, 2024
Copy link
Member

@yungyuc yungyuc left a comment

Choose a reason for hiding this comment

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

  • Rename the file to /CODINGSTYLE.md
  • Remove Introduction
  • Distinguish non-Qt and Qt code
  • Review all indentation, blank lines, and markdown formatting
  • Remove recursion words

@@ -0,0 +1,43 @@
# Coding Style
## Introduction
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need this section whose only content is the table of contents.

Copy link
Member

Choose a reason for hiding this comment

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

Please rename to /CODINGSTYLE.md. The directory contrib/ is for code not for how to write code.

* [Functions](#functions)

### Naming
1. The class names use Camel case, such as ```class R3DWidget;```.
Copy link
Member

Choose a reason for hiding this comment

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

Correct. Please distinguish Qt and non-Qt classes. If not mentioned, the style refers to non-Qt classes, and you should take non-Qt classes for example.

### Naming
1. The class names use Camel case, such as ```class R3DWidget;```.
2. The name of the variable / using-declarations uses snake case,
such as ```R3DWidget * viewer;``` and ```using size_type = std::size_t;```.
Copy link
Member

Choose a reason for hiding this comment

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

Please use perfect indentation of markdown. And "```" is rst, not markdown.

3. The QT-related function name uses Camel case,
whereas non-QT-related function name uses snake case.
4. The name of the class member starts with ```m_```.
5. The name of [macros](https://en.cppreference.com/w/cpp/preprocessor/replace) starts with ```DECL_MM_```.
Copy link
Member

Choose a reason for hiding this comment

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

Let's use MM_DECL*.

3. It is highly recommended to add the reference in the comment block.

### Formatting
1. It is necessary to obey the [clang-format](https://clang.llvm.org/docs/index.html), or you must fail the github action.
Copy link
Member

Choose a reason for hiding this comment

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

clang-format is enforced by CI already. I don't think we need to write it down in the style guide.


### Formatting
1. It is necessary to obey the [clang-format](https://clang.llvm.org/docs/index.html), or you must fail the github action.
2. There exists a space between the definition of the classes/functions.
Copy link
Member

Choose a reason for hiding this comment

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

Do you mean a blank line by "space"?

### Formatting
1. It is necessary to obey the [clang-format](https://clang.llvm.org/docs/index.html), or you must fail the github action.
2. There exists a space between the definition of the classes/functions.
3. A long line should be devided to several shorter lines.
Copy link
Member

Choose a reason for hiding this comment

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

No, we have not been able to decide what is a proper length. @ThreeMonth03 , could you please make it explicit that we do not yet have a set limitation on line width?

3. A long line should be devided to several shorter lines.

### Class
1. The class should follow [the rule of five](https://en.cppreference.com/w/cpp/language/rule_of_three).
Copy link
Member

Choose a reason for hiding this comment

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

The rule of five is not exactly a coding style. Or do you mean to explicit write = default and = delete as much as possible?

Copy link
Collaborator

Choose a reason for hiding this comment

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

yes, that's what I meant.

Comment on lines 34 to 43
### Formatting
1. It is necessary to obey the [clang-format](https://clang.llvm.org/docs/index.html), or you must fail the github action.
2. There exists a space between the definition of the classes/functions.
3. A long line should be devided to several shorter lines.

### Class
1. The class should follow [the rule of five](https://en.cppreference.com/w/cpp/language/rule_of_three).

### Functions
1. The recursive function should be replaced with the iterative function, unless it is inevitable to be used.
Copy link
Member

Choose a reason for hiding this comment

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

This does not look like a coding style. It is a performance (HPC) concern. It should not be included in this file.

Copy link
Collaborator Author

@ThreeMonth03 ThreeMonth03 left a comment

Choose a reason for hiding this comment

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

  • Rename the file to /CODINGSTYLE.md
  • Remove Introduction
  • Distinguish non-Qt and Qt code
  • Review all indentation, blank lines, and markdown formatting
  • Remove recursion words

Comment on lines +1 to +2
# Coding Style

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I remove the introduction, and modify the filename to CODINGSTYLE.md.

Comment on lines +5 to +16
1. Class names use CamelCase, for example: `class CallProfiler;`.
- Qt-related classes prepend an additional `R`, like `class RLine;`.
* A class is considered Qt-related if its parent class belongs to the Qt library.

2. Variable names and using-declarations use snake_case:
- Example variable: `R3DWidget * viewer;`
- Example using-declaration: `using size_type = std::size_t;`

3. Qt-related function names use CamelCase, while non-Qt-related function names use snake_case.
- Functions are Qt-related if they belong to a Qt-related class.
- Example Qt-related function: `QMdiSubWindow * RManager::addSubWindow(Args &&... args);`
- Example non-Qt-related function: `void initialize_buffer(pybind11::module & mod);`
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I distinquish and define the Qt-related classes and functions in the list.
Additionally, I reformat the file.

Comment on lines +24 to +26
7. Acronyms within names should be treated as words instead of using ALL_CAPS_CONSTANTS.
- Example classes: `class HttpRequest;`
- Example function: `void update_geometry_impl(StaticMesh const & mh, Qt3DCore::QGeometry * geom);`
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Here is the naming rule about acronyms.


## Class

1. It's advisable to explicitly define constructors and destructors for classes whenever possible.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

According to the discussion, I modify the rule of five.

@ThreeMonth03
Copy link
Collaborator Author

ThreeMonth03 commented Jun 17, 2024

  • Rename the file to /CODINGSTYLE.md
  • Remove Introduction
  • Distinguish non-Qt and Qt code
  • Review all indentation, blank lines, and markdown formatting
  • Remove recursion words

@yungyuc I finish the modification of the document, but I'm not sure whether it could be code review.
I only modified the document and synchronized the latest main branch,
however, the github workflow failed.

@yungyuc
Copy link
Member

yungyuc commented Jun 17, 2024

Thanks, @ThreeMonth03 . The CI failure might be #365 , for which I just merged the fix @tigercosmos made in #367 . Could you please rebase to rerun the CI once the merged code has CI passed?

@ThreeMonth03
Copy link
Collaborator Author

Thanks, @ThreeMonth03 . The CI failure might be #365 , for which I just merged the fix @tigercosmos made in #367 . Could you please rebase to rerun the CI once the merged code has CI passed?

@yungyuc
Thank you for your suggestion.
The latest commit is merged, and the pr could be reviewed.

Copy link
Member

@yungyuc yungyuc left a comment

Choose a reason for hiding this comment

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

  • For Qt just say Qt. No need to be "-related".
  • When it's type alias, say type alias. Do not mistake with using-declaration.
  • Wording to distinguish C++ and Python.
  • More accurate title is "Constructors", not "Class".

## Naming

1. Class names use CamelCase, for example: `class CallProfiler;`.
- Qt-related classes prepend an additional `R`, like `class RLine;`.
Copy link
Member

Choose a reason for hiding this comment

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

Just say "Qt classes". It is redundant to write "-related".

- Qt-related classes prepend an additional `R`, like `class RLine;`.
* A class is considered Qt-related if its parent class belongs to the Qt library.

2. Variable names and using-declarations use snake_case:
Copy link
Member

Choose a reason for hiding this comment

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

It is incorrect to say using-declarations. The use case you mentioned is type aliasing: https://en.cppreference.com/w/cpp/language/type_alias . Please correct.

- Example Qt-related function: `QMdiSubWindow * RManager::addSubWindow(Args &&... args);`
- Example non-Qt-related function: `void initialize_buffer(pybind11::module & mod);`

4. Class members begin with `m_`.
Copy link
Member

Choose a reason for hiding this comment

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

Only C++


5. [Macros](https://en.cppreference.com/w/cpp/preprocessor/replace) start with `MM_DECL_`.

6. [Using-declarations](https://en.cppreference.com/w/cpp/language/using_declaration) end with `_type`.
Copy link
Member

Choose a reason for hiding this comment

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

}; /* end of modmesh */
```

3. It is highly recommended to include references in comment blocks.
Copy link
Member

Choose a reason for hiding this comment

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

Just say it is recommended.

- For C++, no specific line length limit is set.
- For Python, adhere to an 80-character limit per line.

## Class
Copy link
Member

Choose a reason for hiding this comment

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

It's more accurate to use "Constructors" as the subject.

@tigercosmos
Copy link
Collaborator

@ThreeMonth03 why closed?

@yungyuc
Copy link
Member

yungyuc commented Jun 25, 2024

@ThreeMonth03 why closed?

@ThreeMonth03 please make complete and explicit arguments before closing the PR.

@ThreeMonth03
Copy link
Collaborator Author

ThreeMonth03 commented Jun 25, 2024

@ThreeMonth03 why closed?

@yungyuc @tigercosmos
According to the suggestion of the @yungyuc ,
he suggest that I could solve the issue343 at first,
and close this PR.
Here is the new PR for issue 343.

@ThreeMonth03 ThreeMonth03 reopened this Jun 25, 2024
@yungyuc
Copy link
Member

yungyuc commented Jun 25, 2024

@ThreeMonth03 You confuse people by referring to the private discussions between you and me. Please make an argument instead of delegating the reasoning to information that is not made public.

@ThreeMonth03
Copy link
Collaborator Author

You confuse people by referring to the private discussions between you and me. Please make an argument instead of delegating the reasoning to information that is not made public.

@yungyuc I feel terribly sorry for my imprecise argument.

@tigercosmos
I think I'm unable to solve this issue now,
and I'm dealing with the issue343.
Thus, I decide to close the PR.
I'm sorry for closing the PR without any comment.

@tigercosmos
Copy link
Collaborator

@ThreeMonth03 it's okay to keep the PR open until you can continue, but it's also okay to reopen it when you are available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants