[Development] Naming convention for (scoped) enums

Tor Arne Vestbø Tor.arne.Vestbo at qt.io
Tue Aug 14 11:30:26 CEST 2018


> On 14 Aug 2018, at 09:16, Alex Blasche <alexander.blasche at qt.io> wrote:
> 
>> I
>> do not think that using class enums inside existing classes is a win for code
>> readability/writability:
>> 
>> When you have
>> 
>> switch (point->state()) {
>> 
>> It's pretty obvious what case QQuickEventPoint::Pressed: refers to. being overly
>> explicit with case QQuickEventPoint::State::Pressed: just adds more to type for
>> no reason.
> 
> Similar to Guiseppe, I would argue that the above enum does not comply with the convention for unscoped enums either.

I would disagree with that interpretation.

> The enum value would have to repeat at least some part of the enum type. That's a rule that has existed for decades now.

That circular logic 😊 Or at least arguing that we should maintain the policy not because it makes sense, but just because we’ve done so in the past. 

You are not questioning _why_ we have done so in the past (for decades as you say), which should be the input to whether or not we want to maintain the policy as is, or update it to match the current landscape of Qt and C++. 

The mere fact that we’ve had a policy does not have any value.

> Therefore QQuickEventPoint::State::Pressed is no more verbose than QQuickEventPoint::PressedState. 

Again, the policy does not mandate that in it’s current form in my reading of it, and even if it did, that would be a bad policy and we should fix it, not just blindly accept it as set in stone, because we’ve followed it "for decades".

> In summary, there is very little verbosity added even by existing rules. Mind you, Qt has always favoured readability and accepted verbosity as a consequence in times of code completion.

Code completion (for those that use it), only helps with writing code, and as we all know, most time is spent reading code 😊 

> There is one more very important aspect. Scoped enums can have dedicated types and are type safe. This could have easily caught issues like (which coincidently was pointed out to me this morning):
> 
> https://codereview.qt-project.org/#/c/236736/
> 
> And I believe that is a very good reason to still prefer scoped enums.

How frequent is this class of bugs? 

Clang warns about this with -Wenum-compare for both unstopped and scoped enums, we could easily make that and error and have the best of both.

Cheers,
Tor Arne 


> 
> --
> Alex
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development



More information about the Development mailing list