卡马克谈Functional programming in C++

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


这个在《程序员》杂志上有译文,也很不错。


functional programming is good

卡马克根据自己的实践得出的结论是,软件开发过程中的很大一部分的不足来自开发者对于自己程序将会运行于哪些可能的状态(state)认识不足,而这样的认识不足导致的问题在多线程并行环境中会被扩大化。

而过程式编程倾向于将程序中的state明显化,这样我们就更容易有充足的认识,相比而言,OO编程则很容易把state的转换给封装起来,让我们对state的控制和认识变得更难。

c++并不鼓励functional programming,但是也不禁止,而且还赋予了我们很多底层编程能力,这也是c++黑很多,但是绝大多数的高品质游戏还是由c++写的原因所在。


Pure Function

carmack定义了一个pure function,这是一个很有意义的抽象(尽管它不存在),pure function只关注传入的参数,然后返回一个或多个基于参数的结果。

并且它:

  • 不维护自己的内部状态
  • 不和全局数据做交互(读或者写),不做IO
  • 不修改参数
读到这部分的时候,我已经很明显的感觉到这样的pure function的简洁和强大了,它有这样的强力属性:
  • 易于重用
  • 易于测试---由于不依赖很多东西,传入一个值,那么返回值是一定的
  • 易于维护和理解
如果只是不用考虑长远的处理问题,那么怎么方便怎么搞,但是如果你是想写一个在多年后仍旧很有战斗力的代码,那么还是深思熟虑的好,比如写一些纯函数,而不是很随意的读写全局变量。

而实践中显然会有相当的情况,让我们很难甚至无法使用pure function来搞定问题,carmack认为不能绝对纯,但是尽可能纯,也会大幅度的提高代码质量,并且把烂代码弄得比较纯性价比颇高,而做到绝对纯则常常是边际效应递减的很厉害。

而代码质量对代码的未来有巨大的决定作用,代码质量差,会把项目自己拖入沼泽,最后死了拉到。

而到object oriented programming中来看,const是有着很大的设计意义,有const的成员函数在维护内部状态方面仍旧可以认为是纯的。

效率与实践
一般来说,效率和生产力是此消彼长的矛盾的,使得我们不得不常常打破纯函数这样的实现。
实践中,carmack建议关注state的变换,如果可以的话,让函数返回一份copy而不是自己修改自己(比如vector的normalize),尽可能在可以使用const的地方使用const。



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Learning C++ Functional Programming by Wisnu Anggoro English | 10 Aug. 2017 | ISBN: 1787281973 | ASIN: B06WVD7CVT | 304 Pages | AZW3 | 2.4 MB Key Features Modularize your applications and make them highly reusable and testable Get familiar with complex concepts such as metaprogramming, concurrency, and immutability A highly practical guide to building functional code in C++ filled with lots of examples and real-world use cases Book Description Functional programming allows developers to divide programs into smaller, reusable components that ease the creation, testing, and maintenance of software as a whole. Combined with the power of C++, you can develop robust and scalable applications that fulfill modern day software requirements. This book will help you discover all the C++ 17 features that can be applied to build software in a functional way. The book is divided into three modules—the first introduces the fundamentals of functional programming and how it is supported by modern C++. The second module explains how to efficiently implement C++ features such as pure functions and immutable states to build robust applications. The last module describes how to achieve concurrency and apply design patterns to enhance your application's performance. Here, you will also learn to optimize code using metaprogramming in a functional way. By the end of the book, you will be familiar with the functional approach of programming and will be able to use these techniques on a daily basis. What you will learn Get to know the difference between imperative and functional approaches See the use of first-class functions and pure functions in a functional style Discover various techniques to apply immutable state to avoid side effects Design a recursive algorithm effectively Create faster programs using lazy evaluation Structure code using design patterns to make the design process easier Use concurrency techniques to develop responsive software Learn how to use the C++ Standard Template Library and metaprogramming in a functional way to improve code optimization About the Author Wisnu Anggoro is a Microsoft Certified Professional in C# programming and an experienced C/C++ developer. He has also authored the books Boost.Asio C++ Network Programming - Second Edition and Functional C# by Packt. He has been programming since he was in junior high school, which was about 20 years ago, and started developing computer applications using the BASIC programming language in the MS-DOS environment. He has solid experience in smart card programming, as well as desktop and web application programming, including designing, developing, and supporting the use of applications for SIM Card Operating System Porting, personalization, PC/SC communication, and other smart card applications that require the use of C# and C/C++. He is currently a senior smart card software engineer at CIPTA, an Indonesian company that specializes in innovation and technology for smart cards. He can be reached through his email at wisnu@anggoro.net. Table of Contents Diving into Modern C++ Manipulating functions in functional programming Applying immutable state to the function Recurring method invocation using recursive algorithm Procrastinating the execution process using Lazy Evaluation Optimizing code with Metaprogramming Running parallel execution using Concurrency Creating and debugging application in functional approach
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值