Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Conditional operator as a statement
Forum Updated to NodeBB v4.3 + New Features

Conditional operator as a statement

Scheduled Pinned Locked Moved Unsolved C++ Gurus
40 Posts 9 Posters 775 Views 5 Watching
  • 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.
  • J.HilkJ J.Hilk

    @JonB I would have gone with a lambda function and a full switch case approach. We're dealing with enums after all, and it screams at me: "THIS WILL EXPAND TO MORE FILES!"

    JonBJ Online
    JonBJ Online
    JonB
    wrote last edited by JonB
    #31

    @J.Hilk said in Conditional operator as a statement:

    lambda function and a full switch case approach

    Nah....!! KISS!! :)

    And for the record I wouldn't use a switch statement to return a simple value where there are only 2 explicit case and a default. Why write a multiline essay to pick between a couple of literal string values? Of course it's only IMHO, and each to their own....

    J.HilkJ 1 Reply Last reply
    0
    • JonBJ JonB

      @J.Hilk said in Conditional operator as a statement:

      lambda function and a full switch case approach

      Nah....!! KISS!! :)

      And for the record I wouldn't use a switch statement to return a simple value where there are only 2 explicit case and a default. Why write a multiline essay to pick between a couple of literal string values? Of course it's only IMHO, and each to their own....

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote last edited by
      #32

      @JonB I value readability over simplicity for simplicity’s sake..


      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.

      JonBJ 1 Reply Last reply
      0
      • J.HilkJ J.Hilk

        @JonB I value readability over simplicity for simplicity’s sake..

        JonBJ Online
        JonBJ Online
        JonB
        wrote last edited by JonB
        #33

        @J.Hilk I agree.
        And personally I find a couple of ? :s, in one line, as more readable than a multiple line switch statement and a lambda. Each to their own :)

        J.HilkJ 1 Reply Last reply
        0
        • JonBJ JonB

          @J.Hilk I agree.
          And personally I find a couple of ? :s, in one line, as more readable than a multiple line switch statement and a lambda. Each to their own :)

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote last edited by J.Hilk
          #34

          @JonB

          auto winter_stats_filename = [](int year, Type type) -> std::string {
              const char* suffix = "mean"; // Fallback
              switch (type) {
                  case Type::MIN:  suffix = "min";  break;
                  case Type::MAX:  suffix = "max";  break;
                  case Type::MEAN: suffix = "mean"; break;
                  default:
                      assert(false && "winter_stats_filename: unexpected Type value");
                      break;
              }
              return "stats-" + std::to_string(year) + "-winter-" + std::string(suffix) + ".csv";
          };
          
          std::ifstream in_winter(winter_stats_filename(year, type));
          

          I agree, sometimes I have the correct opinion and sometime the others have to wrong opinion :P


          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.

          JonBJ 1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            @JonB

            auto winter_stats_filename = [](int year, Type type) -> std::string {
                const char* suffix = "mean"; // Fallback
                switch (type) {
                    case Type::MIN:  suffix = "min";  break;
                    case Type::MAX:  suffix = "max";  break;
                    case Type::MEAN: suffix = "mean"; break;
                    default:
                        assert(false && "winter_stats_filename: unexpected Type value");
                        break;
                }
                return "stats-" + std::to_string(year) + "-winter-" + std::string(suffix) + ".csv";
            };
            
            std::ifstream in_winter(winter_stats_filename(year, type));
            

            I agree, sometimes I have the correct opinion and sometime the others have to wrong opinion :P

            JonBJ Online
            JonBJ Online
            JonB
            wrote last edited by JonB
            #35

            @J.Hilk
            To pick whether you want min, max or mean in a string I have to read through 10 lines of your code (and check a lambda for sanity to add to it). Madness! :)

            I can grasp

            var amount = (type == MIN) ? "min" : (type == MAX) ? "max" : "mean";
            

            at a single glance.

            (Yes, I know you have added enum-range checking in your code which adds a few lines compared to mine.)

            J.HilkJ 1 Reply Last reply
            0
            • JonBJ JonB

              @J.Hilk
              To pick whether you want min, max or mean in a string I have to read through 10 lines of your code (and check a lambda for sanity to add to it). Madness! :)

              I can grasp

              var amount = (type == MIN) ? "min" : (type == MAX) ? "max" : "mean";
              

              at a single glance.

              (Yes, I know you have added enum-range checking in your code which adds a few lines compared to mine.)

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote last edited by
              #36

              @JonB I'm sorry but very time I see chained ternary operators, I get an Undertale rash.


              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.

              JonBJ 1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @JonB I'm sorry but very time I see chained ternary operators, I get an Undertale rash.

                JonBJ Online
                JonBJ Online
                JonB
                wrote last edited by
                #37

                @J.Hilk Fair enough! But what is "an Undertale rash", Google talks about it in some game but that's it?

                J.HilkJ 1 Reply Last reply
                0
                • JonBJ JonB

                  @J.Hilk Fair enough! But what is "an Undertale rash", Google talks about it in some game but that's it?

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote last edited by
                  #38

                  @JonB Undertale is a game that’s kind of infamous among programmers for having huge, messy if-else chains in its code.
                  So when I say “Undertale rash,” I mean I get an allergic reaction to code that looks like that


                  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.

                  JonBJ 1 Reply Last reply
                  1
                  • Chris KawaC Offline
                    Chris KawaC Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote last edited by
                    #39

                    I really don't like lambdas used like this. Pack it in a function and you first have to go to the end of it to see what it's doing and then go back up to see how it is doing it. Have 2 or 3 of those in a function and the flow is completely ruined - you have to jump around and scroll multiple screens up and down to figure out what's going on.
                    I'm with @JonB on this - readability doesn't mean verbosity. Often fewer words express the intent better than paragraphs of syntax with sprinkles of what the code is actually doing.

                    1 Reply Last reply
                    1
                    • J.HilkJ J.Hilk

                      @JonB Undertale is a game that’s kind of infamous among programmers for having huge, messy if-else chains in its code.
                      So when I say “Undertale rash,” I mean I get an allergic reaction to code that looks like that

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote last edited by
                      #40

                      @J.Hilk said in Conditional operator as a statement:

                      So when I say “Undertale rash,” I mean I get an allergic reaction to code that looks like that

                      Not knowing about the game or its code, I wondered if you intended "Undertail rash" and were thinking of a rash on your private back bits! :)

                      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