Codementor Events

Explicit block & code folding for better communicating your thoughts

Published May 29, 2017Last updated May 30, 2017
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.

  1. 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.
  2. 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:

  1. These statements are logically related (similar to a function)
  2. 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.

Capture.PNG

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! ๐Ÿ˜‰

Discover and read more posts from Cedric Poilly
get started
post commentsBe the first to share your opinion
Kate Johnson
5 years ago

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.

Cedric Poilly
5 years ago

Hi Kate. Kindly remove that spam. :)

Julie Mitchell
5 years ago

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.

Cedric Poilly
5 years ago

Hi Julie, kindly remove that spam. Thanks :)

Jackson Holiday Wheeler
6 years ago

This is clever! Nice approach, Iโ€™ll play around with this.

Cedric Poilly
6 years ago

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

Show more replies