My focus is on making software, coaching Agile/Scrum teams, and being a husband and father--definitely not on blogging--so please do not expect very frequent posts here.

Sunday, December 13, 2020

C# ascendant: Nuget packages to try (and some that are no-brainers)

 

I feel like with the advent of Blazor, the power, usefulness, and ubiquity of C# and .NET in general has kicked up a notch.  At the same time, the growth in functional programming (LINQ in C#, F#, Clojure, Scala) and separately, dynamic typing (Javascript, Python), and fluent APIs/DSLs all give us better ways of both thinking about and writing our code--we can be more expressive and simpler while being less error-prone.  C# is becoming the best of all worlds as these paradigms become nearly fully enabled in C# with the variety of NuGet libraries enabled.

​Here are some interesting NuGet packages that I haven't used yet, but might take advantage of soon.

  • FluentValidation - On my current work project, we just wrote a bunch of complicated (though not complex) validation code, all custom, and mostly with nested if statements.  I wonder if this would be more readable and intuitive using a library like this?
  • Humanizer - So much of our code is just variations on mapping identifiers, timestamps, or logic into human-readable forms.  This package provides a ton of methods for doing this in a simpler and more intuitive way.
  • FluentMigrator - So far, I've never worked on a project that has successfully kept its database schema in code, other than by retaining SQL scripts.  Could this library help?
  • Polly - Retries, timeouts, and more - I've often written custom methods to deal with various service accessing needs.  Next time I feel like I should, I'll have a look at Polly.
  • OneOf - I see often where I need to return two different types--one for success and one for an expected failure or "nothing to do" case.  Functional languages have handled this better than OO languages from the start, but maybe this library could bring me the ability to return OneOf<bool, FailureMessage> or any other union that might be useful.
  • Dynamic LINQ - Could it help or hurt to be able to dynamically build your LINQ logic depending on runtime state?  I've often wanted to, but understood why C# can't do it by default.  Could this package enable such power? Or would it do more harm than good?

These are of course in addition to the ones I've already used repeatedly--if you don't know them well, take a look for sure:

  • Dapper - EF is great but I still think it is overfkill for most needs, especially if your team is proficient in writing SQL (and they should be).  I cannot bear the thought of spending an hour to make EF do what I want when I could just write my own SQL and be done with it.  Dapper does the part I want (Mapping rows to objects) and leaves out the part I don't (Mapping code to queries).
  • FluentAssertions - It is so much better to write intuitive assertions in my tests than to even take the 20 seconds to write it with traditional MSTest assertion syntax. Goodbye Assert.IsTrue, hello result.Should.
  • Moq - There seems to be nothing better for detouring/mocking in your automated tests.  Even though .Setup and It.IsAny<T> get tiresome, I haven't found anything better.
  • Swashbuckle with Swagger and SwaggerUI - This trio gives my API documentation with hardly any effort.  Nice!
  • Serilog - We finally have a winner in the logging library fight.  Everyone loves it; you will too.  Ditch Log4net, nLogger, or anything else you might have struggled with before.

Sunday, April 12, 2020

See me at the all-online Minnesota Developer's Conference, May 4, 2020

On May 4, 2020, I gave a talk on Practical Refactoring in C# at the Minnesota Developer's Conference, live online. The all-day virtual conference was only $20 and has some of the best speakers around on C#, Blazor, Javascript, Azure, and soft skills for developers.

My session is all about how to make your C# code better and to do it more routinely and with high-confidence. Too often, we are afraid to refactor because we might break something--but we err on the side of false caution, because our dirty, confusing code causes more bugs than a refactoring would. I'll show my favorite refactorings and demonstrate how to get into the habit of always improving.

I also am excited to see my colleagues Kamran Ayub, Erik Onarheim, and Elsa Vezino present there, and I always love more Benko-tips and wisdom from Ward. Thanks a ton to Jason Erdahl and his team at ILM for moving this thing online rather than cancelling--I've never been as interested in a live online conference before!

Video: Practical Refactoring in C#

Slides

Demo Code
My favorite refactorings:
  • Tool-assisted:
    • Rename
    • Extract method
    • Move
    • Remove unused usings
    • Reduce nesting: Invert if; combine singly-nested Ifs;
    • Extract interface
    • Restrict access modifiers
    • Code against nonexisting members then generate
     
  • Manual:
    • Simplify boolean logic
    • Early return
    • Reduce newline usage
    • Convert If-else to ternary
    • Eliminate/simplify loops with LINQ
    • Iterate instead of repeat code (try lambdas or private classes)
    • Replace Start/Stop methods with an IDisposable
    • 7 steps to add dependency injection
Other sources:

Sunday, February 2, 2020

Presenting at Twin Cities .NET User Group, Thu Feb 6 at 5:00

On Thursday, February 6, I will be presenting at the Twin Cities .NET User Group on Practical Refactoring With C#. It is at 5:00 PM at General Mills in Golden Valley, MN. Please introduce yourself afterward if I haven't met you before. Hope to see you there.