Explicit block & code folding for better communicating your thoughts
It happens that we end up writing long methods with a lot of lines, and even break lines and comments are not enough to reason about the code.
The best practice (or is it?) is often to extract related statements into smaller functions and calling them into the original one.
- The issue with this approach (especially in a collaborative environment) is that functions are often meant to be re-used, but it might not be what you want in that case.
- Another weakness of factoring the code into smaller functions, where we know all of them are going to be called one after the other, is that we will have to navigate back and forth between their definitions and the main method where they are called.
Alternative
In JavaScript (and TypeScript) an explicit block allows us to isolate the statements inside it from the rest of the code of the function definition. Grouping statements into an explicit block expresses two things:
- These statements are logically related (similar to a function)
- This group is meant to be used here only and nowhere else (as opposed to functions)
Code folding & Comments
Add a succinct comment just before the block and see at glance what the purpose of the block is (you could even add keywords if that can help).
Now fold everything at block level: you now have a very descriptive overview of the whole flow of the method. You can now expand only the block that you need.
Using this technique will save your teammates a lot of time (and headaches) as they will have a clear summary of the method and will only have to read the code they are concerned about.
Let me know if that technique helps. Suggestions and critiques are most welcome.
Happy coding!
So as to change the world, we should change humankind, a new beginning. Perfect world. Also, here, none everybody get a new beginning https://www.celler.ca/. There will be no executing, no despise. Just participation and the modifying of our general public.
Hi Kate. Kindly remove that spam. :)
What exactly is the problem? Is there an error message? Do a and b not get the right values? If you want help with programming, itโs important to be explicit about what the problem is and how it https://www.customassignment.com/assignment-writing-service.html happens.
Hi Julie, kindly remove that spam. Thanks :)
This is clever! Nice approach, Iโll play around with this.
Thanks Jackson. Feel free to use it in your personal apps and see how it goes. However see with your team before using it in a project, as it might be considered as a anti-pattern