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. General Programming
  3. C / C++ / MFC
  4. design question : variadic functions use ? .

design question : variadic functions use ? .

Scheduled Pinned Locked Moved C / C++ / MFC
questiondatabasevisual-studiodesigndevops
8 Posts 3 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.
  • M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #1

    (probably a TOmato vs toMAto kind of questions... ) At what point should I use variadic functions vs. keeping it local ? I have a library that handles SQL queries. is there a difference between something like : (overly simplified)

    // do the formatting before calling the function.
    std::string fullyFormedQuery = sprintf (queryFormat, param1, param2, ... );
    void DoRequest( std::string fullyFormedQuery){
    // call SQL with the fullyFormedQuery
    }

    // do the formatting in the function.
    void DoRequest( std::string queryFormat, param1, param2, ... ){
    std::string fullyFormedQuery= queryFormat(format, param1, param2, ... );
    // call SQL ...
    }

    CI/CD = Continuous Impediment/Continuous Despair

    M J 2 Replies Last reply
    0
    • M Maximilien

      (probably a TOmato vs toMAto kind of questions... ) At what point should I use variadic functions vs. keeping it local ? I have a library that handles SQL queries. is there a difference between something like : (overly simplified)

      // do the formatting before calling the function.
      std::string fullyFormedQuery = sprintf (queryFormat, param1, param2, ... );
      void DoRequest( std::string fullyFormedQuery){
      // call SQL with the fullyFormedQuery
      }

      // do the formatting in the function.
      void DoRequest( std::string queryFormat, param1, param2, ... ){
      std::string fullyFormedQuery= queryFormat(format, param1, param2, ... );
      // call SQL ...
      }

      CI/CD = Continuous Impediment/Continuous Despair

      M Offline
      M Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      As you say it's more of a style question. My preference would be for the first option (pass fully formed string) because: - there is less documenting to do - some compilers (MSVC for one), check matching between format strings and parameters in (s)printf functions but don't do that in a user defined variadic function.

      Mircea

      M 1 Reply Last reply
      0
      • M Mircea Neacsu

        As you say it's more of a style question. My preference would be for the first option (pass fully formed string) because: - there is less documenting to do - some compilers (MSVC for one), check matching between format strings and parameters in (s)printf functions but don't do that in a user defined variadic function.

        Mircea

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

        thanks.

        CI/CD = Continuous Impediment/Continuous Despair

        1 Reply Last reply
        0
        • M Maximilien

          (probably a TOmato vs toMAto kind of questions... ) At what point should I use variadic functions vs. keeping it local ? I have a library that handles SQL queries. is there a difference between something like : (overly simplified)

          // do the formatting before calling the function.
          std::string fullyFormedQuery = sprintf (queryFormat, param1, param2, ... );
          void DoRequest( std::string fullyFormedQuery){
          // call SQL with the fullyFormedQuery
          }

          // do the formatting in the function.
          void DoRequest( std::string queryFormat, param1, param2, ... ){
          std::string fullyFormedQuery= queryFormat(format, param1, param2, ... );
          // call SQL ...
          }

          CI/CD = Continuous Impediment/Continuous Despair

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

          Just noting that your examples leave out using bind variables. My understanding, and I just googled to see if that seems to be correct, is that bind variables make optimizations in the database easier. Not to mention of course that they are a better fit for avoiding SQL injection problems. Setting up bind variables is rather complex so putting it in the method is going to be better.

          M 1 Reply Last reply
          0
          • J jschell

            Just noting that your examples leave out using bind variables. My understanding, and I just googled to see if that seems to be correct, is that bind variables make optimizations in the database easier. Not to mention of course that they are a better fit for avoiding SQL injection problems. Setting up bind variables is rather complex so putting it in the method is going to be better.

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

            Thanks. :thumbsup: We already have a complete (decades old) framework for SQL. (seems to be working fine) maybe I should have left out the SQL reference and make it a more generic question.

            CI/CD = Continuous Impediment/Continuous Despair

            J 1 Reply Last reply
            0
            • M Maximilien

              Thanks. :thumbsup: We already have a complete (decades old) framework for SQL. (seems to be working fine) maybe I should have left out the SQL reference and make it a more generic question.

              CI/CD = Continuous Impediment/Continuous Despair

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

              Maximilien wrote:

              complete (decades old) framework for SQL.

              Then you should use the same idiom as the existing code. Changing it for some and not others just adds to the confusion. Although if a preference for one is given and there is a large amount of new work (new files not modifying existing code) then maybe go with there.

              M 1 Reply Last reply
              0
              • J jschell

                Maximilien wrote:

                complete (decades old) framework for SQL.

                Then you should use the same idiom as the existing code. Changing it for some and not others just adds to the confusion. Although if a preference for one is given and there is a large amount of new work (new files not modifying existing code) then maybe go with there.

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

                I will not change existing code; it's just too dangerous. I'm just curious; if there's a best practice.

                CI/CD = Continuous Impediment/Continuous Despair

                J 1 Reply Last reply
                0
                • M Maximilien

                  I will not change existing code; it's just too dangerous. I'm just curious; if there's a best practice.

                  CI/CD = Continuous Impediment/Continuous Despair

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

                  My best practice doesn't fit either of your suggestions... If I am creating a new database layer (which I have numerous times) then the caller would never provide the SQL. If the business layer(s) need new database logic then the database layer should be modified to provide it. The caller provides the parameters that would be used in the call. Then within that database layer method it would decide how to process the parameters and SQL (provided by the method.) So it looks like the following.

                  void DatabaseDoX(param1, param2,...)
                  {
                  // multiple variations possible
                  std::string fullyFormedQuery = sprintf (SQL_X, param1, param2, ... );

                  There are quite a few variations on the above. For example consider the complication of a user initiated call where they might specify different 'query' terms (like data range, customer name, etc) But all still hide the SQL. Certainly there are design considerations for example your code might be coming from within the database layer (not a business call at all.) If so then I suspect that usage would actually vary depending on what the database layer was specifically doing in the code path.

                  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