Code style



  • There are some style of code formatting.
    The greatest number of questions regarding the registration conditions. For example "compact" style:

    if (a > b) {
        qDebug() << "Good day";
    } else {
        qDebug() << "Good night";
    }
    

    and another

    
    if (a > b)
    {
        qDebug() << "Good day";
    }
    else
    {
        qDebug() << "Good night";
    }
    

    I understand, that second is more easy to read. But first is more compact.
    The similar question regarding naming of variables (private members of the class); I see the most popular ceases are:

    int name_;
    int m_name;
    

    Different employers require the use of different styles. Tell me, what kind of style is the most popular, or correct for using or so say the classic for Qt?



  • You can get a look at this for internal style, and this one.



  • @aliks-os

    to add to your example you can also write it this way

    if (a > b) 
        qDebug() << "Good day";
    else 
        qDebug() << "Good night";
    

    {} are only needed for multi line commands

    or

    a > b ? qDebug() << "Good day" : qDebug() << "Good night";
    

    in the end, it doesn't matter much, but its important, once you choose a style don't deviate from it.



  • yes, I know, I have special indicate a brace as example



  • @JohanSolo
    thank you, intersting links



  • There are other guides other than the Qt one (eve though I quite like the Qt one), the one from Google is pretty popular: https://google.github.io/styleguide/cppguide.html

    There are also some general guidelines the standard committee is trying to put together although this has less to do with "how many spaces to indent": https://github.com/isocpp/CppCoreGuidelines


  • Moderators

    I've worked with many different styles and each company/project had its own guidelines.
    In my opinion the style doesn't really matter. What matters to me is that whatever style is chosen everyone on a project sticks to it. I can get used to any style if I work with it for some time as long as it is consistently applied. Tools like clang-format are pretty neat for helping with that. The worst you can do is join a very old project and start to convince everyone that their giant codebase needs fundamental refactoring because you don't like that space over here or that m_ over there.

    If you're starting a new project just choose something you and your team can agree on. I guarantee you can't please everyone so the bigger the team the more compromises you need on where to put this or that whitespace. If you're using a lot of third party code it's also good to try and converge on the style these libraries use or at least on some sensible subset, otherwise your own code will end up an inconsistent mess.



  • @Chris-Kawa
    Many thanks for irrefragable answer. I fully agree with you.

    I was confused as the following links Qt style and CppCoreGuidelines sometimes contrary. And thanks to clang-format we can change style according the requirement of employers.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.