如何防止解决方案或设计过于复杂?

发布于 2024-07-04 09:35:00 字数 1448 浏览 10 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

小霸王臭丫头 2024-07-11 09:35:00

花时间好好命名系统的概念,并找到相关的名称,这会让系统更加熟悉。 不要犹豫重新命名概念,与你所知道的世界联系得越好,你的大脑就能更好地处理它。

向那些喜欢干净、简单的解决方案的人征求意见。

仅实现当前项目所需的概念(对未来验证或通用系统的渴望会使您的设计变得臃肿)。

Take time to name the concepts of the system well, and find names that are related, this makes the system more familiar. Don't be hesitant to rename concepts, the better the connection to the world you know, the better your brain can work with it.

Ask for opinions from people who get their kicks from clean, simple solutions.

Only implement concepts needed by the current project (a desire for future proofing or generic systems make your design bloated).

丶情人眼里出诗心の 2024-07-11 09:35:00

我问我的客户为什么他们需要某些功能。 我尝试深入了解他们的请求并找出他们遇到的问题。 这通常会提供比我(或他们)想象的更简单的解决方案。

当然,如果您了解客户的工作习惯以及他们必须解决的问题,您从一开始就可以更好地理解他们的问题。 如果你“了解他们”,那么你就能更好地理解他们的讲话。 因此,与您的用户建立密切的工作关系。 这是工程的零步。

I ask my customers why they need some feature. I try and get to the bottom of their request and identify the problem they are experiencing. This often lends itself to a simpler solution than I (or they) would think of.

Of course, if you know your clients' work habits and what problems they have to tackle, you can understand their problems much better from the get-go. And if you "know them" know them, then you understand their speech better. So, develop a close working relationship with your users. It's step zero of engineering.

墨小墨 2024-07-11 09:35:00

这是一个微妙的平衡行为:一方面你不想要一些需要太长时间来设计和实现的东西,另一方面你不想要一个不够复杂的黑客来处理下周的问题,或者更糟糕的是需要重写才能适应。

我发现一些有用的技巧:

如果某件事看起来比你想要的更复杂,那么在你思考完它之后,不要立即坐下来实施它。 在这一天剩下的时间里找点别的事情做。 很多次我最终都会为问题的早期部分考虑不同的解决方案,从而消除后来的许多复杂性。

同样,找一个可以与你交流想法的人。 确保您可以向他们解释为什么复杂性是合理的!

如果您增加复杂性是因为您认为将来是合理的,那么请尝试确定将来何时使用它。 如果您无法(现实地)想象一三年内需要这种复杂性,那么现在为此付费可能是不合理的。

This is a delicate balancing act: on the one hand you don't want something that takes too long to design and implement, on the other hand you don't want a hack that isn't complicated enough to deal with next week's problem, or even worse requires rewriting to adapt.

A couple of techniques I find helpful:

If something seems more complex than you would like then never sit down to implement it as soon as you have finished thinking about it. Find something else to do for the rest of the day. Numerous times I end up thinking of a different solution to an early part of the problem that removes a lot of the complexity later on.

In a similar vein have someone else you can bounce ideas off. Make sure you can explain to them why the complexity is justified!

If you are adding complexity because you think it will be justified in the future then try to establish when in the future you will use it. If you can't (realistically) imagine needing the complexity for a year or three then it probably isn't justifiable to pay for it now.

与风相奔跑 2024-07-11 09:35:00
  • 与其他程序员讨论每一步。 对设计的关注越多,在代码库变得过于僵化之前,越有可能尽早揭示过于复杂的方面。
  • 不断地问自己,你将如何利用你目前正在做的事情。 如果答案是您不确定,请停下来重新思考您正在做的事情。
  • 我发现记下关于如何简化我当前正在做的事情的想法很有用。 这样,一旦我真正让它工作起来,就可以更容易地根据需要返回并重构或重做,而不是搞乱那些还没有功能的东西。
  • Talk to other programmers every step of the way. The more eyes there are on the design, the more likely an overcomplicated aspect is revealed early, before it becomes too ossified in the codebase.
  • Constantly ask yourself how you will use whatever you are currently working on. If the answer is that you're not sure, stop to rethink what you're doing.
  • I've found it useful to jot down thoughts about how to potentially simplify something I'm currently working on. That way, once I actually have it working, it's easier to go back and refactor or redo as necessary instead of messing with something that's not even functional yet.
智商已欠费 2024-07-11 09:35:00

通过将任务序列化为一系列较小的任务,减少您正在处理的数据量。 大多数人在编码时只能在头脑中记住六个(加或减)条件,因此将其作为实现单元。 设计您需要完成的所有任务,然后无情地修改设计,这样您就不必在模块中使用超过六条路径。

这是从 Bendazo 的帖子中得出的——简化直到变得容易。

Reduce the amount of data you're working with by serialising the task into a series of smaller tasks. Most people can only hold half a dozen (plus or minus) conditions in their head while coding, so make that the unit of implementation. Design for all the tasks you need to accomplish, but then ruthlessly hack the design so that you never have to play with more than half a dozen paths though the module.

This follows from Bendazo's post - simplify until it becomes easy.

ぶ宁プ宁ぶ 2024-07-11 09:35:00

一旦你成为一名程序员,这种情况就会不可避免地发生。 如果您严重低估了工作量,或者遇到了解决方案不起作用的问题,那么请停止编码并与您的项目经理交谈。 我总是喜欢带着解决方案去参加会议,问题是A,你可以做x,这需要3天,或者我们可以尝试y,这将需要6天。 不要自己做选择。

It is inevitable once you have been a programmer that this will happen. If you seriously have unestimated the effort or hit a problem where your solution just doesn't work then stop coding and get talking to your project manager. I always like to take the solutions with me to the meeting, problem is A, you can do x which will take 3 days or we can try y which will take 6 days. Don't make the choice yourself.

怪我太投入 2024-07-11 09:35:00

先测试可能会有所帮助,但并不适合所有情况。 而且它无论如何也不是万能的。

从小事做起是另一个好主意。 你真的需要把所有 10 种设计模式都塞进这个东西里吗? 首先尝试用“愚蠢的方式”来做。 还不太切吗? 好吧,就用“稍微不那么愚蠢的方式”吧。 等等

对其进行审核。 正如其他人所写,两双眼睛更好。 更好的是有两个大脑。 你的伴侣可能只是看到了一个需要简化的空间,或者一个你认为很好的有问题的区域,只是因为你花了很多时间来破解它。

使用精益语言。诸如 Java 或 C++ 之类的语言有时似乎会鼓励令人讨厌、复杂的解决方案。 简单的事情往往会跨越多行代码,您只需要使用 3 个外部库和一个大框架来管理这一切。 考虑使用 Python、Ruby 等 - 如果不是为了您的项目,那么也可以用于一些私人用途。 它可以改变您的心态,偏向简单,并确保简单是可能的。

Test first may help here, but it is not suitable for all situation. And it's not a panacea anyway.

Start small is another great idea. Do you really need to stuff all 10 design patterns into this thing? Try first to do it "stupid way". Doesn't quite cut it? Okay, do it "slightly less stupid way". Etc.

Get it reviewed. As someone else wrote, two pairs of eyes are better. Even better are two brains. Your mate may just see a room for simplification, or a problematic area you thought was fine just because you spend many hours hacking it.

Use lean language. Languages such as Java, or sometimes C++ sometimes seem to encourage nasty, convoluted solutions. Simple things tend to span over multiple lines of code, and you just need to use 3 external libraries and a big framework to manage it all. Consider using Python, Ruby, etc. - if not for your project, then for some private use. It can change your mindset to favor simplicity, and to be assured that simplicity is possible.

孤独岁月 2024-07-11 09:35:00

使用测试驱动开发并遵循 Robert C. Martin 的 TDD 三规则

  1. 您不得编写任何生产代码,除非它是为了通过失败的单元测试。
  2. 不允许您编写超过足以失败的单元测试; 编译失败就是失败。
  3. 您编写的生产代码不得超过足以通过一个失败的单元测试的代码。

通过这种方式,您不太可能获得太多不需要的代码。 你将始终专注于完成一件重要的事情,并且永远不会在复杂性方面超出自己太多。

Using Test Driven Development and following Robert C. Martin's Three Rules of TDD:

  1. You are not allowed to write any production code unless it is to make a failing unit test pass.
  2. You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
  3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.

In this way you are not likely to get much code that you don't need. You will always be focused on making one important thing work and won't ever get too far ahead of yourself in terms of complexity.

情未る 2024-07-11 09:35:00

阅读 Michael C. Feathers 所著的《有效处理遗留代码》。

关键是,如果您有有效的代码,并且需要更改设计,那么没有什么比使代码可进行单元测试并将代码分解为更小的部分更好的了。

Read "Working Effectively With Legacy Code" by Michael C. Feathers.

The point is, if you have code that works, and you need to change the design, nothing works better than making your code unit testable, and breaking your code into smaller pieces.

仅此而已 2024-07-11 09:35:00

我创建一个设计等,然后查看它并尝试(积极地)删除所有似乎不需要的东西。 如果我稍后在完善设计时需要它,我会将其添加回来。我会进行多次迭代,并在进行过程中不断完善。

I create a design etc., and then I look at it and try and remove (agressively) everything that doesn't seem to be needed. If it turns out I need it later when I am polishing the design I add it back in. I do this over several iterations, refining as I go along.

鸠书 2024-07-11 09:35:00

如果测试太难,那么你的设计就太复杂了。 这是我使用的第一个指标。

If its too hard to test, your design is too complicated. That's the first metric I use.

静若繁花 2024-07-11 09:35:00

这里有一些让设计变得更简单的想法:

  • 阅读一些编程书籍和文章,然后将它们应用到你的工作中并编写代码
  • 阅读大量其他人编写的代码(好的和坏的)(例如 Open源项目),并了解哪些有效,哪些无效,
  • 构建安全网(单元测试),以便对代码进行实验
  • ,如果这些实验出现错误,则使用版本控制来启用回滚
  • TDD(测试驱动开发)BDD(行为驱动开发)
  • 改变你的态度,问问你如何才能做到这一点,“它简单有效”(约定优于配置可能会有所帮助;或者询问 Apple 将如何做到这一点)这样做)
  • 练习(就像爵士乐手一样——用代码即兴创作,尝试 Code Kata
  • 多次编写相同的代码,使用不同的语言,一段时间后
  • 学习具有新概念的新语言(如果您使用静态语言,请学习动态语言; 如果你使用过程语言,就学习函数式语言; ...) [每年一种语言就差不多了]
  • 请某人审查您的代码并积极询问如何使您的代码更简单、更优雅(然后做到),
  • 让您获得数年的经验通过做以上事情(时间有助于思维活跃)

Here are some ideas to get design more simpler:

  • read some programming books and articles, and then apply them in your work and write code
  • read lots of code (good and bad) written by other people (like Open Source projects) and learn to see what works and what does not
  • build safety nets (unit tests) to enable experimentations with your code
  • use version control to enable rollback, if those experimentations take wrong turn
  • TDD (test driven development) and BDD (behaviour driven development)
  • change your attitude, ask how you can make it so, that "it simply works" (convention over configuration could help there; or ask how Apple would do it)
  • practice (like jazz players -- jam with code, try Code Kata)
  • write same code multiple times, with different languages and after some time has passed
  • learn new languages with new concepts (if you use static language, learn dynamic one; if you use procedural language, learn functional one; ...) [one language per year is about right]
  • ask someone to review you code and actively ask how you can make your code simpler and more elegant (and then make it)
  • get years under your belt by doing above things (time helps active mind)
沒落の蓅哖 2024-07-11 09:35:00

根据我的经验,针对过于笼统的案例进行设计往往会产生过多的复杂性。

工程文化鼓励设计时减少对环境的假设; 这通常是一件好事,但有些人却太过分了。 例如,如果您的汽车设计没有假设特定的引力,那么可能会很好,实际上没有人会在月球上驾驶您的汽车,如果他们这样做了,它就行不通,因为没有氧气使燃料燃烧。

困难的部分是,开发“适用于任何星球”设计的人通常被认为是聪明的,因此您可能需要更加努力地证明他的设计聪明。

了解权衡,以便您可以在好的假设和坏的假设之间做出决定,将大大有助于避免不必要的复杂设计。

In my experience, designing for an overly general case tends to breed too much complexity.

Engineering culture encourages designs that make fewer assumptions about the environment; this is usually a good thing, but some people take it too far. For example, it might be nice if your car design doesn't assume a specific gravitational pull, nobody is actually going to drive your car on the moon, and if they did, it wouldn't work, because there is no oxygen to make the fuel burn.

The difficult part is that the guy who is developed the "works-on-any-planet" design is often regarded as clever, so you may have to work harder to argue that his design is too clever.

Understanding trade-offs, so you can make the decision between good assumptions and bad assumptions, will go a long way into avoiding a needlessly complicated design.

债姬 2024-07-11 09:35:00

让新人来看看。

Getting someone new to look at it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文