the code need to be secure, all need to be checked... sarcasm
In my days I am sometimes wondering why we humanins are so hateful to computers. We ask them to make a job that is not used, even we order them to do it twice or more in a process flow.
A time ago I was shocked when I saw a code review that was approved by a senior developer with more than 8 years of experience and written by other senior with more than 10 years of experience in the .Net area.
What killed me is that nicely looking code makes more noise and brings no value, but cost precious CPU ticks.
I hope that this not look familiar to you!!!
So let's try to add some context, so the parameter is received by api endpoint, and then passed down. Now firstly we can see that it is converted from string to int, in a safe manner using int.TryParse. Next we see the same method invocation (as Int32 is return type of try/parse) and then this parameter is passed down to document generator, then to submethod where we can see an expensive database call, to finally check if we get any results...
So let's try to add some context, so the parameter is received by api endpoint, and then passed down. Now firstly we can see that it is converted from string to int, in a safe manner using int.TryParse. Next we see the same method invocation (as Int32 is return type of try/parse) and then this parameter is passed down to document generator, then to submethod where we can see an expensive database call, to finally check if we get any results...
Why database call is expensive?
Hmm - let's do a simple and stupid benchmark on good PC and database installed locally.
From this benchmark, if our parameter is 0, when we are just blindly asking database for this record we are spending thousands of CPU clicks on nothing, making electrons angry.
Now I reduced the first parsing and we shall shave off around 50% of processing time on this variable.
As we can see, as this looks (and it is) an extending change to current codebase, we are expecting more situations where the document number is provided, so the if statement will be executed more frequently - up to 100prc, means our CPU will overheat for nothing...
So what is the takeaway from this post?
So what is the takeaway from this post?
- we are inheriting legacy code - but we are responsible to make it clean
- some people are adding code to the methods to save money as they use white background and all 3 leds are always on, so more code on screen = less consumed energy
- provided parameter shall be checked once, then execution in the final method shall be based on the value greater than 0
- there is no need for default parameter value IF a method is called only once!
- take breaks, fart a lot. Why farts are helping out? When you don't fart, all the gases are getting to brain, and then shitty ideas are born...
Have a great day!
Comments
Post a Comment