简体   繁体   中英

Avoiding non symbolic constants in Qt

One thing that often comes up in code reviews I participate in is 'magic numbers' inline in code as being a bad thing.

The preference is that a symbol be assigned somewhere.

Now what I am working with at the moment is in the QT sdk. The class member QButtonGroup.checkedId() will return -1 as a magic number to signify that there are no radio buttons in the group selected.

And if I write something like

if ( buttongroup->checkedId() == -1 )
{
   //yadda yadda
}

it will come up in the code review.

While certainly I can define a const static int symbol to give me that -1 , I'd much rather use something in the Qt namespace if it has a relevant constant defined.

Is there such a symbol already defined??

Code reviews are about your code not the code of 3rd party libraries so you can't and shouldn't deal with it. Qt doesn't have any named entity to compare against so you have to compare the resulting code against -1 . There is a bright side about it, though: -1 is a broadly understood magic number so there should not be any misunderstanding — it is used in many different languages and libraries. However bad it is it is here and we have to live with it.

No such symbol is predefined; you would have to define it yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM