可靠性建模 编程
Originally published on Aug. 25, 2020, on Hewlett Packard Enterprise’s Enterprise.nxt, publishing insights about the future of technology.
最初于2020年8月25日在Hewlett Packard Enterprise的 Enterprise.nxt上 发布,发布了有关技术未来的见解。
Functional programming eliminates many characteristics that make programs buggy. Although some languages are more suitable for it than others, support for functional programming is increasing across the board.
函数式编程消除了许多使程序出错的特征。 尽管某些语言比其他语言更适合,但对功能编程的支持也在不断增加。
Among the many programming paradigms, functional programming has been in quite a niche for the past 60 years. Although game changers like Google’s search algorithm rely on its very key concepts, the average programmer of today knows little to nothing about it. Until recently, object-oriented programming has always taken the crown — and functional programming has been left behind, despite its beauty and applicability.
在许多编程范例中 ,函数式编程在过去60年中一直处于相当的市场地位。 尽管像Google搜索算法这样的游戏规则改变者依赖其非常关键的概念 ,但当今的普通程序员对此几乎一无所知。 直到最近,面向对象的程序设计一直占据主导地位,而功能性程序设计虽然美观和适用,但一直被抛在后面。
That’s about to change. With the rise of big data, artificial intelligence, and other technologies, functional programming is becoming more and more advantageous. It’s therefore no wonder that popular languages like Java and Python are adopting more and more concepts from functional programming. Other languages like Haskell are almost purely functional.
那将要改变。 随着大数据,人工智能和其他技术的兴起,函数式编程变得越来越有优势。 因此,难怪像Java和Python这样的流行语言正在采用功能编程中越来越多的概念。 其他语言,例如Haskell ,几乎都是纯功能的。
In simple terms, functional programming is all about building functions for immutable variables. That is, once a value is assigned to a variable, this assignment stays until the end. To add more values, one must add more variables. This is useful when dealing with vast amounts of data that shouldn’t be copied a million times, to save computing power and memory space.
简单来说 ,函数式编程就是为不可变变量构建函数。 也就是说,将值分配给变量后,该分配将一直保留到最后。 要添加更多值,必须添加更多变量。 当处理不应复制一百万次的大量数据时,此功能很有用,以节省计算能力和内存空间。
A function without an explicit input is guaranteed to have a side effect, since every function needs some kind of input.
没有显式输入的函数可以保证具有副作用,因为每个函数都需要某种输入。
In contrast, object-oriented programming is about having a relatively fixed set of functions and modifying or adding new variables. This is useful for relatively small datasets but can quickly get out of hand as the data volume increases.
相反,面向对象的编程是关于具有相对固定的一组功能以及修改或添加新变量的。 这对于相对较小的数据集很有用,但随着数据量的增加会很快失去控制。
Because of its nature, functional programming is great for in-demand tasks such as data analysis and machine learning. This doesn’t mean you should say goodbye to object-oriented programming and go completely functional. It is useful, however, to know about the basic principles so you can use them to your advantage when appropriate.
由于其性质,函数式编程非常适合诸如数据分析和机器学习之类的需求任务。 这并不意味着您应该告别面向对象的编程并完全使用它。 但是,了解基本原理很有用,因此您可以在适当时使用它们以发挥自己的优势。
是什么使程序出错,以及函数式编程如何提供帮助 (What makes programs buggy, and how functional programming can help)
It’s not like there are no functions in object-oriented programming. But often, these functions are muddled with the data and implemented in a manner that easily introduces bugs. This way of coding is easy to learn and makes beginners productive faster. But from the perspective of a functional programmer, they’re a stylistic no-go.
就像在面向对象的编程中没有函数一样。 但是,这些功能经常与数据混淆,并以易于引入错误的方式实现。 这种编码方式易于学习,可以使初学者提高生产效率。 但是从功能性程序员的角度来看,它们是风格上的硬汉。
The basic idea is that states don’t change in functional programming. Therefore, persistent data is never part of a function but rather used to test functions. This alone makes debugging easier because a programmer can quickly see whether a function is misbehaving or a faulty dataset is at the core of an error. In addition, there are other, more theoretical advantages:
基本思想是状态在函数式编程中不会改变。 因此,持久性数据绝不是功能的一部分,而是用于测试功能的。 仅此一项就使调试更加容易,因为程序员可以快速查看函数是否行为不当或错误的数据集是错误的核心。 此外,还有其他更多的理论优势 :
Locality: Not separating data from functions means that data is potentially spread out all over the source code. By separating it off, one can put all data into a few input files. This way, there is less danger of accidentally introducing bugs in the source code when data needs to be changed.
局部性 :不将数据与功能分开意味着数据可能会散布在整个源代码中。 通过分离,可以将所有数据放入几个输入文件中。 这样,当需要更改数据时,将意外地在源代码中引入错误的风险较小。
Timeliness: Because functions don’t change states in functional programming, it’s easier to determine when data is known, be it while the code is written, when it starts executing, while it’s running, or when it’s finished. This helps keep an overview over the data and makes debugging easier.
及时性 :由于函数在函数式编程中不会改变状态,因此更容易确定何时知道数据,无论是在编写代码时,在开始执行时,在运行时或何时结束。 这有助于保持对数据的概览,并使调试更加容易。
Structure: It goes without saying that it’s easier to get more structure into your code if you separate off the data. That’s not to say that object-oriented programming is unstructured; rather, it’s easier to create clear source code when following clear guidelines that impose a minimum of structure. This also makes it easier to keep an overview of all functions.
结构 :不用说,如果分离数据,则更容易在代码中获得更多结构。 这并不是说面向对象的编程是非结构化的; 相反,遵循遵循最少结构的清晰准则,创建清晰的源代码会更容易。 这也使得更容易保持所有功能的概览。
Note that while separating functions and data is indispensable in functional programming, it’s completely possible to implement in object-oriented programming. It is therefore one of the ways in which programmers can improve their code through some tricks of functional programming without having to learn anything fundamentally new.
请注意,尽管在函数式编程中将函数和数据分开是必不可少的,但完全有可能在面向对象的编程中实现。 因此,它是程序员可以通过一些函数式编程技巧来改进其代码而无需学习任何根本性的新方法的方式之一。
函数式编程正在编写纯函数 (Functional programming is writing pure functions)
A pure function is one that always returns the same output for a given input and has no side effects — that is, that modifies data outside of the function. Pure functions are almost a corollary of separating data from functions.
纯函数是始终为给定输入返回相同输出且没有副作用的函数,也就是说,该函数会修改函数外部的数据。 纯函数几乎是将数据与函数分离的必然结果。
Object-oriented programming effectively encourages side effects by giving them a place within objects. In a large program, this makes debugging harder because the programmer needs to check the whole function for additional dependencies.
面向对象的编程通过在对象内放置一个位置来有效地鼓励副作用。 在大型程序中,这使调试更加困难,因为程序员需要检查整个函数是否存在其他依赖项。
In functional programming, only pure functions are allowed. This not only makes debugging a lot easier but also facilitates writing functions inside one another by creating functions that return a function, or functions that take another function as an argument. When facing complex problems, this can be a huge advantage.
在函数式编程中,仅允许使用纯函数。 这不仅使调试变得更加容易,而且还通过创建返回函数的函数或将另一个函数作为参数的函数,方便了彼此之间编写函数的编写。 当面临复杂的问题时,这可能是一个巨大的优势。
这些语言正在(或没有)看到趋势 (These languages are seeing the trend (or not))
While by no means a complete list, the following provides a brief overview of how much popular languages are adopting functional programming.
虽然绝非完整列表,但以下内容简要概述了采用功能编程的流行语言。
Perl (Perl)
In stark contrast to other languages, Perl embraces side effects instead of discouraging them. This makes sense, considering its inception as a language for text manipulation, where side effects can indeed be useful. Although Perl does support some functional concepts, I wouldn’t try too much functional programming with it.
与其他语言形成鲜明对比的是,Perl包含副作用而不是阻止它们。 考虑到它最初是一种用于文本处理的语言,这是有道理的,在这种情况下,副作用确实有用。 尽管Perl 支持某些功能概念,但我不会尝试太多的功能编程。
走 (Go)
As an imperative and procedural language, Go isn’t the first choice when it comes to functional programming. It is, however, possible to implement recursions, pure functions, and more in a relatively straightforward way.
作为一种命令式和过程性语言,在函数式编程中,Go并不是首选。 但是, 可以以相对简单的方式实现递归,纯函数等。
Java (Java)
Programmers who try to write functional code in Java usually end up with code that is littered with static keywords. This doesn’t add to the readability of the code and often reverses any gains in readability through using functional concepts. While it is in principle possible, functional programming and Java are not a good match in practice.
试图用Java编写功能代码的程序员通常最终会得到带有静态关键字的代码。 这不会增加代码的可读性,并且通常会通过使用功能概念来扭转可读性的任何提高。 虽然原则上可行,但是函数式编程和Java在实践中并不是很好的匹配。
Swift (Swift)
Although Swift, being a multi-paradigm language, supports some key concepts of functional programming, resources about it are rather scarce. This could be due to the fact that Swift is still relatively young compared with other languages and its community is smaller than that of others. In any case, other languages like the ones listed below might be a better option to learn some concepts of functional programming.
尽管Swift是一种多范式语言,它支持函数式编程的一些关键概念,但有关它的资源却相当稀缺。 这可能是由于Swift与其他语言相比还相对年轻,并且其社区比其他语言要小。 在任何情况下,下面列出的其他语言都可能是学习函数编程某些概念的更好的选择。
C族 (The C family)
Because C is a procedural language, it doesn’t support functional programming natively. There are, however, packages like FFCALL that support some elements of functional programming, such as lambda expressions. In a similar way, Objective-C is an imperative language, which makes functional code almost impossible to implement.
因为C是一种过程语言,所以它本身不支持函数式编程。 但是,有些软件包(例如FFCALL)支持功能编程的某些元素,例如lambda表达式。 以类似的方式,Objective-C是命令式语言,这使得功能代码几乎无法实现。
C++ supports lambda expressions in a simpler way since version 11; it doesn’t support immutable data types, however. It’s therefore considered an impure functional programming language. C# is similar in that sense because it also supports some concepts of functional programming, while others are hard to implement.
从版本11开始 ,C ++以更简单的方式支持lambda表达式; 但是,它不支持不可变的数据类型。 因此,它被视为不纯的函数式编程语言。 从这个意义上讲,C#是相似的,因为它还支持某些函数式编程概念,而其他一些则很难实现。
JavaScript (JavaScript)
As a multi-paradigm language, JavaScript supports functional programming but doesn’t force it on the developer. When using frameworks like Angular and React, however, using immutable data structures can lead to a performance boost. This makes JavaScript a great language for learning some functional concepts without giving up the advantages of other paradigms.
作为一种多范式语言,JavaScript支持函数式编程,但并不强加于开发人员。 但是,当使用Angular和React这样的框架时,使用不可变的数据结构可以提高性能 。 这使JavaScript成为一种用于学习某些功能概念而又不放弃其他范例优势的出色语言。
PHP (PHP)
Similar to JavaScript, PHP is a multi-paradigm language and has built-in support for some key concepts like recursions and functions that take or return other functions. Developers who are using PHP anyway might want to consider making use of these concepts.
与JavaScript相似,PHP是一种多范式语言,并且对某些关键概念(例如递归和采用或返回其他函数的函数)具有内置支持。 无论如何,使用PHP的开发人员可能要考虑利用这些概念。
Ruby (Ruby)
Just like the two aforementioned languages, Ruby is a multi-paradigm language that supports key concepts such as immutable datasets. This, again, makes it very suitable for functional programming novices.
就像前面提到的两种语言一样,Ruby是一种多范式语言,它支持诸如不变数据集之类的关键概念 。 同样,这使得它非常适合函数式编程新手。
Scala (Scala)
Scala’s goal is to unify object-oriented and functional programming. This seems odd to many users because functional programming aims at eliminating side effects completely while object-oriented programming tries to keep them inside objects.
Scala的目标是统一面向对象和函数式编程。 这对许多用户来说似乎很奇怪,因为功能编程旨在完全消除副作用,而面向对象的编程则试图将其保留在对象内部。
The point is, however, that Scala is mostly functional, and object-oriented only if functional would be too much of a hassle. This makes it an excellent language to manage transitioning from object-oriented to functional programming.
关键是,Scala主要是功能性的,并且仅在功能性太麻烦时才面向对象。 这使它成为管理从面向对象编程到函数式编程过渡的优秀语言。
Python (Python)
Python actively encourages functional programming. You can see this by the fact that every function has, by default, at least one input: self. A function without an explicit input is guaranteed to have a side effect, since every function needs some kind of input. This mirrors the Zen of Python: Explicit is better than implicit.
Python积极鼓励函数式编程。 您可以通过以下事实看到这一点:每个函数默认都有至少一个输入: self 。 没有显式输入的函数可以保证具有副作用,因为每个函数都需要某种输入。 这反映了Python的禅:显式优于隐式。
Clojure (Clojure)
According to its creator, Clojure is about 80 percent functional. All values are immutable by default, just like you need them in functional programming. However, you can get around that by using mutable-value wrappers around these immutable values. When you open such a wrapper, the thing you get out is immutable again.
根据其创建者的说法,Clojure的功能性约为80%。 默认情况下,所有值都是不可变的,就像在函数编程中需要它们一样。 但是,您可以通过对这些不可变值使用可变值包装器来解决此问题。 当您打开这样的包装纸时,您得到的东西将再次变得不可变。
哈斯克尔 (Haskell)
This is one of the few languages that are purely functional and statically typed. While this might seem like a time drainer during development, it pays off bigly when debugging a program. It’s not as easy to learn as other languages, but it’s definitely worth the investment.
这是纯粹的功能性和静态类型化的少数语言之一。 尽管这在开发过程中可能会浪费时间,但在调试程序时会大有收获。 它不像其他语言那么容易学习,但是绝对值得投资。
函数式编程的优点和局限性 (The beauty and limitations of functional programming)
Even though functional programming has many upsides, the ubiquity of object-oriented programming indicates that functional isn’t the end of the story. Let’s face it: Some of the virtues of functional programming are also its biggest downsides:
尽管函数式编程有很多好处,但是面向对象编程的普遍性表明函数式并不是故事的结局。 让我们面对现实:函数式编程的一些优点也是其最大的缺点:
No I/O: One of the most practical features of non-functional programming (that includes object-oriented programming) is that you can process user-generated input and write output to the screen. However, I/O is a side effect in a fundamental way. So, if you want to write purely functional code, you’ll have to live with the fact that I/O isn’t possible or find a rather complex workaround.
没有I / O :非功能编程(包括面向对象的编程)最实用的功能之一就是您可以处理用户生成的输入并将输出写入屏幕。 但是,从根本上说,I / O是一种副作用。 因此,如果您要编写纯功能代码,则必须忍受无法进行I / O的事实,或者找到一个相当复杂的解决方法。
Recursions and memory usage: One key feature of functional programming is that loops are abstracted away and replaced by recursions. Because these recursions always create new objects instead of manipulating old ones, this can result in rather high memory usage. There are relatively simple workarounds around this issue, but beginners in particular might be tempted to overuse memory initially.
递归和内存使用 :函数式编程的一项主要功能是将循环抽象化并由递归代替。 因为这些递归总是创建新对象而不是操纵旧对象,所以这可能导致相当高的内存使用率。 解决此问题的方法相对简单,但特别是初学者可能会在一开始就过度使用内存。
No beginner-friendliness: Functional programming is directly borrowed from mathematics and, as such, comes charged with a lot of special jargon, proofs, and theorems. This can intimidate beginners. And although one can in principle prove that a functional program is correct, in practice these proofs are rather lengthy and become nearly impossible to understand when the program is large.
没有初学者友好 :函数式编程是直接从数学中借用的,因此要承担很多特殊的术语,证明和定理。 这会吓到初学者。 并且尽管原则上可以证明一个功能程序正确,但是实际上这些证明很长,当程序很大时,几乎变得难以理解。
Smaller community: Even though there are quite lively forums for Haskell, Scala, and other languages, the community for object-oriented programming is dozens of times larger. It’s therefore more difficult to find solutions to problems by browsing Stack Overflow or related forums, or by asking a colleague.
较小的社区 :尽管有很多关于Haskell,Scala和其他语言的活跃论坛,但是面向对象编程的社区却大了数十倍。 因此,通过浏览Stack Overflow或相关论坛或询问同事来查找问题的解决方案更加困难。
These drawbacks demonstrate that pure functional programming is often not a good idea. However, including key concepts of functional programming into otherwise object-oriented code can make programs a lot more structured, readable, and easier to debug.
这些缺点表明,纯函数式编程通常不是一个好主意。 但是,将功能性编程的关键概念包含在其他面向对象的代码中会使程序更加结构化,易读且易于调试。
大数据来了,它带来了一个朋友:函数式编程 (Big data is coming and it’s bringing a friend: Functional programming)
In contrast to object-oriented programming, functional programming is still a niche phenomenon. If the inclusions of functional programming principles in Python and other languages are of any significance, however, then functional programming seems to be gaining traction.
与面向对象的编程相反,函数式编程仍然是一种小众现象。 但是,如果在Python和其他语言中包含函数式编程原理具有任何意义,那么函数式编程似乎正越来越受到关注。
Even though some languages are adopting few concepts of functional programming or none at all, many popular languages are including them more and more. It turns out that separating data from functions, eliminating side effects, and taking advantage of recursions and nested functions can make a program a lot cleaner and easier to maintain.
即使某些语言只采用了很少的功能编程概念或根本没有采用任何概念,许多流行的语言也越来越多地使用它们。 事实证明,将数据与功能分离,消除副作用以及利用递归和嵌套功能可以使程序更简洁,更易于维护。
Even though it’s an old paradigm, it makes sense that functional programming is gaining traction now: Among other things, it’s great for big databases, parallel programming, and machine learning. And all these fields have been booming over the past decade.
尽管这是一个古老的范例,但有意义的是,函数式编程现在正在受到关注:除其他外,它对于大型数据库,并行编程和机器学习非常有用。 在过去的十年中,所有这些领域都在蓬勃发展。
Functional programming has many virtues worth considering. Although pure functional code is unsuitable in many situations, including some basic functional concepts in existing code, it can make programs much more suitable to the problems they’re addressing.
函数式编程具有许多值得考虑的优点。 尽管纯功能代码在许多情况下都不适合使用,包括现有代码中的一些基本功能概念,但它可使程序更适合其要解决的问题。
函数式编程:给领导者的教训 (Functional programming: Lessons for leaders)
- Functional programming can improve the reliability of software, where practical. 在可行的情况下,函数式编程可以提高软件的可靠性。
- Sometimes it’s best to change programming languages and paradigms based on the nature of the application. 有时,最好根据应用程序的性质更改编程语言和范例。
- Old ideas, like functional programming, can find new significance as the tech landscape changes. 随着技术格局的变化,旧的想法(例如函数式编程)可以找到新的意义。
有关: (RELATED:)
4 lessons for modern software developers from 1970s mainframe programming
This article/content was written by the individual writer identified and does not necessarily reflect the view of Hewlett Packard Enterprise Company.
本文/内容由确定的个人作家撰写,不一定反映Hewlett Packard Enterprise Company的观点。
翻译自: https://medium.com/enterprise-nxt/programming-software-reliable-technology-8e2e3c8caf28
可靠性建模 编程