Jun 2, 2015

[design] Functions Want To Be Free

Study note excerpt from:
https://github.com/boostcon/cppnow_presentations_2015/raw/master/files/functions_want_to_be_free.pdf

Functions Want To Be Free

● Focus on the goal, not the syntax
● The goal is to maximize encapsulation
  – Not to write member functions


Prefer non-friend free functions (friend can be used in class and found by ADL)
● Isolates changes in implementation
● Forces implementation via the public interface
  – Improves your interface

How non member function improve encapsulation


Guideline: Prefer "one class (or function), one responsibility."
Guideline: Where possible, prefer writing functions as nonmember nonfriends.


Implement std::vector
● A top-down approach
● Implement functions in terms of other functions
● Everything left over cannot be a normal function


Guidelines
● If it must be a member, make it a member
  – Virtual functions
  – Member operators
  – Constructors
● If it can be a non-friend function, make it free
  – Only if no loss of efficiency
● Remember insert
● Otherwise, maximize consistency


Functions Want To Be Free
● Maximize code reuse
● Maximize encapsulation

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.