javaweb解决编码问题_学习编码? 首先,学会解决问题。

javaweb解决编码问题

Most of the tutorials you have read or watched before now usually do one thing, spoon-feeding.

您以前阅读或观看过的大多数教程通常都是一勺子喂。

This is “OK” to learn a new thing, but not good when you need to solve a real task.

学习新事物是“可以的”,但是当您需要解决实际任务时则不好。

A task usually involves reaching a goal by overcoming a problem. This post unveils the most important “tip” for all beginner software engineers.

任务通常涉及通过克服问题来达成目标。 这篇文章揭示了所有初学者软件工程师最重要的“技巧”。

TL; DR (TL;DR)

First, solve the problem. Then, write the code. — John Johnson

首先,解决问题。 然后,编写代码。 约翰·约翰逊

In other words, work the solution out on paper in steps. Then start writing the code for it. Don’t get tangled in the code and software design first.

换句话说,分步解决该问题。 然后开始为其编写代码。 不要首先纠缠于代码和软件设计中。

那么,会发生什么呢? (So, what happens?)

Many times I hear beginners and junior software engineers say: “I could follow the tutorial and thought I understood the concept. But when I tried to do a similar thing on my pet project, I could not do it.”

我多次听到初学者和初级软件工程师说:“我可以按照教程学习,以为自己理解了这个概念。 但是,当我尝试在宠物项目中做类似的事情时,我做不到。”

This happens for two reasons.

发生这种情况有两个原因。

First, you lost your train of thought somewhere and could not establish a chain.

首先,您迷失了思路,无法建立连锁关系。

Second, you were so tangled in the code that the main problem you were trying to solve fell out of focus.

其次,您在代码中纠缠不清,以至于试图解决的主要问题没有引起人们的关注。

This problem also happens for Software Engineers and even Senior Software Engineers.

软件工程师甚至高级软件工程师也会发生此问题。

The good thing is that with experience, you know when to stop or take a break. Then, come back to the problem with a different prescription and find a solution faster.

好消息是,有了经验,您知道何时该停止或休息一下。 然后,以不同的处方返回问题,并更快地找到解决方案。

Many of you can relate to this: You were trying so hard to fix an issue for hours. You took a break or slept over it, and next session the solution was there in minutes.

你们中的许多人都可能与此有关:您一直在努力解决一个小时的问题。 您稍作休息或就寝,接下来的会议只需几分钟即可解决。

This is not magic. This is looking at the problem from another viewpoint.

这不是魔术。 这是从另一个角度看问题。

让我们用一个例子来说明 (Let’s illustrate with an example)

You have to do a task — for example, create a refund with payments.

您必须完成一项任务-例如,创建带有付款的退款。

You are given the database schema. This is a back-end task and you need to create a POST /refunds API that can create the refund and its related payments. The database structure is as below:

您将获得数据库架构。 这是一项后端任务,您需要创建一个POST / refunds API,该API可以创建退款及其相关付款。 数据库结构如下:

A refund always has a reason, such as ‘damaged goods’ or ‘late delivery’.

退款总是有原因的,例如“货物损坏”或“交货延迟”。

You could discuss the API payload with one of the team members. You might agree on the below JSON payload:

您可以与团队成员之一讨论API有效负载。 您可能会同意以下JSON有效负载:

通常情况 (The usual scenario)

What most beginner software engineers will do is start scanning the codebase, if there is one. They will immediately start writing some code. If there is any testing culture in place, maybe they write some automated test code.

大多数初学者软件工程师会做的是开始扫描代码库(如果有的话)。 他们将立即开始编写一些代码。 如果有任何测试环境,也许他们会编写一些自动化测试代码。

This is where most beginner and even some experienced software engineers slip. Don’t write code when you have not solved the problem.

大多数初学者,甚至是一些经验丰富的软件工程师都在这里滑倒。 未解决问题时请勿编写代码。

适当的步骤 (The appropriate step)

The most appropriate step is to sit down and solve the problem on paper in steps.

最合适的步骤是坐下来逐步解决纸上的问题。

So you ask yourself what you need to do. You come up with a plan in steps and tweak it.

所以你问自己自己需要做什么。 您逐步提出了一个计划并对其进行了调整。

If you have someone senior in your team, you can validate your steps and get feedback.

如果您的团队中有资深人士,则可以验证您的步骤并获得反馈。

This will also decrease the code review time. Both of you have already agreed on the modality of the solution.

这也将减少代码检查时间。 你们俩都已经同意解决方案的方式。

那么怎么做 (So how to do it)

The task described earlier is to write a create/POST API where Refunds with payments can be created.

前面描述的任务是编写一个create / POST API,可以在其中创建带付款的退款。

Each refund can have a maximum of two payments. One is of type ‘cash’ and the other is of type ‘credit’. It can also be one refund with only one payment of either cash or credit. This is how I would have written the following steps on paper:

每次退款最多可分两次付款。 一种是“现金”类型,另一种是“信贷”类型。 也可以是一次退款,只用现金或信用卡一次付款即可。 这就是我在纸上写以下步骤的方式:

  1. Create a method to get the data from the controller sent by the user

    创建一种方法来从用户发送的控制器中获取数据
  2. Validate all the input for values, refund types, and reasons.

    验证所有输入的值,退款类型和原因。
  3. If all validation passes, generate a random alphanumeric number of length 10 which is not existing in the refund table (recursive check)

    如果所有验证均通过,则生成一个长度为10的随机字母数字,该数字在退款表中不存在(递归检查)
  4. If validation fails to respond with a proper validation failure message, decide on response structure

    如果验证未能通过正确的验证失败消息进行响应,请确定响应结构
  5. Start a database transaction

    开始数据库事务
  6. Insert the refund related values of refund_nr, reason_reason, is_premium_customer to the refund table

    将与退款相关的值refund_nrreason_reasonis_premium_customerrefund表中

  7. On insert success, get the id of the last insert

    插入成功后,获取上一次插入的ID
  8. With the refund_id, insert payment related values of fk_refund, fk_item, amount, is_cash to the payment table

    使用refunding_id,将与付款相关的值fk_refund,fk_item,金额,is_cash插入payment

  9. If all went well commit the database transaction

    如果一切顺利,请提交数据库事务
  10. If there was any issue, rollback database transaction

    如果有任何问题,请回滚数据库事务
  11. Respond with success or failure message depending on the database transaction’s success with proper structure

    根据数据库事务的成功与否,以适当的结构响应成功或失败消息
  12. Wire up the controller and this method

    连接控制器并使用此方法

逐步执行计划,现在编写代码 (Follow the plan in steps, now write the code)

After you have a step by step plan you can start writing code. Then, you can go more in-depth on the method names, how to get the database connection and other details.

在制定分步计划之后,您可以开始编写代码。 然后,您可以更深入地了解方法名称,如何获得数据库连接以及其他详细信息。

Depending on the language and framework you can also decide where should the validation code stay.

根据语言和框架,您还可以确定验证代码应保留在哪里。

You could even write tests if the company and culture supports and encourages it.

如果公司和文化支持并鼓励,您甚至可以编写测试。

When your solution is evident in your mind and you have a step-by-step action plan on paper, you can now write code.

当您的解决方案在您的脑海中浮现时,并且在纸上有了逐步的行动计划,您现在就可以编写代码。

You can even break up the parts in ways that will be easier to finish and wire up.

您甚至可以以更容易完成和连接的方式分解零件。

For example, the testing logic can be something that can be written separately and tested on its own. It is thinking about independent parts that can be wired up together to form the solution.

例如,测试逻辑可以是可以单独编写并可以单独测试的东西。 它正在考虑可以连接在一起以形成解决方案的独立零件。

结论 (Conclusion)

When you face your next task, don’t start writing code from the get-go.

当您面对下一个任务时,请不要从一开始就编写代码。

First, get a hold of the problem then devise a solution with steps. This is best done away from a screen on paper.

首先,抓住问题,然后逐步设计解决方案。 最好不要放在纸上屏幕上。

Then, refine your solution and discuss with someone. When you are satisfied, translate that solution to code. This is a bit methodical but very effective.

然后,完善您的解决方案并与某人讨论。 当您满意时,可以将该解决方案转换为代码。 这有点有条理,但非常有效。

Code is always a medium to the solution, not the solution itself.
代码始终是解决方案的媒介,而不是解决方案本身。

You can read more of my blog posts at geshan.com.np.

您可以geshan.com.np上阅读更多我的博客文章

翻译自: https://www.freecodecamp.org/news/learning-to-code-first-learn-to-solve-the-problem-128475b91301/

javaweb解决编码问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值