小甲鱼数据结构与算法代码_将数据结构与算法结合在一起以获取PHP中的干净代码...

小甲鱼数据结构与算法代码

Before I start, sorry about my English, I’m striving to improve it ;)

开始之前,对我的英语感到抱歉,我正在努力提高英语水平;)

I that understand that data structures and algorithms are more complex and deeper concepts than I will address in this article. But even a complex concept can be applied to simple everyday things, as I intend to exemplify below…

我了解到,数据结构和算法比我将在本文中讨论的更为复杂和深入。 但是,即使是一个复杂的概念也可以应用于简单的日常事物,正如我打算在下面举例说明的那样……

I have been working with web development for almost 10 years and whenever I go through a legacy code I almost always identify the same pattern: many unnecessary conditionals.

我从事Web开发已经有将近10年的时间了,每当经过一个遗留代码时,我几乎总是会发现相同的模式:许多不必要的条件。

Whenever I refactor the codes, I can reduce many lines, sometimes hundreds of lines in the same class. Obviously, a large class is a sign that it has been badly done.

每当我重构代码时,我就可以减少很多行,有时可以减少同一类中的数百行。 显然,一个大类表明它做得不好。

In simple, in many cases, the conditionals in the code check the return key/value of an API, a function or a query in the database. Then after doing conditional validation, the programmer adds a value to a variable to display to the user on the screen or do something with it.

在许多情况下,在简单的情况下,代码中的条件检查数据库中API,函数或查询的返回键/值。 然后,在进行条件验证后,程序员将一个值添加到变量以在屏幕上向用户显示或使用它来执行某些操作。

The problem is that the programmer almost always uses too many if, else or a switch case, making the code large and difficult to maintain.

问题在于,程序员几乎总是使用太多(如果不这样做)或切换用例,从而使代码变大且难以维护。

Ok, as Linus would say, let’s go to the code.

好的,正如Linus所说的,让我们看一下代码。

Say you consult a payments API and it returns you some statuses that you need to translate.

假设您咨询了付款API,它会返回一些您需要转换的状态。

We usually see something like this:

我们通常会看到以下内容:

Now combining a array and the ternary operator:

现在结合一个数组和三元运算符:

Let’s go to another example using a Laravel handler to call custom error pages.

让我们转到另一个使用Laravel处理程序调用自定义错误页面的示例。

Approach commonly used:

常用方法:

Now, a logic + data structure simple approach:

现在,采用逻辑+数据结构的简单方法:

Of course, the second approach can be improved with strict type declaration and typing in the methods, but to exemplify I only refactored the class.

当然,可以通过使用严格的类型声明和方法键入来改进第二种方法,但是举例来说,我仅重构了类。

In both examples, I used an array and only a conditional. In the array you can pass the key value or not, It depends on the context.

在两个示例中,我都使用了一个数组,并且仅使用了条件数组。 在数组中,您可以传递或不传递键值,这取决于上下文。

In addition to making the code cleaner, it is also optimized, even if little, since the PHP interpreter will have less work parsing your code.

除了使代码更整洁外,它也进行了优化(即使很小),因为PHP解释器将减少解析代码的工作。

In some cases you will need an observer pattern, but that is a subject for another article.

在某些情况下,您将需要一个观察者模式,但这是另一篇文章的主题。

Finally, I mentioned Clean Code in this article. I also know it’s a deeper concept than just reducing lines of code, but this approach has two factors that Clean Code considers: simplicity and cohesion!

最后,我在本文中提到了“清理代码”。 我还知道,这不仅仅是减少代码行数,而是更深入的概念,但是这种方法具有Clean Code考虑的两个因素:简单性和内聚性!

翻译自: https://medium.com/@diegospm/combining-data-structure-with-algorithm-for-clean-code-in-php-684c2f44223d

小甲鱼数据结构与算法代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值