[Development] Best practice for defining an integer constant

Mandeep Sandhu mandeepsandhu.chd at gmail.com
Fri Feb 14 12:43:23 CET 2014


I agree with you if the constant represents some sort of 'range' (like the
error code ex you gave). But what about single value constants like a 'max
of something'? Won't a const be a better fit here rather having an enum
with just 1 entry?

-mandeep



On Fri, Feb 14, 2014 at 5:02 PM, Blasche Alexander <
Alexander.Blasche at digia.com> wrote:

>  Hi.
>
> An enum has the advantage that you have a certain level of type safety and
> in my opinion it makes the API much more readable.
>
> foo(int x)
>
> is far less certain on what to pass then
>
> foo(ErrorCode x)
>
> I immediately see what the range of possible values is (based on the enum
> definition) while the int could be any arbitrary int constant you came
> along. QtCreator and its predictive input/tab completion will thank you for
> it.
>
>  --
>
> Alex
>   ------------------------------
> *From:* development-bounces+alexander.blasche=digia.com at qt-project.org[development-bounces+alexander.blasche=
> digia.com at qt-project.org] on behalf of Mandeep Sandhu [
> mandeepsandhu.chd at gmail.com]
> *Sent:* Friday, February 14, 2014 11:29
> *To:* development at qt-project.org
> *Subject:* [Development] Best practice for defining an integer constant
>
>    I have a need for defining an integer constant that'll be used for
> initializing a member variable of a private class.
>
>  The Qt coding conventions (http://qt-project.org/wiki/Coding-Conventions)
> recommend using an enum over 'const int'.
>
> The rationale given there is that an enum will be replaced at compile-time
> resulting in 'faster code'. Won't that be the case with 'const int' as
> well? I think a 'const int' will be inlined in the code. CMIIW.
>
>  Also, there are a couple of instances in our current code where such
> (static and non-static) constants are used. Whats the recommended way?
>
>  Thanks,
>  -mandeep
>
>
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20140214/d753af65/attachment.html>


More information about the Development mailing list