Blog
Lessons from open-source: Use nested functions for logic encapsulation and separation of concerns.

Lessons from open-source: Use nested functions for logic encapsulation and separation of concerns.

This lesson is picked from @next/mdx package. In this article, you will learn how next.js source code applies encapsulation and separation of concerns with nested functions in @next/mdx package.

Compile nested inside createFormatAwareProcessors function

createFormatAwareProcessors returns an object with two properties extnames, compile.

compile here is a function that encapsulates processing logic.

You could have just let createFormatAwareProcessors do the processing logic but, context and proximity matter when you write functions and to decide what they (functions) should return. This is why you have ‘extnames’ property returned by createFormatAwareProcessors along with ‘compile’.

By returning a function (compile), the external code using this processor can decide when to call it. It provides a level of abstraction, allowing the external code to control when and how the compilation should occur, giving more flexibility and control.

This design choice makes your code more modular and allows for better organization and reuse of the processing logic.

Conclusion:

You can encapsulate logic in a nested function (function within a function). To have more control over triggering a logic, you can simply return this function along with other properties in the object.

The following is a snippet from nextjs source code, an object returned by createFormatAwareProcessors:

return {
  extnames:
  compileOptions.format === 'md'
  ? mdExtensions
  : compileOptions.format === 'mdx'
  ? mdxExtensions
  : mdExtensions.concat(mdxExtensions),
  compile,
}

I just admire the fact that the compile function is used to encapsulate logic and to create separation of concerns because you might want to put certain logic processing on hold until you reach/meet some criteria.

About me:

Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.

I am open to work on interesting projects. Send me an email at ramu.narasinga@gmail.com

My Github —  https://github.com/ramu-narasinga

My website —  https://ramunarasinga.com

My Youtube channel —  https://www.youtube.com/@ramu-narasinga

Learning platform —  https://thinkthroo.com

Codebase Architecture —  https://app.thinkthroo.com/architecture

Best practices —  https://app.thinkthroo.com/best-practices

Production-grade projects —  https://app.thinkthroo.com/production-grade-projects