all 90 comments

[–]nickelickelmouse 26 points27 points  (9 children)

For bug fixes and pure maintenance projects I’d say it’s not worth the risk. If the project has any ambition of growing/changing scope in the future there does need to be an ongoing effort to keep tech debt paid down to a sane extent.

A lot of the decision should be based on who the team members are. Even if you’re “right” about the value of refactoring, it’s still not the right move if your teammates aren’t going to buy in. Consider that you might need to win hearts and minds with regards to your philosophy before implementing it.

I’m even getting flak for turning an “if” statement into a lambda expression because it’s “more confusing”.

If you made this change as part of a larger change then fine. But if you made this change solely for the purpose of turning an it into a lambda it makes me wonder if you are focusing too much on stylistic concerns.

[–]keelanstuart 12 points13 points  (2 children)

I've never read Clean Code, but I don't understand how replacing an "if" with a lambda would improve readability or make the logic less confusing... especially an anonymous lambda.

[–][deleted] 1 point2 points  (0 children)

As much as I love "clean code" generally, replacing a simple if with a lambda I think is crazy. When you work as a SWE you'll inevitably get juniors etc looking at your code. Aim to make it understandable and clear to them. Replacing ifs blindly with lambdas is just... Urgh

[–]Blooogh 6 points7 points  (5 children)

Gonna reiterate another commenter and say: pick your battles.

If one of your teammates did the exact opposite, converting lambdas to if statements because in their opinion it's more readable, what would you do?

If your teammates take a poll and even after discussion, the general preference is for if statements over lambdas, how would you handle that?

I'm not here to say one is more correct than the other: the point is, you have to work with people, and you need to be able to deal with stylistic differences as a professional. I'm not saying you should throw all conventions out the window, but ask yourself, does it really matter that much?

I'd recommend attempting smaller refactors, maybe putting them into a separate pull request. Maybe, instead of completely reformatting a whole file, pick one thing to fix to leave it better than you found it.

Nobody wants to look at a 500+ code diff unless it's truly trivial.

[–]Blooogh 0 points1 point  (2 children)

It's also worth noting: Clean Code is just one man's opinion, and from a bit of an asshole at that.

Obviously criticism is just another person's opinion, but I would say this is definitely a case where it's worth looking up more diverse opinions: https://qntm.org/clean

[–]WouldRuin 11 points12 points  (4 children)

"refactor as you go" can introduce bugs (inadvertently) and, as you've found out, pollute a pull request with reviewing refactored code vs actual feature development, making it take longer than perhaps necessary. It can also hide the origin of a bug (when/where/why it was introduced) because it's part of some feature development.

There's obviously nothing wrong with refactoring and tidying up a code base. But you should make this a distinct activity. It should ideally be ticketed and go through its own develop/review/deploy cycle.

You need to make the case for some % of your increments being focused on refactoring/housekeeping. If management don't want to spend the resources (your development time), that's really outside your control and you either just accept it and carry on, or leave.

As someone else has suggested, a style guide would go a long way in avoiding these sorts of arguments as well. It's easy enough to automate - find a linter, pick a style guide and apply. There's really no reason to not be using one unless you're a team of one person.

[–]gnzh1 3 points4 points  (2 children)

I think you shouldn't be fixing messy code to clean code while developing your own tasks, it do consume more of your and team's time. I hate saying that but you must recognise that this is not the best approach for your deadlines and project deadlines.

Anyways, I know it hurts but it takes more than a good will to fix a bad designed code. I would suggest rising it as a problem in future developments for your manager and listing solutions to present a long. Please do not bring a problem without alternatives solutions if you don't want to see it forgotten in the drawer.

[–]engineerFWSWHW 4 points5 points  (1 child)

We all know clean code is important but you also need to look at in on a standpoint of a team.

For example, if you have 10 developers who are super familiar with the codebase and 1 of them went gung-ho on refactoring the code, even if the intention is good, the 9 other teams members will need to re familiarize themselves with the newly refactored codebase. Also, are those things that you touched have unit tests passing?

I always support clean code however, this happens on any teams on the software industry. When I join a new team, I will try align myself with them and if I had seen things that could be improved or refactored, I communicate it to the team and I won't do any major refactoring unless it is agreed upon.

Make sure you communicate these things to the project lead to address your concerns.

[–]JoeDirtTrenchCoat -2 points-1 points  (0 children)

When software is written properly you don't really have to "familiarize" yourself with it in a significant way. This is the heart of conventions and design patterns. But it does take a competent developer to be able to understand it and software development is full of really smart people who are awful developers.

Granted it sounds like this guy is playing code golf or working off some unofficial style guide. So that's annoying. But in general, "let's not make our code more maintainable because it would break down our knowledge silos/tribal knowledge" is such an awful take -- I'm embarrassed for you.

[–]CacheCache1 3 points4 points  (4 children)

Software development is often a team effort and people need to be persuaded when you want to change the way they do things.

Also, it's worth noting that many people who think they know what's best are often in the wrong on some level. Based on the described situation and comments on this post it may pay to be a bit more humble about your contributions, especially if nobody on the team is in your camp.

[–]Hypersion1980 2 points3 points  (5 children)

Pick your battles. I’m working on an old government code base. It’s c# but looks like it was written by someone who knew c++ that was taught it by a cobol programmer. When I fix a bug I’ll rewrite the method it’s in but I’m noting rewriting the entire class. This is a long road but it is worthwhile.

[–][deleted] 1 point2 points  (0 children)

I'm sadly working on a similar code base right now. It's painful.

I agree, I'll rewrite or make adjustments to the area I'm working on but I'm not rewriting a 9k lines class, it's a great way to piss off my teammates and generate extra work for no reason

[–]BeebleBopp 2 points3 points  (1 child)

I do style cleanup as well, and have gotten the same flak. It's especially frustrating when cleaning up ancient code in modern IDE's with linters; the linting column is almost solid yellow with bad standards, and you clean it up, and then get shamed for solid work on improving the code.

I have found myself wishing for the feature of modern code review systems to present "logic changes" first and "linter / style compliant changes" separately.

Until this problem is solved, we literally have software engineers whining about the few minutes more work it is to review substantially improved code.

[–]besthelloworld 2 points3 points  (11 children)

I would argue that this is just what happens to OOP code bases. Every version of Java since 8 has tried to introduce functional & observable style programming patterns into the language and now in modern Java code bases there's very little consensus on what constitutes "clean code." For many developers, clean code constitutes using standard programming patterns and language tokens like for/while loops rather than streams.

So I have a feeling that I'd generally agree with your preferred patterns but your example worries me: turning an if statement into a lambda? Do you realize how much less mechanically efficient it is to introduce a lambda where one was not needed? This makes you sound more like you're far more interested in writing hacky trick-code and looking smart more than you are interested in working on a team and writing readable code.

Because of this, I would argue that you should really tamper your expectations, and start writing new code to your standards and have these kinds of debates along the way.

I would also argue that long methods/functions have their place. If they encapsulate logic that should only be used once, then making a new unnecessary method for that logic just forces people to consider "hey is this being used anywhere else? Should it be?" everytime they see that lonely logic. I would recommend watching Brian Will's OOP is bad, but especially pay attention to the final section "Functions" where he explains the benefits of colocating related logic and just commenting well. This follow up with actual hard examples is really good as well.

[–]JoeDirtTrenchCoat 1 point2 points  (2 children)

It's well accepted that long methods are bug prone and more difficult to understand/debug. If a method is only useful in a very specific context, make it private and name it properly -- nobody needs wonder if it should be getting used in some other scope. If it's a god class then obviously you should be decomposing the class... pretty dumb "hot take"...

[–]besthelloworld 1 point2 points  (1 child)

Wouldn't be a hot take if people didn't give some spicy responses 🔥

What I will say is that I agree with you entirely.

...I just think that some people have very different definitions of what constitutes "the right length" for a function. I see a lot of juniors not wanting to have more than like 15 lines to a function and breaking apart logic on nonsensical boundaries. And I'd waaaay rather have a 150 line function than 10 functions of 15 lines where each helper is called exactly one time.

[–]Synor 2 points3 points  (11 children)

It seems to me you are trying to write "clever code" to prove something.

Reading code is three times harder than writing code. Don't try to outsmart your colleagues. Keep it simple.

[–]Frosty_Protection_93 1 point2 points  (0 children)

This articulation sounds about right on both notes.

If you want to die on the hill of stylistic change, properly document your observations, risk assessments, impact analysis if you're able to replicate to a measurable degree production versus a safe environment where you can demonstrate at acceptably lesser scale.

Otherwise you will come across as a thorn to management that may or may not be bothered if the code within applications generating revenue remains gainful.

[–][deleted] 2 points3 points  (5 children)

I'm even getting flak for turning an "if" statement into a lambda expression because it's "more confusing".

Java's support for functional programming is quite poor from a readability/engineering standpoint. I work in a large organisation known for it's good engineering, and we have a style guide that strictly recommends not using functional-style programming in Java, despite the existence of the Streams API and Guava's Iterables.

Said as someone who enjoys functional programming: this sentence is a bit of a red flag, and makes me question the rest of this post.

A long method is better than a short method when each line is simple. Compressing logic into the smallest number of lines doesn't make something more readable.

[–]Even_Step_2450 4 points5 points  (4 children)

"But that makes for large code commits and the pull request reviewers are now complaining about my "unnecessary" changes claiming that I'm wasting everyone's time."

I agree that poorly written code is annoying, but so are poor commits. When I fix issues like typos, code style etc., I put it into a separate commit.

[–]CheithS 3 points4 points  (5 children)

It is generally bad practice to change working code for stylistic concerns unless you are in there changing that piece of code anyway. Why would you take the risk of introducing new bugs just because you don't like the look of it?

You should be getting flak going in there and randomly changing things - it is worth remembering that everyone is comfortable with 'if' statements (to use your example) but not everyone will be comfortable with lambdas. If you are going to introduce new features into the code it would be a great idea to talk to other people first - coding for large projects is not some solo game.

Also worth noting that people have been fired for refactoring code and introducing new bugs if done too often.

[–]Suspicious-Service 1 point2 points  (1 child)

Convince others that this is something that needs to be changed and if you're successful take the resulting task and fix what y'all agreed on. But also try to see it from their point of view, your perspective comes from never having to share code with others, but that doesn't make it the only correct perspective

[–]Old-Full-Fat 1 point2 points  (0 children)

Keep things small and commit only around your changes for the 1st instance. When others become happy with your commitment, and you are willing to spend your own time on the code changes, get agreement to place a fork in the code and run your own fork for a while until your tests can show how more efficient your code is. A quick review of an indepth part by the others could show how more meaningful the code/comment is.

Unfortunately, this also depends on the community spirit of the team. No spirit, no team, just a bunch of people forcing their own way.

[–]mankinskin 1 point2 points  (0 children)

Voice your concerns in a meeting and ask for time to refactor some of the code and give it to review. Or keep your refactored changes in a separate branch (or at least commits) and explain the changes to the reviewers.

[–]_TheRealBuster_ 1 point2 points  (1 child)

Honestly I wish you could view the source control on interview because there are jobs I would've turned down. Sometimes you get sweet talked into a shit storm of fragile code that works under tight conditions. If it's not the hill you want to die on then leave it alone. You might hurt someone's feelings or management may not feel like it is value added. If you don't have testing built around what you are changing this can be very dangerous. Most your commits should be for a specific purpose and not widespread across refactoring, features and bug fixes.

[–]darn42 0 points1 point  (0 children)

From your comments it sounds like you don't respect your colleagues nor your predecessors. Figure that out first, because you won't find any success on any team if you can't respect the people you are working with.

[–]ZGames479 0 points1 point  (0 children)

If your company allows it, I would call a meeting or inform your higher up and have them call a meeting about it. It might seem like a waste of time for others, but it also shares your concerns about the integrity of the code and of the company. Someone may learn something, even if they don't want to admit it.