任何解决方案与最佳解决方案

再度Miren
2020-07-17 / 0 评论 / 436 阅读 / 正在检测是否收录...


by Sam Galizia

由山姆·加利齐亚(Sam Galizia)

任何解决方案与最佳解决方案 (Any Solution Versus the Best Solution)

A couple of months ago, I was searching for resources to help me improve my coding skills. It didn’t take long before I stumbled upon Free Code Camp.

几个月前,我正在寻找资源来帮助我提高编码技能。 不久,我偶然发现了Free Code Camp

This open source community hosts a website dedicated to helping people learn how to code, with a focus on web development. Students can sign up for free and learn HTML, CSS, JavaScript, data visualization, and even back end technologies like Node.js.

这个开源社区拥有一个致力于帮助人们学习编码的网站,重点是Web开发。 学生可以免费注册并学习HTML,CSS,JavaScript,数据可视化,甚至包括Node.js之类的后端技术。

The ultimate goal is to give people coding practice by building projects, and ultimately get real world experience by working with nonprofit organizations.

最终目标是通过构建项目为人们提供编码实践,并通过与非营利组织合作最终获得现实世界的经验。

When I started their program, I flew through a lot of the HTML and CSS lessons, as I already had a little bit of experience with these technologies.

当我开始他们的程序时,我经历了很多HTML和CSS课程,因为我已经对这些技术有了一点经验。

Once I got to the sections on JavaScript, though, I slowed down and took my time. Then I hit the Basic Algorithm Scripting section.

但是,一旦到达JavaScript的各个部分,我就会放慢脚步并花些时间。 然后点击“基本算法脚本”部分。

Now, I’ve been programming for a number of years, using a couple different languages, and I honestly thought that this was going to be a breeze.

现在,我已经使用多种不同的语言进行了多年编程,老实说,我认为这将是一件轻而易举的事情。

I found out the hard way that these challenges are a little tougher than they appear on the surface. They not only tax your brain for the solutions, but also encourage you to use different features of the JavaScript language.

我艰难地发现,这些挑战要比表面上的挑战要难一些。 它们不仅使您费解解决方案,而且还鼓励您使用JavaScript语言的不同功能。

I spent some time working through a couple of the basic challenges, but inevitably, I got stuck.

我花了一些时间解决一些基本挑战,但是不可避免地,我陷入了困境。

黑暗中的光 (A Light in the Dark)

When we get stuck on problems, usually our first thought is to look up some resources and try and figure out what we are doing wrong.

当我们陷入问题中时,通常我们的首要想法是查找一些资源,并尝试找出我们在做错什么。

Luckily, Free Code Camp has provided an amazing resource to help you out when you get stuck, like the Free Code Camp Wiki. I like to refer to this resource as a “light in the dark” because, just when you feel like giving up and throwing in the towel, this bright torch suddenly lights up the darkness around you!

幸运的是,像Free Code Camp Wiki一样,Free Code Camp提供了一个了不起的资源来帮助您解决问题。 我喜欢将此资源称为“黑暗中的光”,因为当您想要放弃并扔进毛巾时,这盏明亮的手电筒突然照亮了您周围的黑暗!

The Free Code Camp wiki is an invaluable resource as you work your way through the front end project challenges. Each wiki article contains an explanation of the challenge, and even hints and several possible solutions.

当您处理前端项目挑战时,免费代码营Wiki是宝贵的资源。 每个Wiki文章都包含对挑战的解释,甚至包括提示和几种可能的解决方案。

Many times when I found myself stuck, I would visit the wiki and just read a hint or two to make sure that I was on the right track. I never really looked at the solutions until after I had solved the problems. I wanted to conquer the challenges on my own!

很多时候,当我发现自己陷入困境时,我会访问Wiki并阅读一两个提示,以确保自己处在正确的轨道上。 在解决问题之前,我从未真正研究过解决方案。 我想自己克服挑战!

Along this path, I also discovered Free Code Camp’s large network of chat rooms where you can hang out with other campers (Free Code Camp students) and discuss what’s going wrong with your code.

沿着这条路,我还发现了Free Code Camp的大型聊天室网络,您可以在其中与其他露营者(Free Code Camp的学生)闲逛,并讨论您的代码出了什么问题。

Among the many people I encountered in the rooms, one person turned out to be an invaluable resource to me. Justin Richardsson helped me with one of the challenges I was struggling on and, unbeknownst to him, started me down the path to realizing the missing step in my learning process.

我在房间里遇到的许多人中,有一个人原来对我来说是宝贵的资源。 贾斯汀·理查兹森(Justin Richardsson)帮助我解决了我所面临的挑战之一,而让他不为人所知的是,这使我开始了意识到学习过程中缺少的步骤。

小心台阶! 缺了点什么! (Watch Your Step! Something is Missing!)

Justin was explaining an error in my code, and he started talking about a better way to tackle the problem. He walked me through the process by asking me questions, guiding me toward discovering the steps for myself as we went along.

贾斯汀(Justin)解释了我的代码中的错误,他开始谈论解决该问题的更好方法。 他通过问我问题来引导我完成整个过程,并指导我逐步了解自己的工作步骤。

Eventually, I understood what he was talking about, and I had a realization. I had been solving the challenges and coming up with a solution, but was my solution the best solution?

最终,我了解了他在说什么,并且我意识到了。 我一直在解决挑战并提出解决方案,但是我的解决方案是最好的解决方案吗?

It turned out that I was not taking full advantage of best practices and core concepts. This discovery lead me to believe that my solutions were in fact not the best way to solve the challenges, and that somewhere along the way I had missed a step.

原来,我没有充分利用最佳实践和核心概念。 这一发现使我相信,我的解决方案实际上并不是解决挑战的最佳方法,而且我一路走错了一步。

The idea of this missing step boils down to doing one thing:

缺少这一步的想法归结为要做一件事:

Once you have found a working solution to the problem, you need to reevaluate your code and determine whether it can be improved.
找到问题的可行解决方案后,您需要重新评估代码并确定是否可以对其进行改进。

The improvement could be as simple as using fewer temporary variables, or fewer loops. JavaScript has many useful features that — when truly understood — can be powerful tools for more efficient problem solving. Let me give you an example of what I mean.

改进可以像使用更少的临时变量或更少的循环一样简单。 JavaScript具有许多有用的功能-真正地理解后,它们可以成为有效解决问题的强大工具。 让我举一个例子说明我的意思。

One challenge involves a function taking in a string, and you must reverse the string and return it.

一个难题涉及到一个函数,它接收一个字符串,并且您必须反转该字符串并返回它。

This first solution shows how many beginners attempt to solve this challenge. The first solution is not wrong, and it is okay to do this while you figure out the flow of what needs to happen. Let’s take a look at the first solution now:

第一个解决方案显示了有多少初学者尝试解决此挑战。 第一个解决方案是没有错的,并且可以在确定需要发生的情况时进行此操作。 现在让我们看一下第一个解决方案:

function reverseString(str) {  var splitString = str.split('');  var reversedString = splitString.reverse();  var finalString = reversedString.join('');   return finalString;}

Now looking at the above code, there is nothing inherently wrong with it. This solution to the challenge is technically valid, as it passes the required checks, but at this point we need to evaluate whether or not this is really the best way to solve this.

现在看上面的代码,它本质上没有错。 这种挑战的解决方案在技术上是有效的,因为它通过了必需的检查,但是在这一点上,我们需要评估这是否真的是解决此问题的最佳方法。

The first way I notice I can improve my code is by removing all of the unnecessary variables.

我注意到可以改善代码的第一种方法是删除所有不必要的变量。

One thing that I feel many students forget while learning JavaScript is that you don’t need to create a new variable for every modification to an existing variable.

我感到许多学生在学习JavaScript时会忘记的一件事是,您无需为现有变量的每次修改都创建一个新变量。

When you write an expression, the right side of the expressions is calculated before it executes. This is an important concept to understand, because it means that we can do this instead:

当您编写表达式时,表达式的右侧会在执行之前计算出来。 这是一个重要的概念,因为它意味着我们可以改为:

function reverseString(str) {  str = str.split('');  str = str.reverse();  str = str.join('');  return str;}

Look at that! By leveraging the built in language features, we have already improved the readability of our code and removed all the unnecessary variables.

看那个! 通过利用内置的语言功能,我们已经提高了代码的可读性,并删除了所有不必要的变量。

There is still more that we can do to improve this function though. We have utilized expressions and assignment, but lets take this one step further. I am going to make one more change to this function:

但是,我们还有许多工作可以改进此功能。 我们已经利用了表达式和赋值,但让我们更进一步。 我将对该功能进行其他更改:

function reverseString(str) {  return str.split('').reverse().join('');}

In this third solution, we have removed the multiple assignments to str and have used a very important feature,function chaining.

在第三个解决方案中,我们删除了对str的多个分配,并使用了一个非常重要的功能, 函数链接

It seems that many students while learning, forget some of these important features that honestly make our code so much more readable, and produce more correct solutions.

似乎许多学生在学习时忘记了其中的一些重要功能,这些功能实际上使我们的代码更具可读性,并且产生了更正确的解决方案。

All three of the above solutions are technically correct, but most developers would probably consider the third solution to be the best for this problem. This is because we aren’t using any more resources than are necessary. Therefore the function can be run quickly, and not waste CPU cycles or other resources. Granted, when we are talking about it being faster to execute, we are talking relative to other functions being processed.

以上三种解决方案在技术上都是正确的,但是大多数开发人员可能会认为第三种解决方案是解决此问题的最佳方案。 这是因为我们没有使用超出必要数量的资源。 因此,该功能可以快速运行,而不会浪费CPU周期或其他资源。 当然,当我们谈论它的执行速度更快时,我们是在谈论其他正在处理的功能。

Now I can already hear the critics saying that a best solution is subjective, and that other people might think another solution is the best. I am not claiming that there is only one way to solve these challenges.

现在,我已经听到批评者说最佳解决方案是主观的,其他人可能会认为另一种解决方案是最佳的。 我并不是在说只有一种方法可以解决这些挑战。

Clearly, there are many different ways to solve the challenges, but once we’ve come up with a solution, we need to make sure we are doing it efficiently. The difference between the first solution and the third solution is very clear. In terms of using this particular strategy to solve the challenge, the third solution is clearly better than the first.

显然,有许多种解决挑战的方法,但是一旦我们提出了解决方案,就需要确保我们高效地进行了解决。 第一种解决方案与第三种解决方案之间的区别非常明显。 就使用这种特定策略来解决挑战而言,第三个解决方案明显优于第一个解决方案。

什么是我们最好的解决方案? (What is Our Best Solution?)

Justin recommended I help by contributing my new-found knowledge to the Free Code Camp wiki. I was very excited to help contribute, and that very night I worked on my first contribution.

贾斯汀建议我通过将我的新发现知识贡献给Free Code Camp Wiki来提供帮助。 我很高兴能提供帮助,那天晚上我做了我的第一份贡献。

I had realized that some of the articles in the wiki showed how to solve a challenge, but not necessarily the best way(s) to solve them. I made it my goal to try and contribute the best I could, so that I could help teach others this missing step when they looked for help. Somewhere along the line of our teaching process, we forgot that it is not just about solving a problem, but also teaching the best practices along the way.

我已经意识到Wiki中的一些文章展示了如何解决挑战,但不一定是解决挑战的最佳方法。 我的目标是尽力而为,尽我所能,这样当别人寻求帮助时,我可以帮助他们教他们这失踪的步骤。 在教学过程中的某个地方,我们忘记了这不仅是解决问题,而且还沿途教授最佳实践。

I sincerely feel that when we help people with challenges on FreeCodeCamp, we should not only focus on teaching the basic concepts. When people come to those of us with more experience, we should help guide them toward adopting best practices.

我真诚地感到,当我们在FreeCodeCamp上帮助有挑战的人们时,我们不仅应该专注于教授基本概念。 当人们有更多经验来到我们中间时,我们应该帮助引导他们采用最佳实践。

It’s important to teach concepts and not just code. By helping people to understand concepts, many times they will figure out on their own that there is a better way to solve a problem.

教授概念而不只是代码很重要。 通过帮助人们理解概念,很多时候他们会自己想出一种解决问题的更好方法。

One such experience I had involved a gentleman looking for help with a challenge. After examining his code, I could tell he knew what he wanted to do, but he was struggling to get there effectively.

我曾经遇到过这样的经历,一位绅士在挑战中寻求帮助。 在检查了他的代码之后,我可以告诉他知道他想做什么,但是他正在努力有效地到达那里。

Typically, the more code we have in a function, the more complicated it gets. Sometimes we end up making things way too complicated, like this camper had.

通常,函数中包含的代码越多,它变得越复杂。 有时我们最终使事情变得过于复杂,就像这个露营者那样。

I proceeded to offer some help in the form of two hints. I explained two concepts to him, that he most likely forgot about, using very small snippets of code that weren’t relevant to the actual solution.

我开始以两个提示的形式提供一些帮助。 我用很少的与实际解决方案无关的代码片段向他解释了他最有可能忘记的两个概念。

Upon reading the second hint I gave him, he excitedly asked me to hold on a minute as he thought he knew what to do. After a minute or two, he ecstatically posted that he had solved the challenge and posted his code!

读完我给他的第二条提示后,他兴奋地要求我稍等一下,因为他认为自己知道该怎么做。 一两分钟后,他欣喜若狂地张贴自己已经解决了挑战并张贴了代码!

This camper was so excited that he had finally figured it out, and I felt great knowing I had helped him solidify those core concepts. He then proceeded to tell me he wanted to go back and rework his other solutions before moving on, because he now knew a better way to solve them.

这个露营者非常兴奋,以至于他终于想通了。我很高兴知道我已经帮助他巩固了这些核心概念。 然后,他继续告诉我,他想在继续之前重新尝试其他解决方案,因为他现在知道解决这些问题的更好方法。

That right there shows exactly why it is so important to teach concepts and best practices. I am confident that the camper will remember those concepts and hopefully pass them on to someone else in need!

那里的权利恰恰说明了为什么讲授概念和最佳实践如此重要。 我相信露营者会记住这些概念,并希望将其传递给需要帮助的其他人!

So what is our best next step? To teach people learning to code how to do it correctly and efficiently using best practices and core concepts. Only when we can confidently say that we are teaching the best ways to code, and instilling the best practices in students, can we say that we have solved the issue of the missing step in the learning process.

那么,我们最好的下一步是什么? 教人们学习如何使用最佳实践和核心概念来正确有效地编写代码。 只有当我们可以自信地说我们正在教最好的编码方法,并向学生灌输最佳实践时,才可以说我们已经解决了学习过程中缺少步骤的问题。

翻译自: https://www.freecodecamp.org/news/the-difference-between-a-solution-and-the-best-solution-c5ff0cd573e3/

0

评论 (0)

取消