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: Relax prohibition against "auto" #319

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

Conversation

nyalldawson
Copy link
Contributor

Allow use of auto for variable types when the variable type is explicitly stated during its initialization.

Specifically:

auto may be used for variable types if the type is explicit during variable initialization. Eg

// allowed, as the QgsPoint type is explicit during initialization:
auto pointObject = QgsPoint( 3, 4 );
// allowed, as the std::unique_ptr< QgsPoint >, std::shared_ptr< QgsPoint > types are explicit during initialization:
auto pointUniquePointer = std::make_unique< QgsPoint >( 3, 4 );
auto pointSharedPointer = std::make_shared< QgsPoint >( 3, 4 );

// NOT allowed, the argument types for the std::tuple are not explicit:
auto myTuple = std::make_tuple( 0, 5 );

Allow use of "auto" for variable types when the variable type
is explicitly stated during its initialization
@nyalldawson
Copy link
Contributor Author

FYI @rouault

qep-314-coding-style.md Outdated Show resolved Hide resolved
@m-kuhn
Copy link
Member

m-kuhn commented Jan 23, 2025

What is the motivation for this?
While it's less typing for the author it's more brainwork for readers.

Comment on lines 84 to 85
// allowed, as the QgsPoint type is explicit during initialization:
auto pointObject = QgsPoint( 3, 4 );
Copy link
Member

Choose a reason for hiding this comment

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

Is there any reason we would want to write this instead of QgsPoint pointObject( 3, 4 ); ? The notation with auto is longer, and may involve extra copy of the temporary object?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@wonder-sk

Is there any reason we would want to write this instead of QgsPoint pointObject( 3, 4 ); ? The notation with auto is longer, and may involve extra copy of the temporary object?

That's a good point. Should we reword this change to only allow for some explicit types like std::unique_ptr / std::shared_ptr ?

Copy link
Member

Choose a reason for hiding this comment

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

Should we reword this change to only allow for some explicit types like std::unique_ptr / std::shared_ptr ?

yeah I think that would be good...

@nyalldawson
Copy link
Contributor Author

nyalldawson commented Jan 23, 2025

@m-kuhn

What is the motivation for this?
While it's less typing for the author it's more brainwork for readers.

It's to make super verbose lines like these less painful to write:

std::unique_ptr< QgsProcessingParameter > extentParam = std::make_unique< QgsProcessingParameter >( .... ); 

in this case it's still immediately clear to reviewers/readers if it's written like:

auto extentParam = std::make_unique< QgsProcessingParameter >( .... ); 

@nirvn
Copy link

nirvn commented Jan 23, 2025

Being allergic to auto, +1 to restricting this relaxation to only std::unique_ptr / std::shared_ptr as suggested above.

@elpaso
Copy link

elpaso commented Jan 23, 2025

Being an auto worshiper, +1 to relaxing.

@nirvn
Copy link

nirvn commented Jan 23, 2025

@elpaso , heh, that had my laugh out loud :)

@troopa81
Copy link

not a great fan of auto so +1 to restricting to only std::unique_ptr / std::shared_ptr.

@dvdkon
Copy link

dvdkon commented Jan 23, 2025

Now I feel like I have to voice my opinion too :) I like auto and would prefer allowing it instead of any non-primitive types, especially when the rough type is clear from context.
For example, I feel that QgsPointCloudIndex index = mLayer->index(); can be shortened to auto index = mLayer->index(); without any impact to readability.

By the way, thanks @nyalldawson for these coding style QEPs. A comprehensive coding standard was something I sorely missed when starting with QGIS development.

@nyalldawson
Copy link
Contributor Author

@dvdkon

For example, I feel that QgsPointCloudIndex index = mLayer->index(); can be shortened to auto index = mLayer->index(); without any impact to readability.

My issue with that is that it requires an IDE to actually determine the object type. Eg in this case "index()" could quite validly return a QgsSpatialIndex, a QgsKdBushSpatialIndex, or something else entirely (a QHash? A QMap?). In short, it makes pull request reviews orders of magnitude more difficult.

@m-kuhn
Copy link
Member

m-kuhn commented Jan 23, 2025

+1 for non trivial sounds good. Are iterators listed as acceptable somewhere already?

@dvdkon
Copy link

dvdkon commented Jan 27, 2025

Small note: If auto is going to be restricted to some exhaustive list of cases, I think initialisation with new should also be included.
It's not nice or modern, but it's still used in many parts of the codebase.

@m-kuhn
Copy link
Member

m-kuhn commented Jan 27, 2025

Small note: If auto is going to be restricted to some exhaustive list of cases, I think initialisation with new should also be included. It's not nice or modern, but it's still used in many parts of the codebase

I think this is worth discussing in a different proposal (to keep this one focussed), there are a few considerations to be made but in general it's a desirable direction IMHO.

@rouault
Copy link
Contributor

rouault commented Jan 27, 2025

I think initialisation with new should also be included.

generally speaking, we should avoid using new as much as possible and favor smart pointers (std::make_unique, etc) where possible

@dvdkon
Copy link

dvdkon commented Jan 27, 2025

generally speaking, we should avoid using new as much as possible and favor smart pointers (std::make_unique, etc) where possible

I definitely agree, but it's used in the codebase today and it's not always possible to sanely replace it with a smart pointer.

In the future, I'd love to see some discussion on how we can further move away from raw pointers (the main blockers right now are Qt and SIP AFAIK), but for now I just don't want to have to keep writing LongClassName *longObjectName = new LongClassName;.

@nyalldawson
Copy link
Contributor Author

I think this is worth discussing in a different proposal (to keep this one focused)

+1.

@dvdkon would you like to create that QEP?

@nyalldawson
Copy link
Contributor Author

Ok, based on feedback this is now restricted to std::unique_ptr and std::shared_ptr only.

qep-314-coding-style.md Outdated Show resolved Hide resolved
Co-authored-by: Matthias Kuhn <[email protected]>
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