Appreciating the value of Value Objects

In my most recent employment, I got to work on refactoring some legacy code and one of things that I've learned to appreciate is the use of Value Objects.

The domain focused on money and tax rates of various sorts so the code was filled with decimal variable types. The following is a simplified example:

No alt text provided for this image

As the codebase grows larger, and variables like Tax are coming in from the database or maybe an API, it becomes harder for the developers to keep track whether Tax is in percentage or decimal value form. This is where making use of value objects can help developers work efficiently by eliminating the ambiguity.

No alt text provided for this image

The added benefit is that whenever an operation is required such as salary * taxRate, there is no accidentally multiplying the rate 100 times more than it should be. If we want to take it a step even further, we can encapsulate such operations to avoid mistakes entirely:

No alt text provided for this image

Here we add the ApplyTo behavior to the Rate class in order to remove the thinking entirely. This is one of the ways where we can start moving domain understanding into the code.

While this may seem overkill for this simple example, we start appreciating it when we have complex models. An invoice can for example be comprised of different components: invoice items, expenses, etc. where various types of taxes are applied to the various parts.

Taking the time to build domain models that correctly represent the domain logic can save a lot of time especially for new developers just getting into the code.

Leonard Lin

Senior Software Engineering Manager at Unity Technologies. 3x AWS, 2x Google Cloud certified. Non-fungible.

3 年

Salary * 100 sounds like a nice problem fo have. ??

回复

要查看或添加评论,请登录

Jocelyn Mae Englund的更多文章

  • Teach Your (Domain) Models How to Fish (And Scale)

    Teach Your (Domain) Models How to Fish (And Scale)

    "Give a man a fish and you feed him for a day. Teach him how to fish and you feed him for a lifetime" I feel like we…

  • Severance, would you opt for it?

    Severance, would you opt for it?

    For those who have not seen Severance, you can scroll on to the next post. Those who have, isn't it the most awesome…

    5 条评论
  • Set up MockServiceWorker for easier front-end development

    Set up MockServiceWorker for easier front-end development

    This week I got the opportunity to introduce the Mock Service Worker library into the user story I was working on…

  • Chat GPT and the elections

    Chat GPT and the elections

    I’m not sure people fully grasp the impact of AI on our society. There’s already been discussions around ethical use…

  • Normalize scheduling communication

    Normalize scheduling communication

    I can imagine that the work from home era has produced an equal number of workaholics as trackpad flickers, or at least…

  • Procrastinating with Tests

    Procrastinating with Tests

    Ok, I actually think testing is a very important part of development. I still haven’t come back from vacation feelings…

  • Trying out Co-pilot to Build a Side Project for the Summer

    Trying out Co-pilot to Build a Side Project for the Summer

    I watched a couple of build sessions and the one where Scott and Mark Learn to Code in particular was what prompted…

  • Always wanted to write a novel...

    Always wanted to write a novel...

    But I never had the peace to do it. I asked Bing Chat if it could help me write a novel and it replied I can help you…

  • Hello Twitter Spaces!

    Hello Twitter Spaces!

    This week I finally got access to host Twitter Spaces (For those who want to sign up, their form is still live here)…

  • Clubhouse, privacy?

    Clubhouse, privacy?

    There's been a lot of attention on clubhouse lately. According to the latest townhall, their weekly user numbers have…

社区洞察

其他会员也浏览了