Last active
July 25, 2019 09:57
-
-
Save ralfw/51a2bf2e65ad5d54ecfca54657082dbf to your computer and use it in GitHub Desktop.
Experimenter: Reduce noise in code by pushing try-catch into the background
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Experimenter | |
{ | |
private static ILogging __log; | |
public static void Init(ILogging log) => __log = log; | |
public static bool Try(string logMsg, Action experiment) | |
{ | |
try | |
{ | |
__log.LogSuccess($"Trying: {logMsg}"); | |
experiment(); | |
__log.LogSuccess($"Tried successfully: {logMsg}"); | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
__log.LogFailure($"Failed: {logMsg}, reason: {ex.Message} ({ex.GetType().Name})"); | |
return false; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Xunit; | |
using Xunit.Abstractions; | |
public class Experimenter_tests | |
{ | |
private readonly ITestOutputHelper _testOutputHelper; | |
public Experimenter_tests(ITestOutputHelper testOutputHelper) | |
{ | |
_testOutputHelper = testOutputHelper; | |
} | |
[Fact] | |
public void Demo_success() | |
{ | |
Experimenter.Init(new XunitLog(_testOutputHelper)); | |
var result = Experimenter.Try("Hello, World!", () => _testOutputHelper.WriteLine("Hello, World!")); | |
Assert.True(result); | |
} | |
[Fact] | |
public void Demo_failure() | |
{ | |
Experimenter.Init(new XunitLog(_testOutputHelper)); | |
var result = Experimenter.Try("Work smoothly", () => throw new InvalidOperationException("Does not work!")); | |
Assert.False(result); | |
} | |
} | |
public class XunitLog : ILogging | |
{ | |
private readonly ITestOutputHelper _output; | |
public XunitLog(ITestOutputHelper output) => _output = output; | |
public void LogSuccess(string message) { | |
_output.WriteLine($"{DateTime.Now:s}: {message}"); | |
} | |
public void LogFailure(string message) { | |
_output.WriteLine($"{DateTime.Now:s}: *** {message}"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ILogging | |
{ | |
void LogSuccess(string message); | |
void LogFailure(string message); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Demo_success(): | |
2019-07-25T12:03:36: Trying: Hello, World! | |
Hello, World! | |
2019-07-25T12:03:36: Tried successfully: Hello, World! | |
Demo_failure(): | |
2019-07-25T12:02:58: Trying: Work smoothly | |
2019-07-25T12:02:58: *** Failed: Work smoothly, reason: Does not work! (InvalidOperationException) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment