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. Design and Architecture
  4. Calling class function vs function getting class variables

Calling class function vs function getting class variables

Scheduled Pinned Locked Moved Design and Architecture
workspacevisual-studiodesigntutorialquestion
6 Posts 4 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 Offline
    H Offline
    hpjchobbes
    wrote on last edited by
    #1

    This is more of a general design concept, but if I had a class which I intend to have a list of those objects (around 5000) and I need to perform some function on each of these, should I be designing around a function that is part of the class or a separate function that takes a collection of these objects? I know that there's always situations where one may be better than another, but when first starting to think about the design, which way would be recommend For example:

    class Person {
    float HungerLevel;
    public void AdjustHunger(float amount) { HungerLevel += amount; }
    }

    public void AdjustAllHunger(List PersonList, float amount) { foreach(Person in PersonList) { Person.AdjustHunger(amount); } }

    or

    class Person {
    float HungerLevel;
    public void AdjustHunger(float amount) { HungerLevel += amount; }
    }

    public void AdjustAllHunger(List PersonList, float amount) { foreach(Person in PersonList) { Person.HungerLevel += amount; } }

    I would imagine that the second option is more efficient if I need to change all objects in the collection by the same value, but not sure. I guess it could even depend on what language I'm using and how it would try to optimize the code? Does a call to a function have a higher cost than a call to an object's variable? Expanding on this example, what if the hunger change was based on another of the objects variables.

    class Person{
    float HungerLevel;
    float Metabolism;
    public void AdjustHunger(float amount) { HungerLevel += (amount * Metabolism); }

    public void AdjustAllHunger(List PersonList, float amount) { foreach(Person in PersonList) { Person.AdjustHunger(amount); } }

    vs.

    class Person{
    float HungerLevel;
    public void AdjustHunger(float amount) { HungerLevel += amount; }
    }

    public void AdjustAllHunger(List PersonList, float amount) { foreach(Person in PersonList) { Person.HungerLevel += (amount * Person.Metabolism); } }

    I don't have a project right now that deals with this, but the concept popped into my head and I was thinking about how I would start designing this. I could probably setup a test case and try it out with a sample, but is that what most programmers do at the designing stage, creating multiple test implementations? Is there some sort of 'design theory' that I would/should be using? I'm self taught, so I don't know if this would be something that is covered in a more structured training environment (so please excuse me if this is a dumb quest

    M L J 3 Replies Last reply
    0
    • H hpjchobbes

      This is more of a general design concept, but if I had a class which I intend to have a list of those objects (around 5000) and I need to perform some function on each of these, should I be designing around a function that is part of the class or a separate function that takes a collection of these objects? I know that there's always situations where one may be better than another, but when first starting to think about the design, which way would be recommend For example:

      class Person {
      float HungerLevel;
      public void AdjustHunger(float amount) { HungerLevel += amount; }
      }

      public void AdjustAllHunger(List PersonList, float amount) { foreach(Person in PersonList) { Person.AdjustHunger(amount); } }

      or

      class Person {
      float HungerLevel;
      public void AdjustHunger(float amount) { HungerLevel += amount; }
      }

      public void AdjustAllHunger(List PersonList, float amount) { foreach(Person in PersonList) { Person.HungerLevel += amount; } }

      I would imagine that the second option is more efficient if I need to change all objects in the collection by the same value, but not sure. I guess it could even depend on what language I'm using and how it would try to optimize the code? Does a call to a function have a higher cost than a call to an object's variable? Expanding on this example, what if the hunger change was based on another of the objects variables.

      class Person{
      float HungerLevel;
      float Metabolism;
      public void AdjustHunger(float amount) { HungerLevel += (amount * Metabolism); }

      public void AdjustAllHunger(List PersonList, float amount) { foreach(Person in PersonList) { Person.AdjustHunger(amount); } }

      vs.

      class Person{
      float HungerLevel;
      public void AdjustHunger(float amount) { HungerLevel += amount; }
      }

      public void AdjustAllHunger(List PersonList, float amount) { foreach(Person in PersonList) { Person.HungerLevel += (amount * Person.Metabolism); } }

      I don't have a project right now that deals with this, but the concept popped into my head and I was thinking about how I would start designing this. I could probably setup a test case and try it out with a sample, but is that what most programmers do at the designing stage, creating multiple test implementations? Is there some sort of 'design theory' that I would/should be using? I'm self taught, so I don't know if this would be something that is covered in a more structured training environment (so please excuse me if this is a dumb quest

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      I presume you would need to store the result into a database so I would do the processing in the database. Define the list, pass it to a stored proc that does the calculation and writes to the database (UPDATE) and returns you the result. I avoid double handling IE fetch from the database, process the function and write back to the database. Make the database do it's job.

      Never underestimate the power of human stupidity RAH

      H 1 Reply Last reply
      0
      • M Mycroft Holmes

        I presume you would need to store the result into a database so I would do the processing in the database. Define the list, pass it to a stored proc that does the calculation and writes to the database (UPDATE) and returns you the result. I avoid double handling IE fetch from the database, process the function and write back to the database. Make the database do it's job.

        Never underestimate the power of human stupidity RAH

        H Offline
        H Offline
        hpjchobbes
        wrote on last edited by
        #3

        I actually never thought about it being a separate database, as I was thinking more in a video game setting. The thought came up when looking at a park simulation (Planet Coaster) and trying to decide how I would tackle the same situation. Like I said, I don't have a project that has run into this situation, but I'm curious about how I would approach this concept. In this case the function doesn't really care about the result, just the computation. Is there a word or definition for this concept of 'figuring out how best to store and manipulate data' in programming terminology? I feel as I am floundering when it comes to these thoughts. How to best approach data structure and program structure. I feel like an idiot as I have no idea what words to use when asking about this kind of situation.

        M 1 Reply Last reply
        0
        • H hpjchobbes

          This is more of a general design concept, but if I had a class which I intend to have a list of those objects (around 5000) and I need to perform some function on each of these, should I be designing around a function that is part of the class or a separate function that takes a collection of these objects? I know that there's always situations where one may be better than another, but when first starting to think about the design, which way would be recommend For example:

          class Person {
          float HungerLevel;
          public void AdjustHunger(float amount) { HungerLevel += amount; }
          }

          public void AdjustAllHunger(List PersonList, float amount) { foreach(Person in PersonList) { Person.AdjustHunger(amount); } }

          or

          class Person {
          float HungerLevel;
          public void AdjustHunger(float amount) { HungerLevel += amount; }
          }

          public void AdjustAllHunger(List PersonList, float amount) { foreach(Person in PersonList) { Person.HungerLevel += amount; } }

          I would imagine that the second option is more efficient if I need to change all objects in the collection by the same value, but not sure. I guess it could even depend on what language I'm using and how it would try to optimize the code? Does a call to a function have a higher cost than a call to an object's variable? Expanding on this example, what if the hunger change was based on another of the objects variables.

          class Person{
          float HungerLevel;
          float Metabolism;
          public void AdjustHunger(float amount) { HungerLevel += (amount * Metabolism); }

          public void AdjustAllHunger(List PersonList, float amount) { foreach(Person in PersonList) { Person.AdjustHunger(amount); } }

          vs.

          class Person{
          float HungerLevel;
          public void AdjustHunger(float amount) { HungerLevel += amount; }
          }

          public void AdjustAllHunger(List PersonList, float amount) { foreach(Person in PersonList) { Person.HungerLevel += (amount * Person.Metabolism); } }

          I don't have a project right now that deals with this, but the concept popped into my head and I was thinking about how I would start designing this. I could probably setup a test case and try it out with a sample, but is that what most programmers do at the designing stage, creating multiple test implementations? Is there some sort of 'design theory' that I would/should be using? I'm self taught, so I don't know if this would be something that is covered in a more structured training environment (so please excuse me if this is a dumb quest

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

          Nobody gets it right the first time; design is an iterative process. A good designer is curious and tries to think of alternatives. In your case, if you're in the habit of increasing or decreasing "everyone's" welfare by a certain % or fixed amount, you might consider adding a "static" % or amount to the Person class that is always used to affect the HungerLevel, but only if it has a value set by a game rule (in order words, it functions as a global weighing factor).

          static float ExtraPain {get;set;} = .1f; // Default
          ..
          float _hunger = 10f;
          float HungerLevel { get { return _hunger * (1f + ExtraPain);}}

          1 Reply Last reply
          0
          • H hpjchobbes

            I actually never thought about it being a separate database, as I was thinking more in a video game setting. The thought came up when looking at a park simulation (Planet Coaster) and trying to decide how I would tackle the same situation. Like I said, I don't have a project that has run into this situation, but I'm curious about how I would approach this concept. In this case the function doesn't really care about the result, just the computation. Is there a word or definition for this concept of 'figuring out how best to store and manipulate data' in programming terminology? I feel as I am floundering when it comes to these thoughts. How to best approach data structure and program structure. I feel like an idiot as I have no idea what words to use when asking about this kind of situation.

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            hpjchobbes wrote:

            I was thinking more in a video game setting

            That just shows my prejudice, I'm a LOB developer and EVERYTHING revolves around the database. I doubt there is a performance difference that is significant, I know I only split a function out if it is to be reused or if the method is too complex and splitting it makes sense when supporting the app (there is that LOB thinking again).

            Never underestimate the power of human stupidity RAH

            1 Reply Last reply
            0
            • H hpjchobbes

              This is more of a general design concept, but if I had a class which I intend to have a list of those objects (around 5000) and I need to perform some function on each of these, should I be designing around a function that is part of the class or a separate function that takes a collection of these objects? I know that there's always situations where one may be better than another, but when first starting to think about the design, which way would be recommend For example:

              class Person {
              float HungerLevel;
              public void AdjustHunger(float amount) { HungerLevel += amount; }
              }

              public void AdjustAllHunger(List PersonList, float amount) { foreach(Person in PersonList) { Person.AdjustHunger(amount); } }

              or

              class Person {
              float HungerLevel;
              public void AdjustHunger(float amount) { HungerLevel += amount; }
              }

              public void AdjustAllHunger(List PersonList, float amount) { foreach(Person in PersonList) { Person.HungerLevel += amount; } }

              I would imagine that the second option is more efficient if I need to change all objects in the collection by the same value, but not sure. I guess it could even depend on what language I'm using and how it would try to optimize the code? Does a call to a function have a higher cost than a call to an object's variable? Expanding on this example, what if the hunger change was based on another of the objects variables.

              class Person{
              float HungerLevel;
              float Metabolism;
              public void AdjustHunger(float amount) { HungerLevel += (amount * Metabolism); }

              public void AdjustAllHunger(List PersonList, float amount) { foreach(Person in PersonList) { Person.AdjustHunger(amount); } }

              vs.

              class Person{
              float HungerLevel;
              public void AdjustHunger(float amount) { HungerLevel += amount; }
              }

              public void AdjustAllHunger(List PersonList, float amount) { foreach(Person in PersonList) { Person.HungerLevel += (amount * Person.Metabolism); } }

              I don't have a project right now that deals with this, but the concept popped into my head and I was thinking about how I would start designing this. I could probably setup a test case and try it out with a sample, but is that what most programmers do at the designing stage, creating multiple test implementations? Is there some sort of 'design theory' that I would/should be using? I'm self taught, so I don't know if this would be something that is covered in a more structured training environment (so please excuse me if this is a dumb quest

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

              hpjchobbes wrote:

              but is that what most programmers do at the designing stage

              Yes.

              hpjchobbes wrote:

              so I don't know if this would be something that is covered in a more structured training environment

              I doubt it. Vast majority of business problems cannot be taught because by the time you teach it then you have created the solution anyways and there are just too many to do that. If you are interested I suggest the following site to get a real idea of how big projects actually got big. It is always via iteration over time. highscalability[^]

              hpjchobbes wrote:

              I would imagine that the second option is more efficient

              Based on your original description and your examples I would use neither. First you must load the actual 'algorithm' via some mechanism. And that can fail. So you certainly can't load all of them and then process each. And some of them might fail as your process it - then what? So presuming that you want to process as many as possible. 1. Loop a. Attempt to load current one. If fail log error so it can be identified then proceed to next b. Process it. If fail log it. If necessary rollback. c. Continue looping on next one until done. Variations on the above. 1. If you need to do this once a day and each item is in your control logging an error might be relevant. But if doing it once a minute then you need to tag items that fail with a flag(s) to indicate that you do not process them until flag(s) cleared (manually by someone that fixed the problem.) 2. Might want to provide notifications of failures, perhaps owners of each item. 3. Might want to have an error that indicates if a 'large' percentage failed since it might indicate a problem with the system and not the items. 4. What if the system is done for a period of time? Do you need to catch up? If so how?

              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