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. do any of you others have little coding mantras that save your behind?

do any of you others have little coding mantras that save your behind?

Scheduled Pinned Locked Moved The Lounge
csharpcssvisual-studioquestion
73 Posts 32 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.
  • H honey the codewitch

    One of mine is - when dealing with IComparable in .NET "Greater than is less than" What it means is Converting

    if(10>5);

    to IComparable it reads

    if(0<10.CompareTo(5));

    Note '>' vs '<'

    When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

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

    I have a few. The popular "if you're doing single line if statements you're doing it wrong." The equally catchy "only psycho's don't put spaces between their operands and operators." And my personal favorite "what would codewitch do, do the opposite." :D And one that's not so much related to code, but to specs. If it'll never happen, it'll happen soon (or it'll never never happen, but people don't tend to understand that one).

    Best, Sander sanderrossel.com Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

    K 1 Reply Last reply
    0
    • H honey the codewitch

      One of mine is - when dealing with IComparable in .NET "Greater than is less than" What it means is Converting

      if(10>5);

      to IComparable it reads

      if(0<10.CompareTo(5));

      Note '>' vs '<'

      When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

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

      [code like your colleagues are serial killers.](https://blog.codinghorror.com/coding-for-violent-psychopaths/)

      I'd rather be phishing!

      OriginalGriffO 1 Reply Last reply
      0
      • M Maximilien

        [code like your colleagues are serial killers.](https://blog.codinghorror.com/coding-for-violent-psychopaths/)

        I'd rather be phishing!

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

        They are serial killers, aren't they? Or is that just me? :~

        Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

        "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
        • H honey the codewitch

          One of mine is - when dealing with IComparable in .NET "Greater than is less than" What it means is Converting

          if(10>5);

          to IComparable it reads

          if(0<10.CompareTo(5));

          Note '>' vs '<'

          When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

          M Offline
          M Offline
          Marc Clifton
          wrote on last edited by
          #8

          Don't write an if unless you know what the else does and why. if and switch statements can be replaced with object inheritance. Let compiler do the work for you! Anything can be written with map-reduce-filter if you try hard enough. Don't try too hard. If you're about to write some code, stop.

          Latest Articles:
          Fun Exploring Div and Table UI Layout

          H K 2 Replies Last reply
          0
          • H honey the codewitch

            One of mine is - when dealing with IComparable in .NET "Greater than is less than" What it means is Converting

            if(10>5);

            to IComparable it reads

            if(0<10.CompareTo(5));

            Note '>' vs '<'

            When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

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

            I try to avoid adding a comparison to a comparison. And if I do I don't do Yoda conditions.

            Wrong is evil and must be defeated. - Jeff Ello

            H 1 Reply Last reply
            0
            • M Marc Clifton

              Don't write an if unless you know what the else does and why. if and switch statements can be replaced with object inheritance. Let compiler do the work for you! Anything can be written with map-reduce-filter if you try hard enough. Don't try too hard. If you're about to write some code, stop.

              Latest Articles:
              Fun Exploring Div and Table UI Layout

              H Offline
              H Offline
              honey the codewitch
              wrote on last edited by
              #10

              all good rules of thumb although the if/switch one gets sticky so that's a rule to be bent a LOT. still, the idea is sound - don't use conditionals where you can use polymorphism although in .NET the runtime still does work to cast :(

              When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

              1 Reply Last reply
              0
              • J Jorgen Andersson

                I try to avoid adding a comparison to a comparison. And if I do I don't do Yoda conditions.

                Wrong is evil and must be defeated. - Jeff Ello

                H Offline
                H Offline
                honey the codewitch
                wrote on last edited by
                #11

                heh. The yoda conditionals were hammered into me in the late 80s/early 90s. The multiple comparisons are a necessary evil, as TKey isn't directly comparable. You have to use its IComparable interface. Oh how I wish .NET would let you declare a contract on operators. You can't. It's a limitation of .NET's generic types.

                When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                K 1 Reply Last reply
                0
                • H honey the codewitch

                  One of mine is - when dealing with IComparable in .NET "Greater than is less than" What it means is Converting

                  if(10>5);

                  to IComparable it reads

                  if(0<10.CompareTo(5));

                  Note '>' vs '<'

                  When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                  R Offline
                  R Offline
                  Rick York
                  wrote on last edited by
                  #12

                  Think twice, write once.

                  "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

                  H K 2 Replies Last reply
                  0
                  • R Rick York

                    Think twice, write once.

                    "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

                    H Offline
                    H Offline
                    honey the codewitch
                    wrote on last edited by
                    #13

                    only twice? I find a bout of analysis paralysis followed by headdesking a few times is really the way to go. :laugh:

                    When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                    R 1 Reply Last reply
                    0
                    • H honey the codewitch

                      One of mine is - when dealing with IComparable in .NET "Greater than is less than" What it means is Converting

                      if(10>5);

                      to IComparable it reads

                      if(0<10.CompareTo(5));

                      Note '>' vs '<'

                      When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                      D Offline
                      D Offline
                      David ONeil
                      wrote on last edited by
                      #14

                      If you can't find a way to keep your logic nested <= three levels deep, find another profession or project, because you certainly don't want to be the one to debug that sucker. A function is an acceptable solution.

                      The forgotten roots of science | C++ Programming | DWinLib

                      H 1 Reply Last reply
                      0
                      • D David ONeil

                        If you can't find a way to keep your logic nested <= three levels deep, find another profession or project, because you certainly don't want to be the one to debug that sucker. A function is an acceptable solution.

                        The forgotten roots of science | C++ Programming | DWinLib

                        H Offline
                        H Offline
                        honey the codewitch
                        wrote on last edited by
                        #15

                        I think it depends on the logic, and should be amended to non-trivial logic, because I wouldn't count things like null checks - validation - that sort of thing, unless they're convoluted. But that's me, and it served me well enough. Usually my debugger problems are complicated. I almost never actually debug. I Ctrl+F5 in visual studio and I either get the expected result, or usually I know where I went wrong because I develop very iteratively.

                        When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                        1 Reply Last reply
                        0
                        • H honey the codewitch

                          only twice? I find a bout of analysis paralysis followed by headdesking a few times is really the way to go. :laugh:

                          When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                          R Offline
                          R Offline
                          Rick York
                          wrote on last edited by
                          #16

                          It's a metaphorical twice as in more than once. Although I have found sometimes just taking a shot in the dark can be useful if you can learn from failure.

                          "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

                          H T 3 Replies Last reply
                          0
                          • R Rick York

                            It's a metaphorical twice as in more than once. Although I have found sometimes just taking a shot in the dark can be useful if you can learn from failure.

                            "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

                            H Offline
                            H Offline
                            honey the codewitch
                            wrote on last edited by
                            #17

                            I hear you. I just did that. But I learned from success. I mean, I was working from some sample code, in C++, on implementing B+ trees, but I ported it to C# and then rewrote it using .NETisms and adding features. Then I realized it was almost pointless without a little database system going with it because it only optimized situations where nodes are directly tied to disk access. On the other hand, I did the same thing with the regular B tree and it worked flawlessly, and is useful as an in-memory autobalancing tree structure (inserts and deletions are slow, searches are very fast and consistent - every search takes the same number of comparisons) so woo. but i guess someone already implemented one here. Not sure how mine stacks up, but it works.

                            When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                            1 Reply Last reply
                            0
                            • H honey the codewitch

                              One of mine is - when dealing with IComparable in .NET "Greater than is less than" What it means is Converting

                              if(10>5);

                              to IComparable it reads

                              if(0<10.CompareTo(5));

                              Note '>' vs '<'

                              When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                              M Offline
                              M Offline
                              Member 9167057
                              wrote on last edited by
                              #18

                              Something similar: Never compare floats for equality. It may bite sooner than later.

                              G K P 3 Replies Last reply
                              0
                              • H honey the codewitch

                                One of mine is - when dealing with IComparable in .NET "Greater than is less than" What it means is Converting

                                if(10>5);

                                to IComparable it reads

                                if(0<10.CompareTo(5));

                                Note '>' vs '<'

                                When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                                X Offline
                                X Offline
                                xtofl
                                wrote on last edited by
                                #19

                                * How would I explain this code to the cleaning personnel * If you can explain how it's done, you can script it

                                R 1 Reply Last reply
                                0
                                • H honey the codewitch

                                  One of mine is - when dealing with IComparable in .NET "Greater than is less than" What it means is Converting

                                  if(10>5);

                                  to IComparable it reads

                                  if(0<10.CompareTo(5));

                                  Note '>' vs '<'

                                  When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                                  Richard DeemingR Offline
                                  Richard DeemingR Offline
                                  Richard Deeming
                                  wrote on last edited by
                                  #20

                                  Another reason to avoid "Yoda conditionals":

                                  if (10 > 5)
                                  if (10.CompareTo(5) > 0)

                                  :)


                                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                                  H 1 Reply Last reply
                                  0
                                  • H honey the codewitch

                                    One of mine is - when dealing with IComparable in .NET "Greater than is less than" What it means is Converting

                                    if(10>5);

                                    to IComparable it reads

                                    if(0<10.CompareTo(5));

                                    Note '>' vs '<'

                                    When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                                    S Offline
                                    S Offline
                                    S Houghtelin
                                    wrote on last edited by
                                    #21

                                    My mantra is "Dammit! Dammit! Dammit!". Way back when learning C, to help me remember to add the extra equal sign when writing a conditional statement as opposed to an assignment I would say "Equals to" and press the equal sign == for each word and to say "Equals" and only hit the equal = sign once for assigning a value.

                                    It was broke, so I fixed it.

                                    K 1 Reply Last reply
                                    0
                                    • H honey the codewitch

                                      One of mine is - when dealing with IComparable in .NET "Greater than is less than" What it means is Converting

                                      if(10>5);

                                      to IComparable it reads

                                      if(0<10.CompareTo(5));

                                      Note '>' vs '<'

                                      When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.

                                      U Offline
                                      U Offline
                                      User 12228503
                                      wrote on last edited by
                                      #22

                                      "Developers may come and go, but bugs will stay forever"

                                      1 Reply Last reply
                                      0
                                      • M Member 9167057

                                        Something similar: Never compare floats for equality. It may bite sooner than later.

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

                                        Yep. Been there, done that, got the scars on my back from self-flagellation for trying it.

                                        Software Zen: delete this;

                                        1 Reply Last reply
                                        0
                                        • R Rick York

                                          Think twice, write once.

                                          "They have a consciousness, they have a life, they have a soul! Damn you! Let the rabbits wear glasses! Save our brothers! Can I get an amen?"

                                          K Offline
                                          K Offline
                                          Kent K
                                          wrote on last edited by
                                          #24

                                          Similar to the woodworking saying - measure twice, cut once.

                                          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