TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Tips for refactoring your code with no pain

9 pointsby CedricTeytonover 2 years ago

2 comments

DivisionSolover 2 years ago
At the risk of getting dinged, I have a hard time thinking this is a real article.<p>Example 1: Changing `s` to `displayScore` doesn&#x27;t fix the problem the code is using `p1`, `p2`, `p1N`, `p2N` AND defining a new `p`. WITH multiple ternaries and 3 mid-function returns.<p>Example 2: Reasonable, but now you have two functions with similar signatures. Reducing the number of indentation is always good.<p>Example 3: Creates ANOTHER layer of indentation. Without add&#x27;l context the code looks like it could be reduced to a series of inverted conditions with happy&#x2F;golden-path style sequential if-s instead of 5 indentation deep.<p>Example 4: Pet peeve and chided by a lot of linters. Empty blocks are generally frowned upon. (You say: &quot;If not equals DO&quot; not &quot;If equals don&#x27;t do anything ELSE&quot;)<p>Example 5: Good in the context of testing, annoying in that it is a function with a single line. You&#x27;d probably be better off stubbing the User class all together. I mean you&#x27;re already providing a User-type object.<p>Example 6: Great, 10&#x2F;10 love it. Would remove the {} and just toss the Throws on the if-statement line itself but that&#x27;s personal taste.<p>Example 7: Not awful, but I like to front-load my variable declarations so you can get error-handling out of the way at the start of the function then use those variables to do the computation later. More up front about saying: &quot;Here are the items I will use later in this function&quot;<p>Example 8: Probably not a simple refactor. If you aren&#x27;t going to update all the locations in which an &quot;Amount&quot;&#x2F;&quot;Currency&quot;, you&#x27;re creating an artisanal class for a single use.<p>Why am I code reviewing this article? Shoo.
评论 #34365576 未加载
kevsimover 2 years ago
I&#x27;ve never seen tip #4 (Rewrite negative conditions) or anything like it before. I can&#x27;t see how that improves readability at all. If I saw it in a code review I&#x27;d ask the developer if they forgot to fill in the body of the if statement.
评论 #34365532 未加载