Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. IF-free codes

IF-free codes

Scheduled Pinned Locked Moved The Weird and The Wonderful
htmlcomtutorial
32 Posts 22 Posters 0 Views 1 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.
  • S Offline
    S Offline
    SarK0Y
    wrote on last edited by
    #1

    Good Time to all, Amici(Friends). Here, i'd like to discuss how to reduce conditional branches in the code. 1st of the all, i would like to share some tricks. Their description is here. :-D ----------------------- Thanks in Advance for attention of Yours.

    P P B L S 13 Replies Last reply
    0
    • S SarK0Y

      Good Time to all, Amici(Friends). Here, i'd like to discuss how to reduce conditional branches in the code. 1st of the all, i would like to share some tricks. Their description is here. :-D ----------------------- Thanks in Advance for attention of Yours.

      P Offline
      P Offline
      Paul Conrad
      wrote on last edited by
      #2

      The readability of the if-less code is bad. What do you have against conditional branches? I would much rather have readable code with conditional branches instead of less readable code with out them.

      "I've seen more information on a frickin' sticky note!" - Dave Kreskowiak

      S R 2 Replies Last reply
      0
      • P Paul Conrad

        The readability of the if-less code is bad. What do you have against conditional branches? I would much rather have readable code with conditional branches instead of less readable code with out them.

        "I've seen more information on a frickin' sticky note!" - Dave Kreskowiak

        S Offline
        S Offline
        SarK0Y
        wrote on last edited by
        #3

        yes, readability is worse :) but superfluous IFs make code slower. Actually, it will be extremely helpful, if new compilers shall be able to generate such blocks of IFs. :-D

        G D D 3 Replies Last reply
        0
        • S SarK0Y

          Good Time to all, Amici(Friends). Here, i'd like to discuss how to reduce conditional branches in the code. 1st of the all, i would like to share some tricks. Their description is here. :-D ----------------------- Thanks in Advance for attention of Yours.

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          I'm not going to read your blog. http://www.codeproject.com/Insider.aspx?fid=1658735&tid=4492333[^]

          1 Reply Last reply
          0
          • S SarK0Y

            Good Time to all, Amici(Friends). Here, i'd like to discuss how to reduce conditional branches in the code. 1st of the all, i would like to share some tricks. Their description is here. :-D ----------------------- Thanks in Advance for attention of Yours.

            B Offline
            B Offline
            Brisingr Aerowing
            wrote on last edited by
            #5

            That's a little iffy. Sorry

            Bob Dole

            The internet is a great way to get on the net.

            :doh: 2.0.82.7292 SP6a

            1 Reply Last reply
            0
            • S SarK0Y

              Good Time to all, Amici(Friends). Here, i'd like to discuss how to reduce conditional branches in the code. 1st of the all, i would like to share some tricks. Their description is here. :-D ----------------------- Thanks in Advance for attention of Yours.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              :omg: With this code you are a winner of obfuscated C contest.

              K 1 Reply Last reply
              0
              • S SarK0Y

                yes, readability is worse :) but superfluous IFs make code slower. Actually, it will be extremely helpful, if new compilers shall be able to generate such blocks of IFs. :-D

                G Offline
                G Offline
                Gary Wheeler
                wrote on last edited by
                #7

                SarK0Y wrote:

                superfluous IFs make code slower.

                That isn't necessarily true. Modern processors use a variety of tricks that reduce the 'cost' of unused branches. One of the problems of 'computed conditional' logic is that you incur the cost for all cases, not just the unused branch. Your overall performance actually decreases.

                Software Zen: delete this;

                C 1 Reply Last reply
                0
                • S SarK0Y

                  Good Time to all, Amici(Friends). Here, i'd like to discuss how to reduce conditional branches in the code. 1st of the all, i would like to share some tricks. Their description is here. :-D ----------------------- Thanks in Advance for attention of Yours.

                  S Offline
                  S Offline
                  Stefan_Lang
                  wrote on last edited by
                  #8

                  CP wrote:

                  The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices

                  Congratulations, your contribution was spot-on! :cool:

                  1 Reply Last reply
                  0
                  • S SarK0Y

                    Good Time to all, Amici(Friends). Here, i'd like to discuss how to reduce conditional branches in the code. 1st of the all, i would like to share some tricks. Their description is here. :-D ----------------------- Thanks in Advance for attention of Yours.

                    J Offline
                    J Offline
                    JHubSharp
                    wrote on last edited by
                    #9

                    If you did that to my codebase I'd probably avoid working with you as much as possible. You can always throw hardware at code performance. You can't throw hardware at code readability.

                    G Z 2 Replies Last reply
                    0
                    • J JHubSharp

                      If you did that to my codebase I'd probably avoid working with you as much as possible. You can always throw hardware at code performance. You can't throw hardware at code readability.

                      G Offline
                      G Offline
                      Gary Huck
                      wrote on last edited by
                      #10

                      Indeed. Thank you. As I was reading that post I wondered why the editors would choose to put that out on the daily email.

                      1 Reply Last reply
                      0
                      • S SarK0Y

                        yes, readability is worse :) but superfluous IFs make code slower. Actually, it will be extremely helpful, if new compilers shall be able to generate such blocks of IFs. :-D

                        D Offline
                        D Offline
                        dusty_dex
                        wrote on last edited by
                        #11

                        Are you aware of the 90/10 law? 90% of the time is spent in 10% of the code, and only 10% of the time in the remaining 90% of the code. If you are attempting to optimize code in the 90% of execution time, great. Otherwise leave optimizing alone. Use macros with good names to do optimizations, and it will *hopefully* make your code clearer. If you were targeting code for the ARM cpu then eliminating branches is totally pointless because.. ARM cpu has conditional branching on almost every cpu instruction, but doesn't cost extra cpu time to fetch a separate branch instruction like on many CISC cpus.

                        1 Reply Last reply
                        0
                        • S SarK0Y

                          Good Time to all, Amici(Friends). Here, i'd like to discuss how to reduce conditional branches in the code. 1st of the all, i would like to share some tricks. Their description is here. :-D ----------------------- Thanks in Advance for attention of Yours.

                          5 Offline
                          5 Offline
                          5urd
                          wrote on last edited by
                          #12

                          Oh my gosh! A program with no if-else statements! Please don't make me learn assembly! Oh, wait - I already did...

                          D 1 Reply Last reply
                          0
                          • S SarK0Y

                            Good Time to all, Amici(Friends). Here, i'd like to discuss how to reduce conditional branches in the code. 1st of the all, i would like to share some tricks. Their description is here. :-D ----------------------- Thanks in Advance for attention of Yours.

                            R Offline
                            R Offline
                            RafagaX
                            wrote on last edited by
                            #13

                            What? do i need to write 4 lines to avoid an If?, I have better things to do... ;P

                            CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...

                            1 Reply Last reply
                            0
                            • S SarK0Y

                              Good Time to all, Amici(Friends). Here, i'd like to discuss how to reduce conditional branches in the code. 1st of the all, i would like to share some tricks. Their description is here. :-D ----------------------- Thanks in Advance for attention of Yours.

                              J Offline
                              J Offline
                              jschell
                              wrote on last edited by
                              #14

                              SarK0Y wrote:

                              Here, i'd like to discuss how to reduce conditional branches in the code. 1st of the all, i would like to share some tricks.

                              I would suggest that you learn what automated profiling is. Then buy or find a tool that does that. Then learn how to use it effectively.

                              1 Reply Last reply
                              0
                              • P Paul Conrad

                                The readability of the if-less code is bad. What do you have against conditional branches? I would much rather have readable code with conditional branches instead of less readable code with out them.

                                "I've seen more information on a frickin' sticky note!" - Dave Kreskowiak

                                R Offline
                                R Offline
                                RonDsz
                                wrote on last edited by
                                #15

                                in OOP use duck typing as means to avoid using if's and case statement to invoke objects of different classes. [^]

                                B 1 Reply Last reply
                                0
                                • J JHubSharp

                                  If you did that to my codebase I'd probably avoid working with you as much as possible. You can always throw hardware at code performance. You can't throw hardware at code readability.

                                  Z Offline
                                  Z Offline
                                  Zan Lynx
                                  wrote on last edited by
                                  #16

                                  That's a bit harsh isn't it? Used in the right place, a trick like this could shave days off a long simulation run on a research cluster. Finding the right place to use something like this is always the hard part.

                                  1 Reply Last reply
                                  0
                                  • L Lost User

                                    :omg: With this code you are a winner of obfuscated C contest.

                                    K Offline
                                    K Offline
                                    KP Lee
                                    wrote on last edited by
                                    #17

                                    Monaco.Bavarian wrote:

                                    you are a winner of obfuscated C contest

                                    Thanks, I generally dislike reading C++ because its nature seems to be obfuscated = better. That one was so hard to read, I couldn't understand the point. From comments it looks like the point is that using if tests to branch to another location is a bad practice. Everything I've read leads to not using spagetti code. IE branching is good. I just read an article about bubble sorting and someone reading the article believing it was efficient. I was just going to leave the article thinking it was written by a beginner, leave him alone, but when a reader believes it is efficient. I couldn't just drop it. On initial tests, just 200K takes over 2.5 minutes. I calculated 20M would take 70 weeks. I built a less readable set of code that sorts 50M records in 14+ seconds. Less readable = much greater efficiency to me is a good thing. (At least when comparing weeks to seconds.) I didn't see the benefit of his code in the article.

                                    L 1 Reply Last reply
                                    0
                                    • S SarK0Y

                                      Good Time to all, Amici(Friends). Here, i'd like to discuss how to reduce conditional branches in the code. 1st of the all, i would like to share some tricks. Their description is here. :-D ----------------------- Thanks in Advance for attention of Yours.

                                      J Offline
                                      J Offline
                                      Josh_T
                                      wrote on last edited by
                                      #18

                                      I'm definitely a beginner but I have a question: Doesn't the program end up doing an if/then regardless of whether or not you're removing the actual syntax? There still needs to be a comparison done somewhere, or at least I thought.

                                      1 Reply Last reply
                                      0
                                      • K KP Lee

                                        Monaco.Bavarian wrote:

                                        you are a winner of obfuscated C contest

                                        Thanks, I generally dislike reading C++ because its nature seems to be obfuscated = better. That one was so hard to read, I couldn't understand the point. From comments it looks like the point is that using if tests to branch to another location is a bad practice. Everything I've read leads to not using spagetti code. IE branching is good. I just read an article about bubble sorting and someone reading the article believing it was efficient. I was just going to leave the article thinking it was written by a beginner, leave him alone, but when a reader believes it is efficient. I couldn't just drop it. On initial tests, just 200K takes over 2.5 minutes. I calculated 20M would take 70 weeks. I built a less readable set of code that sorts 50M records in 14+ seconds. Less readable = much greater efficiency to me is a good thing. (At least when comparing weeks to seconds.) I didn't see the benefit of his code in the article.

                                        L Offline
                                        L Offline
                                        Lost User
                                        wrote on last edited by
                                        #19

                                        If you use constructs like this below (or with more if else) there are much combinations to test. It would give a bad software metric if you use a test tool. We have to use test tools for our tests of space applications. But the way the guy wrote his code is more bad. A construct like below written in his if free style would be unreadable for someone else and I bet the performance would be very bad too.

                                        if ( expression1) {
                                        if ( expression2) {
                                        if ( expression3) {
                                        ...
                                        } else {
                                        ...
                                        }
                                        } else {
                                        if ( expression4) {
                                        ...
                                        } else {
                                        ...
                                        }
                                        }
                                        }
                                        else {
                                        if ( expression5) {
                                        if ( expression6) {
                                        ...
                                        } else {
                                        ...
                                        }
                                        } else {
                                        if ( expression7) {
                                        ...
                                        } else {
                                        ...
                                        }
                                        }
                                        }

                                        K 1 Reply Last reply
                                        0
                                        • L Lost User

                                          If you use constructs like this below (or with more if else) there are much combinations to test. It would give a bad software metric if you use a test tool. We have to use test tools for our tests of space applications. But the way the guy wrote his code is more bad. A construct like below written in his if free style would be unreadable for someone else and I bet the performance would be very bad too.

                                          if ( expression1) {
                                          if ( expression2) {
                                          if ( expression3) {
                                          ...
                                          } else {
                                          ...
                                          }
                                          } else {
                                          if ( expression4) {
                                          ...
                                          } else {
                                          ...
                                          }
                                          }
                                          }
                                          else {
                                          if ( expression5) {
                                          if ( expression6) {
                                          ...
                                          } else {
                                          ...
                                          }
                                          } else {
                                          if ( expression7) {
                                          ...
                                          } else {
                                          ...
                                          }
                                          }
                                          }

                                          K Offline
                                          K Offline
                                          KP Lee
                                          wrote on last edited by
                                          #20

                                          I've never encountered a business case that needed that many branches for a single set of conditions. I have encountered "cute" programmers who thought that unnecessary complexity made sense and did things that didn't make sense to me. (Throwing unrelated logic together in one big if-else if- cluster.) I'm also wondering if branching might help both the readability and maintainability of the logic and if that supercedes efficiency if it meets the business logic? IE

                                          if ( expression1) Condition1();
                                          else NotCondition1();
                                          ...
                                          void Condition1()
                                          {
                                          if ( expression2) Condition2();
                                          else NotCondition2();
                                          }
                                          void NotCondition1()
                                          {
                                          if ( expression5) Condition3();
                                          else NotCondition3();
                                          }

                                          void Condition2()
                                          {
                                          if ( expression3) {
                                          ...
                                          } else {
                                          ...
                                          }
                                          }
                                          etc.

                                          I'm not all that convinced that branching to routines won't be performant as well as more readable. I just recently read about a bubble sort routine. OK, old news + inexperienced developer = time to move on. One of the respondents liked how easy it was to read and how efficient it was. Man, I can't let that misinformation go. I explained how it rated as N^2 efficiency. (IE double the number of fields will quadruple the time to compute.) 200K records sorted in over 2 minutes. (The posted program sorted 11 hardcoded values in an int[] array. Of course that would be fast.) I calculated a million numbers would sort in about 56 minutes. I ran 50 million numbers through a binary sort process in just over 14 seconds. The time consistently increased by a second for just under 3 million numbers being added. A LOT of if tests being done in recursive routine calls. Slightly slower than List's built-in Sort routine, List blows up faster with memory problems as the numbers increase. (Can't do 20 million int[] and 20 million List)

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups