天天看点

什么样的代码是好代码_什么使好的代码2 7 语言很重要 (Language is important)

什么样的代码是好代码

语言很重要 (Language is important)

This is the second section of the article about how to make good code. If you miss the first part, click here. I would like to make this article as accessible as possible, so if you are already a programmer, you can skip this part, but it is always good to remind of the basics.

这是本文的第二部分,关于如何编写良好的代码。 如果您错过了第一部分, 请单击此处 。 我想使本文尽可能地易于理解,因此,如果您已经是程序员,则可以跳过本部分 ,但是经常提醒您一些基础知识。

Every code is expressed in a language that communicates with the machine. Computers only understand basic instructions, assembly language, which is the closest thing to the zeros and ones of the logic circuit. The complexity of writing code at this level grows quickly, because you have to manage loading from memory to registers (place where processor can perform operations); saving data from register to memory; process basic operation like adding, copying with registers; and jump directly to some instruction or based on the value of a register. This is a grotesque simplification, but I think it gets the point of how low level these instructions are. When you are at this level you can do anything and it is not because you can do something, that you should be doing it.

每个代码均以与机器通信的语言表示。 计算机只理解基本指令和汇编语言,这是最接近逻辑电路的零和一。 由于必须管理从内存到寄存器(处理器可以在其中执行操作的位置)的加载,因此在此级别编写代码的复杂性Swift增长。 将数据从寄存器保存到内存; 处理基本操作,例如添加,复制寄存器; 并直接跳转到某些指令或基于寄存器的值。 这是一个怪诞的简化,但是我认为这说明了这些指令的水平如何。 当您处于此级别时,您可以做任何事情, 不是因为您可以做某事,而是应该这样做 。

When humans must handle every tiny detail of code flow and memory, a lot of common mistakes happen and there is nothing we can do about it. So, it was a necessity to have languages that we could communicate with machines at a higher level and leave that tedious task to a compiler to figure out. There are thousands of high-level computer languages, but there are few programming paradigms. Understanding programming paradigms is the key to good code. We are going to take an overview on the most influential paradigms for general purpose programming.

当人类必须处理代码流和内存的每个微小细节时,会发生许多常见错误,而我们无能为力。 因此,有必要使我们能够与更高级别的机器进行通信的语言并将繁琐的任务留给编译器来解决。 有成千上万种高级计算机语言,但是编程范例很少。 了解编程范例是编写好代码的关键 。 我们将概述通用编程中最有影响力的范例。

The procedural programming is the closest to assembly code, it adds two basic concepts: procedures (or functions) and variables. Procedures/Functions are black boxes with a sequence of instructions that receives parameters and have a return value. Variables are an abstraction of memory, so instead of dealing with just numbers in binary format we can have many types of numbers, letters and custom types that are more useful to programming. Here, functions are first-class citizens so you can call functions inside functions and use functions as parameters. If you want to calculate the magnitude of a 2D vector in a procedural language, you will have something like:

过程编程最接近汇编代码,它增加了两个基本概念:过程(或函数)和变量。 过程/功能是黑盒子,带有一系列接收参数并具有返回值的指令。 变量是内存的抽象,因此,我们不仅可以处理二进制格式的数字,还可以拥有许多对编程更有用的数字,字母和自定义类型。 在这里,函数是一等公民,因此您可以在函数内部调用函数并将函数用作参数。 如果要使用过程语言计算2D向量的大小,将具有以下内容:

float getMagnitude(float x, float y)

float getMagnitude( float x, float y)

The line above is in C-like syntax and it expresses the declaration of a function that receives two numbers (float x and y) and returns a number (float). By the way, C is a popular language created in the early 1970’s and still influences a lot of programming languages today. Float is a variable type that represents real numbers. If you want to get the magnitude of a 2D vector you do not have to know how it works, just use this function passing the values of the X and Y components of the vector.

上面的行采用类似C的语法,它表示一个函数的声明,该函数接收两个数字(浮点数x和y)并返回一个数字(浮点数)。 顺便说一句,C是一种在1970年代初期创建的流行语言,并且至今仍影响着许多编程语言。 浮点数是代表实数的变量类型。 如果要获取2D向量的大小,则不必知道其工作原理,只需使用此函数传递向量的X和Y分量的值即可。

Black box is an important concept for software engineering. The function above is a good example of black box because it is stateless. It doesn’t matter in which order you call this function or which combination of parameter you use, it always returns the right value. This is true because it is a mathematical function, but most of the time you need to add state to your software and it’s the root of many problems. The worst place to add state in your software is inside a black box. Good code uses stateless black boxes and it is concerned about the current state of its systems.

黑匣子是软件工程的重要概念。 上面的函数是黑匣子的一个很好的例子,因为它是无状态的。 无论以什么顺序调用此函数或使用哪种参数组合,它始终返回正确的值。 这是正确的,因为它是一个数学函数,但是大多数时候您都需要向软件中添加状态,这是许多问题的根源。 在软件中添加状态的最糟糕的地方是黑匣子。 好的代码使用无状态的黑匣子,并且担心其系统的当前状态 。

Object Oriented programming is an evolution of the procedural. It adds the concept of Classes. In this, your functions and variables must belong to a Class that consist of a group that operate on the same data. A Class has the control of what can be accessed by other Classes, so the programmer does not mess with variables (or state) that they should not touch. This is a huge problem in procedural programming, because all data can be accessed. When you create a Class, you are instantiating an Object, that is why it is called Object Oriented. A Class is the model or type of an Object. Classes can inherit from other Classes. There is also the concept of Interfaces, that are kind of contracts that you can add to your Classes that guarantees that those Classes have specific functions (now called methods).

面向对象的编程是过程的演变。 它增加了类的概念。 在这种情况下,您的函数和变量必须属于由对相同数据进行操作的组组成的类。 一个类可以控制其他类可以访问的内容,因此程序员不会弄乱它们不应接触的变量(或状态)。 这是过程编程中的一个大问题,因为可以访问所有数据。 创建类时,您正在实例化一个对象,这就是为什么它被称为面向对象的原因。 类是对象的模型或类型。 类可以从其他类继承。 还有接口的概念,您可以将这些合同添加到类中,以保证那些类具有特定的功能(现在称为方法)。

In a whole different approach, we have Functional programming. It is not aiming to evolve from the imperative model to tell the computer what to do. It emerges from the concept of making function like in mathematics, where you do not have a sequence of instructions, but you declare a function that evaluates and returns other functions. It is a mixture of lambda calculus, lazy evaluation, immutable data and some other ideas that is kind of tricky to explain better in a few words, so I won’t even try. Historically, functional programming was much less popular than the imperative ones, but most modern languages are adding Functional elements to it. It might not be handy to write all your software in pure Functional programming, but in some cases it thrives.

在完全不同的方法中,我们有函数式编程。 它并非旨在从命令式模型演变为告诉计算机该怎么做。 它源自像在数学中那样制作函数的概念,在这里您没有一系列的指令,但是您声明了一个求值并返回其他函数的函数。 它是lambda演算,惰性评估,不可变数据和其他一些想法的混合体,很难用几句话更好地解释,所以我什至不会尝试。 从历史上看,函数式编程并不比命令式编程流行得多,但是大多数现代语言都在其中添加了函数式元素。 用纯函数式编程编写所有软件可能并不方便,但是在某些情况下它会蓬勃发展。

In the paradigms we discussed so far, we saw that all the evolution was to control access to memory and avoid human error. Even with all the validations that Object Oriented has added to help programmers, the most common problem of software is getting into an invalid state when two or more Classes use the same resource. This problems gets more evident when we are making parallel software like using multi-thread (where you create simultaneous process that might run in different processor or threads at the same time) or distributed system (system that uses a cluster of computers to run), because if more than one process is acting in the same memory at the same time we have to deal with this conflict. This is a big problem in Computer Science and Functional programming uses the concept of immutable data. Immutable data solves access issues by avoiding reuse of the same memory location. Each function receives its parameters and returns a result that is a new entity in a new memory address, that way it will never generate conflict.

在到目前为止讨论的范例中,我们看到所有的发展都是为了控制对内存的访问并避免人为错误。 即使已经添加了所有面向对象的验证以帮助程序员,但当两个或多个类使用同一资源时,软件最常见的问题就是进入无效状态。 当我们制作并行软件(例如使用多线程(您在其中创建可能同时在不同处理器或线程中运行的并发进程)或分布式系统(使用计算机集群运行的系统))时,此问题变得更加明显,因为如果有多个进程同时在同一内存中运行,我们就必须处理这种冲突。 这是计算机科学和函数式编程中使用不可变数据概念的一个大问题。 不变数据通过避免重复使用相同的内存位置来解决访问问题 。 每个函数接收其参数并返回结果,该结果是新的内存地址中的新实体,这样就永远不会产生冲突。

High level languages can be classified as compiled or interpreted. Compiled languages use a compiler that is a software that translates the code to a specific processor architecture. Once the code is compiled, the generated application can only run on that specific type of processor. Interpreted languages run over another process called virtual machine, so the software does not have to be compiled in every architecture out there, you just need a virtual machine running. Compiled languages are by their nature faster than interpreted languages, but interpreted languages have more functionalities and they are easy to deploy to different targets. It is common in computing where we have such opposition to have a hybrid approach. They are the precompiled interpreted languages, which are popular today.

高级语言可以分类为编译或解释。 编译语言使用编译器,该编译器是将代码转换为特定处理器体系结构的软件。 编译代码后,生成的应用程序只能在该特定类型的处理器上运行。 解释后的语言在另一个称为虚拟机的进程上运行,因此不必在每个体系结构中都编译该软件,您只需要运行一个虚拟机即可。 从本质上讲,编译语言比解释语言要快,但是解释语言具有更多的功能,并且易于部署到不同的目标。 在计算中,我们通常会遇到这样的反对意见,以采用混合方法。 它们是预编译的解释型语言,在当今很流行。

The last topic about languages is strongly and weakly typed. The strongly typed languages are languages where variables assume a type and it is fixed, if you want to change the type of this variable you have to create another variable and perform a conversion. In weakly typed language, this is not necessary. The variable will be evaluated with the type that the context asks for. This can be handy in some cases and many interpreted languages are weakly typed, but it can lead to a lot of mistakes and unexpected behavior if you do not master the language. Use tools to validate your code, especially when using interpreted weakly typed languages.

关于语言的最后一个主题是强类型和弱类型的。 强类型语言是其中变量采用某种类型且已固定的语言,如果要更改此变量的类型,则必须创建另一个变量并执行转换。 在弱类型语言中,这不是必需的。 变量将使用上下文要求的类型进行评估。 在某些情况下这可能很方便,并且许多解释型语言的类型都很弱,但是如果您不掌握该语言,可能会导致很多错误和意外行为。 使用工具来验证您的代码,尤其是在使用解释性弱类型语言时 。

In the real world we use languages all the time. You can only comprehend this article because it is written in a language that you understand. Lera Boroditsky studies how language influences us. She claims that language shapes the most fundamental human experience like space, time, causality and relationship. We have a lot of other languages in your life, for example, math, traffic signs, music, body language, etc. This is no different in computer languages. The language you use shapes the way you solve problems and write quality code.

在现实世界中,我们一直在使用语言。 您只能理解本文,因为它是以您理解的语言编写的。 Lera Boroditsky研究语言如何影响我们。 她声称语言塑造了人类最基本的体验,例如空间,时间,因果关系和人际关系。 您生活中还有很多其他语言,例如数学,交通标志,音乐,肢体语言等。计算机语言也是如此。 您使用的语言塑造了解决问题和编写质量代码的方式。

演示地址

That is for today. Now that we know the basics of computer languages, in the next section we are going to talk about the SOLID principles.

那是今天。 现在我们已经了解了计算机语言的基础知识, 在下一部分中,我们将讨论SOLID原理。

翻译自: https://medium.com/@joaobsneto/what-makes-good-code-2-7-207023b6c9fe

什么样的代码是好代码