[Development] Best practice for defining an integer constant

Blasche Alexander Alexander.Blasche at digia.com
Fri Feb 14 12:32:30 CET 2014


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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20140214/c996b696/attachment.html>


More information about the Development mailing list