Why is writing clean code so hard?


"The business logic is too complicated to simplify the code." 


Could there be anything more frustrating to hear? Why do so many of us truly believe that? Sure, some code gets to that point, but its not because its solving the Vaught conjecture, but because some basic code organization tricks weren't followed. So what's the deal? I follow these few steps to help me simplify my code.

Step 1- PLAN!

If I had eight hours to chop down a tree, I'd spend six hours sharpening my ax.


Basically, write a flow diagram... pretend like you're explaining the flow to a child; they will keep asking: "Then what happens? Then what happens..." over and over until you give them the pieces to solve the puzzle. This should be your process boxes in your flow.  Now, each of those boxes need to be a method your main public method will call them in the same way your diagram presents them.

Step 2 - Organize your parameters!

I'm not going to go into the proper naming of variables and parameters (which you should do anyway), but more so in what you pass back and forth from one method to another. Instead of passing bits and pieces from one method to another try creating a document message and updating properties as you go. Think of it as a Strongly typed property bag that encapsulates all the data needed for the entire flow without having to pass them one by one or to make them variables in the class. This will let you test cleanly and carve out

Step 3 - Organize your files!

Partial nested files...use them... a lot! Try avoiding multiple classes, instantiating multiple objects to do mundane things doesn't always scale. Instead, if your logic is huge, make the main method the root file and nest all the other portions in their own partials named appropriately. In other words:

                        BigComplicatedFile.cs
                                      |_BigComplicatedFile.FirstLogicBranch.cs
                                      |_BigComplicatedFile.SecondLogicBranch.cs

Already easier to decipher, right?

Step 4 - Test Test Test!

This isn't what you think it is; its a way to make sure your code is well organized and understandable. For example, stop creating virtual methods unless logically needed... they are a way to cheat when your testing. If you have to override a virtual method in order to write a test, then your logic is probably to opaque and hard to understand and therefore test.
Now, doesn't this contradict with Step 1? In the sense that we should break our logic into multiple logical private methods? No it doesn't, but I understand how it can seem to be. Testing should be primarily against your public methods, and the method will utilize the private ones as it needs them. But if you do need to test them, there are many ways you can do so using either InternalsVisibleTo
or private methods accessors.



I have been doing this for a while, and haven't found a scenario where the following these four steps didn't make the logic simpler to write, easier to debug and quicker to finish. So, next time you've got a complicated piece of code in front of you, take a breath and try to follow these few steps and let me know what happens.






Comments

Popular posts from this blog

How to unit Test Entity Framework

//TODO: Tackling Technical Debt

The Yin and Yang of software