简体   繁体   English

你如何处理过去的编程暴行?

[英]How do you deal with your past programming atrocities?

None of us started off as experts (and most of us still aren't). 我们都没有成为专家(我们大多数人还没有)。 Sure, we all knew how to write programs in our preferred languages, but writing quality applications and writing syntactically correct programs are two very different things. 当然,我们都知道如何用我们喜欢的语言编写程序,但编写高质量的应用程序和编写语法正确的程序是两回事。 We work, we learn, we struggle, we keep learning, and eventually we reach that threshold where others can learn from us. 我们工作,学习,努力,不断学习,最终达到其他人可以向我们学习的门槛。

If your learning process was anything like mine, you probably wrote a few perfectly functional, reliable applications that are still used daily, yet can't help but drop your head in shame whenever you think about them. 如果你的学习过程与我的学习过程类似,你可能会写一些功能齐全,可靠的应用程序,这些应用程序仍然每天都在使用,但每当你想到它们时,都会感到羞愧。 Procedural code? 程序代码? Mixing business and presentation logic? 混合业务和表示逻辑? SQL injection vulnerabilities? SQL注入漏洞? Wastelands of code barren of any comments? 代码荒芜荒芜的任何评论? The list goes on. 名单还在继续。

Unfortunately, none of us can expect our employers to allow us to go back and rewrite our old applications just because they give us nightmares whenever we think about them. 不幸的是,我们都不能指望我们的雇主允许我们回去重写我们的旧应用程序,因为只要我们想到它们就会给我们做噩梦。 Worse, some of us have to actually work with our old applications, but know that it would be way too risky and/or time consuming to do a whole lot of good as far as refactoring goes. 更糟的是,我们中的一些实际与我们的旧应用程序的工作 ,但要知道,这将是太危险的和/或费时做了一大堆的好尽可能重构去。

How do you deal with the past programming atrocities you've committed and with the people who discover the dirty secret that you didn't always know what you were doing? 你如何应对过去曾经犯下的编程暴行,以及那些发现肮脏秘密的人,你并不总是知道自己在做什么?

No regrets. 不后悔。 If you're not ashamed of the code that you wrote last week that means that you've not improved as a programmer ;-) 如果你不为上周写的代码感到羞耻,这意味着你没有作为程序员改进;-)

Seriously though, my approach has always been to fix stuff that's dangerous (SQL injection vulnerabilities for instance) with urgency and then to refactor other code ugliness if and when I happen to be working in a particular area that needs refactoring. 但说真的,我的方法一直是紧急修复那些危险的东西(例如SQL注入漏洞) 然后在我碰巧在需要重构的特定区域工作时重构其他代码丑陋。 When fixing bugs or introducing new features I often keep a list of bits of code that require refactoring and then refactor the code after I'm done with my 'real' task. 在修复错误或引入新功能时,我经常会列出需要重构的代码列表,然后在完成“真实”任务后重构代码。 It usually doesn't take an inordinate amount of time to do and I have unit tests to make sure I didn't break anything (if your code is not unit tested, this is a great reason to get it tested!). 它通常不需要花费过多的时间来完成,我进行单元测试以确保我没有破坏任何东西(如果你的代码没有经过单元测试,这是测试它的一个很好的理由!)。

Jeff Atwood wrote a great blog post on this some time ago ( here ). 杰夫阿特伍德不久前在这里写了一篇很棒的博客文章( 这里 )。

Whenever I did something less-than-optimally as a child, my father would kindly say, "Well, life is a learning experience." 每当我小时候做一些不太理想的事情时,我的父亲会说,“好吧,生活是一种学习经历。” Our applications can learn too: 我们的应用也可以学习:

In every release of our applications, we deliver a mix of changes: 在我们的每个应用程序版本中,我们提供了各种变化:

  • A big new feature or two 一两个重要的新功能
  • A set of smaller improvements 一组较小的改进
  • Internal changes that make our lives easier 内部变化使我们的生活更轻松
  • Gradually deprecate and remove worst offenders 逐渐弃用并删除最严重的违法者

So gradually, we work to replace troublesome code with the third and fourth items. 因此,我们逐渐尝试用第三和第四项替换麻烦的代码。

Sometimes you can leave your mistakes behind. 有时你可以抛弃你的错误。 I know a senior developer who was once hired away from a company and asked to produce something similar to what he had written for the first company, but better . 我认识一位高级开发人员曾经被雇用过一家公司,并被要求生产类似于他为第一家公司所写的东西,但更好 They gave him the staff and the tools, and he did . 他们给了他工作人员和工具,他做到了

My first programs exist only on TRS-80 cassettes where the iron oxide has flaked off. 我的第一个程序仅存在于TRS-80磁带上,其中氧化铁已剥落。 A couple are printed out with a seven-pin 40-column thermal dot matrix printer which burned the letters onto a special silvery metallic coated paper. 一对夫妇打印出一个7针40列热点阵打印机,将字母刻在特殊的银色金属涂层纸上。 All that paper turned black in the attic one summer. 一个夏天,所有那张纸都在阁楼里变黑了。

I think I'm good. 我觉得我很好。 My oldest atrocities are safely destroyed. 我最古老的暴行被安全地摧毁了。


Actually, there is one atrocity that continues to gnaw at me. 实际上,有一个暴行继续啃着我。 I created a video mode for the Amiga called " Sliced HAM ," which was popular for a while. 我为Amiga创建了一个名为“ Sliced HAM ”的视频模式,这种模式很受欢迎。 It changed the base colors per line to allow more photorealistic image with less fringing. 它改变了每行的基色,以允许更逼真的图像,更少的边缘。

I quickly wrote a converter and viewer. 我很快写了一个转换器和查看器。 In the viewer I sat in a busy loop waiting for the user to close the image. 在观察者中,我坐在忙碌的循环中等待用户关闭图像。 This was not acceptable on the Amiga--the first mass market multitasking computer system. Amiga是第一个大众市场多任务计算机系统,这是不可接受的。 I should have waited for the window's close event. 我应该等待窗口的关闭事件。

For ages, people said the video mode was slow, but it was just my lousy example viewer. 多年来,人们说视频模式很慢,但它只是我糟糕的示例查看器。 The graphics coprocessor was doing all the work per scan line. 图形协处理器正在完成每条扫描线的所有工作。

Still haunts me. 仍然困扰着我。

That's a great question. 这是一个很好的问题。

Unless you are writing open-source software or just dumped your old code online, I don't think that it is as big a problem because future employers won't see your past indiscretions. 除非您正在编写开源软件或者只是将旧代码转移到网上,否则我认为这不是一个大问题,因为未来的雇主不会看到您过去的轻率行为。 You are also likely not bound to your former employers and they won't release the source code. 您也可能不会受到前雇主的约束,他们也不会发布源代码。

Most (good) developers improve over time, and past peers will realize that you are probably better today than you were then, just like they are likely better as well. 大多数(好的)开发人员会随着时间的推移而改善,过去的同行会意识到你今天可能比以前更好,就像他们可能更好一样。 Your employer paid you based on your experience then rather than your experience now, so they got what they paid for. 您的雇主根据您的经验而不是您现在的经验向您付款,因此他们得到了他们付出的代价。

If somebody else besides you cares enough about your application and uses it daily, then they are free to maintain it (if they have the source code). 如果除了你以外的其他人关心你的应用程序并且每天都使用它,那么他们可以自由地维护它(如果他们有源代码)。 Code goes stale. 代码变得陈旧。 An application that doesn't get maintained, improved and re factored is either perfect (unlikely), or not important enough to be worth the effort. 没有得到维护,改进和重新考虑的应用程序要么完美(不太可能),要么不够重要,不值得付出努力。 If a former employer keeps making money on your old application without maintaining it, it's their problem and the problem of their clients, and eventually the cash cow will run dry. 如果一位前雇主在没有维护它的情况下继续在你的旧应用程序上赚钱,那就是他们的问题和他们客户的问题,最终现金牛将会枯竭。

If you feel that your application is widely used, the code is freely available, and something there is likely to be featured on the next thedailywtf post, go ahead and fix it. 如果您觉得您的应用程序被广泛使用,那么代码是免费提供的,并且可能会在下一个thedailywtf帖子中显示某些内容,请继续修复它。 Otherwise, be glad that there is enough old mediocre code out there so that you won't attract attention. 否则,很高兴有足够的旧平庸代码,所以你不会引起注意。

I think a bigger problem is the textual crap we leave around from our earlier years - old forum posts, incorrect blog posts, flame wars over language features, and the like. 我认为更大的问题是我们早年留下的文字废话 - 旧的论坛帖子,不正确的博客帖子,语言功能的火焰战争等等。

Maybe you are at an organization in which you may or may not have an opportunity to correct past errors. 也许您所在的组织中您可能有或没有机会纠正过去的错误。

What you can do is remember to stay modest and helpful to people who are now at a stage where you once were. 可以做的就是记得保持谦虚,乐于助人的人谁现在在哪里,你曾经是一个阶段。

This used to bother me more than it does now. 这比以前更加困扰我。 I've come to the realization that it's just a part of the development process. 我已经认识到它只是开发过程的一部分。 No one started (or even does now) writing perfect code from day 1. Many times the code you wrote was sufficient given the technologies and resources you had at that time. 没有人从第1天开始(甚至现在就开始)编写完美的代码。鉴于您当时拥有的技术和资源,您编写的代码很多时候已经足够了。 It's not fair (for you or anyone else) to compare code you wrote 10 years ago with a 1 week deadline with what you could do today with current technologies and skill sets. 将您在10年前编写的代码与1周的截止日期与当前使用当前技术和技能组合的代码进行比较是不公平的(对于您或其他任何人)。

I apologize publicly. 我公开道歉。 I would like to take this opportunity to apologize for INewWindowManager. 我想借此机会为INewWindowManager道歉。 Sorry. 抱歉。

If it ain't broke, don't fix it. 如果没有损坏,请不要修理它。

I know the feeling that something could be done a lot more simply and elegantly, maybe because you understand the framework better by now or because you learnt some new techniques. 我知道可以更简单,更优雅地完成某些事情的感觉,也许是因为你现在更好地理解了框架,或者因为你学到了一些新技术。 But that's really only relevant when you want to extend or change some code, otherwise just let it run if it works. 但是,当你想要扩展或改变一些代码时,这才真正有用,否则只要它运行就让它运行。 If it doesn't work (ie if it's buggy, too slow, eats resources etc.), you should still be fixing it to begin with and hence have little regret just yet. 如果它不工作 (即如果它是越野车,速度太慢,吃资源等),你仍然应该修复它开始,因此有一点遗憾,只是还没有。 Otherwise, well, what are you worrying about? 否则,你还在担心什么? Beauty in code is so fleeting... ;o) 代码之美是如此短暂......; o)

From the other perspective, if you come in to work on somebody else's code, or even your own old code, you shouldn't be surprised to find sub-optimal code. 从另一个角度来看,如果你进入其他人的代码,甚至是你自己的旧代码,你不应该对找到次优代码感到惊讶。 We all do it. 我们都这样做。 In fact, it's an opportunity to feel smug and play the hero when you successfully refactor the old code. 事实上,当你成功地重构旧代码时,这是一个让自己变得沾沾自喜并扮演英雄的机会。

Non-optimal code is just part of the job. 非最佳代码只是工作的一部分。 We should expect it around every corner and always be prepared to refactor old code when we need to touch it. 我们应该期待它遍布每个角落,并且在我们需要触摸时始终准备重构旧代码。 That's only a problem if management doesn't understand it and doesn't allow you the time to do it. 如果管理层不了解它并且不允许你有时间这样做,这只是一个问题。

Given that the job is still there and you still have chance to commit new stuff, fix the crap and check in! 鉴于工作仍然存在,你仍然有机会提交新的东西,修复废话并检查! It's never too late for a bug fix. 修复错误永远不会太晚。 And it's nothing to be embarrased about. 并没有什么可尴尬的。 Everyone makes mistakes. 每个人都会犯错误。

I also believe in honesty and that a good offense is the best defense. 我也相信诚实,好的进攻是最好的防守。 Just say loud, "this is my code and it's crap" and people will laugh with you :) There's no need to try and hide your code. 只是大声说,“这是我的代码,它是废话”,人们会和你一起笑:)没有必要尝试隐藏你的代码。 It's there in your VCS, with your name on it anyway. 它在您的VCS中,无论如何都有您的名字。

There's a new service for coders: Bad Code Offsets . 编码器有一项新服务: 错误代码偏移 Pay to make up for past programming atrocities... 支付以弥补过去的编程暴行......

The best way to deal with these bad memories is to learn from them, as I am sure you have. 处理这些不良记忆的最好方法是向他们学习,我相信你已经有了。 It is also important as you ask for forgiveness from your team members that you extend them the same. 当您向团队成员请求宽恕时,同样重要的是,这也很重要。 Most importantly, when you should remember these 'bad examples' when you are mentoring junior members of the team. 最重要的是,当你指导团队的初级成员时,你应该记住这些“坏榜样”。

It can be a balancing act. 它可以是一种平衡的行为。 I've been bitten by "improving" some code that was working, but I thought was ugly, and introduced some stupid new bug. 我一直被“改进”一些正在运行的代码所困扰,但我认为这很难看,并引入了一些愚蠢的新bug。

On the other hand, on several occasions in my career, I've struggled with some badly designed piece of crap code (written by me or someone else) - sometimes for weeks - because I didn't think I had time to fix it. 另一方面,在我职业生涯中的几次,我一直在努力设计一些设计糟糕的垃圾代码(由我或其他人编写) - 有时会持续数周 - 因为我觉得我没有时间修复它。

In each case, I finally bit the bullet and did it over again properly, and the problems went away almost immediately. 在每一种情况下,我终于咬紧牙关并再次正确地完成它,问题几乎立即消失了。 Then I wished I'd fixed it sooner - would have been faster in the long run. 然后我希望我早点修好它 - 从长远来看会更快。

Focus your efforts on the sections that are giving you actual grief - the sections that you need to work on (new features, bug fixes), but you're afraid to touch because they're so fragile, or hideous. 把你的精力集中在给你实际悲伤的部分 - 你需要处理的部分(新功能,错误修复),但你害怕因为它们太脆弱或太可怕而触摸。

That's probably a good indicator - the more you're afraid of it, the more it needs to be fixed. 这可能是一个很好的指标 - 你越害怕它,就越需要修复它。

-Tom Bushell -Tom Bushell

I release it anonymously on the net and make others suffer! 我在网上匿名发布,让别人受苦! No wait, I do it on my blog, and probably get laughed at by my peers. 不用等了,我在我的博客上做了,可能会被同行嘲笑。

I make sure I have a backup of the old stuff first, both 'just in case I'm not as clever as I thought I was' and 'If I am as clever as I thought I was, I can prove it!' 我确保我先备份旧的东西,“以防万一我不像我想的那样聪明”,“如果我像我想的那样聪明,我就能证明这一点!” :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM