Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SUGGESTION] Cleaner class declaration and implementation #120

Closed
hattesen opened this issue Nov 15, 2022 · 3 comments
Closed

[SUGGESTION] Cleaner class declaration and implementation #120

hattesen opened this issue Nov 15, 2022 · 3 comments

Comments

@hattesen
Copy link

hattesen commented Nov 15, 2022

Proposal to improve class declaration and implementation syntax

class implementation details leak into declaration

Any private member variables and methods of a class implementation have to be expolicitly declared in the class declaration (header), leading to implementation details leaking into the (public) class interface. This causes a cognitive overload of irrelevant clutter to class users, causes dependent code to require recompilation, and causes the (public) interface to mutate more frequently then necessary when changes to the implementation are made.

Proposal: a new syntax allows the class declaration (header) to remain free of implementation details (private member variables and methods) leaking into the public/published interface.
Implementation: This proposal could be implemented by synthetically allocating and generating a PImpl (pointer to implementation) in every class declaration, and causing all private member variables and methods defined in the class implementation (cpp), but not declared in the class declaration (.hpp), to become member of this automatically generated PImpl struct/class. Although this proposal will lead to every class incurring the overhead/cost of an additional pointer, this could be made optional, controlled by a (pre-)compiler option and/or a class declaration keyword to indicate the desired (implicit) use of the PImpl idiom.

C++ class implementation source is fragmented

The current syntax for implementating class methods leads to fragmented code with a large amount of repetitive "noise" with each method having to declare the class for which the method is implemented void my_class::my_method(). The encapsulation that OOP provides is not carried over into the source code.

Proposal: class implementation syntax encapsulates the implementation source within a class implementation block, and removes the need for each method to explicitly declare which class it belongs to, e.g.

//my_class.cpp`
class my_class impl {
  void my_method() {
    //...
  }
}

Adopting above suggestions will lead to...

  • Cleaner (more readable) class declaration syntax that does not contain leaked implementation details or explicit Pimpl forward declaration
  • Better separation between class/interface declaration and implementation
  • More stable class declaration causing less need for recompilation when implementation changes are made
  • Cleaner (more readable) class implementation syntax
  • Better encapsulation (less fragmentation) of class implementation source

Example of proposed CPP2 syntax

//Declaration: my_class.hpp`
class my_class {
public:
  void my_method();
private:
  int my_var;
  // CPP2: Automatic declaration of my_class::PImpl (forward class and pointer)
  // CPP2: no need to declare private implementation member variables and functions in header
}
//Implementation: my_class.cpp`
class my_class impl { // impl keyword provides context for member variables and functions
  void my_method() { // my_class:: prefix not required
    // implementation
  }
private:
  // private (undeclared) member variable used by implementation
  int var_impl {0}; // CPP2: implicitly declared in my_class::PImpl
  // private (undeclared) member function used by implementation
  void method_impl() { //CPP2: implicitly declared in my_class::PImpl
    // implementation
  }
}
@hattesen hattesen changed the title [SUGGESTION] Class declaration and implementation: Reduce fragmentation and implementation leaking into headers [SUGGESTION] Cleaner class declaration and implementation Nov 15, 2022
@SebastianTroy
Copy link

SebastianTroy commented Nov 15, 2022 via email

@filipsajdak
Copy link
Contributor

Need to confront that with modules.

@hsutter
Copy link
Owner

hsutter commented Nov 25, 2022

Thanks for the suggestions! I hope to implement classes over the winter vacation (if I'm lucky!) and I hope you'll find the design addresses the concerns above. Also, as some have noted, Cpp2 is a modules-first design, and Cpp2 won't have header files or separate declarations from definitions, which simplifies some of these concerns. More in the coming months... again, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants