C ++ functional programming (4) Object-Oriented Programming

Source: Internet
Author: User
Tags string methods

Original article: http://www.altdevblogaday.com/2012/04/26/functional-programming-in-c/

Author: John Carmack


-Pan Hong

-March January 2013

-Email:
Popyy@netease.com

-Weibo.com/panhong101

Michael feathers @ mfeathers
Object-oriented code can be understood by encapsulating the activity part. Function programming minimizes the amount of activity to make code understandable.

The "activity part" refers to the changing status. In general OOP Basic Books, the author often begins to talk about how to change the object itself, and programmers still remember this. However, this is an anti-functional behavior. That's right. It makes sense to combine functions with the data they want to operate. However, if you want to add some functional programs to your code and gain advantages, you have to avoid oo behavior in those areas.

In terms of definition, non-const member functions are not pure functions because they may change the state of class objects. It is not thread-safe, so it is easy for objects to enter an unexpected state without knowing it and become a hotbed of bugs.

Const member functions are purely functional technically, provided that the implicit const this pointer in the function is not considered and may be used to change the object state. However, some objects are so large that they can all be called a global environment, which reduces the advantages of pure functions. The constructor can be a pure function, and it is best to make it a pure function-only accept parameters and then return an object.

At the development strategy level, you can often process objects in a more mathematical manner, but you may need to change some interfaces. For example, in ID, we have an idvec3 class that has been used for more than 10 years. It contains a void normalize () method that modifies the object itself, instead of the corresponding idvec3 normalized () const method. Many string methods only change the object itself, rather than returning a copy of the processing result, such as tolowercase () and stripfileextension.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.