locked
Private Method Arguments RRS feed

  • Question

  • This is more of an OO question really than C# so maybe not the right place to ask but I mostly program C#

    I have studied OO formally and I know both these things are valid but just want to put this out there to see what response I get.

    If I have a private class method that uses a private class instance variable should I declare it in the method signature as an argument and pass the variable when I make the call inside the class or should I just not bother and simply use the class instance variable inside the method. Both are valid - i.e. they work.

    I'm favouring towards the less lazy option and putting the argument in because it makes it easier when I see the call I know what variables it's using to do it's job. Although I've found that I'm a bit inconsistent.

    Thanks


    shaun




    Friday, December 5, 2014 9:50 AM

Answers

  • The point of a private member is that it's used through the class but internal to that class.

    If you only use a variable in a method then I should only be declared in that method.

    Otherwise then it's preferable IMO to use the one object scoped across the class.

    If you use something throughout a class then there's an argument you're just creating work if you declare that thing again.  You could conceivably introduce the potential for subtle bugs.

    If this is actual a reference type then you change values as you use it inside a method.

    I would use the private instance rather than adding parameters to all the methods for those 3 reasons:

    • A bit less code
    • A bit less scope for introducing bugs

    But mainly.

    • Easier to understand you're using the one object

    Hope that helps
    Please don't forget to upvote posts which you like and mark those which answer your question.

    • Proposed as answer by Barry Wang Tuesday, December 9, 2014 10:23 AM
    • Marked as answer by shaun_chiburi Thursday, December 11, 2014 11:26 AM
    Friday, December 5, 2014 2:09 PM

All replies

  • >>If I have a private class method that uses a private class instance variable should I declare it in the method signature as an argument and pass the variable when I make the call inside the class or should I just not bother and simply use the class instance variable inside the method. Both are valid - i.e. they work.

    It depends. If you pass the object as a parameter to the method, you are implicitly implying that you are going the perform the perform the calculation or whatever you are doing inside the method on this very specific instance of the object and not on an (the) object that is encapsulated within the class.

    There is no reason to pass the object as a parameter if it is already encapsulated in the class. One reason for defining instance fields in the first place is to avoid having to pass them as parameters, i.e. the methods within the class can access them anyway since they are in fact member variables.

    Only if the method is static or if you only want to modify the passed in value you should pass the object as a parameter.

    Please remember to mark helpful posts as answer and helpful.

    • Proposed as answer by Barry Wang Tuesday, December 9, 2014 10:23 AM
    Friday, December 5, 2014 1:57 PM
  • The point of a private member is that it's used through the class but internal to that class.

    If you only use a variable in a method then I should only be declared in that method.

    Otherwise then it's preferable IMO to use the one object scoped across the class.

    If you use something throughout a class then there's an argument you're just creating work if you declare that thing again.  You could conceivably introduce the potential for subtle bugs.

    If this is actual a reference type then you change values as you use it inside a method.

    I would use the private instance rather than adding parameters to all the methods for those 3 reasons:

    • A bit less code
    • A bit less scope for introducing bugs

    But mainly.

    • Easier to understand you're using the one object

    Hope that helps
    Please don't forget to upvote posts which you like and mark those which answer your question.

    • Proposed as answer by Barry Wang Tuesday, December 9, 2014 10:23 AM
    • Marked as answer by shaun_chiburi Thursday, December 11, 2014 11:26 AM
    Friday, December 5, 2014 2:09 PM