前进教程:伪代码
伪代码是编程中经常被忽视的一个部分。伪代码指的是思考解决问题的步骤,并用简单的语言(而不是任何正在运行的编程语言)编写它们。
你可能遇到过“盯着空白的文本编辑器文件,却不知从何下手”的问题,这个问题通常可以通过编写伪代码并认真思考手头的问题来解决。如果你在编写代码时遇到瓶颈,你可能需要开始编写一些伪代码。
首先,为了编写伪代码,我们需要开始思考问题的各个部分。由于计算机非常注重文字,我们需要考虑每一个微小的细节并将其纳入其中——如果我们忽略了一些细节,那么以后可能会出现问题。
首先,让我们想想带我的狗出去的步骤。
- I have to find my shoes.
- If my dog steals said shoes, I have to retrieve them
- If it's cold or I'm going on a long walk and I'm wearing sneakers or boots, I need to find socks.
- If I need socks, I put one sock on my foot and then the other on my other foot.
- Then, I put the left shoe on my left foot and then the right shoe on the right foot.
- If they have laces, I tie each shoelace.
- I find Blair's leash, poop bags, and my keys
- they're usually on the counter, but sometimes she will move her leash to somewhere else and I'll have to go looking
- I clip on her leash. I then keep holding onto her leash
- I open the door to my apartment, then close it, then lock it.
- We walk to the elevator, press the down button and then wait.
- When the elevator door opens, I walk in and also make sure Blair follows me in. If she doesn't I need to pull her in or pick her up and carry her
- We hit the button to go down to the lobby of our building, wait for the elevator to get to the lobby, and then exit the elevator and the building
Etc.
我可以继续说下去,但我想你现在明白了——这里有几点需要注意:我非常具体。我会写下所有可能的步骤。我还会写下可能出错的地方——可能发生的极端情况有哪些?以及我该如何解决这些问题?
但是,第二步我可以重构它,使它看起来更像真正的代码!
If it's cold:
wear boots
If I'm going for a long walk:
wear sneakers
If my shoes are not at the door:
find them
else if my dog steals my shoes:
chase her down and take them back
If I'm wearing sneakers or boots:
get socks out of the drawer
put one sock on my foot and then the other on my other foot
put on left shoe
put on right shoe
if the shoes have laces:
tie the left shoe
tie the right shoe
If Blair's leash, poop bags, and my keys are on the counter:
grab them
else:
find them in my apartment
Clip on Blair's leash
Hold on to her leash
Open the door to my apartment
Walk through it with Blair
Lock the door
Walk to the elevator
Press the down button
Wait for the elevator until it arrives
When the elevator arrives, get into it
Make sure Blair gets on too.
Press the button for the lobby
Wait for the elevator to get to the lobby
Exit the elevator
Exit the building
这还不算代码,以后我们还需要弄清楚函数和结构,但我们已经有一个起点了。我们不再需要面对一个空白的文本编辑器,而且其中很多内容可以很容易地转换成实际代码——比如条件语句。
对于代码挑战
当然,遛狗可不是什么常见的程序。我们来聊聊在真正的代码挑战中,比如反转字符串,会是什么样子。
我可能要做的第一件事就是尝试在纸上解决这个问题几次,看看我如何在纸上解决这个问题——出于这个原因,我甚至在办公桌旁放了一块白板!
所以,在这种情况下,我会注意到我取出单词的最后一个字母,并将其添加到反转的字符串中。然后是倒数第二个字母,依此类推,直到没有其他字母可以添加到反转的字符串中。但是,还有另一种方法:我可以从字符串中取出第一个字母,并将其添加到新字符串的末尾,实际上做同样的事情。所以,对于“hello, world”,进程看起来应该是这样的:
h
eh
leh
lleh
olleh
olleh
w olleh
ow olleh
row olleh
lrow olleh
dlrow olleh
现在我已经在纸上解决了这个问题,我需要编写它的伪代码。如果我在代码中重复执行相同的操作,我知道我需要使用循环——在这种情况下,我肯定会对多个字母执行相同的操作。
因此,伪代码可能看起来像这样
reversed_string = ''
for letter in my_string
reversed_string = letter + reversed_string
如果您需要更多中间步骤,我们可以写出如下内容:
reversed_string = my_string[0] + reversed_string
reversed_string = my_string[1] + reversed_string
reversed_string = my_string[2] + reversed_string
reversed_string = my_string[3] + reversed_string
这时,你可能会注意到其中的模式,并能从中看出循环!你总是可以从更多的过程式代码入手,然后在此基础上添加抽象和逻辑,让代码变得 DRY,并使其越来越接近最终形式。
应用程序的伪代码
所以,在现实世界中,你很可能要创建完整的应用程序或为应用程序添加功能,而不是整天解决代码难题。在这种情况下,在概述了预期的功能之后(就像我们在上一篇文章中讨论的那样),你可以一次选择一个功能并为其编写伪代码。
例如:
User clicks on a button:
A modal pops up with a form that has the fields name, address, and phone number
When the user submits the form:
an AJAX request is sent to the server with the values from the form
the modal closes
if the form submission was successful:
show an alert at the top of the page that says a new value was added
else:
show an alert with the errors
一次只做一个功能,而不是整个应用程序,这样如果一个规范发生变化,你的工作就不会白费!
我向你发起的挑战
选择一个代码挑战并为其编写伪代码。完成后,继续上次的项目,为其中的一个功能编写伪代码。
结论
伪代码是开始思考问题的一个非常有用的工具。每个人对伪代码的理解都不一样,有些人觉得它像编程语言,而有些人觉得它更像非编程语言。它甚至可能看起来完全不一样!我有时会在开始深入代码之前先写一下函数名,以便给自己写个提纲!找到适合自己的方法吧。
如果您正在查看空白的文本编辑器屏幕而不知道从哪里开始,那么伪代码是一个很好的起点,或者甚至可以退一步在纸上画出该过程,详细观察您的步骤。
文章来源:https://dev.to/aspittel/moving-past-tutorials-pseudocode-13a6