My first coding job was far from being like a Silicon Valley job at a startup.
I didn’t have ping-pong tables or slides to go between offices.
It was, by all means, a boring job at a local non-tech company. There’s nothing wrong with a boring job if that’s what you want. But it taught me valuable lessons about life, coding, and money. Here they are.
You Are Not Your Code
My first job was with a small team. One team member took care of one project, from start to end, wearing multiple hats.
Things got complicated when we had to work together and inherited the code from a coworker. It was messy code, to say the least. He put all actions and logic inside event handlers. Those were the days of WinForms applications: Drag and drop a button, double-click, and connect to the database right there.
We had to fix his issues and rewrite his code. Nobody wanted to do that. And I started to judge him because of his code. “What if he does everything else the same way he codes?”
Don’t judge someone by their code. Don’t take it personally either. You could miss professional connections or friendships by judging people for their code.
Assume everyone does their best with the resources they have at hand.
In the future, someone will inherit your code and say “What a crappy code. Who wrote this?”
There will always be different opinions and better ways of doing things. And even you will think of better ways to do your current work!
Coding Is Not the Only Thing
In my first days, I only wanted to code.
I had just finished reading the Clean Code and wanted to refactor everything around me. I didn’t want to attend meetings, answer phone calls, or even reply to emails. I only wanted to code. That’s what I was paid for, right?
More than once, my boss called me to his office and I arrived minutes late because I was coding in my cubicle. I don’t know why I didn’t get in trouble for that.
Software engineering is about collaboration.
You won’t be locked in a basement coding. You must talk to clients, conduct meetings, agree on estimations, and ask for help.
I had to learn there’s more than just typing symbols in text files.
Live With Half of Your Salary
That’s the best life advice I’ve ever received for free.
My cubicle was next to the coffee machine, in a corner that had once been a bathroom.
One day, another coworker, a “veteran of many battles,” came over to have his coffee. And he said something like this:
“Hey Cesar, here’s a free piece of advice. Now that you can, imagine you only make half your salary and save the other half. Sooner than later, you can buy your own apartment.”
Years later, while reading money books, I found similar advice. And it reminded me of that conversation.
I’ve followed that advice, but not exactly. I’ve saved less than half of my salary.
Today, I’d rephrase it like this: “Imagine you make half of your salary, save, and invest the other half.”
You Don’t Have To Feel Miserable
I thought talent and good work were shortcuts to breaking the rules of the corporate world. I was soooo wrong! The Matrix is real!
Endless meetings, office politics, and a fixed schedule.
It all started to take its toll on me.
There were days when I felt I was leaving my life behind while sitting at a computer. I felt demotivated and disengaged. I was craving variety and change. I didn’t know there was a term for that: burnout.
Always have an exit plan.
Change jobs when you wake up and can’t get out of bed to work.
Find a way to motivate yourself: start a side project, learn a new tech stack, or discover a new way of doing your work. Or simply update your CV and LinkedIn profile and move on.
Parting Thoughts
It’s been more than 10 years since my first job. I’m only grateful for it. Somebody took a leap of faith with me and gave me a chance when I had 0 hours of flight time.
I took these lessons to my next job. And every time I can, I give the same money advice my coworker gave me: save and invest half of your salary.
Often what we value the most from past jobs is not the money, but the friendships and connections. From time to time, I meet with coworkers I met at this first job for coffee.
C# is getting more and more functional with every release.
I don’t mean functional in the sense of being practical or not. I mean C# is borrowing features from functional languages, like records from F#, while staying a multi-paradigm language.
Yes, C# will never be a fully functional language. And that’s by design.
But, it still misses one key feature from functional languages: Discriminated Unions.
Discriminated Unions Are a Closed Hierarchy of Classes
Think of discriminated unions like enums where each member could be an object of a different, but somehow related, type.
Let me show you an example where a discriminated union makes sense. At a past job, while working with a reservation management software, hotels wanted to charge a deposit before the guests arrived. They wanted to charge some nights, a fixed amount, or a percentage of room charges, right after getting the reservation or before the arrival date.
Here’s how to represent that requirement with a discriminated union:
publicrecordDeposit(ChargeCharge,DelayDelay);// Of course, I'm making this up...// This is invalid syntaxpublicunionrecordCharge// <--{NightCountNights;FixedAmountAmount;PercentagePercentage;}publicrecordNightCount(intCount);publicrecordFixedAmount(decimalAmount,CurrencyCodeCurrency);publicenumCurrencyCode{USD,Euro,MXN,COP}publicrecordPercentage(decimalAmount);publicrecordDelay(intDays,DelayTypeDelayType);publicenumDelayType{BeforeCheckin,AfterReservation}
Here, the Charge class would be a discriminated union. It could only hold one of three values: NightCount, FixedAmount, or Percentage.
You might be thinking it looks like a regular hierarchy of classes. And that’s right.
But, the missing piece is that discriminated unions are exhaustive. We don’t need a default case when using a discriminated union inside a switch. And, if we add a new member to the discriminated union, the compiler will warn us about where we should handle the new member.
Discriminated unions are helpful to express restrictions, constraints, and business rules when working with Domain Driven Design. In fact, using types to represent business rules is the main idea of the book Domain Modeling Made Functional.
For example, discriminated unions are a good alternative when moving I/O to the edges of our apps and just returning decisions from our domains.
I told you that C# is borrowing some new features from functional languages. Well, if you’re curious, this is how our example will look like in F# using real discriminated unions:
typeDeposit={Charge:Charge;Delay:Delay;}typeCharge=// <-- Look, ma! A discriminated union|NightCountofint|FixedAmountofdecimal*CurrencyCode|PercentageofdecimaltypeCurrencyCode=|USD|Euro|MXN|COPtypeDelay=|BeforeCheckinofint|AfterReservationofint
All credits to Phind, “an intelligent answer engine for developers,” for writing that F# example.
A Proposal for a Union Keyword
It seems the C# language team is considering fully supporting discriminated unions.
In the official C# GitHub repository, there’s a recent proposal for discriminated unions. The goal is to create a new type to “store one of a limited number of other types in the same place” and let C# do all the heavy work to handle variables of that new type, including checking for exhaustiveness.
The proposal suggests introducing a new union type for classes and structs. Our example using the union type will look like this:
publicunionCharge// ^^^^^{NightCount(intCount);FixedAmount(decimalAmount,CurrencyCodeCurrency);Percentage(decimalAmount);}// This is how to instantiate a union typeChargechargeOneNight=newNightCount(1);// <--varoneNightBeforeCheckin=newDeposit(chargeOneNight// ^^^^^newDelay(1,DelayType.BeforeCheckin));// This is how to use it inside a switchvaramountToCharge=chargeswitch{NightCountn=>DoSomethingHere(n),FixedAmounta=>DoSomethingElseHere(a),Percentagep=>DoSomethingDifferentHere(p)// No need to declare a default case here...}
The new union type will support pattern matching and deconstruction too.
Under the hood, the union type will get translated to a hierarchy of classes, with the base class annotated with a new [Closed] attribute. And, if the default union type doesn’t meet our needs, we can use that new attribute directly.
Two Alternatives While We Wait
The union type is still under discussion. We’ll have to wait.
In the meantime, we can emulate this behavior using third-party libraries like OneOf.
Here’s how to define our Charge type using OneOf:
publicclassCharge:OneOfBase<NightCount,FixedAmount,Percentage>// ^^^^^{publicCharge(OneOf<NightCount,FixedAmount,Percentage>input):base(input){}}// Or using OneOf Source Generation:////[GenerateOneOf] // <--//public partial class Charge : OneOfBase<NightCount, FixedAmount, Percentage>// ^^^^^//{//}publicrecordNightCount(intCount);// <-- No base class herepublicrecordFixedAmount(decimalAmount,CurrencyCodeCurrency);publicrecordPercentage(decimalAmount);// Here's how to instantiate a OneOf typevaroneNightBeforeCheckin=newDeposit(newCharge(newNightCount(1)),// ^^^^^newDelay(1,DelayType.BeforeCheckin));
OneOf brings methods like Match, Value, and AsT0/AsT1/AsT2 to work with and unwrap the underlying type.
Apart from third-party libraries to emulate discriminated unions, there’s an alternative approach abusing records: Discriminated Onions.
And here’s our Charge type using discriminated onions:
publicabstractrecordCharge// ^^^^^{privateCharge(){}// <--publicrecordNightCount(intCount):Charge;// <--publicrecordFixedAmount(decimalAmount,CurrencyCodeCurrency):Charge;publicrecordPercentage(decimalAmount):Charge;publicUMatch<U>(// ^^^^^Func<NightCount,U>onNightCount,Func<FixedAmount,U>onFixedAmount,Func<Percentage,U>onPercentage)=>thisswitch{NightCountr=>onNightCount(r),FixedAmountc=>onFixedAmount(c),Percentagep=>onPercentage(p),_=>thrownewArgumentOutOfRangeException()};}// Here's how to instantiate a discriminated onionvaroneNightBeforeCheckin=newDeposit(newCharge.NightCount(1),newDelay(1,DelayType.BeforeCheckin));
Voilà! That’s what discriminated unions are, and the official proposal to bring them to the C# language.
You see, C# is getting more functional on each release. While we wait to get it more funcy with discriminated unions, we have to go with libraries and workarounds. Let’s wait to see how it goes.
That’s how I’d summarize the talk “Moving IO to the edges of your app” by Scott Wlaschin at NDC Sydney 2024.
In case you don’t know Scott Wlaschin’s work, he runs the site F# for Fun and Profit and talks about Functional Programming a lot. He’s a frequent speaker at the NDC Conference.
Here’s the YouTube video of the talk, in case you want to watch it:
These are the main takeaways from that talk and how I’d follow them to refactor a piece of code from one of my past projects.
I/O Is Evil: Keep It at Arm’s Length
In a perfect world, all code should be pure. The same inputs return the same outputs with no side effects.
But we’re not in a perfect world, and our code is full of impurities: retrieving the current time, accessing the network, and calling databases.
Instead of aiming for 100% pure code, the guideline is to move I/O (or impurities) away from the business logic or rules.
When we mix I/O with our domain logic, we make our domain logic harder to understand and test, and more error-prone.
So let’s pay attention to functions with no inputs or no outputs. Often, they do I/O somewhere.
If you think we don’t write functions with no outputs, let’s take another look at our repositories.
Sure, our Create or Update methods might return an ID. But they’re not deterministic. If we insert the same record twice, we get different IDs or even an error if we have unique constraints in our tables.
The guideline here is to write code that is:
Comprehensible: it receives what it needs as input and returns some output.
Deterministic: it returns the same outputs, given the same input.
Free of side effects: it doesn’t do anything under the hood.
Just Return the Decision
This is the example shown in the talk:
Let’s say we need to update a customer’s personal information. If the customer changes their email, we should send a verification email. And, of course, we should update the new name and email in the database.
We’re mixing the database calls with our decision-making code. IO is “close” to our business logic.
Of course, we might argue static methods are a bad idea and pass two interfaces instead: ICustomerDb and IEmailServer. But we’re still mixing IO with business logic.
This time, the guideline is to create an imperative shell and just return the decision from our business logic.
Here’s how to update our customers “just returning the decision,”
enumUpdateCustomerDecision{DoNothing,UpdateCustomerOnly,UpdateCustomerAndSendEmail}// This is a good place for discriminated unions.// But we still don't have them in C#. Sorry!recordUpdateCustomerResult(UpdateCustomerDecisionDecision,Customer?Customer,EmailMessage?Message);staticUpdateCustomerResultUpdateCustomer(Customerexisting,CustomernewCustomer){varresult=newUpdateCustomerResult(UpdateCustomerDecision.DoNothing,null,null);if(existing.Name!=newCustomer.Name||existing.EmailAddress!=newCustomer.EmailAddress){result=resultwith{Decision=UpdateCustomerDecision.UpdateCustomerOnly,Customer=newCustomer};}if(existing.EmailAddress!=newCustomer.EmailAddress){varmessage=newEmailMessage(newCustomer.EmailAddress,"Some message here...");result=resultwith{Decision=UpdateCustomerDecision.UpdateCustomerAndSendEmail,Message=message};}returnresult;}asyncstaticTaskImperativeShell(CustomernewCustomer){varexisting=awaitCustomerDb.ReadCustomer(newCustomer.Id);varresult=UpdateCustomer(existing,newCustomer);// ^^^^^// Nothing impure hereswitch(result.Decision){caseDoNothing:// Well, doing nothing...break;caseUpdateCustomerOnly:// Updating the database here...break;caseUpdateCustomerAndSendEmail:// Update the database here...// And, send the email here...break;}}
With the imperative shell, we don’t have to deal with database calls and email logic inside our UpdateCustomer(). And we can unit test it without mocks.
As a side note, UpdateCustomerDecision and UpdateCustomerResult are a simple alternative to discriminated unions. Think of discriminated unions like enums where each member could be an object of a different type.
In more complex codebases, ImperativeShell() would be like a use case class or command handler.
Pure Code Doesn’t Talk to the Outside
When we push I/O to the edges, our pure code doesn’t need exception handling or asynchronous logic. Our pure code doesn’t talk to the outside world.
These are the three code smells the speaker shared to watch out for in our domain code:
Is it async? If so, you’re doing I/O somewhere
Is it catching exceptions? Again, you’re (probably) doing I/O somewhere
Is it throwing exceptions? Why not use a proper return value?
If any of these are true, we’re doing IO inside our domain. And we should refactor our code. “All hands man your refactoring stations.”
Moving I/O to the Edges When Sending an Email
While watching this talk, I realized I could refactor some code I wrote for sending emails in a past project.
Before sending an email, we need to validate if we’re sending it to valid domains. And, after calling a third-party email service, we should store a tracking number and update the email status. Something like this,
publicclassEmail{// Imagine more properties like From, Subject, Body here...privatereadonlyIEnumerable<Recipient>_recipients=newList<Recipient>();publicasyncTaskSendAsync(IEmailServiceemailService,IDomainValidationServicevalidationService,CancellationTokencancellationToken){try{awaitvalidationService.ValidateAsync(this,cancellationToken);// It assumes that ValidateAsync changes the recipient's statusif(_recipients.Any(t=>t.LastStatus!=DeliveryStatus.FailedOnSend)){vartrackingId=awaitemailService.SendEmailAsync(this,cancellationToken);SetTrackingId(trackingId);MarkAsSentToProvider();}}catch(Exceptionex){UpdateStatus(DeliveryStatus.FailedOnSend);thrownewSendEmailException("Sending email failed.",ex);}}}
But this code contains the three code smells we should avoid: it has asynchronous logic and throws and catches exceptions, and even our Domain is aware of cancellation tokens. Arrggg!
That was an attempt to do Domain Driven Design (DDD) at a past team. And probably, our team at that time picked those conventions from the book Hands-on Domain-Driven Design with .NET Core.
And the imperative shell that calls SendAsync() is something like this,
publicclassSendEmailHandler:IEventHandler<EmailCreatedEvent>{// Imagine some fields and a constructor here...publicasyncTaskHandle(EmailCreatedEventevt,CancellationTokencancellationToken){varemail=await_emailRepository.GetByIdAsync(evt.EmailId);??thrownewEmailNotFoundException(evt.EmailId);try{awaitemail.SendAsync(_emailService,_validationService,cancellationToken);await_emailRepository.UpdateAsync(email,cancellationToken);}catch(Exceptionex){email.SetFailedOnSend(ex.Message);await_emailRepository.UpdateAsync(email,cancellationToken);}}}
And here’s the same logic “returning the decision,”
// This is a poor man's discriminated unionpublicabstractrecordSendingAttempt{privateSendingAttempt(){}publicrecordSentToSome(GuidTrackingId,IEnumerable<Recipient>Recipients):SendingAttempt;publicrecordSentToNone():SendingAttempt;publicrecordFailedToSend(stringMessage):SendingAttempt;}publicclassEmail{// Imagine more properties like From, Subject, Body here...privatereadonlyIEnumerable<Recipient>_recipients=newList<Recipient>();publicEmailSend(SendingAttemptattempt){switch(attempt){caseSendingAttempt.SentToSome:// Set trackingId and mark as Sent for some recipients// Mark all other recipients as Invalidbreak;caseSendingAttempt.SentToNone:// Mark all recipients as Invalidbreak;caseSendingAttempt.FailedToSend:// Mark all recipients as Failedbreak;}}}
In this refactored version, we’ve removed the asynchronous logic and exception handling. Now, it receives a SendingAttempt with the result of validating domains and email delivery to the email provider.
Also, it doesn’t have any dependencies passed as interfaces. It embraces Dependency Rejection.
And here’s the imperative shell,
publicclassSendEmailHandler:IEventHandler<EmailCreatedEvent>{// Imagine some fields and a constructor here...publicasyncTaskHandle(EmailCreatedEventevt,CancellationTokencancellationToken){varemail=await_emailRepository.GetByIdAsync(evt.EmailId)??thrownewEmailNotFoundException(evt.EmailId);varresult=await_validationService.ValidateAsync(email,cancellationToken);// Use result to find valid and invalid destinations...// Attempt to send email and catch any exceptions...varsendingAttempt=BuildASendingAttemptHere();email.Send(sendingAttempt);// ^^^^// Nothing impure hereawait_emailRepository.UpdateAsync(email,cancellationToken);}}
Now, the imperative shell validates email domains and tries to send the email, encapsulating all the I/O around Send(). After this refactoring, we should rename Send() inside our domain to something else.
Voila! That’s one approach to have pure business logic, not the one and only approach.
Whether we follow Ports and Adapters, Clean Architecture, or Functional Core-Imperative Shell, the goal is to abstract dependencies and avoid “contaminating” our business domain.
Recently, I found a NDC talk titled “.NET Testing Best Practices” by Rob Richardson.
Today I want to share five unit testing best practices I learned from that talk, along with my comments on other parts of it.
Here’s the YouTube video of the talk, in case you want to watch it, and the speaker’s website,
During the presentation, the speaker coded some unit tests for the LightActuator class. This class powers an IoT device that turns a light switch on or off based on a motion sensor input.
The LightActuator turns on lights if any motion is detected in the evening or at night. And, it turns off lights in the morning or if no motion has been detected in the last minute.
publicclassLightActuator:ILightActuator{privateDateTimeLastMotionTime{get;set;}publicvoidActuateLights(boolmotionDetected){DateTimetime=DateTime.Now;// Update the time of last motion.if(motionDetected){LastMotionTime=time;}// If motion was detected in the evening or at night, turn the light on.stringtimePeriod=GetTimePeriod(time);if(motionDetected&&(timePeriod=="Evening"||timePeriod=="Night")){LightSwitcher.Instance.TurnOn();}// If no motion is detected for one minute, or if it is morning or day, turn the light off.elseif(time.Subtract(LastMotionTime)>TimeSpan.FromMinutes(1)||(timePeriod=="Morning"||timePeriod=="Noon")){LightSwitcher.Instance.TurnOff();}}privatestringGetTimePeriod(DateTimedateTime){if(dateTime.Hour>=0&&dateTime.Hour<6){return"Night";}if(dateTime.Hour>=6&&dateTime.Hour<12){return"Morning";}if(dateTime.Hour>=12&&dateTime.Hour<18){return"Afternoon";}return"Evening";}}
And here’s the first unit test the presenter live-coded,
publicclassLightActuator_ActuateLights_Tests{[Fact]publicvoidMotionDetected_LastMotionTimeChanged(){// ArrangeboolmotionDetected=true;DateTimestartTime=newDateTime(2000,1,1);// random value// ActLightActuatoractuator=newLightActuator();actuator.LastMotionTime=startTime;actuator.ActuateLights(motionDetected);DateTimeactualTime=actuator.LastMotionTime;// AssertAssert.NotEqual(actualTime,startTime);}}
Of course, the presenter refactored this test and introduced more examples throughout the rest of the talk. But this initial test is enough to prove our points.
Looking closely at the sample test, we notice the Assert part has a bug.
The actual and expected values inside NotEqual() are in the wrong order. The expected value should go first. Arrrggg!
[Fact]publicvoidMotionDetected_LastMotionTimeChanged(){boolmotionDetected=true;DateTimestartTime=newDateTime(2000,1,1);LightActuatoractuator=newLightActuator();actuator.LastMotionTime=startTime;actuator.ActuateLights(motionDetected);DateTimeactualTime=actuator.LastMotionTime;Assert.NotEqual(actualTime,startTime);// ^^^^^// They're in the wrong order. Arrrggg!}
To avoid flipping them again, it’s a good idea to use more human-friendly assertions using libraries like FluentAssertions or Shouldly.
Here’s our tests using FluentAssertions,
[Fact]publicvoidMotionDetected_LastMotionTimeChanged(){boolmotionDetected=true;DateTimestartTime=newDateTime(2000,1,1);LightActuatoractuator=newLightActuator();actuator.LastMotionTime=startTime;actuator.ActuateLights(motionDetected);DateTimeactualTime=actuator.LastMotionTime;// Before//Assert.NotEqual(actualTime, startTime);// ^^^^^// They're in the wrong order. Arrrggg!//// After, with FluentAssertionsactualTime.Should().NotBe(startTime);// ^^^^^}
4. Don’t be too DRY
Our sample test only covers the scenario when any motion is detected. If we write another test for the scenario with no motion detected, our tests look like this,
But, inside unit tests, being explicit is better than being DRY.
Turning our two tests into a parameterized test would make us write a weird Assert part to switch between Equal() and NotEqual() based on the value of motionDetected.
Let’s prefer clarity over dryness. Tests serve as a living documentation of system behavior.
5. Replace dependency creation with auto-mocking
ActuateLights() uses a static class to turn on/off lights,
It’d be hard to assert if the lights were turned on or off with a static method.
A better approach is to replace LightSwitcher.Instance with an interface.
But adding a new dependency to the LightActuator would break our tests.
Instead of manually passing the new LightSwitch abstraction to the LightActuator constructor inside our tests, we could rely on auto-mocking tools like Moq.AutoMocker.
We don’t add // class, // fields, and // methods in other parts of our code, so it shouldn’t be necessary in our tests either.
Instead, I prefer using blank lines to visually separate the three sections of the Arrange/Act/Assert pattern.
In the examples I’ve shown you, I completely removed those comments.
2. Name test values instead of using comments
It’s a good idea to document our test values. But, let’s avoid using comments when we can use a descriptive name.
I’d rename startTime with a comment at the end to anyStartTime or randomStartTime,
[Fact]publicvoidMotionDetected_LastMotionTimeChanged(){boolmotionDetected=true;// Before://DateTime startTime = new DateTime(2000, 1, 1); // random value// ^^^^^varanyStartTime=newDateTime(2000,1,1);// ^^^^^// or//var randomStartTime = new DateTime(2000, 1, 1);// ^^^^LightActuatoractuator=newLightActuator();actuator.LastMotionTime=anyStartTime;actuator.ActuateLights(motionDetected);DateTimeactualTime=actuator.LastMotionTime;Assert.NotEqual(anyStartTime,actualTime);}
3. Don’t expose private parts
In the talk, as part of the refactoring session, the presenter tested some internals. Specifically, he made the LastMotionTime property inside the LightActuator class public to use it inside the tests.
Even somebody in the audience raised this question too.
I understand the presenter had less than an hour to show a complete example and he chose a simple approach.
Voilà! Those are the five lessons I learned from this talk.
My favorite quote from the talk:
“What’s cool about unit testing is we can debug our code by writing code”
— Rob Richardson
As an exercise left to the reader, the presenter didn’t cover testing time. But we already covered how to write tests that use DateTime.Now using a custom abstraction.
Another thing I didn’t like is that at some point in the testing session, a TimePeriodHelper was added. And that’s one of the method and class names I’d like to ban.
Ready to upgrade your unit testing skills? Write readable and maintainable unit test with my course Mastering C# Unit Testing with Real-world Examples on Udemy. Learn unit testing best practices while refactoring real unit tests from my past projects. No tests for a Calculator class.
And I don’t want to touch on primary constructors. It’s like classes got jealous of records and started crying for a similar feature, like a baby boy jealous of his brother.
Voilà! That’s what I don’t like about C#. Don’t take me wrong, C# is a great language with excellent tooling. But my favorite features are quite old: LINQ, async/await, and extension methods.
Some new features have lowered the barrier to entry. Now a “Hello, world” is a single line of code: Console.WriteLine("Hello, world!");.
Other C# features are making the language inconsistent and easier to write but not easier to read.
Often C# doesn’t feel like a single language, but three: the classic one from the early 2000s, another one between the classic and the .NET era, and the one we’re living these days.