Does anyone actually think this is a good reason to "Dumb down" your code? When a manager asks you to make your code simple (in terms of technology skills required to understand it) at the cost of more verbose cluttered code what should you do?
有人真的认为这是“愚蠢”你的代码的一个很好的理由吗?当经理要求您使代码变得简单(在理解它所需的技术技能方面)时,代价是更加冗长的混乱代码,您应该怎么做?
25 个解决方案
#1
I highly disagree. Junior developers will end up being Senior developers. How? By encountering advanced topics that aren't taught in school.
我非常不同意。初级开发人员最终将成为高级开发人员。怎么样?通过遇到未在学校教授的高级主题。
My code base now makes heavy use of Inversion of Control containers. I would never revert my code to the old way because a junior developer had issues groking IoC. Instead I would take them out for a beer after work and discuss it. The more the junior dev learns the less hand holding needs to be done.
我的代码库现在大量使用Inversion of Control容器。我永远不会将我的代码恢复到原来的方式,因为初级开发人员遇到了IoC问题。相反,我会在下班后带他们去喝啤酒并讨论它。初级开发者学习的手越少,需要做的就越少。
Here's a blog post discussing this very topic.
这是一篇讨论这个主题的博客文章。
#2
If you're constantly dumbing down your code or designs, it's a pretty good way to make sure your junior developers stay dumb. Challenge them and use it as a mentoring opportunity. Of course, some will never learn, but you've got bigger problems at that point.
如果你不断减少你的代码或设计,这是确保你的初级开发人员保持愚蠢的一个很好的方法。挑战他们并将其作为辅导机会。当然,有些人永远不会学习,但那时你遇到了更大的问题。
It's not just pointy-haired bosses either. As a senior dev, it's often difficult to resist the urge to mommy junior developers. "Oh I'll just do this part because it's way too hard for them", or it'll take them too long, or they'll get way off in the weeds.
它不仅仅是尖头发的老板。作为一名高级开发人员,通常很难抵制妈妈初级开发人员的冲动。 “哦,我只是做这个部分,因为它对他们来说太难了”,或者它会花费太长时间,或者它们会在杂草中消失。
And finally, make sure you strike a balance between idiomatic code that uses the full power of a language vs idiomatic code that abuses that power. There's no reason you need to override the || operator just to run its args in two separate threads. At least dumb the code down a little for your older, dumber, future self.
最后,确保在使用语言的全部功能的惯用代码与滥用该功能的惯用代码之间取得平衡。没有理由需要覆盖||运算符只是在两个单独的线程中运行它的args。对于你年长,笨拙,未来的自我,至少愚蠢的代码。
#3
Well, I think it's reasonable to avoid using "clever" language constructs unless they really, really make the code better - at which point if a junior developer sees it, hopefully they'd ask rather than just being flumoxed.
好吧,我认为避免使用“聪明”的语言结构是合理的,除非他们确实真正地使代码更好 - 在这一点上,如果一个初级开发人员看到它,希望他们要求而不仅仅是陷入困境。
Here's an alternative way of phrasing it though: "Write your code so that it's easy enough to understand that if you get called at 3am and asked to fix a bug in it, you can still understand it."
以下是另一种表达方式:“编写代码,以便很容易理解如果你在凌晨3点被调用并要求修复其中的错误,你仍然可以理解它。”
Seriously, make it as easy to understand as possible. That doesn't mean a comment every other line - it means a comment where the purpose of a piece of code isn't obvious, and only then where the preferred choice of "well make it obvious then" doesn't work.
说真的,让它尽可能容易理解。这并不意味着每隔一行都有一个注释 - 它意味着一段代码的目的并不明显的评论,只有这样才能选择“很好地使其显而易见”。
#4
There's a difference between puzzle code and complex code.
拼图代码和复杂代码之间存在差异。
I've found that the single biggest issue is that there is a big difference between "easy to understand by reading" versus "well-factored", and that the two goals are often in direct tension with one another. In well-factored code, there is a lot more jumping around between classes and a lot of virtual dispatch, so the path through the code is very non-linear.
我发现最大的问题是“阅读容易理解”与“考虑周全”之间存在很大差异,而且这两个目标往往彼此直接紧张。在良好的代码中,类和大量的虚拟调度之间存在更多的跳跃,因此通过代码的路径非常非线性。
#5
Yes readability and being able to easily understand code is a big part of maintainability in my opinion.
在我看来,可读性和能够轻松理解代码是可维护性的重要组成部分。
#6
Well if you intend to maintain your code forever, never change jobs, never feel the urge to work on something new, and can assure everyone you will never be hit by a truck, then sure there is no need to dumb down that puzzle code.
好吧,如果你打算永远保持你的代码,永远不要改变工作,永远不会感到有新的工作的冲动,并且可以向所有人保证你永远不会被卡车击中,那么肯定没有必要愚蠢的谜题代码。
#7
No. In the past, I've learned a lot from seeing the tricks of more experienced developers. I'd much rather have had the opportunity to learn something new from them than have had them dumb things down for me.
不。在过去,我从看到更有经验的开发人员的技巧中学到了很多东西。我宁愿有机会从他们那里学到新东西,也不会让他们为我愚蠢。
#8
Its a balancing act...
If any 3 people on your team can 'read' your code and know what its doing... no need to change. However if you're the only person who can understand your code (no matter how rad/clever you think it is).. maybe you should take it down a few notches.
它是一种平衡行为......如果你团队中有3个人可以“阅读”你的代码并知道它在做什么......不需要改变。但是,如果你是唯一一个能够理解你的代码的人(不管你认为它是多么聪明/聪明)..也许你应该把它记下来几个档次。
Another guideline to help would be to 'Try the simplest thing that works.' All the latest buzz words are nice to know however what it is even more important is having the skill to spot where you could get by without using them. You don't need to spray paint your code with IOC or Frameworks or Design Patterns...
另一个帮助的指导方针是“尝试最简单的工作方式”。所有最新的流行语都很难知道然而更重要的是拥有在不使用它们的情况下找到你可以获得的技能。您不需要使用IOC或框架或设计模式喷涂代码......
The manager's side of this argument is sorely missed in this thread :) (and for the record.. I'm not one). His/Her major concern being he doesn't want a dark area of code that no one else dares to venture into.. so if you can convince your boss that a few other people on the team can make an arbitrary fix (or better yet.. show an actual bug fixed by someone else) - the mgr should let you off the hook. Disagreeing with your boss is another art :).. but you can talk things out usually.
You dont have to go all the way backward to Lowest Common Denominator.. strike a balance.
这个论点的经理方面在这个帖子中非常错过:)(并且为了记录......我不是一个)。他/她的主要关注点是他不想要一个其他人不敢冒险的代码的暗区......所以,如果你能说服你的老板,团队中的其他几个人可以做出任意修复(或更好) ..显示由其他人修复的实际错误) - mgr应该让你摆脱困境。不同意你的老板是另一种艺术:) ..但你可以通常谈论事情。你不必一直向后退到最低公分母......取得平衡。
#9
Your goal should not be for your code to be easy to understand for a junior developer. Instead, it should be easy to understand for a maintainence programmer.
您的目标不应该是您的代码易于理解为初级开发人员。相反,维护程序员应该很容易理解。
This means:
- Local "complexity" is okay, when needed. If they see the complex code they'll know they need to dig deeper.
- Hidden complexity is bad. If you can't see that changing a piece of code will have subtle side effects then maintaining the code will be a nightmare.
- New technologies that are visible are also okay, when not taken to extremes.
当需要时,本地“复杂性”是可以的。如果他们看到复杂的代码,他们就会知道他们需要深入挖掘。
隐藏的复杂性很糟糕。如果您看不到更改一段代码会产生微妙的副作用,那么维护代码将是一场噩梦。
当没有达到极端时,可见的新技术也是可以的。
This is because those that maintain code rarely have the same overall understanding of the system. Or the time to develop it.
这是因为维护代码的人很少对系统有全面的了解。或者是开发它的时候。
#10
I disagree with the manager: What needs to be simple is the code, not the technology used to write it.
我不同意经理:需要简单的是代码,而不是用来编写代码的技术。
I would, however, impose a closely related requirement:
但是,我会强加一个密切相关的要求:
- The internal documentation states clearly what technologies are needed to understand this code, and it gives references to places where those technologies can be learned.
内部文档清楚地说明了理解这些代码所需的技术,并提供了可以学习这些技术的地方的参考。
For example, even as a senior developer, I find all matrix codes baffling. But if somebody gives me a reference to the right part of Numerical Recipes, I can puzzle out the details.
例如,即使作为高级开发人员,我发现所有矩阵代码都令人困惑。但如果有人给我参考数字食谱的正确部分,我可以弄清楚细节。
#11
Yes. It's a very valid reason to take it down a notch. The reality is that a very, very large number of developers (as in most) are at the junior level.
是。这是一个非常有效的理由,可以把它降低一个档次。现实情况是,非常大量的开发人员(与大多数人一样)处于初级水平。
As far as what you should do... Say "Yes Sir" or "Yes Ma'am" and do it. There is only one boss in that relationship.
至于你应该做什么...说“是先生”或“是女士”并做到这一点。在这种关系中只有一个老板。
UPDATE: As some people seem to think that having a jr dev learning advanced topics while wading through obfuscated code I want to throw one more thing in here.
更新:由于有些人似乎认为让jr开发人员学习高级主题,同时涉及混淆代码,我想在这里再抛出一件事。
When ANY developer (jr or otherwise) runs into code they don’t understand, their first approach is to refactor it into something that is understandable. This is called the “Wow that code is crap I must rewrite it!” syndrome. I’m willing to bet everyone on this board has experienced it. So, as a business owner, do I want to pay for code to be developed each time a new person comes by or do I want to pay for new features to be added?
当任何开发人员(jr或其他)遇到他们不理解的代码时,他们的第一种方法是将其重构为可理解的东西。这被称为“哇,代码是废话,我必须重写它!”综合症。我愿意打赌这个董事会的每个人都经历过这个。那么,作为企业所有者,我是否希望为每次新人来时开发的代码付费,或者我是否想要为要添加的新功能付费?
Guess which person I’m going to keep around longer.
猜猜我要留哪个人的时间更长。
#12
If you dumb down your code, you're going to be stuck working with dummy junior programmers who will never be familiar with advanced coding techniques. If there's any verbose code that's trying to express an inherently complex procedure that you wrote, the aforementioned junior developer probably wouldn't be able to see the forest for the trees anyways. And they'd probably screw up if they had to express a complex concept if all they knew were basic primitive constructs whereas if they knew how to express what they meant tersely and elegantly, the code has a better chance of being correct.
如果你愚弄你的代码,那么你将无法与那些永远不熟悉高级编码技术的虚拟初级程序员一起工作。如果有任何详细的代码试图表达您编写的固有的复杂过程,那么前面提到的初级开发人员可能无法看到树的森林。如果他们所知道的只是基本的原始结构,他们可能会搞砸,如果他们知道如何简洁明了地表达他们的意思,那么代码就更有可能是正确的。
#13
Scott Muc said:
Scott Muc说:
"I've found that the single biggest issue is that there is a big difference between "easy to understand by reading" versus "well-factored", and that the two goals are often in direct tension with one another. In well-factored code, there is a lot more jumping around between classes and a lot of virtual dispatch, so the path through the code is very non-linear."
“我发现最大的问题是”通过阅读易于理解“与”考虑周全“之间存在很大差异,而且这两个目标往往彼此直接紧张。代码,类和大量的虚拟调度之间有很多跳跃,所以通过代码的路径是非线性的。“
Quoted for truth, and I think this is one of the biggest problems with C++ code in general. If you're the one that wrote the code, it's pretty easy to come up with a very complicated set of stuff that is well factored, makes lots of sense if you already know it, works well, and generally resembles a diamond crystal, etc. but which, from the perspective of someone who's trying to figure out how you got there and why things are the way they are and how things work, and how one might make changes that fit into the existing system and satisfy new requirements, is almost completely opaque and impenetrable.
引用真相,我认为这是C ++代码最常见的问题之一。如果你是那个编写代码的人,很容易想出一套非常复杂的东西,如果你已经知道它,效果很好,并且通常类似于钻石水晶等,那么很有意义。但是,从那些试图弄清楚你是如何到达那里的人的角度来看,以及为什么事情就是这样,事情是如何运作的,以及人们如何做出适合现有系统并满足新要求的变化,几乎是完全不透明和难以穿透。
How does this kind of situation help maintainability? That situation is one of my main beefs with C++ programmers. Far better to have a mess of plain C code which can be hacked upon than a diamond crystal of inpenetrably super-factored code which nearly nobody can figure out how to sensibly modify without smashing the crystalline structure.
这种情况如何帮助维护?这种情况是我对C ++程序员的主要兴起之一。更好的是有一堆普通的C代码可以被攻击而不是钻石晶体的超级超级因素代码,几乎没有人能够弄清楚如何在不破坏晶体结构的情况下明智地修改。
#14
One way to "dumb down" code that I actually think is an excellent practice is to use longer variable names and longer function names. Naming variables and functions to make their purpose easily understandable is a significant engineering task, IMHO, and takes extra effort on the part of the original author of the code. Damian Conway has some excellent examples in "Perl Best Practices". Some examples include: Prefer "final_total" to "sum
"; prefer "previous_appointment" to "previous_elem"
, prefer "next_client" to "next_elem"
. Prefer "sales_records" to "data".
Etc. He also pushes for using grammatical templates (Noun-adjective) and staying consistent. Don't use max_displacement
one place and then use VelocityMax
in another. Index variables need real names too:sales_record[i] vs sales_record[cancelled_transaction_number]
“哑巴”代码的一种方法,我认为这是一个很好的做法是使用更长的变量名称和更长的函数名称。命名变量和函数使其目的易于理解是一项重要的工程任务,恕我直言,并且需要代码的原始作者付出额外的努力。 Damian Conway在“Perl最佳实践”中有一些很好的例子。一些例子包括:首选“final_total”为“sum”;更喜欢“previous_appointment”到“previous_elem”,更喜欢“next_client”到“next_elem”。首选“sales_records”为“数据”。等等。他也推动使用语法模板(名词 - 形容词)并保持一致。不要在一个地方使用max_displacement,然后在另一个地方使用VelocityMax。索引变量也需要真实姓名:sales_record [i] vs sales_record [cancelled_transaction_number]
I frequently "refactor" my code at the end of a development cycle by finding new names for all my functions and variables. In a modern editor it's trivial to change them all, and it's only at the end that I really figure out what I used them for. "Dumbing down" code this way isn't classic C, but it's easier for me when I come back months later asking WTF did I do?
我经常在开发周期结束时通过为所有函数和变量查找新名称来“重构”我的代码。在一个现代编辑器中,改变它们是微不足道的,而且最后我才能弄明白我用它们的原因。以这种方式“降低”代码并不是经典的C,但是当我几个月后回来问我WTF我做了什么时,对我来说更容易?
#15
It depends on the code. Is this something being shipped in your flagship product that requires use of the features your manager wants you to remove for performance reasons? If the answer is yes I would try to have your manager let you keep the code and just write up a document explaining in detail the section of code that is hard to understand. If it's an internal app that needs to be maintained by lots of different people and the complex features can be removed with out negatively affecting the program remove them and pick more important battles to fight.
这取决于代码。您的旗舰产品是否需要使用您的经理希望您出于性能原因删除的功能?如果答案是肯定的,我会尝试让您的经理让您保留代码,然后编写一份文档,详细解释难以理解的代码部分。如果它是一个内部应用程序,需要由许多不同的人维护,复杂的功能可以删除而不会对程序产生负面影响,删除它们并选择更重要的战斗来进行战斗。
#16
You should just remind your boss that you can build rocket ships or chicken coops, and he will have to pay you the same for either one. Do what they say but generally an environment like that lends itself to people looking for a new environment.
你应该提醒你的老板,你可以建造火箭船或鸡舍,他将不得不为任何一个支付相同的费用。做他们说的话但通常这样的环境适合寻找新环境的人。
#17
The old quote is appropriate here:
旧报价适用于此:
Make everything as simple as possible, but not simpler.
让一切尽可能简单,但并不简单。
#18
I've known developers who wrote highly obfuscated code that they felt was advanced but which the rest of the team felt was unreadable and unmaintainable. Part of this involved overuse of advanced language features (in C, the ternary operator and the comma operator) and writing in an obscure personal idiom (for example, replacing ptr->item with (*ptr).item everywhere) that no-one else would ever be able to maintain. The author was trying to outsmart the optimizer (which to be fair, was far from good).
我认识的开发人员编写了高度混淆的代码,他们觉得这些代码已被提升,但团队的其他成员认为这些代码难以理解且无法维护。其中一部分涉及过度使用高级语言功能(在C中,三元运算符和逗号运算符),并用一种模糊的个人习语写作(例如,用(* ptr).item替换ptr-> item),没有人否则将永远能够维持。作者试图超越优化器(这是公平的,远非好的)。
Note: I'm not suggesting that "x = (p == NULL) ? "default" : p->value;" is complicated, but when someone uses ternary operators that span many lines, are nested, and make heavy use of the comma operator, code quickly becomes unreadable.
注意:我不是建议“x =(p == NULL)?”默认“:p-> value;”很复杂,但是当有人使用跨越多行的三元运算符,嵌套并大量使用逗号运算符时,代码很快变得不可读。
In this sort of case, "dumbing down" the code would have been a good idea. The problem was not advanced algorithms nor advanced language features, but overuse and inappropriate use of advanced language features, and an obscure personal idiom.
在这种情况下,“减少”代码将是一个好主意。问题不是高级算法,也不是高级语言功能,而是过度使用和不恰当地使用高级语言功能,以及一种模糊的个人习惯用法。
However, in the case you are asking about, where the manager's changes make the code more difficult to read and maintain, I agree with you and the others who have responded. I just wanted to point out the alternative that no-one else has mentioned.
但是,在您询问的情况下,经理的更改会使代码更难以阅读和维护,我同意您和其他已做出回复的人。我只想指出其他人没有提到过的替代方案。
#19
I suggest keeping the code in a "Geeky-level" and comment it well so that the juniors can understand the intention behind the code and simultaneously learn a better way (or a right way) to code, so we have the best of both he worlds.
我建议将代码保持在“Geeky级别”并对其进行评论,以便大三学生能够理解代码背后的意图并同时学习更好的方法(或正确的方法)来编写代码,因此我们充分利用了他世界。
#20
I think it is the manager's way of politely telling you that your code is too obfuscated/complex/jumbled/puzzle code...whatever you want to call it. Sometimes we get so involved writing our codes that we forget that someone else will have to come along and read it later.
我认为这是经理礼貌地告诉你你的代码太混淆/复杂/混乱/拼图代码......无论你想怎么称呼它。有时我们会如此参与编写代码,以至于我们忘记了其他人必须在以后阅读它。
#21
I learned it the hard way and, in retrospect, find that it was the better way. Let the cycle repeat itself.
我很难学到它,回想起来,发现这是更好的方式。让循环重复。
#22
I agree 100% with the argument. With one major addition: Train your junior developers until they understand your code ;-)
我同意100%的论点。还有一个重要的补充:训练你的初级开发人员,直到他们理解你的代码;-)
#23
I'm talking about using "unusual" technologies. In this case it's JQuery. This issue came up when I was writing a wizard control for user registration. The navigation menu needed to be customised and the current step in the wizard had to have a different css class in the menu. This meant I needed to get access to the currently selected step when generating the menu. My solution was to output the current step index in a hidden html field which could then be read by JQuery in order to customise the css.
我在谈论使用“不寻常”的技术。在这种情况下,它是JQuery。当我编写用于用户注册的向导控件时,出现了此问题。导航菜单需要自定义,向导中的当前步骤必须在菜单中具有不同的css类。这意味着我需要在生成菜单时访问当前选定的步骤。我的解决方案是在隐藏的html字段中输出当前步骤索引,然后JQuery可以读取该字段以自定义css。
I thought that this was much nicer and cleaner than using the databinding syntax in ASP.NET which doesn't have compile-time checking and messes up the layout of the html.
我认为这比使用ASP.NET中的数据绑定语法更好,更清晰,它没有编译时检查并且弄乱了html的布局。
The databinding solutions is "standard" while the JQuery one is "unusual" which means that it's less likely to be understood by a junior.
数据绑定解决方案是“标准”,而JQuery是“不寻常”,这意味着它不太可能被大三学生理解。
I'm trying more and more these days to provide the required data for the UI rather than hack it into the UI with databinding which is why I added the hidden field with the current step index.
我现在越来越多地尝试为UI提供所需的数据而不是通过数据绑定将其破解到UI中,这就是为什么我使用当前步骤索引添加隐藏字段的原因。
#24
It is simply impossible to make progress or to innovate in any industry without doing things that others don't understand. Innovation is necessarily blasphemous. Why? Because if you're doing things that make sense to everyone else around you, the odds are you're not the first one doing it. ;)
如果不做别人不理解的事情,就不可能在任何行业中取得进步或创新。创新必然是*神明的。为什么?因为如果你做的事情对你身边的其他人都有意义,那么你很可能不是第一个这样做的人。 ;)
That being said, there is a significant difference between doing something that is difficult to understand simply because it's a new or complicated problem versus doing something that's difficult to understand because you're trying to show off or you think confusing people will somehow gain you job security (which I've never seen work, but I've heard of people trying).
话虽如此,做一些难以理解的事情之间存在显着差异,因为这是一个新的或复杂的问题而不是做一些难以理解的事情,因为你试图炫耀或者你认为混乱的人会以某种方式获得你的工作安全(我从未见过工作,但我听说有人在尝试)。
Should you make things easy to understand? Yes absolutely, as much as humanly possible. However a program that works and does its job well is the higher priority.
你应该让事情易于理解吗?是绝对的,尽可能的人性化。然而,一个有效并且工作良好的计划是更高优先级。
The manager's complaint should never be "don't do this because our junior guys don't understand it" -- it should only ever be "do x instead of y whenever feasible because x is easier to read / understand". This also assumes that x and y are equivalent (accept the same input and produce the same result).
经理的抱怨永远不应该是“不要这样做,因为我们的初级人员不理解” - 它应该只是“在可行时做x而不是y,因为x更容易阅读/理解”。这也假设x和y是等价的(接受相同的输入并产生相同的结果)。
I can't stand when managers do that... I've had three different managers bawl me out for using perfectly normal code the way it was designed to work, not because I was doing anything complicated, but rather only because they felt like it was too much effort for the other guys on our team to go RTFM on the language we were using. As a management strategy, that's totally backwards. It's like being the Holy Roman Catholic church and insisting that the laymen are too dumb to be trusted with literacy.
当管理人员这样做时,我无法忍受......我有三个不同的经理让我用完正常的代码按照设计的方式工作,不是因为我做了任何复杂的事情,而是因为他们感觉像对于我们团队中的其他人来说,使用我们使用的语言进行RTFM是太费劲了。作为一种管理策略,这完全是倒退的。这就像是神圣罗马天主教会,坚持认为外行人太愚蠢,不值得信赖。
If you want to know really how ridiculous some of these managers get, try this: I had one manager bawl me out for declaring a variable as a type of "boolean" because he didn't feel the other programmers could handle it. Actually when I asked why, his answer was "because we don't do that here", which is a non-answer, but I interpreted it to mean "dumb it down". They were also berating me for that and similar practices as though it should be obvious that good programming habits were actually "bad" and that I should already know why even though they had never expressed a preferred programming style (either formally or informally). Needless to say, it was a bad job.
如果你想知道其中一些管理者真的有多荒谬,试试这个:我有一位经理让我把一个变量称为“布尔”类型,因为他觉得其他程序员无法处理它。实际上,当我问为什么时,他的答案是“因为我们不在这里做”,这是一个非答案,但我把它解释为“愚蠢”。他们也因为这个和类似的做法而指责我,好像很明显好的编程习惯实际上是“糟糕的”,我应该已经知道为什么即使他们从未表达过一种首选的编程风格(正式或非正式)。不用说,这是一个糟糕的工作。
#25
Make sure you can understand what it does 6 months down the road.
确保你能在6个月后了解它的作用。
When in doubt, COMMENT your code. That's what comments are for.
如有疑问,请注明您的代码。这就是评论的意思。
#1
I highly disagree. Junior developers will end up being Senior developers. How? By encountering advanced topics that aren't taught in school.
我非常不同意。初级开发人员最终将成为高级开发人员。怎么样?通过遇到未在学校教授的高级主题。
My code base now makes heavy use of Inversion of Control containers. I would never revert my code to the old way because a junior developer had issues groking IoC. Instead I would take them out for a beer after work and discuss it. The more the junior dev learns the less hand holding needs to be done.
我的代码库现在大量使用Inversion of Control容器。我永远不会将我的代码恢复到原来的方式,因为初级开发人员遇到了IoC问题。相反,我会在下班后带他们去喝啤酒并讨论它。初级开发者学习的手越少,需要做的就越少。
Here's a blog post discussing this very topic.
这是一篇讨论这个主题的博客文章。
#2
If you're constantly dumbing down your code or designs, it's a pretty good way to make sure your junior developers stay dumb. Challenge them and use it as a mentoring opportunity. Of course, some will never learn, but you've got bigger problems at that point.
如果你不断减少你的代码或设计,这是确保你的初级开发人员保持愚蠢的一个很好的方法。挑战他们并将其作为辅导机会。当然,有些人永远不会学习,但那时你遇到了更大的问题。
It's not just pointy-haired bosses either. As a senior dev, it's often difficult to resist the urge to mommy junior developers. "Oh I'll just do this part because it's way too hard for them", or it'll take them too long, or they'll get way off in the weeds.
它不仅仅是尖头发的老板。作为一名高级开发人员,通常很难抵制妈妈初级开发人员的冲动。 “哦,我只是做这个部分,因为它对他们来说太难了”,或者它会花费太长时间,或者它们会在杂草中消失。
And finally, make sure you strike a balance between idiomatic code that uses the full power of a language vs idiomatic code that abuses that power. There's no reason you need to override the || operator just to run its args in two separate threads. At least dumb the code down a little for your older, dumber, future self.
最后,确保在使用语言的全部功能的惯用代码与滥用该功能的惯用代码之间取得平衡。没有理由需要覆盖||运算符只是在两个单独的线程中运行它的args。对于你年长,笨拙,未来的自我,至少愚蠢的代码。
#3
Well, I think it's reasonable to avoid using "clever" language constructs unless they really, really make the code better - at which point if a junior developer sees it, hopefully they'd ask rather than just being flumoxed.
好吧,我认为避免使用“聪明”的语言结构是合理的,除非他们确实真正地使代码更好 - 在这一点上,如果一个初级开发人员看到它,希望他们要求而不仅仅是陷入困境。
Here's an alternative way of phrasing it though: "Write your code so that it's easy enough to understand that if you get called at 3am and asked to fix a bug in it, you can still understand it."
以下是另一种表达方式:“编写代码,以便很容易理解如果你在凌晨3点被调用并要求修复其中的错误,你仍然可以理解它。”
Seriously, make it as easy to understand as possible. That doesn't mean a comment every other line - it means a comment where the purpose of a piece of code isn't obvious, and only then where the preferred choice of "well make it obvious then" doesn't work.
说真的,让它尽可能容易理解。这并不意味着每隔一行都有一个注释 - 它意味着一段代码的目的并不明显的评论,只有这样才能选择“很好地使其显而易见”。
#4
There's a difference between puzzle code and complex code.
拼图代码和复杂代码之间存在差异。
I've found that the single biggest issue is that there is a big difference between "easy to understand by reading" versus "well-factored", and that the two goals are often in direct tension with one another. In well-factored code, there is a lot more jumping around between classes and a lot of virtual dispatch, so the path through the code is very non-linear.
我发现最大的问题是“阅读容易理解”与“考虑周全”之间存在很大差异,而且这两个目标往往彼此直接紧张。在良好的代码中,类和大量的虚拟调度之间存在更多的跳跃,因此通过代码的路径非常非线性。
#5
Yes readability and being able to easily understand code is a big part of maintainability in my opinion.
在我看来,可读性和能够轻松理解代码是可维护性的重要组成部分。
#6
Well if you intend to maintain your code forever, never change jobs, never feel the urge to work on something new, and can assure everyone you will never be hit by a truck, then sure there is no need to dumb down that puzzle code.
好吧,如果你打算永远保持你的代码,永远不要改变工作,永远不会感到有新的工作的冲动,并且可以向所有人保证你永远不会被卡车击中,那么肯定没有必要愚蠢的谜题代码。
#7
No. In the past, I've learned a lot from seeing the tricks of more experienced developers. I'd much rather have had the opportunity to learn something new from them than have had them dumb things down for me.
不。在过去,我从看到更有经验的开发人员的技巧中学到了很多东西。我宁愿有机会从他们那里学到新东西,也不会让他们为我愚蠢。
#8
Its a balancing act...
If any 3 people on your team can 'read' your code and know what its doing... no need to change. However if you're the only person who can understand your code (no matter how rad/clever you think it is).. maybe you should take it down a few notches.
它是一种平衡行为......如果你团队中有3个人可以“阅读”你的代码并知道它在做什么......不需要改变。但是,如果你是唯一一个能够理解你的代码的人(不管你认为它是多么聪明/聪明)..也许你应该把它记下来几个档次。
Another guideline to help would be to 'Try the simplest thing that works.' All the latest buzz words are nice to know however what it is even more important is having the skill to spot where you could get by without using them. You don't need to spray paint your code with IOC or Frameworks or Design Patterns...
另一个帮助的指导方针是“尝试最简单的工作方式”。所有最新的流行语都很难知道然而更重要的是拥有在不使用它们的情况下找到你可以获得的技能。您不需要使用IOC或框架或设计模式喷涂代码......
The manager's side of this argument is sorely missed in this thread :) (and for the record.. I'm not one). His/Her major concern being he doesn't want a dark area of code that no one else dares to venture into.. so if you can convince your boss that a few other people on the team can make an arbitrary fix (or better yet.. show an actual bug fixed by someone else) - the mgr should let you off the hook. Disagreeing with your boss is another art :).. but you can talk things out usually.
You dont have to go all the way backward to Lowest Common Denominator.. strike a balance.
这个论点的经理方面在这个帖子中非常错过:)(并且为了记录......我不是一个)。他/她的主要关注点是他不想要一个其他人不敢冒险的代码的暗区......所以,如果你能说服你的老板,团队中的其他几个人可以做出任意修复(或更好) ..显示由其他人修复的实际错误) - mgr应该让你摆脱困境。不同意你的老板是另一种艺术:) ..但你可以通常谈论事情。你不必一直向后退到最低公分母......取得平衡。
#9
Your goal should not be for your code to be easy to understand for a junior developer. Instead, it should be easy to understand for a maintainence programmer.
您的目标不应该是您的代码易于理解为初级开发人员。相反,维护程序员应该很容易理解。
This means:
- Local "complexity" is okay, when needed. If they see the complex code they'll know they need to dig deeper.
- Hidden complexity is bad. If you can't see that changing a piece of code will have subtle side effects then maintaining the code will be a nightmare.
- New technologies that are visible are also okay, when not taken to extremes.
当需要时,本地“复杂性”是可以的。如果他们看到复杂的代码,他们就会知道他们需要深入挖掘。
隐藏的复杂性很糟糕。如果您看不到更改一段代码会产生微妙的副作用,那么维护代码将是一场噩梦。
当没有达到极端时,可见的新技术也是可以的。
This is because those that maintain code rarely have the same overall understanding of the system. Or the time to develop it.
这是因为维护代码的人很少对系统有全面的了解。或者是开发它的时候。
#10
I disagree with the manager: What needs to be simple is the code, not the technology used to write it.
我不同意经理:需要简单的是代码,而不是用来编写代码的技术。
I would, however, impose a closely related requirement:
但是,我会强加一个密切相关的要求:
- The internal documentation states clearly what technologies are needed to understand this code, and it gives references to places where those technologies can be learned.
内部文档清楚地说明了理解这些代码所需的技术,并提供了可以学习这些技术的地方的参考。
For example, even as a senior developer, I find all matrix codes baffling. But if somebody gives me a reference to the right part of Numerical Recipes, I can puzzle out the details.
例如,即使作为高级开发人员,我发现所有矩阵代码都令人困惑。但如果有人给我参考数字食谱的正确部分,我可以弄清楚细节。
#11
Yes. It's a very valid reason to take it down a notch. The reality is that a very, very large number of developers (as in most) are at the junior level.
是。这是一个非常有效的理由,可以把它降低一个档次。现实情况是,非常大量的开发人员(与大多数人一样)处于初级水平。
As far as what you should do... Say "Yes Sir" or "Yes Ma'am" and do it. There is only one boss in that relationship.
至于你应该做什么...说“是先生”或“是女士”并做到这一点。在这种关系中只有一个老板。
UPDATE: As some people seem to think that having a jr dev learning advanced topics while wading through obfuscated code I want to throw one more thing in here.
更新:由于有些人似乎认为让jr开发人员学习高级主题,同时涉及混淆代码,我想在这里再抛出一件事。
When ANY developer (jr or otherwise) runs into code they don’t understand, their first approach is to refactor it into something that is understandable. This is called the “Wow that code is crap I must rewrite it!” syndrome. I’m willing to bet everyone on this board has experienced it. So, as a business owner, do I want to pay for code to be developed each time a new person comes by or do I want to pay for new features to be added?
当任何开发人员(jr或其他)遇到他们不理解的代码时,他们的第一种方法是将其重构为可理解的东西。这被称为“哇,代码是废话,我必须重写它!”综合症。我愿意打赌这个董事会的每个人都经历过这个。那么,作为企业所有者,我是否希望为每次新人来时开发的代码付费,或者我是否想要为要添加的新功能付费?
Guess which person I’m going to keep around longer.
猜猜我要留哪个人的时间更长。
#12
If you dumb down your code, you're going to be stuck working with dummy junior programmers who will never be familiar with advanced coding techniques. If there's any verbose code that's trying to express an inherently complex procedure that you wrote, the aforementioned junior developer probably wouldn't be able to see the forest for the trees anyways. And they'd probably screw up if they had to express a complex concept if all they knew were basic primitive constructs whereas if they knew how to express what they meant tersely and elegantly, the code has a better chance of being correct.
如果你愚弄你的代码,那么你将无法与那些永远不熟悉高级编码技术的虚拟初级程序员一起工作。如果有任何详细的代码试图表达您编写的固有的复杂过程,那么前面提到的初级开发人员可能无法看到树的森林。如果他们所知道的只是基本的原始结构,他们可能会搞砸,如果他们知道如何简洁明了地表达他们的意思,那么代码就更有可能是正确的。
#13
Scott Muc said:
Scott Muc说:
"I've found that the single biggest issue is that there is a big difference between "easy to understand by reading" versus "well-factored", and that the two goals are often in direct tension with one another. In well-factored code, there is a lot more jumping around between classes and a lot of virtual dispatch, so the path through the code is very non-linear."
“我发现最大的问题是”通过阅读易于理解“与”考虑周全“之间存在很大差异,而且这两个目标往往彼此直接紧张。代码,类和大量的虚拟调度之间有很多跳跃,所以通过代码的路径是非线性的。“
Quoted for truth, and I think this is one of the biggest problems with C++ code in general. If you're the one that wrote the code, it's pretty easy to come up with a very complicated set of stuff that is well factored, makes lots of sense if you already know it, works well, and generally resembles a diamond crystal, etc. but which, from the perspective of someone who's trying to figure out how you got there and why things are the way they are and how things work, and how one might make changes that fit into the existing system and satisfy new requirements, is almost completely opaque and impenetrable.
引用真相,我认为这是C ++代码最常见的问题之一。如果你是那个编写代码的人,很容易想出一套非常复杂的东西,如果你已经知道它,效果很好,并且通常类似于钻石水晶等,那么很有意义。但是,从那些试图弄清楚你是如何到达那里的人的角度来看,以及为什么事情就是这样,事情是如何运作的,以及人们如何做出适合现有系统并满足新要求的变化,几乎是完全不透明和难以穿透。
How does this kind of situation help maintainability? That situation is one of my main beefs with C++ programmers. Far better to have a mess of plain C code which can be hacked upon than a diamond crystal of inpenetrably super-factored code which nearly nobody can figure out how to sensibly modify without smashing the crystalline structure.
这种情况如何帮助维护?这种情况是我对C ++程序员的主要兴起之一。更好的是有一堆普通的C代码可以被攻击而不是钻石晶体的超级超级因素代码,几乎没有人能够弄清楚如何在不破坏晶体结构的情况下明智地修改。
#14
One way to "dumb down" code that I actually think is an excellent practice is to use longer variable names and longer function names. Naming variables and functions to make their purpose easily understandable is a significant engineering task, IMHO, and takes extra effort on the part of the original author of the code. Damian Conway has some excellent examples in "Perl Best Practices". Some examples include: Prefer "final_total" to "sum
"; prefer "previous_appointment" to "previous_elem"
, prefer "next_client" to "next_elem"
. Prefer "sales_records" to "data".
Etc. He also pushes for using grammatical templates (Noun-adjective) and staying consistent. Don't use max_displacement
one place and then use VelocityMax
in another. Index variables need real names too:sales_record[i] vs sales_record[cancelled_transaction_number]
“哑巴”代码的一种方法,我认为这是一个很好的做法是使用更长的变量名称和更长的函数名称。命名变量和函数使其目的易于理解是一项重要的工程任务,恕我直言,并且需要代码的原始作者付出额外的努力。 Damian Conway在“Perl最佳实践”中有一些很好的例子。一些例子包括:首选“final_total”为“sum”;更喜欢“previous_appointment”到“previous_elem”,更喜欢“next_client”到“next_elem”。首选“sales_records”为“数据”。等等。他也推动使用语法模板(名词 - 形容词)并保持一致。不要在一个地方使用max_displacement,然后在另一个地方使用VelocityMax。索引变量也需要真实姓名:sales_record [i] vs sales_record [cancelled_transaction_number]
I frequently "refactor" my code at the end of a development cycle by finding new names for all my functions and variables. In a modern editor it's trivial to change them all, and it's only at the end that I really figure out what I used them for. "Dumbing down" code this way isn't classic C, but it's easier for me when I come back months later asking WTF did I do?
我经常在开发周期结束时通过为所有函数和变量查找新名称来“重构”我的代码。在一个现代编辑器中,改变它们是微不足道的,而且最后我才能弄明白我用它们的原因。以这种方式“降低”代码并不是经典的C,但是当我几个月后回来问我WTF我做了什么时,对我来说更容易?
#15
It depends on the code. Is this something being shipped in your flagship product that requires use of the features your manager wants you to remove for performance reasons? If the answer is yes I would try to have your manager let you keep the code and just write up a document explaining in detail the section of code that is hard to understand. If it's an internal app that needs to be maintained by lots of different people and the complex features can be removed with out negatively affecting the program remove them and pick more important battles to fight.
这取决于代码。您的旗舰产品是否需要使用您的经理希望您出于性能原因删除的功能?如果答案是肯定的,我会尝试让您的经理让您保留代码,然后编写一份文档,详细解释难以理解的代码部分。如果它是一个内部应用程序,需要由许多不同的人维护,复杂的功能可以删除而不会对程序产生负面影响,删除它们并选择更重要的战斗来进行战斗。
#16
You should just remind your boss that you can build rocket ships or chicken coops, and he will have to pay you the same for either one. Do what they say but generally an environment like that lends itself to people looking for a new environment.
你应该提醒你的老板,你可以建造火箭船或鸡舍,他将不得不为任何一个支付相同的费用。做他们说的话但通常这样的环境适合寻找新环境的人。
#17
The old quote is appropriate here:
旧报价适用于此:
Make everything as simple as possible, but not simpler.
让一切尽可能简单,但并不简单。
#18
I've known developers who wrote highly obfuscated code that they felt was advanced but which the rest of the team felt was unreadable and unmaintainable. Part of this involved overuse of advanced language features (in C, the ternary operator and the comma operator) and writing in an obscure personal idiom (for example, replacing ptr->item with (*ptr).item everywhere) that no-one else would ever be able to maintain. The author was trying to outsmart the optimizer (which to be fair, was far from good).
我认识的开发人员编写了高度混淆的代码,他们觉得这些代码已被提升,但团队的其他成员认为这些代码难以理解且无法维护。其中一部分涉及过度使用高级语言功能(在C中,三元运算符和逗号运算符),并用一种模糊的个人习语写作(例如,用(* ptr).item替换ptr-> item),没有人否则将永远能够维持。作者试图超越优化器(这是公平的,远非好的)。
Note: I'm not suggesting that "x = (p == NULL) ? "default" : p->value;" is complicated, but when someone uses ternary operators that span many lines, are nested, and make heavy use of the comma operator, code quickly becomes unreadable.
注意:我不是建议“x =(p == NULL)?”默认“:p-> value;”很复杂,但是当有人使用跨越多行的三元运算符,嵌套并大量使用逗号运算符时,代码很快变得不可读。
In this sort of case, "dumbing down" the code would have been a good idea. The problem was not advanced algorithms nor advanced language features, but overuse and inappropriate use of advanced language features, and an obscure personal idiom.
在这种情况下,“减少”代码将是一个好主意。问题不是高级算法,也不是高级语言功能,而是过度使用和不恰当地使用高级语言功能,以及一种模糊的个人习惯用法。
However, in the case you are asking about, where the manager's changes make the code more difficult to read and maintain, I agree with you and the others who have responded. I just wanted to point out the alternative that no-one else has mentioned.
但是,在您询问的情况下,经理的更改会使代码更难以阅读和维护,我同意您和其他已做出回复的人。我只想指出其他人没有提到过的替代方案。
#19
I suggest keeping the code in a "Geeky-level" and comment it well so that the juniors can understand the intention behind the code and simultaneously learn a better way (or a right way) to code, so we have the best of both he worlds.
我建议将代码保持在“Geeky级别”并对其进行评论,以便大三学生能够理解代码背后的意图并同时学习更好的方法(或正确的方法)来编写代码,因此我们充分利用了他世界。
#20
I think it is the manager's way of politely telling you that your code is too obfuscated/complex/jumbled/puzzle code...whatever you want to call it. Sometimes we get so involved writing our codes that we forget that someone else will have to come along and read it later.
我认为这是经理礼貌地告诉你你的代码太混淆/复杂/混乱/拼图代码......无论你想怎么称呼它。有时我们会如此参与编写代码,以至于我们忘记了其他人必须在以后阅读它。
#21
I learned it the hard way and, in retrospect, find that it was the better way. Let the cycle repeat itself.
我很难学到它,回想起来,发现这是更好的方式。让循环重复。
#22
I agree 100% with the argument. With one major addition: Train your junior developers until they understand your code ;-)
我同意100%的论点。还有一个重要的补充:训练你的初级开发人员,直到他们理解你的代码;-)
#23
I'm talking about using "unusual" technologies. In this case it's JQuery. This issue came up when I was writing a wizard control for user registration. The navigation menu needed to be customised and the current step in the wizard had to have a different css class in the menu. This meant I needed to get access to the currently selected step when generating the menu. My solution was to output the current step index in a hidden html field which could then be read by JQuery in order to customise the css.
我在谈论使用“不寻常”的技术。在这种情况下,它是JQuery。当我编写用于用户注册的向导控件时,出现了此问题。导航菜单需要自定义,向导中的当前步骤必须在菜单中具有不同的css类。这意味着我需要在生成菜单时访问当前选定的步骤。我的解决方案是在隐藏的html字段中输出当前步骤索引,然后JQuery可以读取该字段以自定义css。
I thought that this was much nicer and cleaner than using the databinding syntax in ASP.NET which doesn't have compile-time checking and messes up the layout of the html.
我认为这比使用ASP.NET中的数据绑定语法更好,更清晰,它没有编译时检查并且弄乱了html的布局。
The databinding solutions is "standard" while the JQuery one is "unusual" which means that it's less likely to be understood by a junior.
数据绑定解决方案是“标准”,而JQuery是“不寻常”,这意味着它不太可能被大三学生理解。
I'm trying more and more these days to provide the required data for the UI rather than hack it into the UI with databinding which is why I added the hidden field with the current step index.
我现在越来越多地尝试为UI提供所需的数据而不是通过数据绑定将其破解到UI中,这就是为什么我使用当前步骤索引添加隐藏字段的原因。
#24
It is simply impossible to make progress or to innovate in any industry without doing things that others don't understand. Innovation is necessarily blasphemous. Why? Because if you're doing things that make sense to everyone else around you, the odds are you're not the first one doing it. ;)
如果不做别人不理解的事情,就不可能在任何行业中取得进步或创新。创新必然是*神明的。为什么?因为如果你做的事情对你身边的其他人都有意义,那么你很可能不是第一个这样做的人。 ;)
That being said, there is a significant difference between doing something that is difficult to understand simply because it's a new or complicated problem versus doing something that's difficult to understand because you're trying to show off or you think confusing people will somehow gain you job security (which I've never seen work, but I've heard of people trying).
话虽如此,做一些难以理解的事情之间存在显着差异,因为这是一个新的或复杂的问题而不是做一些难以理解的事情,因为你试图炫耀或者你认为混乱的人会以某种方式获得你的工作安全(我从未见过工作,但我听说有人在尝试)。
Should you make things easy to understand? Yes absolutely, as much as humanly possible. However a program that works and does its job well is the higher priority.
你应该让事情易于理解吗?是绝对的,尽可能的人性化。然而,一个有效并且工作良好的计划是更高优先级。
The manager's complaint should never be "don't do this because our junior guys don't understand it" -- it should only ever be "do x instead of y whenever feasible because x is easier to read / understand". This also assumes that x and y are equivalent (accept the same input and produce the same result).
经理的抱怨永远不应该是“不要这样做,因为我们的初级人员不理解” - 它应该只是“在可行时做x而不是y,因为x更容易阅读/理解”。这也假设x和y是等价的(接受相同的输入并产生相同的结果)。
I can't stand when managers do that... I've had three different managers bawl me out for using perfectly normal code the way it was designed to work, not because I was doing anything complicated, but rather only because they felt like it was too much effort for the other guys on our team to go RTFM on the language we were using. As a management strategy, that's totally backwards. It's like being the Holy Roman Catholic church and insisting that the laymen are too dumb to be trusted with literacy.
当管理人员这样做时,我无法忍受......我有三个不同的经理让我用完正常的代码按照设计的方式工作,不是因为我做了任何复杂的事情,而是因为他们感觉像对于我们团队中的其他人来说,使用我们使用的语言进行RTFM是太费劲了。作为一种管理策略,这完全是倒退的。这就像是神圣罗马天主教会,坚持认为外行人太愚蠢,不值得信赖。
If you want to know really how ridiculous some of these managers get, try this: I had one manager bawl me out for declaring a variable as a type of "boolean" because he didn't feel the other programmers could handle it. Actually when I asked why, his answer was "because we don't do that here", which is a non-answer, but I interpreted it to mean "dumb it down". They were also berating me for that and similar practices as though it should be obvious that good programming habits were actually "bad" and that I should already know why even though they had never expressed a preferred programming style (either formally or informally). Needless to say, it was a bad job.
如果你想知道其中一些管理者真的有多荒谬,试试这个:我有一位经理让我把一个变量称为“布尔”类型,因为他觉得其他程序员无法处理它。实际上,当我问为什么时,他的答案是“因为我们不在这里做”,这是一个非答案,但我把它解释为“愚蠢”。他们也因为这个和类似的做法而指责我,好像很明显好的编程习惯实际上是“糟糕的”,我应该已经知道为什么即使他们从未表达过一种首选的编程风格(正式或非正式)。不用说,这是一个糟糕的工作。
#25
Make sure you can understand what it does 6 months down the road.
确保你能在6个月后了解它的作用。
When in doubt, COMMENT your code. That's what comments are for.
如有疑问,请注明您的代码。这就是评论的意思。