Live data from Hacker News

Good refactoring vs. bad refactoring

builder.io

131–140 of 154 posts

Re: Good refactoring vs. bad refactoring

#131
post #75
post #74

Earlier quoted context omitted.

Just having some fun with bikeshedding here: Yeah, that could work but IMO in a big/international system the responsibility should ideally live elsewhere, since: * You may need to determine adulthood for a different jurisdiction than where the person currently resides. Their citizenship may be elsewhere, or you may be running a report that expects "adulthood" to be by some other region's standards, etc. * Sometimes t…

Yes this is just extreme bike-shedding at this point. But none of this is impossible with more OO principles, like interfaces: class User { // Convenience function to check if the user is an adult in their current location boolean isAdult() { return this.location.isAdult(this); } boolean isOfDrinkingAge() { return this.location.isOfDrinkingAge(this); } } interface Location { boolean isAdult(User u); boolean isOfDrink…

Wow, thanks for proving that OOP was a mistake.

Re: Good refactoring vs. bad refactoring

#133

Earlier quoted context omitted.

I've had success with strategies at introducing some abstractions/patterns at my current place(doing this alone for a enterprise SaaS company with 200-ish devs). It's weird that we don't teach these or talk about them in software engineering(AFAIK). I see them being re-invented all the time. To borrow from medicine: First step is to always stop the `stop the hemorrhage`, then clean the wound, and then protect the wou…

> test cases might have owners I hope the owner is the same person that owns the code.

Easier said than done :-)

Tests might cover more code than a single unit owned by different teams, thus end up with multiple owners. Prefer "squads" as the owners rather the individuals.

But just like documentation the ownership might be stale and out of sync. So the idea would be let some reds in SLO dashboard correct them over time. It's not possible to automatically link "tests" to the "code" always.

Re: Good refactoring vs. bad refactoring

#134

Earlier quoted context omitted.

Size reduction usually follows but not necessarily. That is easily measured is an irrelevance. The claim that its a decently predictive proxy I can't buy at the moment, can you try justifying that? Some very terse code can be very hard to understand without (comprehensive) commenting eg. https://en.wikipedia.org/wiki/Fast_inverse_square_root#Overv... Maybe you're right. I've had a drink, I'll think over it in the mor…

If you don't care about performance, we can make that code a lot shorter. float Q_rsqrt(float number) { return 1 / sqrt(number); } The "fast inverse square root" is absolutely 100% all about performance. For it to make sense to use as a counter-example, you need to show alternate code that still meets the contract (the contract being: be as fast as this code), that is longer, and clearer.

I was saying that shorter code is not necessarily more readable. But what you're talking about is optimisation and I'm fine with that, uglier code for higher speed (or whatever). But is that refactoring? I don't think so. I may have led you a bit down the garden path here.

Re: Good refactoring vs. bad refactoring

#135
post #131
post #75

Earlier quoted context omitted.

Yes this is just extreme bike-shedding at this point. But none of this is impossible with more OO principles, like interfaces: class User { // Convenience function to check if the user is an adult in their current location boolean isAdult() { return this.location.isAdult(this); } boolean isOfDrinkingAge() { return this.location.isOfDrinkingAge(this); } } interface Location { boolean isAdult(User u); boolean isOfDrink…

Wow, thanks for proving that OOP was a mistake.

I challenge you to do it better then.

Also from the guidelines:

> Please don't post shallow dismissals, especially of other people's work. A good critical comment teaches us something.

Re: Good refactoring vs. bad refactoring

#136
post #94

Good refactoring should significantly reduce the size or complexity of a codebase. These two metrics are interrelated, but as a general rule if the gzipped size of the codebase (ignoring comments) does not go down, it's probably not a good refactoring.

I'm going to disagree and see what other people say. I don't think that reduction of size is of any relevance. I admit my own refractors tend to make things smaller but it's only a tendency. Most definitely some increase the size overall. I'm currently refactoring a code base – for each item there used to be one class. Each object was examined after creation then a runtime flag was set: Rejected or Accepted. As the c…

> I don't think that reduction of size is of any relevance.

More code equals more bugs. This has been a pretty consistent finding dating back decades. Simpler and smaller code bases will generally have fewer bugs.

https://news.ycombinator.com/item?id=33566329

Re: Good refactoring vs. bad refactoring

#137

Earlier quoted context omitted.

The metaphor, I think, is that your code is a mathematical function, and, to be even more specific and "toy example" about it, let's say it's a polynomial. If the old code was x^2 + x z + y x + y z then you notice that you can express the same polynomial as: (x + y)*(x + z) It's still the same polynomial, but you "separated concerns", turning it into a product of two simpler factors. Similar ideas apply to sets of tu…

If you change the polynomial, you change the order of calculations and hence change behaviour. You might get overflows in case of integers or different precision in case of floats. Nice to learn where the word originates from. Often the meaning of words change over time. E.g. today no horses need involved in bootstrapping.

A polynomial is a mathematical object. There are no integer overflows or float imprecisions in mathematics.

Re: Good refactoring vs. bad refactoring

#138

Earlier quoted context omitted.

You say it’s more readable and I disagree with that! Is this just fashion? Is there a way to settle it other than “I like it better?”

> Is this just fashion? Yes. Readability is a characteristic of the reader , not what is being read . This simple truth seems to be so hard for many people to internalize. My theory as to why is that most programmers never get exposed to drastically different and unfamiliar languages and styles of programming. If they were forced to confront and internalize 2-3 different ways of writing code, they would realize this…

30 plus years ago, it was a common trope in programming circles that C code is unreadable, except to, or perhaps even to the author.

Re: Good refactoring vs. bad refactoring

#139

Earlier quoted context omitted.

That's because they are. Functional code is more readable. And if you look back, basically all advances in programming languages have been about "making stuff more readable". Thus, for loops (for this usage) are "old".

> Functional code is more readable. There is no way that name: R.pipe(R.prop('name'), R.toUpper), age: R.prop('age'), isAdult: R.always(true) is more readable than name: user.name.toUpperCase(), age: user.age, isAdult: true

But those are both functional, or can easily be.

Re: Good refactoring vs. bad refactoring

#140

Earlier quoted context omitted.

> test cases might have owners I hope the owner is the same person that owns the code.

Easier said than done :-) Tests might cover more code than a single unit owned by different teams, thus end up with multiple owners. Prefer "squads" as the owners rather the individuals. But just like documentation the ownership might be stale and out of sync. So the idea would be let some reds in SLO dashboard correct them over time. It's not possible to automatically link "tests" to the "code" always.

End-to-end tests might get tricky. But unit tests should be owned by the person/team/squad that owns the unit.

And unit tests should never break/be red. If the code needs to changed, the test needs to be changed at the same time.

End-to-end tests can be flaky. Those probably shouldn't prevent deployments and can be red for awhile. Should probably manually confirm if the test is acting up, there's a change in behavior, or if something is legitimately broken before ignoring them though.

Post reply on HN