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. The Lounge
  3. if else Style

if else Style

Scheduled Pinned Locked Moved The Lounge
tutorial
79 Posts 33 Posters 5 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.
  • B Offline
    B Offline
    BernardIE5317
    wrote on last edited by
    #1

    Greetings & Kind Regards I seek advice and knowledge of others' practice re/ a minor matter of style. In particular to be specific I vacillate betwixt and between two forms of final statement of if ... else ... series of statements assuming every possible condition is accounted for as shown in simple specimen / example / sample below (not snippet as it is not a snip of a larger code base). 1st style : if(expression == true) {...} else {...} 2nd style : if(expression == true) {...} else if(expression == false) {...} Note it is the final statement I am inquiring regards. Do your kind selves accept the default final result as shown in 1st style or perform explicit unnecessary test as shown in 2nd style for no other reason than to make the code self documenting and easier to understand in particular for a complex lengthy series of conditions. btw in 2nd style on occasion I add as final statement else throw who_the_heck_knows_what_happened; Thank You Kindly [edit] I have concluded kind Jacquers is quite correct. As it happened upon some coding just now of a simple series of such it was evident an explicit final test results in confusion as it is clearly not necessary. However in a lengthy complex series an explicit test makes code self documenting and so simpler to understand. KISS

    J L Sander RosselS J A 23 Replies Last reply
    0
    • B BernardIE5317

      Greetings & Kind Regards I seek advice and knowledge of others' practice re/ a minor matter of style. In particular to be specific I vacillate betwixt and between two forms of final statement of if ... else ... series of statements assuming every possible condition is accounted for as shown in simple specimen / example / sample below (not snippet as it is not a snip of a larger code base). 1st style : if(expression == true) {...} else {...} 2nd style : if(expression == true) {...} else if(expression == false) {...} Note it is the final statement I am inquiring regards. Do your kind selves accept the default final result as shown in 1st style or perform explicit unnecessary test as shown in 2nd style for no other reason than to make the code self documenting and easier to understand in particular for a complex lengthy series of conditions. btw in 2nd style on occasion I add as final statement else throw who_the_heck_knows_what_happened; Thank You Kindly [edit] I have concluded kind Jacquers is quite correct. As it happened upon some coding just now of a simple series of such it was evident an explicit final test results in confusion as it is clearly not necessary. However in a lengthy complex series an explicit test makes code self documenting and so simpler to understand. KISS

      J Offline
      J Offline
      Jacquers
      wrote on last edited by
      #2

      It really depends on the logic you're trying to achieve. It's going to depend on the context it's in. And yes, the first style is fine if that's what you're intending to do.

      1 Reply Last reply
      0
      • B BernardIE5317

        Greetings & Kind Regards I seek advice and knowledge of others' practice re/ a minor matter of style. In particular to be specific I vacillate betwixt and between two forms of final statement of if ... else ... series of statements assuming every possible condition is accounted for as shown in simple specimen / example / sample below (not snippet as it is not a snip of a larger code base). 1st style : if(expression == true) {...} else {...} 2nd style : if(expression == true) {...} else if(expression == false) {...} Note it is the final statement I am inquiring regards. Do your kind selves accept the default final result as shown in 1st style or perform explicit unnecessary test as shown in 2nd style for no other reason than to make the code self documenting and easier to understand in particular for a complex lengthy series of conditions. btw in 2nd style on occasion I add as final statement else throw who_the_heck_knows_what_happened; Thank You Kindly [edit] I have concluded kind Jacquers is quite correct. As it happened upon some coding just now of a simple series of such it was evident an explicit final test results in confusion as it is clearly not necessary. However in a lengthy complex series an explicit test makes code self documenting and so simpler to understand. KISS

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

        The second requires more "comprehension" (reading) time. "Else" means "everything else". And beside "true" and "false", there may be "no value"; e.g. tri-value checkboxes / radio buttons.

        "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

        T 1 Reply Last reply
        0
        • B BernardIE5317

          Greetings & Kind Regards I seek advice and knowledge of others' practice re/ a minor matter of style. In particular to be specific I vacillate betwixt and between two forms of final statement of if ... else ... series of statements assuming every possible condition is accounted for as shown in simple specimen / example / sample below (not snippet as it is not a snip of a larger code base). 1st style : if(expression == true) {...} else {...} 2nd style : if(expression == true) {...} else if(expression == false) {...} Note it is the final statement I am inquiring regards. Do your kind selves accept the default final result as shown in 1st style or perform explicit unnecessary test as shown in 2nd style for no other reason than to make the code self documenting and easier to understand in particular for a complex lengthy series of conditions. btw in 2nd style on occasion I add as final statement else throw who_the_heck_knows_what_happened; Thank You Kindly [edit] I have concluded kind Jacquers is quite correct. As it happened upon some coding just now of a simple series of such it was evident an explicit final test results in confusion as it is clearly not necessary. However in a lengthy complex series an explicit test makes code self documenting and so simpler to understand. KISS

          Sander RosselS Offline
          Sander RosselS Offline
          Sander Rossel
          wrote on last edited by
          #4

          if (expression)
          {

          }
          else
          {

          }

          Everything else is just unnecessary typing/reading.

          Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript

          CPalliniC pkfoxP T K 4 Replies Last reply
          0
          • Sander RosselS Sander Rossel

            if (expression)
            {

            }
            else
            {

            }

            Everything else is just unnecessary typing/reading.

            Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Indeed.

            "In testa che avete, Signor di Ceprano?" -- Rigoletto

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • Sander RosselS Sander Rossel

              if (expression)
              {

              }
              else
              {

              }

              Everything else is just unnecessary typing/reading.

              Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript

              pkfoxP Offline
              pkfoxP Offline
              pkfox
              wrote on last edited by
              #6

              I agree :thumbsup:

              In a closed society where everybody's guilty, the only crime is getting caught. In a world of thieves, the only final sin is stupidity. - Hunter S Thompson - RIP

              1 Reply Last reply
              0
              • B BernardIE5317

                Greetings & Kind Regards I seek advice and knowledge of others' practice re/ a minor matter of style. In particular to be specific I vacillate betwixt and between two forms of final statement of if ... else ... series of statements assuming every possible condition is accounted for as shown in simple specimen / example / sample below (not snippet as it is not a snip of a larger code base). 1st style : if(expression == true) {...} else {...} 2nd style : if(expression == true) {...} else if(expression == false) {...} Note it is the final statement I am inquiring regards. Do your kind selves accept the default final result as shown in 1st style or perform explicit unnecessary test as shown in 2nd style for no other reason than to make the code self documenting and easier to understand in particular for a complex lengthy series of conditions. btw in 2nd style on occasion I add as final statement else throw who_the_heck_knows_what_happened; Thank You Kindly [edit] I have concluded kind Jacquers is quite correct. As it happened upon some coding just now of a simple series of such it was evident an explicit final test results in confusion as it is clearly not necessary. However in a lengthy complex series an explicit test makes code self documenting and so simpler to understand. KISS

                J Offline
                J Offline
                Jorgen Andersson
                wrote on last edited by
                #7

                Second style was invented at the Department of Redundency Department.

                Wrong is evil and must be defeated. - Jeff Ello

                B 1 Reply Last reply
                0
                • J Jorgen Andersson

                  Second style was invented at the Department of Redundency Department.

                  Wrong is evil and must be defeated. - Jeff Ello

                  B Offline
                  B Offline
                  BernardIE5317
                  wrote on last edited by
                  #8

                  You can say that again.

                  L 1 Reply Last reply
                  0
                  • B BernardIE5317

                    You can say that again.

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

                    BernardIE5317 wrote:

                    You can say that again.

                    Second style was invented at the Department of Redundency Department.

                    1 Reply Last reply
                    0
                    • B BernardIE5317

                      Greetings & Kind Regards I seek advice and knowledge of others' practice re/ a minor matter of style. In particular to be specific I vacillate betwixt and between two forms of final statement of if ... else ... series of statements assuming every possible condition is accounted for as shown in simple specimen / example / sample below (not snippet as it is not a snip of a larger code base). 1st style : if(expression == true) {...} else {...} 2nd style : if(expression == true) {...} else if(expression == false) {...} Note it is the final statement I am inquiring regards. Do your kind selves accept the default final result as shown in 1st style or perform explicit unnecessary test as shown in 2nd style for no other reason than to make the code self documenting and easier to understand in particular for a complex lengthy series of conditions. btw in 2nd style on occasion I add as final statement else throw who_the_heck_knows_what_happened; Thank You Kindly [edit] I have concluded kind Jacquers is quite correct. As it happened upon some coding just now of a simple series of such it was evident an explicit final test results in confusion as it is clearly not necessary. However in a lengthy complex series an explicit test makes code self documenting and so simpler to understand. KISS

                      A Offline
                      A Offline
                      Amarnath S
                      wrote on last edited by
                      #10

                      BernardIE5317 wrote:

                      final statement else throw who_the_heck_knows_what_happened

                      In such cases, would a switch case statement be more appropriate? I mean when there are more than 2 choices.

                      B 1 Reply Last reply
                      0
                      • A Amarnath S

                        BernardIE5317 wrote:

                        final statement else throw who_the_heck_knows_what_happened

                        In such cases, would a switch case statement be more appropriate? I mean when there are more than 2 choices.

                        B Offline
                        B Offline
                        BernardIE5317
                        wrote on last edited by
                        #11

                        Thank you for the thought provoking suggestion. Please consider per below. if(a==true && b==true) {...} else if(a==true && b == false) {...} else if(a==false && b == true) {...} else if(a==false && b == false) {...} else throw who_the_heck_knows_what_happened; // just for the heck of it

                        1 Reply Last reply
                        0
                        • B BernardIE5317

                          Greetings & Kind Regards I seek advice and knowledge of others' practice re/ a minor matter of style. In particular to be specific I vacillate betwixt and between two forms of final statement of if ... else ... series of statements assuming every possible condition is accounted for as shown in simple specimen / example / sample below (not snippet as it is not a snip of a larger code base). 1st style : if(expression == true) {...} else {...} 2nd style : if(expression == true) {...} else if(expression == false) {...} Note it is the final statement I am inquiring regards. Do your kind selves accept the default final result as shown in 1st style or perform explicit unnecessary test as shown in 2nd style for no other reason than to make the code self documenting and easier to understand in particular for a complex lengthy series of conditions. btw in 2nd style on occasion I add as final statement else throw who_the_heck_knows_what_happened; Thank You Kindly [edit] I have concluded kind Jacquers is quite correct. As it happened upon some coding just now of a simple series of such it was evident an explicit final test results in confusion as it is clearly not necessary. However in a lengthy complex series an explicit test makes code self documenting and so simpler to understand. KISS

                          T Offline
                          T Offline
                          trønderen
                          wrote on last edited by
                          #12

                          I do hope that the "==true" and "==false" are not meant literally! A programmer comparing a logical expression against "true" or "false" have not understood what is meant by a logical expression. I wonder how many of those programming that way also speak that way! "If you have a moment to spare is true, I want a talk with you", or "If the door is unlocked is false, you'll find the key under the door mat" - noone that I know speaks that way. I have met a few programmers who program that way, but I never heard any of them speak with "is true" or "is false".

                          Religious freedom is the freedom to say that two plus two make five.

                          B P M 4 Replies Last reply
                          0
                          • L Lost User

                            The second requires more "comprehension" (reading) time. "Else" means "everything else". And beside "true" and "false", there may be "no value"; e.g. tri-value checkboxes / radio buttons.

                            "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                            T Offline
                            T Offline
                            trønderen
                            wrote on last edited by
                            #13

                            Long ago, before WWW was invented, we did have Internet for email, accessing open source code libraries, exchanging photos - and open discussion forums. One of the discussion platform was COM, running on DEC mainframes. COM frequently asked the user yes/no-questions (e.g. "Do you want to delete this entry?"). The routine accepted answers "Yes", "No" and "Maybe". For a "maybe" answer, COM used a random generator to choose between "yes" and "no" actions. (I was using COM for at least a year before I was made aware of the "maybe" option, but after that, I used it frequently :-))

                            Religious freedom is the freedom to say that two plus two make five.

                            1 Reply Last reply
                            0
                            • Sander RosselS Sander Rossel

                              if (expression)
                              {

                              }
                              else
                              {

                              }

                              Everything else is just unnecessary typing/reading.

                              Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript

                              T Offline
                              T Offline
                              trønderen
                              wrote on last edited by
                              #14

                              Agree. 8 lines is required, even for cases that could have used a simple ?:, but beyond 8 lines is a waste. Many times I have met programmers who really oppose ?: and insist that e.g.

                              ticketClass = (age >= 16)? adult : child;

                              must be written over 8 lines as

                              if (age >= 16)
                              {
                              ticketClass = adult;
                              }
                              else
                              {
                              ticketClass = child;
                              }

                              If their productivity is measured in number of source code lines produced, I can see the justification for it, but that's all I can think of :-) Another funny thing is that those who oppose ?: frequently are proud of their creations in regular expressions.

                              Religious freedom is the freedom to say that two plus two make five.

                              L honey the codewitchH Graeme_GrantG 3 Replies Last reply
                              0
                              • T trønderen

                                I do hope that the "==true" and "==false" are not meant literally! A programmer comparing a logical expression against "true" or "false" have not understood what is meant by a logical expression. I wonder how many of those programming that way also speak that way! "If you have a moment to spare is true, I want a talk with you", or "If the door is unlocked is false, you'll find the key under the door mat" - noone that I know speaks that way. I have met a few programmers who program that way, but I never heard any of them speak with "is true" or "is false".

                                Religious freedom is the freedom to say that two plus two make five.

                                B Offline
                                B Offline
                                BernardIE5317
                                wrote on last edited by
                                #15

                                May I please inquire how else does one evaluate a logical expression in C++. One can write if(a) or if(!a). But these perform the same function as if(a == true) and if(a == false). if(I do not understand your meaning == true) I kindly request clarification.;

                                T 1 Reply Last reply
                                0
                                • T trønderen

                                  I do hope that the "==true" and "==false" are not meant literally! A programmer comparing a logical expression against "true" or "false" have not understood what is meant by a logical expression. I wonder how many of those programming that way also speak that way! "If you have a moment to spare is true, I want a talk with you", or "If the door is unlocked is false, you'll find the key under the door mat" - noone that I know speaks that way. I have met a few programmers who program that way, but I never heard any of them speak with "is true" or "is false".

                                  Religious freedom is the freedom to say that two plus two make five.

                                  B Offline
                                  B Offline
                                  BernardIE5317
                                  wrote on last edited by
                                  #16

                                  May I please inquire how else does one evaluate a logical expression in C++? One can write if(a) or if(!a). But these perform the same function as if(a == true) and if(a == false). if(I do not understand your meaning == true) I kindly request clarification.;

                                  1 Reply Last reply
                                  0
                                  • T trønderen

                                    Agree. 8 lines is required, even for cases that could have used a simple ?:, but beyond 8 lines is a waste. Many times I have met programmers who really oppose ?: and insist that e.g.

                                    ticketClass = (age >= 16)? adult : child;

                                    must be written over 8 lines as

                                    if (age >= 16)
                                    {
                                    ticketClass = adult;
                                    }
                                    else
                                    {
                                    ticketClass = child;
                                    }

                                    If their productivity is measured in number of source code lines produced, I can see the justification for it, but that's all I can think of :-) Another funny thing is that those who oppose ?: frequently are proud of their creations in regular expressions.

                                    Religious freedom is the freedom to say that two plus two make five.

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

                                    Or even worse ...

                                    if (age >= 16)
                                    {
                                    ticketClass = adult;
                                    }
                                    else if (age < 16)
                                    {
                                    ticketClass = child;
                                    }

                                    T pkfoxP CPalliniC 3 Replies Last reply
                                    0
                                    • B BernardIE5317

                                      Greetings & Kind Regards I seek advice and knowledge of others' practice re/ a minor matter of style. In particular to be specific I vacillate betwixt and between two forms of final statement of if ... else ... series of statements assuming every possible condition is accounted for as shown in simple specimen / example / sample below (not snippet as it is not a snip of a larger code base). 1st style : if(expression == true) {...} else {...} 2nd style : if(expression == true) {...} else if(expression == false) {...} Note it is the final statement I am inquiring regards. Do your kind selves accept the default final result as shown in 1st style or perform explicit unnecessary test as shown in 2nd style for no other reason than to make the code self documenting and easier to understand in particular for a complex lengthy series of conditions. btw in 2nd style on occasion I add as final statement else throw who_the_heck_knows_what_happened; Thank You Kindly [edit] I have concluded kind Jacquers is quite correct. As it happened upon some coding just now of a simple series of such it was evident an explicit final test results in confusion as it is clearly not necessary. However in a lengthy complex series an explicit test makes code self documenting and so simpler to understand. KISS

                                      M Offline
                                      M Offline
                                      Maximilien
                                      wrote on last edited by
                                      #18

                                      condition ? f() : g();

                                      :sigh:

                                      CI/CD = Continuous Impediment/Continuous Despair

                                      B T 2 Replies Last reply
                                      0
                                      • B BernardIE5317

                                        May I please inquire how else does one evaluate a logical expression in C++. One can write if(a) or if(!a). But these perform the same function as if(a == true) and if(a == false). if(I do not understand your meaning == true) I kindly request clarification.;

                                        T Offline
                                        T Offline
                                        trønderen
                                        wrote on last edited by
                                        #19

                                        Similarly, "if the door is locked" perform the same function as "if the door is locked is true". In speech, noone that I know of includes the "is true". So why do you program as

                                        if (doorIsLocked == true) ...

                                        rather than

                                        if (doorIsLocked) ...

                                        I see no reason for or advantage of creating a more complex logical expression, adding a second, redundant element. Both "doorIsLocked" and "doorIsLocked == true" are logical expressions, the second one just more complex than it needs to be. (Hopefully, the compiler is able to optimize the redundant element away!) If I program a test like

                                        if (x < 10 && x < 20) ...

                                        all programmers I know would point out that it is redundant to test for "x < 20" if you already have tested that x < 10. Adding an extra element a logical expression, to see whether a true "a" is equal to "true" (or that a false "a" is different from "true") is similarly redundant. The logical "a" expression is true or false, all by itself!

                                        Religious freedom is the freedom to say that two plus two make five.

                                        Richard Andrew x64R J 2 Replies Last reply
                                        0
                                        • M Maximilien

                                          condition ? f() : g();

                                          :sigh:

                                          CI/CD = Continuous Impediment/Continuous Despair

                                          B Offline
                                          B Offline
                                          BernardIE5317
                                          wrote on last edited by
                                          #20

                                          Your kind suggestion ? I agree : I still agree;

                                          T 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