Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. The Lounge
  4. Code style
QtWS25 Last Chance

Code style

Scheduled Pinned Locked Moved Solved The Lounge
8 Posts 5 Posters 2.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aliks-os
    wrote on last edited by aliks-os
    #1

    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?

    J.HilkJ 1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by JohanSolo
      #2

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

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      A 1 Reply Last reply
      3
      • A aliks-os

        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?

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @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.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        1
        • A Offline
          A Offline
          aliks-os
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • JohanSoloJ JohanSolo

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

            A Offline
            A Offline
            aliks-os
            wrote on last edited by
            #5

            @JohanSolo
            thank you, intersting links

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              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

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              2
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                A 1 Reply Last reply
                6
                • Chris KawaC Chris Kawa

                  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.

                  A Offline
                  A Offline
                  aliks-os
                  wrote on last edited by
                  #8

                  @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.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved