8 ms·
> Mantras like "methods should be shorter than 15 lines of code" or "classes should be small" turned out to be somewhat wrong. These hard rules may be useful w
by Aurornis 2y ago
> Mantras like "methods should be shorter than 15 lines of code" or "classes should be small" turned out to be somewhat wrong.
These hard rules may be useful when trying to instill good habits in juniors, but they become counterproductive when you start constraining experienced developers with arbitrary limits.
It’s really bad when you join a team that enforces rules like this. It almost always comes from a lead or manager who reads too many business books and then cargo cults those books on to the team.
- deergomoo 2y agoSame deal with DRY, the principle is obviously correct but people can take it too literally. It's so easy to get yourself in a huge mess trying to extract out two or three bits of code that look pretty similar but aren't really used in the same context.
- skeeter2020 2y agoThe problem with DRY and generic rules around size, etc. really seems to be figuring out the boundaries, and that's tough to get right, even for experienced devs, plus very contextual. If you need to open up a dozen files to make a small change you're overwhelmed, but then if you need to wade through a big function or change code in 2 places you're just as frustrated.
- quesomaster9000 2y agoThis is the bane of my existence at the moment after ~20 years into my career, and it frustrates me when I run into these situations when trying to get certain people to review pull requests (because I'm being kind, and adhering to a process, and there is really valuable feedback at times). But on the whole it's like being dragged back down to working at a snails pace. - Can't refactor code because it changes too many files and too many lines. - Can't commit large chunks of well tested code that 'Does feature X', because... too many files and too many lines. - Have to split everything down into a long sequence of consecutive pull requests that become a process nightmare in its own right - The documentation comments gets nitpicked to death with mostly useless comments about not having periods at the ends of lines - End up having to explain every little detail throughout the function as if I'm trying to produce a lecture, things like `/* loop until not valid */ while (!valid) {...` seemed to be what they wanted, but to me it made no sense what so ever to even have that comment This can turn a ~50 line function into a 3 day process, a couple of hundred lines into a multi-week process, and a thousand or two line refactor (while retaining full test coverage) into a multi-month process. At one point I just downed tools and quit the company, the absurdity of it all completely drained my motivation, killed progress & flow and lead to features not being shipped. Meanwhile with projects I'm managing I have a fairly good handle on 'ok this code isnt the best, but it does work, it is fairly well tested, and it will be shipped as the beta', so as to not be obstinate.
- NotBoolean 2y agoI don’t have your experience but I personally think some of this feedback can be warranted. > Can't refactor code because it changes too many files and too many lines. This really depends on the change. If you are just doing a mass rename like updating a function signature, fair enough but if you changing a lot of code it’s very hard to review it. Lots of cognitive load on the reviewer who might not have the same understanding of codebase as you. > Can't commit large chunks of well tested code that 'Does feature X', because... too many files and too many lines. Same as the above, reviewing is hard and more code means people get lazy and bored. Just because the code is tested doesn’t mean it’s correct, just means it passes tests. > Have to split everything down into a long sequence of consecutive pull requests that become a process nightmare in its own right This is planning issue, if you correctly size tickets you aren’t going to end up in messy situations as often. > The documentation comments gets nitpicked to death with mostly useless comments about not having periods at the ends of lines Having correctly written documentation is important. It can live a long time and if you don’t keep an eye on it can becomes a mess. Ideally you should review it before you submitting it to avoid these issues. > End up having to explain every little detail throughout the function as if I'm trying to produce a lecture, things like `/* loop until not valid */ while (!valid) {...` seemed to be what they wanted, but to me it made no sense what so ever to even have that comment I definitely agree with this one. Superfluous comments are a waste of time. Obviously this is just my option and you can take things too far but I do think that making code reviewable (by making it small) goes a long way. No one wants to review 1000s lines of code at once. It’s too much to process and people will do a worse job. Happy to hear your thoughts.
- quesomaster9000 2y agoI do object to the notion of something being a planning issue when you're talking about a days worth of work. Implement X, needs Y and Z, ok that was straightforward, also discovered U and V on the way and sorted that out, here's a pull request that neatly wraps it up. Which subsequently gets turned into a multi-week process, going back & forth almost every day, meaning I can't move on to the next thing, meanwhile I'm looking at the cumulative hourly wages of everybody involved and the cost is... shocking. Death by process IHMO.
- nradov 2y agoIt's the same with writing. The best authors occasionally break the rules of grammar and spelling in order to achieve a specific effect. But you have to learn the rules first, and break them only intentionally rather than accidentally. Otherwise your writing ends up as sloppy crap. (Of course some organizations have coding conventions that are just stupid, but that's a separate issue.)
- psychoslave 2y agoIf a function is longer than what I can display on a single screen, it better has to be argumented with very exceptional relevant requirements, which is just as straight forward to judge for anyone with a bit of experience.
- raincole 2y agoI've seen a book promoting the idea that methods should not be longer than 5 lines. Of course now I know these ridiculous statements are from people hardly wrote any code in their lives, but if I'd read them at 18 I would have been totally misled.
- quesomaster9000 2y agoWeirdly if you do break everything down into purely functional components it's entirely possible to uncompromisingly make every concept a few lines of code at most, and you will end up with some extremely elegant solutions this way. You wouldn't be misled at all, only that the path you'd go down is an entirely different one to what you expected it to be.
- arzke 2y ago> I know these ridiculous statements are from people hardly wrote any code in their lives Some people who actually wrote a decent amount of code in their lives are sharing that opinion, so your comment just sounds like an ad-hominem attack.
- wholinator2 2y agoI disagree that it's an attack, I've also never heard anyone say methods should be less than 5 lines. 5 lines is an insane limit, 15 is much more reasonable. This kind of enforcement reeks to me of unnecessarily "one-lining" complicated statements into completely unreadable garbage. I mean seriously though, 5 lines? Why not 4, or 3, or 6? 15 lines of well thought out code is infinitely preferable to 3 different 5-line monstrosities. Who(m'st've) among us that actually writes code would preach such a guideline, and can i please see their code for reference. Maybe they are just better than us, i still don't think that makes it a reasonable general rule. And i disagree that calling that out as crazy counts as a personal ad-hominem attack against this nebulous entity
- dboreham 2y agoYou're confusing this with a software development process problem. It's really just good old fashioned psychological abuse.
- charles_f 2y agoI like to call these smells, not rules. They're an indication that something might be wrong because you've repeated code, or because your method is too long, or because you have too many parameters. But it might also be a false positive because in this instance it was acceptable to repeat code or have a long method or have many parameters. Sometimes food smells because it turned bad, and sometimes it's smelly because it's cheese.
- dennis_jeeves2 2y ago>It almost always comes from a lead or manager who reads too many business books and then cargo cults those books on to the team. Worse, they behave as though they have profound insights, and put themselves on an intellectually elevated pedestal, which the rest of their ordinary team mortals cannot achieve.
- zahlman 2y agoI'm an experienced developer and I enforce these kinds of rules upon myself without giving it much thought, and I very much prefer the results.
- ozim 2y agoIn my experience it usually devs that do that to themselves after reading stuff on the internet and thinking “I want to be a professional and I want to show it to everyone.” Then rules stay and new people just continue with same silly rules instead of thinking if those are really that useful.
- diekhans 2y agoHard rules are the problem. There is a lot of "it depends." After over 40 years of programming, I continue to reduce the size of functions and find it easier to write and understand when I return to them. Ten lines are now a personal guideline. However, a linear function with only tiny loops or conditionals can be easily understood when hundreds of lines are long, but not so much with nested conditionals and loops, where there is natural decomposition into functions. I observed that the same guidelines became rules problems when test coverage became popular. They soon became metrics rather than tools to think about code and tests. People became reluctant to add sanity check code for things that could should never happen because it brought down code coverage.
- asdff 2y agoThere are certainly functions written too cleverly to be apparent how they manage to work at all in a few lines. By my own hand six months ago sometimes. The solution is an unsexy one but always works: write a books worth of comments near that function code that explains absolutely everything and why it was done.
- asdff 2y agoDoing things the right way always introduces a shackle to your ankle. Oh am I to package my functions as discrete packages I call via library name carefully crafted to install into some specific folder structure that I now have to learn and not make mistakes with. Or I can do it “improperly” and just write a function and start using it immediately. Not everything has to be designed like some standardized mass produced part ready to drop into anything made in the last 40 years. And what is crazy is that even things written to that standard aren’t even compatible and might have very specific dependencies themselves.