Here is an example of how you can apply the Kiss principle.
Kiss.
Keep it simple, stupid.
Finding a good example here is quite hard, but I'll try to come up with something that you could face
in real world.
So here we got a program and we got a main method here, and we got a list of orders and we want to
print those orders.
Here we got our orders right.
Our orders.
Class.
I know you would put them into a separate file, but for the sake of this video, this is all inside
of one program.cs file.
And then here we got our class order printer.
And here is the actual algorithm that we want to check in order to follow along with the Kiss principle
or to apply it, we want to follow along with some specific steps.
Step number one would be to review the code where you're curious if you could or should change something.
Now, step number two would be to find the parts of that code where you think it's overly complex.
So let's take a look at our print order details method.
Here we receive as a parameter a list of orders.
Here we have a loop and here we have another loop.
All right.
That kind of looks suspicious already.
Always keep in mind loops are very performance heavy.
So whenever you stumble across code and you identify multiple loops, be aware.
All right.
Or same applies for nested loops.
That's a different story.
However, if you see multiple loops inside of a method, you want to dig deeper.
Let's see if this really makes sense.
So this looks kind of complex.
So let's see if we can refactor this code.
Which brings us automatically to step number three, refactoring the code to make it more straightforward.
And to reduce the complexity, however, we need to keep the same output.
So if this solution here is the only way to get the result that we want to have, then it's fine.
You know we have to keep the same output.
That's basically what refactoring means.
So we just reduce the complexity if possible.
Here we iterate over our orders.
Then we check if the order quantity and the order price is bigger than zero.
And if so we simply want to log the order details.
Then it seems like we have a variable here that calculates the total price.
And again we are iterating over the entire list.
We check again for the same condition order quantity and order price.
And then we sum up the total price here.
So we iterate two times over exactly the same list.
Now that is not a good practice, right?
We are iterating in that loop already.
So why are we not counting the total price in that first loop?
Why should we again loop over the entire ordered list just to calculate the price?
We could do that here instead.
So let's refactor this.
Take the total price.
Bring it at the very top.
This variable inside of our method.
Here let's remove this entirely.
Here.
Take the total price calculation that simply adds the quantity times price to the total price.
Let's remove it.
There we go.
And let's put it right here.
It's the same if statement.
We can put it here.
Total price plus equals order quantity times order price.
And then save it like this.
And if we start our application we can now see that we have our orders printed here.
Order ID one.
Laptop.
Quantity two.
Total price 3000 and then 2500.
Here should sum up to 5500.
Let's just double check.
That's three K and that's two and a half k.
So 5500 is the correct amount.
So this would be one applied example of how to use the Kiss principle to keep it simple and stupid,
to reduce redundancy and to find a way to come up with a simple solution possible that still grants
us the same output.
However, one important thing to mention here again is that complexity is not a problem.
If a complex solution is the easiest way to get the job done, then that's totally fine, right?
We're just looking for over complicated parts that we can reduce in complexity to achieve the same result
with a simpler approach, with a simpler solution.
But again, if the simple solution is to use that complex calculation, for example, that's totally
fine.
Alrighty, now let's move on.