为什么我应该重构我的代码?

发布于 2024-12-17 06:26:41 字数 219 浏览 0 评论 0原文

如果我编写了一段代码,可能有点简单,但我了解每一点是如何工作的,如果出现问题,我可以轻松识别并纠正问题,为什么我应该使用像 ReSharper 这样的工具将我的代码变成有什么我不明白的吗?

当然,如果你不能自己编写复杂的代码,那么使用代码重构工具只会导致问题,因为当出现问题时你将无法理解新代码,而如果你可以手动编写重构的代码,为什么你需要这个工具吗?

或者我缺少什么?

谢谢

If I've written a piece of code which is maybe a bit simplistic but I understand how every little bit works and if something goes wrong I can easily identify and correct the problem, why should I use a tool like ReSharper to turn my code into something I don't understand?

Surely if you can't write the complicated code yourself, using a code refactoring tool is just going to cause issues because you won't be able to understand the new code when something goes wrong, and if you can write the refactored code manually why do you need the tool?

Or is there something I'm missing?

Thanks

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

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

发布评论

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

评论(5

茶花眉 2024-12-24 06:26:41

重构用于改进代码的结构,以提高效率、可读性、可维护性、减少冗余等。对于非常简单的代码,可能没有充分的理由进行重构。随着代码复杂性的增加,重构成为控制代码完整性并有助于保持代码可维护性的宝贵工具。自动化工具旨在嗅探代码中是否有不良编码的迹象,并在代码太大而手动执行此操作需要花费极长的时间时应用已知的良好模式和实践。这在具有多个编码器和许多很多行代码的项目中发挥更大的作用。

Refactoring is used to improve the structure of code to improve efficiency, readability, maintainability, reduce redundancy, and the like. For very simple code there probably is no good reason to refactor. As code increases in complexity, refactoring becomes a valuable tool to control the sanity of the code and helps to keep it maintainable. An automated tool is meant to sniff code for signs of bad coding and apply known good patterns and practices when the code is so large that it would take a prohibitavely long amount of time to do so manually. This comes into play more in projects with multiple coders and many, many lines of code.

只有影子陪我不离不弃 2024-12-24 06:26:41

你忽略了要点——可重用性和可维护性。

临时代码可能很简单,但通常缺乏通用性——如果你想做更复杂的事情,你必须重写它。使用重构工具和方法,您应该能够获得更通用、可插入其他程序并且易于更改(维护)的代码。

但是 - 如果您的代码已经具有这些属性,请不要仅为了重构而进行重构,这只是浪费您的时间和精力。此外,与以前的代码相比,您应该能够更好地理解重构的代码。如果你不这样做 - 你正在做一些非常错误的事情,因为重构应该简化,而不是混淆......

You are missing the main point - reusability and maintainability.

The ad-hoc code may be simplistic, but often lacks generality - if you want to do something more complicated, you have to rewrite it. Using refactoring tools and approaches, you should be able to get a code, which is more general, pluggable into your other programs and is easy to change (maintain).

BUT - if your code already has these properties, do not do refactoring only for refactoring, its just a waste of your time and effort. Also you should be able to unterstand you refactored code better, than your previous code. If you don't - you are doing something terribly wrong, because refactoring should simplify, not obfuscate....

爱她像谁 2024-12-24 06:26:41

我从未使用过重构工具,但我想您可以从它为您重构代码的方式中学习。然而,你需要能够感觉到你的代码何时需要重构,并知道如何自己进行重构,否则你将永远无法发挥你作为程序员的潜力。似乎依赖重构工具会让您无法了解何时需要重构。一定要学会重构你自己的代码。

[1] 您将获得一个更清晰的解决方案,更易于您和其他人阅读

[2] 减少代码重复可能会减少错误,或者使它们更容易查找并修复一次而不是在整个代码中多次重构。

[3] 一旦你重构了足够多的代码,它将影响你的编码习惯,并且
风格,这样你第一次就可以更干净地编写代码,这意味着更少的重构。

重构代码的最佳动机:处理别人的草率代码并不有趣(除非你能在他们面前快速将其分解。这总是很有趣。)

如果这个重构工具是您的工作或课程的一些要求,您必须学习它。记住要进行小测试。当您认为需要重构时,您可能想要创建一个小程序的版本,然后在另一个版本中手动进行重构。然后将旧版本输入重构软件并比较结果。

祝你好运! :)

I have never used a refactoring tool, but I imagine you could learn from the way it re-factors the code for you. However, you NEED to be able to sense when your code needs refactoring and know how to carry out the re-factoring on your own, or you will never live up to your potential as a programmer. It seems relying on a refactoring tool deprives you of picking up a sense of when things need refactoring. Definitely learn to refactor you own code.

[1] You will arrive at a cleaner solution that is easier for you and others to read

[2] Reducing code repetition will likely reduce bugs, or make them easier to find and fix only once rather than multiple times throughout the code.

[3] Once you have re-factored enough code, it will influence your coding habits and
style so you more often code clean the first time, implying less refactoring.

Best motivation to refactor your code: It is no fun working with some one else's sloppy code ( unless you get to gut it quickly in front of them. That's always fun. )

If this re-factoring tool is some requirement for your job or class, you must learn it. Remember to test small. You may want to create a version of a small program when you think it needs re-factoring, and then in another version, do the re-factoring manually. Then feed the old version to the re-factoring software and compare your results.

Good luck! :)

再可℃爱ぅ一点好了 2024-12-24 06:26:41

如果我编写了一段代码,可能有点简单,但我了解每一点是如何工作的,如果出现问题,我可以轻松识别并纠正问题,为什么我应该使用像 ReSharper 这样的工具来转向我的代码变成了我不明白的东西?

您应该使用这样的工具来改进设计,而不是将其变成您不理解的东西。如果您不再理解代码,您可能选择了不适当的重构。

当然,如果你自己无法编写复杂的代码,那么使用代码重构工具只会导致问题,因为当出现问题时你将无法理解新代码,而如果你可以编写重构后的代码,手动编码为什么需要该工具?

重构工具很方便,可以使重构过程变得更容易,也许更安全一些。您确实应该能够手动编写新代码。就我个人而言,我有时会手动进行重构,而不是使用工具。

If I've written a piece of code which is maybe a bit simplistic but I understand how every little bit works and if something goes wrong I can easily identify and correct the problem, why should I use a tool like ReSharper to turn my code into something I don't understand?

You should use such a tool to improve the design, not to turn it into something you don't understand. If you no longer understand the code, you have likely chosen an inappropriate refactoring.

Surely if you can't write the complicated code yourself, using a code refactoring tool is just going to cause issues because you won't be able to understand the new code when something goes wrong, and if you can write the refactored code manually why do you need the tool?

The refactoring tool is a convenience, to make the process of refactoring easier and maybe a bit safer. You indeed should be able to write the new code manually. Personally, I sometimes do a refactoring manually rather than using a tool.

千仐 2024-12-24 06:26:41

是的,有。编码(在现代软件流程中)几乎是一项团队工作。因此,仅仅因为您理解了代码,并不意味着其他人都会理解它。

由于您可能不是唯一需要使用它的人,因此您应该重构代码并遵循代码重构的通用规则(或安排的约定)。 (如果您使用工具或手动执行此操作,那当然始终是一个特定的问题)

Yep, there is. Coding (in modern software processes) is almost a team work. So, just because you understand your code, that doesn't mean that everyone else will understand it.

And since you wont mabye be the only one who'll have to work with it, you should refactor your code and follow common rules (or arranged conventions) of code refactoring. (If you use a tool for that or do it manually, thats of course always a specific question)

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