a code review nightmare

Once upon a time in 2023 in the wild west space a code review happend. As it is normal with inherited legacy systems and code, we not always have ability to refactor incoming data shape or values - BUT - we have all the rights to shape objects that are used in our application to be nice and clean. Snipped bellow is an example of not dealing with that approach, rather a lazy way just to  close a ticket.

As there is a hidden gem, that list in not needed, as stored procedure returns only one record with data, so we have ability to simplify thing down in the processing stream. 
And we can start form a proper name for internal variable to match C# codding standards, moreover as this is a mapping only we can reduce the text to minimum.

Now let's get rid of the "if" statement as this is pain to my eyes, as we are dealing with nullable, let's check for a value and then assign "0" or a value.

So now, as we know that we have only one record here, lets introduce a model and put it together.

Looks better - do you agree? Ech I don't really like this code repetition, so let's dig it here  to check if we can make it nicer.

Hmm a property that can do the stuff for us and we don't need to anything more - yeah I love it, so quick replace and our mapper is in a great shape:


// mapper snippet here

Now do we really need to do it? As we are using C#, a kinda business language - do we care about performance (in fact I do care about my cloud provider's bills)?

So let's do a small benchmark - just to see if a nice looking code is a nice working code too!



As we can see - if all the properties are provided - then the low level "if" wins, and the GetValueOrDefault has similar timings as ternary operator.

The performance drop is about 10% with all properties, but we are gaining when the props are nulls, so implementation could depend on the data we have.



 



Comments

Popular posts from this blog

when a new guy joins a team...

FizzBuzz - my first interview task on whiteboard

are YOU a garbage collector?