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. What are some stupid-useful coding tricks you rely on?

What are some stupid-useful coding tricks you rely on?

Scheduled Pinned Locked Moved The Lounge
hardwareiotquestionworkspace
36 Posts 15 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

    I totally get what you're saying. Just for clarity though template-instance statics add another dimension to statics because they are not shared between different instantiations of the same template.

    template struct foo final {
    static int value;
    };
    ...

    ...
    foo<1>::value = 5;
    foo<2>::value = 4;
    printf("%d + %d = %d\n",foo<1>::value,foo<2>::value,foo<1>::value+foo<2>::value);
    // prints 5 + 4 = 9

    To err is human. Fortune favors the monsters.

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

    Congratulations, your Witch-ness! You have successfully taught an Old Dog a wonderful new trick! :-\

    Software Zen: delete this;

    H 1 Reply Last reply
    0
    • G Gary Wheeler

      Congratulations, your Witch-ness! You have successfully taught an Old Dog a wonderful new trick! :-\

      Software Zen: delete this;

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

      I didn't tell you the downside. You must initialize the statics to avoid a linker error, and I don't know about C++20 but previous versions require declarations like this for statics:

      template
      int_button*
      int_button::m_this = nullptr;

      To err is human. Fortune favors the monsters.

      G 1 Reply Last reply
      0
      • H honey the codewitch

        I didn't tell you the downside. You must initialize the statics to avoid a linker error, and I don't know about C++20 but previous versions require declarations like this for statics:

        template
        int_button*
        int_button::m_this = nullptr;

        To err is human. Fortune favors the monsters.

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

        I don't think that's a downside, really. I'd expect that to be required initialization, since non-template static members require it.

        Software Zen: delete this;

        H 1 Reply Last reply
        0
        • G Gary Wheeler

          I don't think that's a downside, really. I'd expect that to be required initialization, since non-template static members require it.

          Software Zen: delete this;

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

          I'm referring to the nasty syntax required. :)

          To err is human. Fortune favors the monsters.

          1 Reply Last reply
          0
          • R raddevus

            Thanks, that was very nice and I accept your apology. I will take a look at the post. I honestly was offering the original code just as an interesting thing. I was probably triggered by your post because I started out in IT back in '91 and since then it has always been a "my code is better than yours" pissing war with Devs. :laugh: I started out in Tech Support and knew I was a slug in the IT world. Worked my way into QA and was there for 5 years or so and remember when this Dilbert came out[^]. Oh, it's funny now. I finally made worked my way into Dev and have been here for about 22 years but I find that Devs are (and have always been) so competitive and are "always right". it just kind of triggers me. When I was in QA if a dev ticked me off, I would just test his code, find a critical bug and then post it Friday afternoon. :laugh:

            M Offline
            M Offline
            Marcelo Huerta
            wrote on last edited by
            #31

            raddevus wrote:

            When I was in QA if a dev ticked me off, I would just test his code, find a critical bug and then post it Friday afternoon.

            That is evil. I love it :laugh:

            1 Reply Last reply
            0
            • H honey the codewitch

              I was just thinking about this today when I was retooling my IoT button library. I use template-instance statics to get around not being able to pass an argument to an interrupt routine (which must be void ()) under the Arduino framework

              template
              class button {
              // assign this to "this" on initialization of a class instance
              static button* m_this;
              // ISR
              static IRAM_ATTR void isr() { m_this->... }
              };
              ...

              like that. It's really useful but it makes me kinda wanna puke too, even though there's absolutely nothing wrong with it, technically speaking, since it doesn't make sense to have two buttons on one pin anyway, and you're not dealing in a pre-emptively threaded environment in 80% of all cases on 80% of all platforms. Template-instance statics (i don't know what else to call them) are something I have to rely on way more than I wish I had to, but I am glad they are there. What's your go to coding technique that nevertheless makes you uncomfortable?

              To err is human. Fortune favors the monsters.

              M Offline
              M Offline
              Member_5893260
              wrote on last edited by
              #32

              In C:

              a^=b^=a^=b;

              ...will swap the values of a and b by XORing them a couple of times, assuming a and b are the same size. Works for large data structures just as well as for ints: quite fast, too. Back in the days before proper video cards, this was good for swapping in entire screen contents, or faking sprites or whatever... Note that this doesn't work in C#: you have to go...:

              a^=b;
              b^=a;
              a^=b;

              ...because unlike C, C# uses the original values of the variables throughout the evaluation of the expression, so what happens if you try to run the original statement is that one variable receives the swap, but the other one is garbage. And really, it only works for ints. But it does let you swap the variables without using a third one.

              1 Reply Last reply
              0
              • H honey the codewitch

                I was just thinking about this today when I was retooling my IoT button library. I use template-instance statics to get around not being able to pass an argument to an interrupt routine (which must be void ()) under the Arduino framework

                template
                class button {
                // assign this to "this" on initialization of a class instance
                static button* m_this;
                // ISR
                static IRAM_ATTR void isr() { m_this->... }
                };
                ...

                like that. It's really useful but it makes me kinda wanna puke too, even though there's absolutely nothing wrong with it, technically speaking, since it doesn't make sense to have two buttons on one pin anyway, and you're not dealing in a pre-emptively threaded environment in 80% of all cases on 80% of all platforms. Template-instance statics (i don't know what else to call them) are something I have to rely on way more than I wish I had to, but I am glad they are there. What's your go to coding technique that nevertheless makes you uncomfortable?

                To err is human. Fortune favors the monsters.

                H Offline
                H Offline
                HappyDotNet
                wrote on last edited by
                #33

                Use Background worker report progress to trigger different conditions on the main thread. *** Ya Ya I know..background workers X| But I didn't architect the project :)

                Private Sub Go(myList as list(of String))
                bgw1.RunWorkerAsync(me,myList)
                End Sub

                Private Sub bgw1_DoWork(sender As Object, e As DoWorkEventArgs) Handles bgw1.DoWork
                'background thread
                Dim myList As List(Of String) = e.Argument
                Dim listCount As Integer = myList.Count
                'long running process
                For i As Integer = 0 To listCount
                Dim resultInteger = DoWork(myList(i))
                bgw1.ReportProgress(resultInteger,myList(i))
                Next
                End Sub

                Private Sub bgw1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles bgw1.ProgressChanged
                'main thread
                Dim p As Integer = e.ProgressPercentage
                Dim s As String = CStr(e.UserState)
                Select Case P
                Case 1
                lbl1.Text = s
                Case 2
                lbl2.Text = s
                Case Else
                lbl1.Text = "Invalid Result for " & s
                lbl2.Text = "Invalid Result for " & s
                End Select
                End Sub

                1 Reply Last reply
                0
                • B BillWoodruff

                  raddevus wrote:

                  What do you think?

                  I think this "belongs" on one of the technical forums ... but, given the general neglect of many forums, and the free-for-all the Lounge has become, I am just "blowing smoke" :wtf: :)

                  «The mind is not a vessel to be filled but a fire to be kindled» Plutarch

                  Mircea NeacsuM Offline
                  Mircea NeacsuM Offline
                  Mircea Neacsu
                  wrote on last edited by
                  #34

                  BillWoodruff wrote:

                  I am just "blowing smoke"

                  Kacey Musgraves - Blowin' Smoke - YouTube[^]

                  Mircea

                  1 Reply Last reply
                  0
                  • H honey the codewitch

                    I was just thinking about this today when I was retooling my IoT button library. I use template-instance statics to get around not being able to pass an argument to an interrupt routine (which must be void ()) under the Arduino framework

                    template
                    class button {
                    // assign this to "this" on initialization of a class instance
                    static button* m_this;
                    // ISR
                    static IRAM_ATTR void isr() { m_this->... }
                    };
                    ...

                    like that. It's really useful but it makes me kinda wanna puke too, even though there's absolutely nothing wrong with it, technically speaking, since it doesn't make sense to have two buttons on one pin anyway, and you're not dealing in a pre-emptively threaded environment in 80% of all cases on 80% of all platforms. Template-instance statics (i don't know what else to call them) are something I have to rely on way more than I wish I had to, but I am glad they are there. What's your go to coding technique that nevertheless makes you uncomfortable?

                    To err is human. Fortune favors the monsters.

                    E Offline
                    E Offline
                    englebart
                    wrote on last edited by
                    #35

                    Always wrap any third party or sufficiently complicated built-in API with your own API. The original API or third party will change or become deprecated in about 10 years. A good rule for code that you expect to last 20 years or more. If you expect a five year life span then it might not be worth it.

                    1 Reply Last reply
                    0
                    • H honey the codewitch

                      I was just thinking about this today when I was retooling my IoT button library. I use template-instance statics to get around not being able to pass an argument to an interrupt routine (which must be void ()) under the Arduino framework

                      template
                      class button {
                      // assign this to "this" on initialization of a class instance
                      static button* m_this;
                      // ISR
                      static IRAM_ATTR void isr() { m_this->... }
                      };
                      ...

                      like that. It's really useful but it makes me kinda wanna puke too, even though there's absolutely nothing wrong with it, technically speaking, since it doesn't make sense to have two buttons on one pin anyway, and you're not dealing in a pre-emptively threaded environment in 80% of all cases on 80% of all platforms. Template-instance statics (i don't know what else to call them) are something I have to rely on way more than I wish I had to, but I am glad they are there. What's your go to coding technique that nevertheless makes you uncomfortable?

                      To err is human. Fortune favors the monsters.

                      F Offline
                      F Offline
                      FormerBIOSGuy
                      wrote on last edited by
                      #36

                      To toggle a flag variable that has values of 0 and 1: Flag = 1 - Flag

                      FormerBIOSGuy

                      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