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

QEP 314: Code style and practice guidelines #314

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

Conversation

nyalldawson
Copy link
Contributor

This QEP documents coding standards and conventions used throughout the QGIS code base. Developers are required to follow these standards when submitting code to QGIS.

It is initially intended as a formal description of CURRENT CODE PRACTICES ONLY, which can be later modified and revised (in an atomic manner) if we want to change any of these standards. As such, please refrain from using this initial request as a place to discuss changes to current practice. Rather, let's keep discussion focused on whether this is an accurate and complete description of our current coding practices. 👍

The end goal here is having a formal policy in place describing all our coding conventions, which will help ease new developers into contributing effectively to QGIS and remove some of the unspoken assumptions surrounding QGIS development.

@nyalldawson
Copy link
Contributor Author

Note: I've arbitrarily restarted the number at 400 here -- this seems right after the restructuring of QEPs to PRs.

qep-400-coding-practice.md Outdated Show resolved Hide resolved
@elpaso
Copy link

elpaso commented Jan 1, 2025

@nyalldawson looks goo to me, thank you for taking care of this.

Should we also mention the use of (possibly redundant but explicit) std::as_const in for loops?

Copy link

@troopa81 troopa81 left a comment

Choose a reason for hiding this comment

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

Should we also include something about using the new settings API instead of the raw string Qt api?

@uclaros
Copy link

uclaros commented Jan 2, 2025

Some more coding conventions we use that are not listed:

  • Header/license for each file
  • Includes
    • Separate qgis from qt includes
    • Include moc file for qobjects
    • Include qt as #include <QMap> and not #include "qmap.h"
    • Prefer forward declaration if possible instead of including from within header files
  • Use QStringLiteral for untranslated literals, QLatin1String for comparisons
  • Don't use qDebug(), use QgsDebugError/QgsDebugMsgLevel (we still need to define some logic for which level < 1 to use)
  • methods that are actually const should be marked const

@troopa81
Copy link

troopa81 commented Jan 6, 2025

@uclaros Maybe that should come in a separate PR. Do you mind if I merge this one and we iterate later on it?

@nyalldawson
Copy link
Contributor Author

@troopa81

Please don't merge -- I want this initial version to be complete.

@troopa81
Copy link

troopa81 commented Jan 7, 2025

Please don't merge -- I want this initial version to be complete.

OK

@m-kuhn m-kuhn marked this pull request as draft January 7, 2025 07:30
@m-kuhn
Copy link
Member

m-kuhn commented Jan 7, 2025

@nyalldawson I have converted to draft, with the approvals in place already it will be easier to monitor, when you are happy with it.

qep-400-coding-practice.md Outdated Show resolved Hide resolved
@3nids
Copy link
Member

3nids commented Jan 7, 2025

One small question regarding the process and scope of QEPs.

To me a QEP should be definitive once merged/accepted.

For this one, it looks like it will be a continuous evolution. So is this the correct place?

For code style, we already have this: https://github.com/qgis/QGIS-Documentation/blob/master/docs/developers_guide/codingstandards.rst which sounds more appropriate than a QEP.

@nyalldawson
Copy link
Contributor Author

@uclaros
Renamed to 314.

@nyalldawson
Copy link
Contributor Author

@elpaso

Should we also mention the use of (possibly redundant but explicit) std::as_const in for loops?

Ok, I've added that for explicitly non const containers only. I don't think we currently have agreement on whether this should be required for already const containers, so if you want that then let's start that change discussion separately after this QEP is formalised.

@nyalldawson
Copy link
Contributor Author

@troopa81

Should we also include something about using the new settings API instead of the raw string Qt api?

No, IMO that one would belong in a formal policy regarding settings usage. (Ideally with code examples for both C++ and python)

@nyalldawson
Copy link
Contributor Author

@3nids

One small question regarding the process and scope of QEPs.
To me a QEP should be definitive once merged/accepted.
For this one, it looks like it will be a continuous evolution. So is this the correct place?

Yes, that's correct. Since #310 QEPs can now be mutable, exactly so that we have a way of formally handling mutable QEPs just like this one.

There's a mix of needs for QEPs. We have short lived project based ones like #318 which likely will remaining unchanged forever after the initial acceptance. Then we have "living" specs like this one, or eg one for backport policies, pull request standards, API stability requirements, etc. These ones are inherently mutable and trying to handle them via seperate independent QEPs where every change is a new document is impractical. The new approach allows for a single point of truth for living specifications like this.

For code style, we already have this: https://github.com/qgis/QGIS-Documentation/blob/master/docs/developers_guide/codingstandards.rst which sounds more appropriate than a QEP.

That page should be removed and changed to a link to this formal specification after merging.

@nyalldawson
Copy link
Contributor Author

@uclaros

Separate qgis from qt includes
Include qt as #include and not #include "qmap.h"
Use QStringLiteral for untranslated literals, QLatin1String for comparisons
Don't use qDebug(), use QgsDebugError/QgsDebugMsgLevel

Thanks, I've added these

Include moc file for qobjects
Header/license for each file

These are already explicitly covered by tests on the CI, I'll omit them as a result.

Prefer forward declaration if possible instead of including from within header files
methods that are actually const should be marked const

These fall under "best practice modern C++" , so I don't want to explicitly add them here. (And the second is already covered by cppcheck on CI)

@nyalldawson nyalldawson marked this pull request as ready for review January 8, 2025 00:42
@nyalldawson
Copy link
Contributor Author

Ok, all comments addressed. How are we looking now?

@nyalldawson nyalldawson changed the title QEP 400: Code style and practice guidelines QEP 314: Code style and practice guidelines Jan 8, 2025
@elpaso
Copy link

elpaso commented Jan 8, 2025

@elpaso

Should we also mention the use of (possibly redundant but explicit) std::as_const in for loops?

Ok, I've added that for explicitly non const containers only. I don't think we currently have agreement on whether this should be required for already const containers, so if you want that then let's start that change discussion separately after this QEP is formalised.

IIRC it was @m-kuhn back in the days who corrected my code when I didn't use as_const over an already const container. I have always thought that it was pointless but also harmless (it would be a no-op in that case), I'm fine with not requiring as_const over an already const container.

@m-kuhn
Copy link
Member

m-kuhn commented Jan 8, 2025

I do think that reviewer time and focus is not best invested to check if a variable over which we loop is already const in a given context, but I am open how we handle this. I would keep this discussion separate and not block this, given there is no agreement yet.

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

Successfully merging this pull request may close these issues.

8 participants