question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Refactoring for the future

See original GitHub issue

The existing OnSuccess methods conflate a mix of “map” and “tap” behaviours. With @golavr’s addition of useful new Tap methods, several problems with the library are starting to coalesce:

  • New users may find it harder to know when to use OnSuccess vs Tap. Some of the signatures overlap, with duplicate logic (i.e. the existing tap-like OnSuccess signatures).
  • Map might seem redundant to a new user - it just passes through to OnSuccess. But semantically it is perhaps more useful.
  • It can be hard to infer intent/behaviour, because OnSuccess is trying to do two things: Map to new results, and Tap that feeds forward the starting result unchanged, having executed a side Action.

(See also discussion on #134.)

I propose the following solutions:

  1. Refactor OnSuccess into Map and Tap. Map methods always return new results (or whatever the user chooses to return from the Func parameters that receive the current value/result). Conversely, Tap methods return the starting result unchanged, the action that gets called therefore having no side effects in the chain itself.

  2. Mark OnSuccess as [Obsolete] to encourage uptake of Map and Tap instead. (If and when there is appetite for a version bump to v2, OnSuccess could be removed to keep the codebase lean, but this is not pressing.)

  3. With a move away from OnSuccess, OnBoth makes a lot less sense semantically. I propose refactoring it to Finally. This encapsulates the sense that these actions occur “no matter what”. Also, as this method doesn’t return a Result, it can only ever appear last in the chain. 😃

I have created a PR which incorporates these changes, plus some other fairly significant refactoring that hopefully will make the library easier to maintain going forward (e.g. breaking up the code into separate files).

This incorporates @golavr’s Tap PR, and other than the [Obsolete] attributes and some improvements to the Match signatures, I tried to avoid any other potentially breaking changes.

The examples are updated with the new semantics swapped in to give an idea.

Look forward to your thoughts.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
space-aliencommented, Sep 2, 2019

All done.

0reactions
space-aliencommented, Mar 3, 2021

Have a look at the home page of this repo, the Examples project, the notes on issue #152 (needs a bit of updating), and also if you have existing code that uses OnSuccess, I believe you should get warnings telling you which new methods to use when you upgrade the package.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Refactoring - Martin Fowler
The guide to how to transform code with safe and rapid process, vital to keeping it cheap and easy to modify for future...
Read more >
The Future of Code Refactoring: Insights from GitHub Copilot
The future of code refactoring is set to undergo a significant transformation with the introduction of GitHub Copilot, an AI-powered code ...
Read more >
Refactoring vs Rewrite: What We Have Learned Until 2023
Refactoring helps software developers to restructure the existing code so they can work easier with it in the future. This can be an...
Read more >
How to create a culture of continuously refactoring code?
Refactoring is a controlled technique for improving the design of an existing code base. All refactoring should be regression free and safe to ......
Read more >
(PDF) Refactoring: Current Research and Future Trends
PDF | In this paper we provide an extensive overview of existing research in the field of software restructuring and refactoring, from a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found