Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. # how to structure a redux application
  2.  
  3. A well-thought structure is helpful for the maintainability of the code.
  4. It also helps to avoid some common anti-patterns.
  5. *A redux application is not necessary a big thing, it can also be a component that is complex enough to require redux.*
  6.  
  7. There are the only 2 rules to comply with, so it is not painful to always have them in mind while developing.
  8.  
  9. ## use a modular organisation
  10.  
  11. In order to have a scalable application, use a modular organisation by opposition with a type organisation:
  12.  
  13. *type organisation:*
  14. ```
  15. actions/
  16. spotify-search.js
  17. twitter-log.js
  18. components/
  19. spotify-search/
  20. spotify-search.js
  21. twitter-log/
  22. twitter-log.js
  23. constants/
  24. spotify-search.js
  25. twitter-log.js
  26. reducers/
  27. spotify-search.js
  28. twitter-log.js
  29. selectors/
  30. spotify-search.js
  31. twitter-log.js
  32. index.js
  33. ```
  34.  
  35. *modular organisation:*
  36. ```
  37. spotify-search/
  38. actions.js
  39. components/
  40. constants.js
  41. reducer.js
  42. selectors.js
  43. index.js
  44. twitter-log/
  45. ...
  46. ```
  47.  
  48. If you want to work on a feature, you don't have to browse the entire application to get all files related to this feature.
  49. *Sometimes it won't be as obvious as it sounds.*
  50.  
  51. It's the opposite of the monolithic approach and offers a way to avoid it, working on a feature should only involve 1 module folder.
  52. *If more than 1 module folder is involved, either the code smells or the task is too big.*
  53.  
  54. The modular organisation also easy the following rule.
  55.  
  56. ## define an API for each module
  57.  
  58. The complexity of an application can have many reasons, one of them is the interleaving of modules (which is commonly called the spaghetti code).
  59. In other words, coupling things together make it complicated to reason about 1 specific thing and not be forced to deal about all other things.
  60. *A simple task become quickly a hard one as the application grows.*
  61.  
  62. *bad (interleaving):*
  63. ```javascript
  64. // in twitter-log
  65. import reducer from '../spotify-search';
  66. import SpotifyView from '../spotify-search/components/spotify-search-view';
  67. ```
  68.  
  69. This code implies that within the twitter-log module, i know how the spotify-search module is made.
  70. If spotify-search is updated, then you have to look for all dependent modules and update them accordingly.
  71. *Interleaving is a structural coupling, coupling is evil.*
  72.  
  73. *good (rely on module API):*
  74. ```javascript
  75. // in twitter-log
  76. import spotifySearch from '../spotify-search';
  77. const { reducer, SpotifyView } = spotifySearch;
  78. ```
  79.  
  80. Decoupling modules allows to freely update them without breaking dependent modules, as soon as the API is not changed.
  81. **Instead of reaching inside other modules, we define and maintain contracts between them.**
  82.  
  83. The last thing to know is how and where I define the API of a module:
  84. ```javascript
  85. // in index.js
  86.  
  87. // the module knows its structure
  88. import reducer from './reducer';
  89. import SpotifyView from './components/spotify-search-view';
  90.  
  91. // public API of the module
  92. export { reducer, SpotifyView };
  93. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement