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. Not really a question of how-do-I...

Not really a question of how-do-I...

Scheduled Pinned Locked Moved The Weird and The Wonderful
question
11 Posts 11 Posters 1 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 SortaCore

    Hmmm... is this dumb or brilliant?

    if ((logicalTabs ? tabs_General1.cbException.Focused : tabs_General1.cbJobStatus.Focused) &&
    e.KeyCode == Keys.Up)
    {

    as opposed to:

    if (( (logicalTabs && tabs_General1.cbException.Focused) ||
    (!logicalTabs && tabs_General1.cbJobStatus.Focused) ) &&
    e.KeyCode == Keys.Up)
    {

    I can't decide now. :~ Readability contrasting with good practice?

    D Offline
    D Offline
    dan sh
    wrote on last edited by
    #2

    Second one. I think in first case, it is easy for someone to incorrectly infer the condition.

    "Bastards encourage idiots to use Oracle Forms, Web Forms, Access and a number of other dinky web publishing tolls.", Mycroft Holmes[^]

    1 Reply Last reply
    0
    • S SortaCore

      Hmmm... is this dumb or brilliant?

      if ((logicalTabs ? tabs_General1.cbException.Focused : tabs_General1.cbJobStatus.Focused) &&
      e.KeyCode == Keys.Up)
      {

      as opposed to:

      if (( (logicalTabs && tabs_General1.cbException.Focused) ||
      (!logicalTabs && tabs_General1.cbJobStatus.Focused) ) &&
      e.KeyCode == Keys.Up)
      {

      I can't decide now. :~ Readability contrasting with good practice?

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #3

      The second one is a lot easier to read - you have to stop and think about the first one.

      The only instant messaging I do involves my middle finger.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • S SortaCore

        Hmmm... is this dumb or brilliant?

        if ((logicalTabs ? tabs_General1.cbException.Focused : tabs_General1.cbJobStatus.Focused) &&
        e.KeyCode == Keys.Up)
        {

        as opposed to:

        if (( (logicalTabs && tabs_General1.cbException.Focused) ||
        (!logicalTabs && tabs_General1.cbJobStatus.Focused) ) &&
        e.KeyCode == Keys.Up)
        {

        I can't decide now. :~ Readability contrasting with good practice?

        M Offline
        M Offline
        Member 2053006
        wrote on last edited by
        #4

        I prefer the first method, but for readability would split it into two separate statements by using an extra variable:

        bool focused = logicalTabs ? tabs_General1.cbException.Focused : tabs_General1.cbJobStatus.Focused;
        if (focused && e.KeyCode == Keys.Up)
        {

        L 1 Reply Last reply
        0
        • S SortaCore

          Hmmm... is this dumb or brilliant?

          if ((logicalTabs ? tabs_General1.cbException.Focused : tabs_General1.cbJobStatus.Focused) &&
          e.KeyCode == Keys.Up)
          {

          as opposed to:

          if (( (logicalTabs && tabs_General1.cbException.Focused) ||
          (!logicalTabs && tabs_General1.cbJobStatus.Focused) ) &&
          e.KeyCode == Keys.Up)
          {

          I can't decide now. :~ Readability contrasting with good practice?

          S Offline
          S Offline
          Suvabrata Roy 0
          wrote on last edited by
          #5

          if (e.KeyCode == Keys.Up)
          {
          bool focused = logicalTabs ? tabs_General1.cbException.Focused : tabs_General1.cbJobStatus.Focused;
          if(focused )
          {
          // Your Logic
          }
          }

          I think it would be also a choice... :)

          Life is all about share and care... public class Life : ICareable,IShareable { // implements yours... }

          1 Reply Last reply
          0
          • M Member 2053006

            I prefer the first method, but for readability would split it into two separate statements by using an extra variable:

            bool focused = logicalTabs ? tabs_General1.cbException.Focused : tabs_General1.cbJobStatus.Focused;
            if (focused && e.KeyCode == Keys.Up)
            {

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

            ..and what if another condition needs to be added? :)

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

            1 Reply Last reply
            0
            • S SortaCore

              Hmmm... is this dumb or brilliant?

              if ((logicalTabs ? tabs_General1.cbException.Focused : tabs_General1.cbJobStatus.Focused) &&
              e.KeyCode == Keys.Up)
              {

              as opposed to:

              if (( (logicalTabs && tabs_General1.cbException.Focused) ||
              (!logicalTabs && tabs_General1.cbJobStatus.Focused) ) &&
              e.KeyCode == Keys.Up)
              {

              I can't decide now. :~ Readability contrasting with good practice?

              D Offline
              D Offline
              Dan Neely
              wrote on last edited by
              #7

              The first is nasty. Embedding one ternary expression in a second or an if results in hard to read code. I'd say it's a tossup between the second and putting the ternary in a temp. Mostly that comes down to if the temp has any intrinsic meaning on its own (regardless of if it's used in a second place).

              Did you ever see history portrayed as an old man with a wise brow and pulseless heart, waging all things in the balance of reason? Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful? --Zachris Topelius Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies. -- Sarah Hoyt

              1 Reply Last reply
              0
              • S SortaCore

                Hmmm... is this dumb or brilliant?

                if ((logicalTabs ? tabs_General1.cbException.Focused : tabs_General1.cbJobStatus.Focused) &&
                e.KeyCode == Keys.Up)
                {

                as opposed to:

                if (( (logicalTabs && tabs_General1.cbException.Focused) ||
                (!logicalTabs && tabs_General1.cbJobStatus.Focused) ) &&
                e.KeyCode == Keys.Up)
                {

                I can't decide now. :~ Readability contrasting with good practice?

                0 Offline
                0 Offline
                0bx
                wrote on last edited by
                #8

                bool rightFocus = logicalTabs ? tabs_General1.cbException.Focused : tabs_General1.cbJobStatus.Focused;
                bool keyUp = e.KeyCode == Keys.Up;

                if (rightFocus && keyUp){
                }

                This is much easier to work with later on imo.

                .

                1 Reply Last reply
                0
                • S SortaCore

                  Hmmm... is this dumb or brilliant?

                  if ((logicalTabs ? tabs_General1.cbException.Focused : tabs_General1.cbJobStatus.Focused) &&
                  e.KeyCode == Keys.Up)
                  {

                  as opposed to:

                  if (( (logicalTabs && tabs_General1.cbException.Focused) ||
                  (!logicalTabs && tabs_General1.cbJobStatus.Focused) ) &&
                  e.KeyCode == Keys.Up)
                  {

                  I can't decide now. :~ Readability contrasting with good practice?

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

                  Only use a ternary where you can't use an if; why the elephant would you ever use one in an if? :wtf:

                  B 1 Reply Last reply
                  0
                  • S SortaCore

                    Hmmm... is this dumb or brilliant?

                    if ((logicalTabs ? tabs_General1.cbException.Focused : tabs_General1.cbJobStatus.Focused) &&
                    e.KeyCode == Keys.Up)
                    {

                    as opposed to:

                    if (( (logicalTabs && tabs_General1.cbException.Focused) ||
                    (!logicalTabs && tabs_General1.cbJobStatus.Focused) ) &&
                    e.KeyCode == Keys.Up)
                    {

                    I can't decide now. :~ Readability contrasting with good practice?

                    B Offline
                    B Offline
                    BuggyTimes
                    wrote on last edited by
                    #10

                    Maybe it's just my brain but the ternary expression is clearer for me :) That being the case I'd never write it this way in code. Complex conditions would most likely be moved into variables like someone has answered already, i.e.:

                    bool tabsAreLogical = logicalTabs ? tabs_General1.cbException.Focused : tabs_General1.cbJobStatus.Focused;

                    if(tabsAreLogical && e.KeyCode == Keys.Up) {

                    }

                    This makes it easier to read/change later, as well as making the condition reusable later in code.

                    1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Only use a ternary where you can't use an if; why the elephant would you ever use one in an if? :wtf:

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

                      Doing so is very iffy.

                      Keep Clam And Proofread -- √(-1) 23 ∑ π... And it was delicious.

                      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