<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Tzach Ovadia on Medium]]></title>
        <description><![CDATA[Stories by Tzach Ovadia on Medium]]></description>
        <link>https://medium.com/@tzachovadia?source=rss-dba4898e5506------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*p-ypwRxQyhnz168ZFzl_rw.jpeg</url>
            <title>Stories by Tzach Ovadia on Medium</title>
            <link>https://medium.com/@tzachovadia?source=rss-dba4898e5506------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 08:15:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tzachovadia/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Making NgRx Cleaner and Simpler]]></title>
            <link>https://medium.com/front-end-beyond/making-ngrx-cleaner-and-simpler-94becad6d30e?source=rss-dba4898e5506------2</link>
            <guid isPermaLink="false">https://medium.com/p/94becad6d30e</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[ngrx]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[front-end-development]]></category>
            <dc:creator><![CDATA[Tzach Ovadia]]></dc:creator>
            <pubDate>Wed, 06 Feb 2019 12:41:36 GMT</pubDate>
            <atom:updated>2019-02-06T12:41:36.788Z</atom:updated>
            <content:encoded><![CDATA[<h4>Patterns to make your NgRx implementation cleaner and clearer</h4><p>NgRx is a great implementation of Redux for Angular. If you already implemented it in a project, you probably noticed your Effects classes were way too big, you had too many selectors and following which component dispatched an action was not as easy as it should have been.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7iP6iO8OyQ0zC-5S6hqezw.jpeg" /></figure><p>We had the same problems, so we came up with some patterns and architectural ideas to solve or at least minimize them.</p><p>These ideas were implemented in two large-scale projects and were very popular among our developers.</p><h3>Module State Service</h3><p>This is a pattern I researched for some time and has proven itself in our projects. The concept is very simple — each module has its own State service and only that State service accesses the store.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/371/1*Yqoq7AhvZe36P6uJOoXhaw.png" /></figure><p>Using a service will make dispatching actions much easier and clearer because you will be calling methods instead of dispatching actions.</p><p>We’ll start by creating the Application-State Service. AppStateService is the root state, also known as shared state, that contains actions and selectors that are relevant for the entire application.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aea93d0e30c7b0ba8ef8d6f7665cb77b/href">https://medium.com/media/aea93d0e30c7b0ba8ef8d6f7665cb77b/href</a></iframe><p>Next, we’ll create a module-specific state service. Components inside this module should not be aware of the Store and only communicate with it using this service.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c436a8726f4f67c0f0fd6cffbbf35ba1/href">https://medium.com/media/c436a8726f4f67c0f0fd6cffbbf35ba1/href</a></iframe><p>In addition to using State Service, I suggest using Container-Component architecture and strive to use the service only in containers.</p><h3>Unifying Selects</h3><p>This is a simple and brilliant piece of code written by Ophir Bushinsky (<a href="https://medium.com/u/43f060b30d61">אופיר בושינסקי</a>):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a18acae382db59b65c689e45dde83537/href">https://medium.com/media/a18acae382db59b65c689e45dde83537/href</a></iframe><p>Now you have a single select method for all state properties (with intellisense, don’t worry…)</p><pre>this.product$ = this.productStateService.select(&#39;product&#39;);</pre><p>This should cover most cases but unfortunately if you need a more complex selector you still need to write it.</p><h3>Cleaning Effects Class</h3><p>Here’s a common effect method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9104e711034d9898990f1cd4e4c51285/href">https://medium.com/media/9104e711034d9898990f1cd4e4c51285/href</a></iframe><p>Normally, you’ll have many methods like this one and soon your Effects class will have 300 lines of code.</p><p>In order to make this class more readable, I suggest extracting the implementation to a separate, pure-as-possible, function. I would also create a separate file for these functions (I usually name it effects.ts)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1abddd46b2d9e0d774e0a3f248bfe1a4/href">https://medium.com/media/1abddd46b2d9e0d774e0a3f248bfe1a4/href</a></iframe><p>Now, we’ll add a simple helper function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec61cf08a8c31507fd6f51c13f12a468/href">https://medium.com/media/ec61cf08a8c31507fd6f51c13f12a468/href</a></iframe><p>And our effects class can be refactored into:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9b9396349a23bbac7f21686b07d39345/href">https://medium.com/media/9b9396349a23bbac7f21686b07d39345/href</a></iframe><p>This refactoring also makes it much easier to test your effects because they are just functions now.</p><h3>Spread Operator</h3><p>The Spread operator (`…`) can be used instead of `<em>Object.assign</em>` to create a cleaner code in reducers.</p><pre>return { …state, loading: true }</pre><blockquote>More about this operator here: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#Spread_in_object_literals">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#Spread_in_object_literals</a></blockquote><h3>Cleaner Error Handling</h3><p>If you want to be able to handle all errors using the same handler (i.e. log them), you can create a class called <em>ErrorAction</em>, which implements <em>Action</em>. Then every error action you create extends it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d411326294ddb539adb05da3d85506e/href">https://medium.com/media/2d411326294ddb539adb05da3d85506e/href</a></iframe><p>Next, catch <em>ErrorAction </em>in effect an do what you want with it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4edd6a2389e196acaef0c5a004d6d65/href">https://medium.com/media/e4edd6a2389e196acaef0c5a004d6d65/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TLnTpQntIzCzf09OEyS4ug.jpeg" /><figcaption>“Look how I simplified everything!”</figcaption></figure><p>That’s it for now. I really hope this will help you bring some order to what sometimes can be chaos.</p><p>I would like to thank Ophir again for his tremendous help.</p><p>If you have any questions, comments or ideas feel free to contact me in the comments section or on Twitter (<a href="https://twitter.com/TzachOvadia"><strong><em>@</em></strong><em>TzachOvadia</em></a>)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=94becad6d30e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/front-end-beyond/making-ngrx-cleaner-and-simpler-94becad6d30e">Making NgRx Cleaner and Simpler</a> was originally published in <a href="https://medium.com/front-end-beyond">Front End Beyond</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[“Fix” Angular in 1 minute]]></title>
            <link>https://medium.com/front-end-beyond/fix-angular-in-1-minute-7d6e3b1901f3?source=rss-dba4898e5506------2</link>
            <guid isPermaLink="false">https://medium.com/p/7d6e3b1901f3</guid>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Tzach Ovadia]]></dc:creator>
            <pubDate>Tue, 11 Dec 2018 13:47:21 GMT</pubDate>
            <atom:updated>2019-01-01T14:17:30.191Z</atom:updated>
            <content:encoded><![CDATA[<h4>Add missing types in Angular and make your coding easier</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*08KnqQi4CMsA_d2FQqoHnQ.jpeg" /></figure><p>I noticed two common cases where Angular uses any instead of the correct type. By using any we’re missing the type-safety and autocomplete features of TypeScript.</p><p>I’ve written these snippets to make those cases strongly-typed and since they work well I decided to share them.</p><h4>Typed SimpleChanges (ngOnChanges)</h4><p><em>This might be a temporary fix because there’s a pending PR (submitted on February 2018).</em></p><pre>import { SimpleChange } from &#39;@angular/core&#39;;</pre><pre>export type TypedSimpleChanges&lt;T&gt; = {<br>  [P in keyof T]: SimpleChange;<br>};</pre><p>Usage:</p><pre>ngOnChanges(changes: TypedSimpleChanges&lt;MyClass&gt;) {<br>  if (changes.myProp &amp;&amp; changes.myProp.isFirstChange()) {<br>  ...<br>  }<br>}</pre><h4>Typed FormGroup</h4><pre>import { FormGroup } from &#39;<a href="http://twitter.com/angular/forms">@angular/forms</a>&#39;;</pre><pre>export interface TypedFormGroup&lt;T&gt; extends FormGroup {<br>  readonly value: Partial&lt;T&gt;;<br>}</pre><p>Usage:</p><pre>form: TypedFormGroup&lt;ContactRequest&gt;;<br>// Now this.form.value type is ContactRequest</pre><h4>Create Typed FormGroup</h4><pre>import { ValidatorFn } from &#39;<a href="http://twitter.com/angular/forms">@angular/forms</a>&#39;;</pre><pre>export type TypedControlsConfig&lt;T&gt; = {<br>  [key in keyof T]?: T[key] | [T[key]] | [T[key], Array&lt;ValidatorFn&gt;];<br>};</pre><pre>export function createForm&lt;T&gt;(fb: FormBuilder, fields: TypedControlsConfig&lt;T&gt;): TypedFormGroup&lt;T&gt; {<br>  return fb.group(fields);<br>}</pre><p>Usage:</p><pre>this.form = createForm&lt;ContactRequest&gt;(this.fb, {<br> fullName: [null, [Validators.required]],<br> email: [null, [Validators.required, Validators.email]],<br> message: [null, [Validators.required, Validators.maxLength(200)]]<br>});</pre><p>Hope you’ll find these snippets useful.</p><p>Follow <a href="https://medium.com/front-end-beyond">Front End Beyond</a> for more Front End related articles.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7d6e3b1901f3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/front-end-beyond/fix-angular-in-1-minute-7d6e3b1901f3">“Fix” Angular in 1 minute</a> was originally published in <a href="https://medium.com/front-end-beyond">Front End Beyond</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Easy Steps to Improve Your Angular Libraries]]></title>
            <link>https://medium.com/front-end-beyond/easy-steps-to-improve-your-angular-libraries-2dd55521eb1?source=rss-dba4898e5506------2</link>
            <guid isPermaLink="false">https://medium.com/p/2dd55521eb1</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[angular-cli]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Tzach Ovadia]]></dc:creator>
            <pubDate>Fri, 13 Jul 2018 08:34:15 GMT</pubDate>
            <atom:updated>2018-07-13T08:34:15.343Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sI03rFHDEkZttdKX43RDag.jpeg" /></figure><p>Angular CLI 6 supports library generation, so it’s a good opportunity to share some patterns, tricks and tips I’ve learned over the past several years I’ve been writing libraries for our Angular applications.</p><h3>Configuration</h3><p>Sometimes you’ll want to give consumers some control over the library’s behavior. You can do this by accepting a configuration from the consumer.</p><p>The cleanest way to enable external configuration for your library is using the forRoot() pattern. The pattern is very simple — create a static method called forRoot that returns `ModuleWithProviders` in your library’s main module.</p><p>This pattern is mostly used for providing services only once so they’ll only have a single instance. However, it can also be used to provide configuration object:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3cd8d22c042984c202e37a0e0f7cc529/href">https://medium.com/media/3cd8d22c042984c202e37a0e0f7cc529/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5fa07ce04b6fc1baa4badea2be5779a3/href">https://medium.com/media/5fa07ce04b6fc1baa4badea2be5779a3/href</a></iframe><p>Then, you simply inject the configuration where needed using @Inject:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b9559cb62c823d056e44e0bd2ebcc60f/href">https://medium.com/media/b9559cb62c823d056e44e0bd2ebcc60f/href</a></iframe><h3>Single Entry Point</h3><p>If possible, make all public API exposed through a single component (service, component, directive etc.) This way users will have an easy access to all library functionality and won’t have any problem finding what they need.</p><p>If your library has several services exposed, try exposing their functionality through a single service.</p><p>If your library is a UI component, use inputs to change its functionality instead of creating several similar components. for example, if you’re writing a carousel component, don’t create one component for carousel with arrows and another for carousel with dots at the bottom.</p><h3>Public Events</h3><p>This is actually a pattern I took from Angular’s Router. The Angular Router exposes events property which is an observable of NavigationEvent. It can be used to listen to every router event and perform actions.</p><p>I use the same pattern to expose events that might be relevant to the consumer. Error events, for example, are something you might want to expose. Instead of using console.error just trigger a LibErrorEvent with all the relevant data and let the consumer decide what to do with it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5649c1081c4e04359a01b0fb925e5312/href">https://medium.com/media/5649c1081c4e04359a01b0fb925e5312/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/60df86e7f696ae195a1fbf11e585efcb/href">https://medium.com/media/60df86e7f696ae195a1fbf11e585efcb/href</a></iframe><h3>Testing Module</h3><p>If you’ll write tests for your library, you’ll probably find yourself providing mock services that are easier to inspect. Instead of providing them in every test suite, create a testing module and export it so consumers can use it in their own tests.</p><h3>Documentation</h3><p>Instead of writing a long and detailed document, I suggest using <a href="http://usejsdoc.org/">JSDoc </a>comments for public components (methods, models etc). JSDoc comments provide intelliSense and are supported by <a href="https://compodoc.github.io/compodoc/">Compodoc</a>,which is a great tool for generating documentation automatically.</p><h3>Peer Dependencies</h3><p>When defining peerDependecies, use the broadest version range possible so npm will be able to reuse packages already installed.</p><h3>SemVer</h3><p>If you are not familiar with the concept of Semantic Versioning (SemVer) you can read about it on <a href="https://semver.org/">https://semver.org/</a></p><p>In a nutshell, it means the version number is defined by its content:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZlMTtRaTt-iEAfNpr_Kaug.png" /><figcaption>Credit: <a href="http://www.jontejada.com/blog/galvanize/talk/2016/01/18/semver/">http://www.jontejada.com/blog/galvanize/talk/2016/01/18/semver/</a></figcaption></figure><p>The first number is incremented when the release contains breaking changes. The second number is incremented when the release contains new features, and the last number is incremented when the release contains bug fixes.</p><p>Following SemVer enables developers to specify a range of versions (~1.0.0, &gt;=1.0.0 &lt;2.0.0) and install the latest version without worrying about breaking their application.</p><p>Thanks to <a href="https://medium.com/u/106f347df3ea">Shai Reznik</a> for his comments.</p><p>Share your thoughts in the comments section below or on Twitter <a href="https://twitter.com/TzachOvadia">@TzachOvadia</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2dd55521eb1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/front-end-beyond/easy-steps-to-improve-your-angular-libraries-2dd55521eb1">Easy Steps to Improve Your Angular Libraries</a> was originally published in <a href="https://medium.com/front-end-beyond">Front End Beyond</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How To Be A Senior Developer]]></title>
            <link>https://medium.com/front-end-beyond/how-to-become-a-senior-developer-227d1fe6772b?source=rss-dba4898e5506------2</link>
            <guid isPermaLink="false">https://medium.com/p/227d1fe6772b</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[developer]]></category>
            <category><![CDATA[team-collaboration]]></category>
            <category><![CDATA[self-improvement]]></category>
            <category><![CDATA[experience]]></category>
            <dc:creator><![CDATA[Tzach Ovadia]]></dc:creator>
            <pubDate>Fri, 22 Dec 2017 12:47:22 GMT</pubDate>
            <atom:updated>2017-12-27T00:49:18.649Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*brvN_0vi3hLWd3YQvvcJrw.jpeg" /></figure><p>I’ll start with a confession: I am not a ninja developer. I’m not the one who can solve any bug in minutes or build an eCommerce site from scratch in a week. I am, however, an important asset for any development team.</p><p>Seniority, unfortunately, is usually determined by years of experience. I believe this should not be the defining parameter, because there’s so much more to being a senior developer than just experience. Here’s what I believe makes a senior developer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZsggKi3Tr5g1Widl3nLoFg.jpeg" /></figure><h3>Experience</h3><p>First, as expected, a senior developer needs to be experienced, at least in his/her own field (front end, back end etc.). This experience does not have to be tied to years in a position: A developer who worked on ten different projects might learn more than a developer who worked on a single project for the same amount of time. Every project I started enabled me to fix problems I previously had and encounter new problems that forced me to learn more about the language, library, framework, platform etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5p3l2tLLRB-VXZo_Dlzv3Q.jpeg" /></figure><h3>Own Your Product</h3><p>Take responsibility for bugs in your product. Many times it won’t be your code that caused the issue, but it’s your responsibility to review every piece of code before it goes to production.</p><p>On the other hand, don’t take credit from your team. If someone praises you for something other team member did, make sure you let that someone know and pass it on to that team member.</p><p>You should not worry about your reputation — No one will think less of you for respecting your team and taking responsibility when things go bad.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FZdafW2he_we_WOy_0MvWw.jpeg" /></figure><h3>Know anything about everything</h3><p>You should have answers for everything, but that’s far from knowing everything.</p><p>You should be able to solve as many problems as possible, however, sometimes you just need to steer a developer in the right direction.</p><p>Knowing a solution exists but not how to implement it can be enough in some cases. For example, you don’t have to know how <a href="https://getbootstrap.com/">Bootstrap</a> works in order to tell a developer it can save him/her time when developing a responsive site.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LTg3z8DXEuzawa8LzVzd_A.jpeg" /></figure><h3>Learn to explain</h3><p>A significant part of your job is teaching and explaining — You’ll need to teach developers how to do things and explain why you think it’s the best solution.</p><blockquote>If you can’t explain something, you probably don’t understand it well enough.</blockquote><p>Many times I find myself presenting a new architecture, a complex solution or some other broad change in code to my team. This requires me to explain why I chose that solution over the other. Right after explaining, I need to teach them how I plan to implement it. It’s worth mentioning that although I come to these meetings with a firm idea, I am very open for discussion.</p><p>The better you explain — the closer the end result will be to your vision.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l8Lw5UB1y3WroNKgqxyNJg.jpeg" /></figure><h3>Be open-minded</h3><p>Do not assume you know more because you are more experienced — you will learn new things from less experienced developers. Also, by explaining them to you, they will have a better grasp on the subject.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*No6MEJ9kBNyx45zLsAaDWA.jpeg" /></figure><h3>Don’t be vague</h3><p>Don’t tell your team they did a poor job, instead, find concrete examples and let them know how they can improve. How many times have someone told you your code doesn’t work? Being this generic has no real value and you immediately dismiss it. However, if the same person comes and explain in details what is not working, you will listen.</p><p>That’s it, my “senior developer manifest”. These ideas worked and still working very well for me, my team, and the company and I hope you can relate to at least some of them.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=227d1fe6772b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/front-end-beyond/how-to-become-a-senior-developer-227d1fe6772b">How To Be A Senior Developer</a> was originally published in <a href="https://medium.com/front-end-beyond">Front End Beyond</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Clean Code]]></title>
            <link>https://medium.com/front-end-beyond/clean-code-6fbe685a8284?source=rss-dba4898e5506------2</link>
            <guid isPermaLink="false">https://medium.com/p/6fbe685a8284</guid>
            <category><![CDATA[best-practices]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[clean-code]]></category>
            <dc:creator><![CDATA[Tzach Ovadia]]></dc:creator>
            <pubDate>Sat, 16 Dec 2017 19:12:17 GMT</pubDate>
            <atom:updated>2018-07-06T09:51:15.332Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/845/1*oFaYb9_wHWVGh7WL7ruNeA.jpeg" /></figure><p>Writing clean code is a virtue of a good programmer. Making your code human-readable and more understandable is usually not a hard task and can be accomplished by doing some fairly simple refactoring.</p><p>In this short post, I’ll tackle cases that I believe are easiest to solve. In future posts on this subject I’ll demonstrate how to refactor more complex issues.</p><h3>Reduce nesting by exiting the function as soon as possible</h3><p>Nesting reduce readability. Take a look at this common code snippet:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f90d233aab6d1d1eddca9b3ba35c19d/href">https://medium.com/media/6f90d233aab6d1d1eddca9b3ba35c19d/href</a></iframe><p>In order to understand which condition returned Username cannot be empty you’ll have to scan the code and match if clauses.</p><p>This can be refactored into a much more easy-to-read code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/35e431d99b29c37ca1aa80fa921978d4/href">https://medium.com/media/35e431d99b29c37ca1aa80fa921978d4/href</a></iframe><h3>Use shorthand when it doesn’t affect readability</h3><p>“Less is more” is actually something worth remembering when it comes to coding. The less code you write — the less code you’ll need to debug.</p><p>Using shorthand can make your code cleaner. When using arrow functions the return is implied, so there’s no need to write it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/adcba1708787ea3bbc340fbd57750bc3/href">https://medium.com/media/adcba1708787ea3bbc340fbd57750bc3/href</a></iframe><p>Can be refactored into this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/93ec23ee97fd8632128d7e7a2c9b9638/href">https://medium.com/media/93ec23ee97fd8632128d7e7a2c9b9638/href</a></iframe><h3>Move complex IF conditions into functions</h3><p>It should not take more than two seconds to understand an IF condition. If understanding the condition takes more than that, you should move its logic into a properly-named function.</p><p>Take a look at this snippet, which was actually taken from a project I’m working on:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0fc43cdea3b432f147d225d81c26c41a/href">https://medium.com/media/0fc43cdea3b432f147d225d81c26c41a/href</a></iframe><p>It’s pretty hard to understand what this statement checks. Let’s refactor it into several functions:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/909c5d0da94f72e216768a7a2ad819a1/href">https://medium.com/media/909c5d0da94f72e216768a7a2ad819a1/href</a></iframe><p>Now you don’t need to spend time on understanding the implementation — you just need to read the function’s name.</p><h3>Write short functions</h3><p>A general rule-of-thumb is that your function should not have scrollbars, this usually means about 35 lines of code for a complex function.</p><p>If you’re following the <a href="http://www.oodesign.com/single-responsibility-principle.html">Single Responsibility Principle</a>, there’s less chance you’ll have long functions. If you do, try splitting it into smaller functions.</p><p>Let’s have a look at a function taken from Google’s Payment API example (+ some UI tweaks):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a8e660758721a83f28ed7b94d952a9d6/href">https://medium.com/media/a8e660758721a83f28ed7b94d952a9d6/href</a></iframe><p>Now, usually, having comments inside a method means you should extract the code to a function named in a way that will eliminate the need for a comment, but let’s not focus on it for now.</p><p>This is a refactored version of the same method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3bb5603b128f95ff63a13d9a748a0568/href">https://medium.com/media/3bb5603b128f95ff63a13d9a748a0568/href</a></iframe><p>That’s all for now, I really hope you’ll find these tips helpful. Let me know what you think.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6fbe685a8284" width="1" height="1" alt=""><hr><p><a href="https://medium.com/front-end-beyond/clean-code-6fbe685a8284">Clean Code</a> was originally published in <a href="https://medium.com/front-end-beyond">Front End Beyond</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>