vsc 搜索特定代码_特定问题的通用解决方案:何时编写代码以及何时编写代码...

vsc 搜索特定代码

by Rina Artstain

通过丽娜·阿斯特斯坦

特定问题的通用解决方案:何时编写代码以及何时编写代码 (Generic solutions to specific problems: when to write some code and when to just do it)

There is a traditional story that tells of a rabbi who comes upon a guy sitting next to a fork in the road. The rabbi asks the guy which way is best to get to the city, and the guy answers: That one is a short road which is long, and the other is a long road which is short. The rabbi chooses the short road, but soon arrives at a field full of thorns which he can’t get through, and must turn back and take the other road, which actually leads him to the city.

有一个传统的故事,讲述一个拉比碰到一个坐在路边叉子旁边的家伙。 拉比问那家伙哪条路最好去城市,那家伙回答:那一条是一条很长的短路,另一条是一条很短的长路。 拉比选择了一条短路,但很快到达一个布满荆棘的田野,他无法穿越,必须回头走另一条路,这实际上将他引向了城市。

The moral of the story, other than wondering if this guy was the first troll in history, is that sometimes trying to take a short cut will end up being longer than just doing it the long way.

除了想知道这个家伙是否是历史上第一个巨魔之外,这个故事的寓意在于,有时尝试捷径最终会比长途跋涉更长。

编码很长的路(很短) (Coding the Long Way (which is Short))

If you’re specifically writing an API or a library for internal or external use, you know that it should be as generic as possible. You want it to be clear, concise and easy to use, while still giving your users a lot of flexibility. There are trade-offs between those goals, but you’ll figure them out.

如果您专门编写供内部或外部使用的API或库,那么您应该知道它应该尽可能通用。 您希望它清晰,简洁且易于使用,同时仍为您的用户提供很大的灵活性。 在这些目标之间需要权衡取舍,但是您会找到答案。

But what happens when you’re given a specific task to complete, and you can either just do it, or write some infrastructure which will make just doing it easier in the future?

但是,当您被赋予要完成的特定任务,而您既可以做到这一点,又可以编写一些基础架构来简化将来的工作时,会发生什么?

Should you get the task done as quickly as possible or spend the time to build some infrastructure which will make future tasks easier to complete?

您应该尽快完成任务还是花时间建立一些基础设施,使将来的任务更容易完成?

Should you choose the short road which might turn out to be long, or the long road which will actually help you hit your target sooner?

您应该选择一条可能会很长的短路,还是选择一条实际上会帮助您更快地达到目标的长路?

一个例子(基于真实的故事) (An example (based on a true story))

(Some of the details have been changed or omitted for simplicity, and, well, better story telling. Reality is much too messy.)

(为简单起见,其中一些细节已更改或省略,并且更好的讲故事。现实实在太混乱了。)

One day, my boss comes up to me and asks me to quickly get some data from the DB and send it to him as an excel sheet. It wasn’t too complicated, so I did it right away.

有一天,我的老板来找我,要求我快速从数据库中获取一些数据,并将其作为excel表发送给他。 它并不太复杂,所以我马上就做到了。

The next day he asks for the same data again, with different dates. He thinks for a bit and says, well — maybe you should just put this report up on the web. I’d like to be able to access this data any time and choose the dates myself.

第二天,他再次要求提供相同的数据,但日期不同。 他思考了一会儿,然后说,很好–也许您应该将这份报告发布到网络上。 我希望能够随时访问此数据并自己选择日期。

No problem! Here you go, some front-end, some back-end, deploy. Wait. Wait. Wait. Deployment done, and the report is online.

没问题! 在这里,您可以进行一些前端,某些后端的部署。 等待。 等待。 等待。 部署完成,报告处于联机状态。

A couple of days go by, and I’m asked for another quick report, and “do it with that nice interface and date filters you gave us last time”. OK, sure. No problem. Front-end, back-end, deploy, wait, online.

过了几天,我被要求提供另一份快速报告,并“使用您上次给我们的漂亮界面和日期过滤器进行操作”。 是当然。 没问题。 前端,后端,部署,等待,在线。

Next time I got a similar request I said: “Look, it will take me half a day to write this report, but if you give me two days, I can build some infrastructure which will allow me to define a query in the DB, add some configuration, and your report will be live without having to wait for deployment”. My request was approved.

下次收到类似的请求时,我说:“看,我将花半天时间编写这份报告,但是如果您给我两天的时间,我可以构建一些基础结构,使我可以在数据库中定义查询,添加一些配置,您的报告将立即生效,而无需等待部署。” 我的请求被批准。

这是我过去两天的工作: (Here’s what I did with my two days:)

I defined a convention for a “report” stored procedure, which expected to get the following parameters:

我为“报告”存储过程定义了一个约定,该约定应获取以下参数:

  • From Date

    从日期
  • To Date

    至今
  • Start At Index (for paging)

    从索引开始(用于分页)

Each procedure returned a result set for the query, and the Total Result Count (for paging).

每个过程都返回查询的结果集和总结果计数(用于分页)。

In addition, I added a Reports table which held:

另外,我添加了一个Reports表,其中包含:

  • Stored Procedure Name (to execute)

    存储过程名称(执行)
  • Title (of the report)

    (报告的)标题
  • Description (to be displayed on the report page)

    说明(将显示在报告页面上)

I also added an endpoint on the server, UI, and some logic to:

我还在服务器,UI和一些逻辑上添加了一个端点:

  • Check the DB for reports and add them to the site navigation.

    检查数据库中的报告,并将其添加到站点导航中。
  • When a report was accessed, execute the matching stored procedure and return the result to the UI.

    访问报告后,执行匹配的存储过程并将结果返回到UI。
  • Display two date pickers for selecting From Date and End Date and executing the query.

    显示两个日期选择器,以选择“从日期”和“结束日期”并执行查询。
  • A generic table to display the results formatted according to the data type of each column. (I already had a generic UI component for paging, yay me!)

    通用表,用于显示根据每一列的数据类型格式化的结果。 (我已经有一个通用的UI组件用于分页,是的!)

When I was done, I could add a stored procedure and an entry in the DB and the report would automagically appear on our site.

完成后,我可以在数据库中添加一个存储过程和一个条目,该报告将自动显示在我们的站点上。

唯一不变的是变化 (The Only Constant is Change)

Everyone was happy and super excited about my time saving report feature, but… What about sorting? And could we have the first column link to the customer’s profile? And could we maybe add filtering by the sales rep responsible for that customer?

每个人都对我的省时报告功能感到高兴和兴奋,但是……排序又如何呢? 我们能否将第一列链接到客户的个人资料? 我们是否可以添加负责该客户的销售代表进行过滤的功能?

Yeah! Sure. That makes a lot of sense. So I added:

是的 当然。 这很有意义。 所以我补充说:

  • Optional Sort By and Sort Direction parameters to the Stored Procedures.

    存储过程的可选“排序依据”和“排序方向”参数。
  • A flag for specifying if the first column should link to the customer’s profile.

    用于指定第一列是否应链接到客户的个人资料的标志。
  • Another flag for specifying if the query included filtering by sales rep.

    用于指定查询是否包括按销售代表过滤的另一个标志。
  • And a few UI/logic changes to support displaying/executing these new requirements.

    以及一些UI /逻辑更改,以支持显示/执行这些新要求。

Happy happy, joy joy. But… How about grouping the results? And when you click the row could you have an email sent to a group of predefined users? And when a sales rep enters the report, could you have the report displayed in their favorite color? (OK, I made that last one up)

快乐快乐,快乐快乐。 但是……如何将结果分组? 当您单击该行时,是否可以将电子邮件发送给一组预定义的用户? 当销售代表输入报告时,可以用他们喜欢的颜色显示报告吗? (好的,我把最后一个弄了)

稍安毋躁 (Hold your Horses)

At this point, I had to stop and think which features should be implemented as part of my generic solution, and which should be developed as a stand-alone solution.

在这一点上,我不得不停下来想一想,哪些功能应作为通用解决方案的一部分实施,哪些功能应作为独立的解决方案开发。

It was obvious to me that specific custom actions which don’t depend on the actual data returned from the query (like sending an email to certain predefined users) was out of the question.

对我来说很明显,不依赖于查询返回的实际数据的特定自定义操作(例如向某些预定义的用户发送电子邮件)是不可能的。

I also thought that uncommon actions which depend on the data returned by the query shouldn’t be added to the generic report infrastructure. So, filter by sales rep — sure, why not. Link to customer — OK, common scenario. But other actions which are very specific to a single report — nope.

我还认为,不应将依赖于查询返回的数据的不常见操作添加到通用报表基础结构中。 因此,按销售代表过滤-当然,为什么不呢? 链接到客户-好的,常见的情况。 但是其他针对单个报告的特定操作-不。

The grouping requirement posed more of a challenge: Grouping is a common and extremely useful scenario. Should I add it to my infrastructure or not?

分组要求带来了更大的挑战:分组是一种常见且极为有用的方案。 是否应该将其添加到我的基础架构中?

Well, what would grouping the results require?

那么,将结果分组需要什么?

  • Adding a Group By flag to the report’s DB entry, to let the logic know to expect two distinct result sets — group header and group details.

    在报表的DB条目中添加一个Group By标志,以使逻辑知道期望两个不同的结果集-组标题和组详细信息。
  • The logic would also have to know how to match the group to the details, and it would have to be done by convention. Extremely risky business.

    逻辑还必须知道如何使组与细节匹配,并且必须按照惯例完成。 极具风险的业务。
  • Figuring out how to generically sort results with a group header. I can’t even.

    弄清楚如何使用组头对结果进行一般排序。 我什至不能
  • And probably some other issues I haven’t thought of.

    可能还有其他我没想到的问题。

代码未写 (The Code Not Written)

I didn’t add grouping.

我没有添加分组。

In retrospect, putting about a week of effort total into this reports feature saved me hundreds of hours developing reports later on. I also managed to avoid the pitfall of trying to be too generic and supporting too much extraneous stuff, which would have hurt the system’s stability in the long run.

回顾一下,在此报告功能中投入了大约一周的时间,使我以后节省了数百小时的报告开发时间。 我还设法避免了过于通用和支持过多无关内容的陷阱这从长远来看会损害系统的稳定性。

But really, I shouldn’t have written a single line of code. I should have looked into a reporting tool.

但实际上,我不应该编写任何代码。 我应该研究一下报告工具。

However, there are other generic helper systems which I wrote which were a good investment and should have been written. For example, a library of helpers which create HTML elements with predictable ids and classes tailored to our data models and UI requirements.

但是,我编写了其他通用帮助程序系统,这是不错的投资, 应该编写。 例如,一个帮助程序库可创建HTML元素,这些HTML元素具有针对我们的数据模型和UI要求量身定制的可预测ID和类。

怎么办? (Now What?)

So how should you decide whether to write some infrastructure or just do it? Here’s how I decide:

那么,您应该如何决定是编写某些基础架构还是仅仅是编写基础架构? 这是我的决定方式:

  • Is it a rote task? If it’s boring and recurring, if you find yourself copy-pasting a whole lot of code often — check if you can generalize what you’re doing and build some code to do it for you.

    死记硬背吗? 如果这很无聊且经常发生,如果您发现自己经常复制粘贴很多代码,请检查您是否可以概括您正在做的事情并构建一些代码来帮助您。
  • How long will building the infrastructure take? If you can build the infrastructure in 5 minutes and each task would have taken you 5 days, it’s an easy answer. If building the infrastructure would take a year and each task would take 5 minutes — don’t build it. But it’s usually not that clear cut. Try to estimate as best you can, try to keep it simple, and be prepared to cut your losses if it takes too long.

    基础设施建设需要多长时间? 如果您可以在5分钟内构建基础架构,而每个任务将花费您5天的时间,这是一个简单的答案。 如果构建基础架构需要花费一年,而每个任务都需要5分钟-那就不要构建它。 但这通常不是那么明确。 尽力估计尽可能多,尽量简化,如果准备时间太长,准备减少损失。
  • Do not add specific implementations to your generic solution. If it doesn’t fit, don’t try to force it. You’ll end up ruining your infrastructure and getting a mediocre result for the task you were actually trying to complete.

    不要将特定的实现添加到您的通用解决方案。 如果不合适,请不要强行使用。 您最终将破坏基础架构,并为您实际上要完成的任务获得平庸的结果。
  • But first, please check if there is ready made solution to your problem.

    但是首先, 检查是否有针对您问题的现成解决方案。

Like what you read? Share the love by clapping. Have a question or comment? Let me know in the comments section.

喜欢你读的书吗? 通过鼓掌分享爱。 有问题或意见吗? 让我知道在评论部分。

翻译自: https://www.freecodecamp.org/news/generic-solutions-to-specific-problems-2562fbd37a5a/

vsc 搜索特定代码

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/393361.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java手动编译jar包_Maven 手动添加第三方依赖包及编译打包和java命令行编译JAVA文件并使用jar命令打包...

一,实例:新建了一个Maven项目,在eclipse中通过 build path –> configure path….将依赖包添加到工程中后,eclipse不报错了。但是用Maven命令 mvn clean compile 时出错如下:原因是在eclipse中添加了 exteneral jar后,还需要在…

SQL like

确定给定的字符串是否与指定的模式匹配。模式可以包含常规字符和通配符字符。模式匹配过程中,常规字符必须与字符串中指定的字符完全匹配。然而,可使用字符串的任意片段匹配通配符。与使用 和 ! 字符串比较运算符相比,使用通配符可使 LIKE 运…

计划备份mysql数据库

1:mysql是我们使用最多的数据库,如果在日常中正确的对mysql数据进行备份,下面我们就来做这事,通过脚本来实现##########################################################################################################################…

leetcode1333. 餐厅过滤器

给你一个餐馆信息数组 restaurants,其中 restaurants[i] [idi, ratingi, veganFriendlyi, pricei, distancei]。你必须使用以下三个过滤器来过滤这些餐馆信息。 其中素食者友好过滤器 veganFriendly 的值可以为 true 或者 false,如果为 true 就意味着你…

3.27下午

转载于:https://www.cnblogs.com/bgd140201228/p/6628194.html

2019春季学期进度报告(十四)

课上花费时间:5h 课下花费时间:6h 学会的新内容:阿里云服务器的购买,websockt入门。 代码量:200h 转载于:https://www.cnblogs.com/Aduorisk/p/11056750.html

rxjs 怎么使用_使用RxJS Observables进行SUPER SAIYAN

rxjs 怎么使用I loved DragonBall Z as a kid, and still love it as an adult. 我从小就爱DragonBall Z,但从小到大仍然喜欢它。 Among the ludicrous number of transformations, the original Super Saiyan remains my favorite. 在可笑的转换数量中&#xff0c…

java编程石头剪刀布_java 开发的石头,剪刀,布的游戏 demo

[java]代码库/** 创建一个类Game,石头,剪刀,布的游戏。*/public class Game {/*** param args*/String[] s {"石头","剪刀","布"};//获取电脑出拳String getComputer(int i){String computerGuess s[i];retur…

JList的基本操作

1.初始化并添加元素 DefaultListModel leftListModelnew DefaultListModel(); String[] items Model.getPairs(); for (int i0; i<items.length; i) { leftListModel.add(i, items[i]); } JList leftLstnew JList(leftListModel); 2.删除所有元素 leftListModel.remove…

请求WebApi的几种方式

请求WebApi的几种方式目前所了解的请求WebAPI的方式有通过后台访问api 和通过js 直接访问api接口 首先介绍下通过后台访问api的方法&#xff0c;可以使用HttpClient的方式也可以使用WebRequest的方式 1、HttpClient的方式 &#xff08;1&#xff09;Get请求 string url "…

Django第三篇

前端反向解析 应用场景&#xff1a;通过访问a路由&#xff0c;到达a的对应视图函数&#xff0c;函数到达对应的前端a标签 a标签的路径是b路由&#xff0c;如果我们在后端改变b路由的路径&#xff0c;那么a标签便无法访问 到b路由&#xff0c;只能手动在前端改变a标签的路径&…

leetcode792. 匹配子序列的单词数

给定字符串 S 和单词字典 words, 求 words[i] 中是 S 的子序列的单词个数。 示例: 输入: S “abcde” words [“a”, “bb”, “acd”, “ace”] 输出: 3 解释: 有三个是 S 的子序列的单词: “a”, “acd”, “ace”。 class Solution {public int numMatchingSubseq(Strin…

react context_使用React Context API-入门

react contextLets use the React Context API to change theme in an app!让我们使用React Context API更改应用程序中的主题&#xff01; 但是首先&#xff0c;一些背景 &#xff01; &#xff1f; (But first, some context! ?) Ok terrible puns aside lets have a look …

redis本地及远程登录

redis默认只能localhost登录&#xff0c;所以需要开启远程登录。解决方法如下&#xff1a;一、在redis的配置文件redis.conf中&#xff0c;找到bind 127.0.0.11、去掉 #bind 127.0.0.1前面的#号注释&#xff0c;将bind 127.0.0.1 改成了bind 0.0.0.0&#xff08;这样不限制连接…

java初始化变量n_java中预构造函数初始化变量的属性

在Java中,可以在调用构造函数之前初始化编程变量.public class StockGraph extends JPanel {public boolean runUpdates true;double TickMarks 18;double MiddleTick TickMarks / 2;double PriceInterval 5;double StockMaximum;double StockMinimum;Random testStockValu…

烦了

桃花飞绿水 一庭芳草围新绿 有情芍药含春泪 野竹上青霄 十亩藤花落古香 无力蔷薇卧晓枝 我愿暂求造化力 减却牡丹妖艳色 花非花 梦非梦 花如梦 梦似花 梦里有花 花开如梦 心非心 镜非镜 心如镜 镜似心 镜中有心 心如明镜 ​​转载于:https://www.cnblogs.com/langdao/p/1099281…

js图片库 案例

事件处理函数&#xff1a;事件处理函数的作用是&#xff0c;在特定事件发生时调用特定的JavaScript代码。本例中想要在用户点击某个链接的时候触发一个动作&#xff0c;所以需要使用onclick事件处理函数。 添加事件处理函数的语法&#xff1a; event"JavaScript statement…

hitchhiker部署_《 Hitchhiker的React Router v4指南》:路由配置的隐藏值

hitchhiker部署Welcome to the Hitchhiker’s Guide to React Router v4, Part IV!欢迎来到《 React Router v4旅行者指南》&#xff0c;第四部分&#xff01; Now that we’ve learned about recursive routes, let’s get back to our initial boilerplate, to avoid mixing…

亮剑:PHP,我的未来不是梦(11)

2. 再说说“必备能力”说道能力&#xff0c;很多应届毕业生都愿意给自己的点评是“我的学习能力很强”或者是“具有良好的学习意识”等。这里存在着这样一个误区&#xff01;把企业当成了学校&#xff01;当成了试验室&#xff01;把老板当成了老师&#xff01;作为用人单位&am…

leetcode457. 环形数组循环

给定一个含有正整数和负整数的环形数组 nums。 如果某个索引中的数 k 为正数&#xff0c;则向前移动 k 个索引。相反&#xff0c;如果是负数 (-k)&#xff0c;则向后移动 k 个索引。因为数组是环形的&#xff0c;所以可以假设最后一个元素的下一个元素是第一个元素&#xff0c;…