locked
class object vs variables as parameters RRS feed

  • Question

  • User-2103146910 posted

    Hi,

    i am confused when i have to pass parameters to a calling function, for me there are a couple of options

    1) create a class containing properties (variables) that you want to pass as arguments and assign values from the variables etc. to class object and pass that object

    [being a single argument/parameter]

    2) pass all variables separately as arguments [being multiple parameters]

    kindly if any could guide me which is more professional and performance cater way.

    with many thanks.

    Thursday, September 27, 2018 1:35 PM

All replies

  • User475983607 posted

    i am confused when i have to pass parameters to a calling function, for me there are a couple of options

    1) create a class containing properties (variables) that you want to pass as arguments and assign values from the variables etc. to class object and pass that object

    [being a single argument/parameter]

    2) pass all variables separately as arguments [being multiple parameters]

    kindly if any could guide me which is more professional and performance cater way.

    with many thanks.

    There's no right or wrong answer to this question.  It depends on what you're doing...

    For example, it does not make sense to wrap an built-in type in a class if all that needed is an int.  Likewise, it does not make sense to pass individual values if there is already a complex type containing the same members.

    Thursday, September 27, 2018 2:42 PM
  • User753101303 posted

    Hi,

    Yes, usually you don't design classes based on just this need. Either values are unrelated so you don't design a class and you'll handle those values separately (everywhere including passing parameters) or it make sense to gather them all inside a single object and then you'll use this object everywhere.

    The basic idea behind OOP is to gather data and code into a "class" that makes sense for your goal (ie a User, an Invoice, a bank Account etc...) allowing to write an app that acts on those real world concepts more easily. It doesn't serve any technical goal and even less about being efficient when passing parameters.

    Thursday, September 27, 2018 3:24 PM