2

In different design books I have read that

  1. First identify the purpose of class (abstraction).
  2. Class should only do one thing (SRP).
  3. methods are defined as responsibility of the class.

As per my understanding public methods handle the responsibilities of the class which require other class [ie interacting classes]

My question is what responsibilites private members of the class do? and how to decide which method to be made private?

Have a look at my code

class calculator
 {
    private:
    long double operand_1;
    long double operand_2;
    long double result;

    int optr;
    int multiplier;

    Button One;
    Button Two;
    Button Three;
    Button Four;
public:
    calculator();

    const long double get_number_operator(const int=0);
    const long double calculate_number(const int [],const int);

    void show_calculator( );
    void calculations( );
    void clear_screen( );
    void show_back(const int);
    void show_about( );
    void show_time( );

The method in public are really public or we can put those in private?

  • 4
    Easy: make every method private, unless it has to form part of the public API of that class to make that class useful. – David Arno Aug 3 '17 at 10:56
  • 1
    Take a look here stackoverflow.com/q/2620699/5934037 – Laiv Aug 3 '17 at 11:04
  • @David Arno- so does it mean I have to make every member private in the initial draft level of design, I m doing so wrong in my design from a long time and make every member public without knowing what to make private inside class. Only variables I assign under private tag, Thanks man!! :) – Narender Parmar Aug 3 '17 at 11:09
  • Possible duplicate of Should a method do one thing and be good at it? – gnat Aug 3 '17 at 11:10
  • @gnat - think the above question more or less defines the SRP in context of method example method add will only add and does not print, it does only one thing ie add, Correct me if i m wrong – Narender Parmar Aug 3 '17 at 11:19
4

Private members come in two flavours:

  • Private data members are used to store the internal state of the object
  • Private methods can be seen as named blocks of code that can be used by other methods of the class to fulfill their responsibility.
    Private methods are typically used when several methods need to do the exact same work as part of their responsibility (like notifying external observers that the object has changed), or when a method is split in smaller steps for readability.

A method should be made private if other classes would have no reason to know that the method exists.

  • 1
    The other use for private methods is to provide an abstraction. isEmpty() rather than size==0 – Nick Keighley Aug 3 '17 at 11:25
  • I have updated the question with code, can you update me the methods I keep in public are really public? like show_calculatorobv need GUI class to interact, so we put show_calculator in public? – Narender Parmar Aug 3 '17 at 11:31
  • @NarenderParmar, As your class is designed, it is hard to tell if all methods are correctly declared public, because your class seems to have multiple responsibilities (handling the display, performing calculations, showing about screens). – Bart van Ingen Schenau Aug 3 '17 at 11:40
  • @Bart van Ingen Schenau - okay cool!! so I need to split my class responsibility, thanks for updating – Narender Parmar Aug 3 '17 at 11:47
  • 1
    A method should be made private if other classes would have no reason to know that the method exists. and no reason to change it. Private methods also protects sensible code. If altering a method, the class miss its reason to change (SRP) , the access to such method should be as constrained as possible. – Laiv Aug 3 '17 at 14:21
2

The public members are the interface through which the outside world interacts with this object.

The private members are implementation details that describe how a class fulfils its purpose. Since they are private they do not affect how other code interacts with our class and are therefore not part of a larger design. The private members are used to implement the public methods. So basically: any member you create should be private if it is not needed for the external interface of that class.

Sometimes private methods implement some functionality that could be modelled as a responsibility in its own right, could be re-used in another context, or is so complex that you want to unit-test it directly. It can then make sense to extract these methods into free functions or into a class of its own. You often end up with a number of utility classes that have nothing to do directly with the core business logic of your software, but are needed by that business logic to get the job done. For example: methods for loading data from files, abstract algorithms, or adapters to other systems. Quite often, you can use existing libraries for these tasks.

  • @amon-i m about to add the unit test part, you explained it a way, can you please elaborate it will be really helpful – Narender Parmar Aug 3 '17 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.