{"id":6822,"date":"2017-01-14T21:10:57","date_gmt":"2017-01-14T21:10:57","guid":{"rendered":"https:\/\/itnigblog.live-website.com\/?p=6822"},"modified":"2020-06-11T13:12:56","modified_gmt":"2020-06-11T13:12:56","slug":"better-code-structure-working-with-pods","status":"publish","type":"post","link":"https:\/\/itnig.net\/blog\/better-code-structure-working-with-pods\/","title":{"rendered":"Better code structure: working with pods"},"content":{"rendered":"<p id=\"e147\" class=\"graf graf--p graf-after--figure\">Usually we organize our javascript apps either by what they are or by what they do. The first is like rails: separate directories like\u00a0<code class=\"markup--code markup--p-code\">components<\/code>,\u00a0<code class=\"markup--code markup--p-code\">containers<\/code>,\u00a0<code class=\"markup--code markup--p-code\">reducers<\/code>, etc. And the second more or less like feature\/DDD:\u00a0<code class=\"markup--code markup--p-code\">user<\/code>,\u00a0<code class=\"markup--code markup--p-code\">cart<\/code>, and so on. Tho both of these options are very mainstream and solid they contain some constraints.<\/p>\n<p id=\"386a\" class=\"graf graf--p graf-after--p\">When you structure the code files tree\u00a0<strong class=\"markup--strong markup--p-strong\">by what they are<\/strong>\u00a0you tend to keep every component of the same feature so distant that it\u2019s very\u00a0<strong class=\"markup--strong markup--p-strong\">difficult to connect\u00a0<\/strong>the pieces. Therefore you dive into some troubles like the path hell; a lot of\u00a0<code class=\"markup--code markup--p-code\">require('..\/..\/..\/etc')<\/code>\u00a0in your code. And everything is extremely coupled to the directory structure.<\/p>\n<p id=\"7819\" class=\"graf graf--p graf-after--p\">In the other hand, when you are driven\u00a0<strong class=\"markup--strong markup--p-strong\">by what they do<\/strong>\u00a0everything is more isolated and maintainable. But there\u2019s\u00a0<strong class=\"markup--strong markup--p-strong\">a lot of duplication<\/strong>.<strong class=\"markup--strong markup--p-strong\">\u00a0<\/strong>And the\u00a0<strong class=\"markup--strong markup--p-strong\">communication<\/strong>\u00a0between the features either is\u00a0<strong class=\"markup--strong markup--p-strong\">based on a weak contract<\/strong>\u00a0or\u00a0<strong class=\"markup--strong markup--p-strong\">depends upon some infrastructure<\/strong>. Both of these options are prone to raise some bugs.<\/p>\n<p id=\"c2ab\" class=\"graf graf--p graf-after--p\">Pods it\u2019s an evolution of the last one.<strong class=\"markup--strong markup--p-strong\">\u00a0You can think about pods like the microservices of the front applications<\/strong>. A pod is an isolated and completely independent microapp which can communicate 100% with other pods. Therefore, you get code base which is\u00a0<strong class=\"markup--strong markup--p-strong\">composable, extendable, reusable<\/strong>and extremely\u00a0<strong class=\"markup--strong markup--p-strong\">easy to test<\/strong>. Tho, the main benefit is that you completely erase the inter components side effects. Once a pod is failing you are totally sure that it\u2019s because itself has something wrong; not an external piece.<\/p>\n<blockquote id=\"56e2\" class=\"graf graf--blockquote graf-after--p\"><p>A pod is an isolated and completely independent microapp which can communicate 100% with other pods<\/p><\/blockquote>\n<p id=\"28ce\" class=\"graf graf--p graf-after--blockquote\">The\u00a0<strong class=\"markup--strong markup--p-strong\">unique requirement<\/strong>\u00a0of the pods is to driven all the pods intercommunication through an\u00a0<strong class=\"markup--strong markup--p-strong\">event bus<\/strong>. If you\u2019re already using any Flux pattern, lib or framework it\u2019s gonna be very easy to lead this communication to the dispatcher; which is actually a single point for the events.<\/p>\n<p id=\"0d01\" class=\"graf graf--p graf-after--p\">For a pod is not mandatory to have the logic, presentational and communication layers.\u00a0<strong class=\"markup--strong markup--p-strong\">Can have all of just one of them<\/strong>. Imagine a router pod. It has the logic and communication layer but doesn\u2019t expose any view. Or the opposite, a form pod which only exposes a presentational layer and depends 100% on the arguments received (see React\u00a0<code class=\"markup--code markup--p-code\">props<\/code>).<\/p>\n<p id=\"6f88\" class=\"graf graf--p graf-after--p\">Because every pod acts like and independent application we place the\u00a0<strong class=\"markup--strong markup--p-strong\">tests inside them<\/strong>. This means two things:\u00a0<strong class=\"markup--strong markup--p-strong\">stop duplicating the files tree<\/strong>, and making\u00a0<strong class=\"markup--strong markup--p-strong\">code coverage meaningful<\/strong>. When you have 100% coverage with a pod you know that it won\u2019t fail. No side effects.<\/p>\n<p id=\"888c\" class=\"graf graf--p graf-after--p\">In a nutshell, by working with pods you get a\u00a0<strong class=\"markup--strong markup--p-strong\">secure and very flexible structure<\/strong>. Very\u00a0<strong class=\"markup--strong markup--p-strong\">easy to test<\/strong>\u00a0and\u00a0<strong class=\"markup--strong markup--p-strong\">without side effects<\/strong>. It scales from 0 to millions without worrying about big refactors due to the fact that\u00a0<strong class=\"markup--strong markup--p-strong\">every pod is<\/strong>\u00a0<strong class=\"markup--strong markup--p-strong\">isolated but available to communicate<\/strong>\u00a0with all the other pods.<\/p>\n<p id=\"d201\" class=\"graf graf--p graf-after--p\">In case you want to see some code here\u2019s a\u00a0<a class=\"markup--anchor markup--p-anchor\" href=\"http:\/\/github.com\/sospedra\/pods\" target=\"_blank\" rel=\"noopener noreferrer\" data-href=\"http:\/\/github.com\/sospedra\/pods\">Github repository<\/a>\u00a0with a simple pod architecture. Give it a look and check the benefits the pods can offer. If you have some doubts or proposals don\u2019t hesitate to open an issue or comment here. This is a living pattern. We\u2019re using the pods at\u00a0<a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/www.ulabox.com\/\" target=\"_blank\" rel=\"noopener noreferrer\" data-href=\"https:\/\/www.ulabox.com\">Ulabox<\/a>\u00a0and they\u2019re proving to be the solution to a lot of structural codebase problems. Tho, we\u2019re willing to listen about your thoughts and concerns.<\/p>\n<p id=\"ff45\" class=\"graf graf--p graf-after--p graf--trailing\">The journey is long, take some pods.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Usually we organize our javascript apps either by what they are or by what they do. The first is like rails: separate directories like\u00a0components,\u00a0containers,\u00a0reducers, etc. And the second more or less like feature\/DDD:\u00a0user,\u00a0cart, and so on. Tho both of these options are very mainstream and solid they contain some constraints. When you structure the code [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-6822","post","type-post","status-publish","format-standard","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/itnig.net\/blog\/wp-json\/wp\/v2\/posts\/6822","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itnig.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itnig.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itnig.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itnig.net\/blog\/wp-json\/wp\/v2\/comments?post=6822"}],"version-history":[{"count":1,"href":"https:\/\/itnig.net\/blog\/wp-json\/wp\/v2\/posts\/6822\/revisions"}],"predecessor-version":[{"id":9626,"href":"https:\/\/itnig.net\/blog\/wp-json\/wp\/v2\/posts\/6822\/revisions\/9626"}],"wp:attachment":[{"href":"https:\/\/itnig.net\/blog\/wp-json\/wp\/v2\/media?parent=6822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itnig.net\/blog\/wp-json\/wp\/v2\/categories?post=6822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itnig.net\/blog\/wp-json\/wp\/v2\/tags?post=6822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}