Menu

The Backing Field a Typo Could Reach
πŸ“°

The Backing Field a Typo Could Reach

DEV Community: dotnetΒ·Sukhpinder SinghΒ·18 days ago
#3M0vaqME
#devtodotnet#devto#dev#field#_percent
Reading 0:00
15s threshold

A discount came back as 110% in a test last week and I spent a solid ten minutes insisting that was impossible. I had a setter. The setter clamped. Math.Clamp(value, 0, 100) , sitting right there, no way past it. Except there was a way past it, and I'd built the door myself.

Here's the property, more or less how it looked:

sealed class DiscountOld { private int _percent ;

public int Percent { get => _percent ; set => _percent = Math . Clamp ( value , 0 , 100 ); } }

Enter fullscreen mode

Exit fullscreen mode

Nothing wrong with that. Assign Percent = 150 and you get 100 back. I'd tested exactly that case and moved on. The setter does its job every single time it runs. That last bit is the part I didn't think hard enough about.

The line I wrote three weeks later

A seasonal promo needed a flat bump, and I added a method to the same class to do it.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.