<?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[Codezillas - Medium]]></title>
        <description><![CDATA[Sharing knowledge, ideas and experiance. - Medium]]></description>
        <link>https://medium.com/codezillas?source=rss----255c770a4a8a---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Codezillas - Medium</title>
            <link>https://medium.com/codezillas?source=rss----255c770a4a8a---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 18:23:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/codezillas" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How To Reuse React Components]]></title>
            <link>https://medium.com/codezillas/how-to-reuse-react-components-851ffcc68a9c?source=rss----255c770a4a8a---4</link>
            <guid isPermaLink="false">https://medium.com/p/851ffcc68a9c</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Sabesan Sathananthan]]></dc:creator>
            <pubDate>Thu, 24 Jun 2021 15:25:34 GMT</pubDate>
            <atom:updated>2021-06-24T15:34:27.299Z</atom:updated>
            <content:encoded><![CDATA[<h4>Mixins, HOC, render props, and Hooks are four ways to reuse components</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HvyPiGMOis-_Gxp6cyttiA.jpeg" /><figcaption>Photo by <a href="https://www.pexels.com/@vlada-karpovich?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Vlada Karpovich</a> from <a href="https://www.pexels.com/photo/photo-of-pen-beside-ink-4668356/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pexels</a></figcaption></figure><p>Now frontend engineering is more and more important. Although Ctrl+C and Ctrl+V can also be used to complete requirements, once they are modified, it becomes a huge task. Therefore, copying of code is reduced, and the packaging and reuse capabilities are increased to achieve maintainability and reversibility. The code used becomes particularly important.</p><p>In React, components are the main unit of code reuse. The combination-based component reuse mechanism is quite elegant, but for more fine-grained logic (state logic, behavior logic, etc.), reuse is not so easy. It is difficult to disassemble the state logic as a reusable function or component. In fact, before the appearance of Hooks, there was a lack of a simple and direct way of component behavior extension, which is considered to be mixins, higher-order components (HOC), and render props. The upper-level model explored under the existing (component mechanism) game rules has not solved the problem of logic reuse between components from the root. This is my 38th Medium article.</p><h3>Mixins</h3><p>Of course, React no longer recommends using mixins as a reuse solution for a long time, but it can still provide support for mixins through create-react-class. Note that mixins are not supported when declaring components in ES6 classes.</p><p>Mixins allow multiple React components to share code. They are very similar to mixins in Python or traits in PHP. The emergence of the mixin solution comes from an OOP intuition. In the early days, it only provided React.createClass() API to define components. (In React v15.5.0, it is officially abandoned and moved to create-react-class). Naturally, (class) inheritance has become an intuitive attempt, and in JavaScript prototype-based extension mode, it is similar to the inherited mixin scheme. It has become a good solution. Mixin is mainly used to solve the reuse problem of life cycle logic and state logic, and allows the component life cycle to be extended from the outside. This is especially important in Flux and other modes, but many defects have also appeared in continuous practice:</p><ul><li>There is an implicit dependency between the component and the mixin (Mixin often depends on the specific method of the component, but the dependency is not known when the component is defined).</li><li>There may be conflicts between multiple mixin (such as defining the same state field).</li><li>Mixin tends to add more states, which reduces the predictability of the application and leads to a sharp increase in complexity.</li><li>Implicit dependencies lead to opaque dependencies, and maintenance costs and understanding costs are rising rapidly.</li><li>It is difficult to quickly understand the behavior of components, and it is necessary to fully understand all the extension behaviors that rely on mixin and their mutual influence.</li><li>The method and state field of the component itself is afraid to be easily deleted because it is difficult to determine whether mixin depends on it.</li><li>Mixin is also difficult to maintain, because Mixin logic will eventually be flattened and merged together, and it is difficult to figure out the input and output of a Mixin.</li></ul><p>There is no doubt that these problems are fatal, so Reactv0.13.0 abandoned Mixin static crosscutting (similar to inherited reuse) and moved to HOC higher-order components (similar to combined reuse).</p><h4>Example</h4><p>The example of the ancient version, a common scenario is: A component needs to be updated regularly. It is easy to do it with setInterval(), but it is very important to cancel the timer when it is not needed to save memory. React provides a lifecycle method to inform the component. The time of creation or destruction, the following Mixin, use setInterval() and ensure that the timer is cleaned up when the component is destroyed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6fd5344ded564231f982182c4310121b/href">https://medium.com/media/6fd5344ded564231f982182c4310121b/href</a></iframe><h3>HOC</h3><p>After Mixin, HOC high-order components take on the heavy responsibility and become the recommended solution for logical reuse between components. High-order components reveal a high-order atmosphere from their names. In fact, this concept should be derived from high-order functions of JavaScript. The high-order function is a function that accepts a function as input or output. It can be thought that currying is a higher-order function. The definition of higher-order components is also given in the React document. Higher-order components receive components and return new components. function. The specific meaning is: High-order components can be seen as an implementation of React decoration pattern. High-order components are a function, and the function accepts a component as a parameter and returns a new component. It will return an enhanced React components. High-order components can make our code more reusable, logical and abstract, can hijack the render method, and can also control propsand state.</p><p>Comparing Mixin and HOC, Mixin is a mixed-in mode. In actual use, Mixin is still very powerful, allowing us to share the same method in multiple components, but it will also continue to add new methods and attributes to the components. The component itself can not only perceive but also need to do related processing (such as naming conflicts, state maintenance, etc.). Once the mixed modules increase, the entire component becomes difficult to maintain. Mixin may introduce invisible attributes, such as in the Mixin method used in the rendering component brings invisible property props and states to the component. Mixin may depend on each other and is coupled with each other, which is not conducive to code maintenance. In addition, the methods in different Mixin may conflict with each other. Previously React officially recommended using Mixin to solve problems related to cross-cutting concerns, but because using Mixin may cause more trouble, the official recommendation is now to use HOC. High-order component HOC belong to the idea of ​​ functional programming. The wrapped components will not be aware of the existence of high-order components, and the components returned by high-order components will have a functional enhancement effect on the original components. Based on this, React officially recommends the use of high-order components.</p><p>Although HOC does not have so many fatal problems, it also has some minor flaws:</p><ul><li>Scalability restriction: HOC cannot completely replace Mixin. In some scenarios, Mixin can but HOC cannot. For example, PureRenderMixin, because HOC cannot access the State of subcomponents from the outside, and at the same time filter out unnecessary updates through shouldComponentUpdate. Therefore, React After supporting ES6Class, React.PureComponent is provided to solve this problem.</li><li>Ref transfer problem: Ref is cut off. The transfer problem of Ref is quite annoying under the layers of packaging. The function Ref can alleviate part of it (allowing HOC to learn about node creation and destruction), so the React.forwardRef API API was introduced later.</li><li>WrapperHell: HOC is flooded, and WrapperHell appears (there is no problem that cannot be solved by one layer, if there is, then two layers). Multi-layer abstraction also increases complexity and cost of understanding. This is the most critical defect. In HOC mode There is no good solution.</li></ul><h4>Example</h4><p>Specifically, a high-order component is a function whose parameter is a component and the return value is a new component. A component converts props into a UI but a high-order component converts a component into another component. HOC is very common in React third-party libraries, such as Redux’s connect and Relay’s createFragmentContainer.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/90068090af75d82f09618a2e3b129735/href">https://medium.com/media/90068090af75d82f09618a2e3b129735/href</a></iframe><p>Attention should be paid here, do not try to modify the component prototype in the HOC in any way, but should use the combination method to realize the function by packaging the component in the container component. Under normal circumstances, there are two ways to implement high-order components:</p><ul><li>Property agent Props Proxy.</li><li>Reverse inheritance Inheritance Inversion.</li></ul><h4>Property Agent</h4><p>For example, we can add a stored id attribute value to the incoming component. We can add a props to this component through high-order components. Of course, we can also operate on the props in the WrappedComponent component in JSX. Note that it is not to manipulate the incoming WrappedComponent class, we should not directly modify the incoming component, but can operate on it in the process of combination.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/49e24540e51916b4fb3de4aaabb24ed1/href">https://medium.com/media/49e24540e51916b4fb3de4aaabb24ed1/href</a></iframe><p>We can also use high-order components to load the state of new components into the packaged components. For example, we can use high-order components to convert uncontrolled components into controlled components.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c21c2997d27e3522421b423881bfa3a3/href">https://medium.com/media/c21c2997d27e3522421b423881bfa3a3/href</a></iframe><p>Or our purpose is to wrap it with other components to achieve the purpose of layout or style.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a67368bf8f443f35fa16cb084dd3581b/href">https://medium.com/media/a67368bf8f443f35fa16cb084dd3581b/href</a></iframe><h4>Reverse inheritance</h4><p>Reverse inheritance means that the returned component inherits the previous component. In reverse inheritance, we can do a lot of operations, modify state, props and even flip the Element Tree. There is an important point in the reverse inheritance that reverse inheritance cannot ensure that the complete sub-component tree is parsed. That means if the parsed element tree contains components (function type or Class type), the sub-components of the component can no longer be manipulated.</p><p>When we use reverse inheritance to implement high-order components, we can control rendering through rendering hijacking. Specifically, we can consciously control the rendering process of WrappedComponent to control the results of rendering control. For example, we can decide whether to render components according to some parameters.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f51f1a2e79f2d530f865980dba2158b/href">https://medium.com/media/2f51f1a2e79f2d530f865980dba2158b/href</a></iframe><p>We can even hijack the life cycle of the original component by rewriting.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d0d1d1a2e3655228e9e6214117d3fa91/href">https://medium.com/media/d0d1d1a2e3655228e9e6214117d3fa91/href</a></iframe><p>Since it is actually an inheritance relationship, we can read the props and state of the component. If necessary, we can even add, modify, and delete the props and state. Of course, the premise is that the risks caused by the modification need to be controlled by yourself. In some cases, we may need to pass in some parameters for the high-order attributes, then we can pass in the parameters in the form of currying, and cooperate with the high-order components to complete the operation similar to the closure of the component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c93764203e66ee528fa2590b469fe091/href">https://medium.com/media/c93764203e66ee528fa2590b469fe091/href</a></iframe><h4>note</h4><p><strong>Don’t change the original components</strong></p><p>Don’t try to modify the component prototype in HOC, or change it in other ways.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38ab4b11e2b2e2bd581eca1ce0cfc6e6/href">https://medium.com/media/38ab4b11e2b2e2bd581eca1ce0cfc6e6/href</a></iframe><p>Doing so will have some undesirable consequences. One is that the input component can no longer be used as before the HOC enhancement. What is more serious is that if you use another HOC that also modifies componentDidUpdate to enhance it, the previous HOC will be invalid, and this HOC cannot be applied to functional components that have no life cycle.<br>Modifying the HOC of the incoming component is a bad abstraction, and the caller must know how they are implemented to avoid conflicts with other HOC. HOC should not modify the incoming components, but should use a combination of components to achieve functions by packaging the components in container components.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d539457779e7022762037d655c757e3/href">https://medium.com/media/9d539457779e7022762037d655c757e3/href</a></iframe><p><strong>Filter props</strong></p><p>HOC adds features to components and should not significantly change the convention itself. The components returned by HOC should maintain similar interfaces with the original components. HOC should transparently transmit props that have nothing to do with itself, and most HOC should include a render method similar to the following.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/674a9a7a926cee12af9dbd9e6423db36/href">https://medium.com/media/674a9a7a926cee12af9dbd9e6423db36/href</a></iframe><p><strong>Maximum composability</strong></p><p>Not all HOCs are the same. Sometimes it only accepts one parameter, which is the packaged component.</p><pre>const NavbarWithRouter = withRouter(Navbar);</pre><p>HOC can usually receive multiple parameters. For example, in Relay, HOC additionally receives a configuration object to specify the data dependency of the component.</p><pre>const CommentWithRelay = Relay.createContainer(Comment, config);</pre><p>The most common HOC signatures are as follows, connect is a higher-order function that returns higher-order components.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/590de18c82ad90e07cf1f8fed23a9845/href">https://medium.com/media/590de18c82ad90e07cf1f8fed23a9845/href</a></iframe><p>This form may seem confusing or unnecessary, but it has a useful property, like the single-parameter HOC returned by the connect function has the signature Component =&gt; Component , and functions with the same output type and input type can be easily combined. The same attributes also allow connect and other HOCs to assume the role of decorator. In addition, many third-party libraries provide compose tool functions, including lodash, Redux, and Ramda.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9cbc152eb50e35834c4d012ba48bfdbe/href">https://medium.com/media/9cbc152eb50e35834c4d012ba48bfdbe/href</a></iframe><p><strong>Don’t use HOC in the render method</strong></p><p>React ’s diff algorithm uses the component identifier to determine whether it should update the existing subtree or discard it and mount the new subtree. If the component returned from the render is the same as the component in the previous render ===, React passes The subtree is distinguished from the new subtree to recursively update the subtree, and if they are not equal, the previous subtree is completely unloaded.<br>Usually, you don’t need to consider this when using it, but it is very important for HOC, because it means that you should not apply HOC to a component in the render method of the component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01ec9da706301817b5d6ae40a4748a33/href">https://medium.com/media/01ec9da706301817b5d6ae40a4748a33/href</a></iframe><p>This is not just a performance issue. Re-mounting the component will cause the state of the component and all its subcomponents to be lost. If the HOC is created outside the component, the component will only be created once. So every time you render it will be the same component. Generally speaking, this is consistent with your expected performance. In rare cases, you need to call HOC dynamically, you can call it in the component’s lifecycle method or its constructor.</p><p><strong>Be sure to copy static methods</strong></p><p>Sometimes it is useful to define static methods on React components. For example, the Relay container exposes a static method getFragment to facilitate the composition of GraphQL fragments. But when you apply HOC to a component, the original component will be packaged with a container component, which means that the new component does not have any static methods of the original component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/efea68083923fe3dd728247948db447f/href">https://medium.com/media/efea68083923fe3dd728247948db447f/href</a></iframe><p>To solve this problem, you can copy these methods to the container component before returning.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9abac726b9373ce25fce08cf13c3fc22/href">https://medium.com/media/9abac726b9373ce25fce08cf13c3fc22/href</a></iframe><p>But to do this, you need to know which methods should be copied. You can use hoist-non-react-statics to automatically copy all non-React static methods.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/791281382a5f23b7de315432be7c3aab/href">https://medium.com/media/791281382a5f23b7de315432be7c3aab/href</a></iframe><p>In addition to exporting components, another feasible solution is to additionally export this static method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d9ffdbc5203c4e03a2864d4b6ceadcb/href">https://medium.com/media/3d9ffdbc5203c4e03a2864d4b6ceadcb/href</a></iframe><p><strong>Refs will not be passed</strong></p><p>Although the convention of high-level components is to pass all props to the packaged component, this does not apply to refs, because ref is not actually a prop, just like a key, it is specifically handled by React. If the ref is added to the return component of the HOC, the ref reference points to the container component, not the packaged component. This problem can be explicitly forwarded to the internal component through the React.forwardRefAPI refs.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b2badf123b0fde8ea364b52fc69fb78c/href">https://medium.com/media/b2badf123b0fde8ea364b52fc69fb78c/href</a></iframe><h3>Render Props</h3><p>Like HOC, Render props is also a veteran model that has always existed. render props refers to a simple technology that uses a props valued as a function to share code between a kind of React components. A component with render props receives a function. This function Return a React element and call it instead of implementing its own rendering logic. Render props is a function props used to tell the component what content needs to be rendered. It is also a way to implement component logic reuse. Simply put, it is being copied. In the component used, pass a prop property named render (the property name may not render, as long as the value is a function). The property is a function. This function accepts an object and returns a subcomponent, which will The object in the function parameter is passed as props to the newly generated component, and when using the caller component, you only need to decide where to renderthe component and what logic to renderand pass in the relevant object.<br>Comparing HOC and Render props, technically, both are based on the component combination mechanism. Render props has the same extensibility as HOC. It is called Render props. It does not mean that it can only be used to reuse rendering logic, but that it is here. In this mode, the components are combined through render(), similar to the establishment of a combination relationship through Wrapper’s render() in HOC mode. The two are very similar, and they will also produce a layer of Wrapper. In fact, Render props and HOC . It can even be converted to each other.<br>Similarly, Render props will have some problems:</p><ul><li>The data flow is more intuitive. The descendant components can clearly see the source of the data, but in essence, Render props is implemented based on closures. A large number of component reuse will inevitably introduce the callback hell problem.</li><li>The context of the component is lost, so there is no this.propsproperty, and this.props.childern cannot be accessed like HOC.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/64a91bfecbecd6c5b4769f5780282dbb/href">https://medium.com/media/64a91bfecbecd6c5b4769f5780282dbb/href</a></iframe><h3>Hooks</h3><p>There are endless code reuse solutions, but overall code reuse is still very complicated. A large part of this is because fine-grained code reuse should not be bundled with component reuse. HOC, Render props, etc. are based on component combination The solution is equivalent to first packaging the logic to be reused into components, and then using the component reuse mechanism to achieve logic reuse. Naturally, it is limited to component reuse, so there are problems such as limited scalability, Ref partition, Wrapper Hell, etc. , Then we need to have a simple and direct way of code reuse. Functions. Separating reusable logic into functions should be the most direct and cost-effective way of code reuse. But for state logic, some abstract patterns are still needed. (Such as Observable) can be reused, which is exactly the idea of ​​Hooks, using functions as the smallest code reuse unit, and built-in some modes to simplify the reuse of state logic. Compared with the other solutions mentioned above, Hooks makes the logic reuse within the component no longer bundled with the component reuse. It is really trying to solve the problem of fine-grained logic reuse (between components) from the lower level. In addition, this statement The modular logic reuse scheme further extends the explicit data flow and combination ideas between components to the components.<br>File Hooks are not perfect either, but for now, its disadvantages are as follows:</p><ul><li>The additional learning cost mainly lies in the comparison between Functional Component and Class Component .</li><li>There are restrictions on the writing method (cannot appear in conditions, loops), and the writing restrictions increase the cost of reconstruction.</li><li>It destroys the performance optimization effect of PureComponent and React.memo shallow comparison. In order to get the latest props and state, the event function must be recreated every render()</li><li>In closure scenarios, old state and props values may be referenced.</li><li>The internal implementation is not intuitive, relying on a mutable global state, and no longer so pure.</li><li>React.memo can’t completely replace shouldComponentUpdate (because state change is not available, only for props change).</li><li>The useStateAPI is not perfect in design.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b1308a30a4cc6f973b8276a25aec06d/href">https://medium.com/media/6b1308a30a4cc6f973b8276a25aec06d/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=851ffcc68a9c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codezillas/how-to-reuse-react-components-851ffcc68a9c">How To Reuse React Components</a> was originally published in <a href="https://medium.com/codezillas">Codezillas</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[PID Controllers explained without Maths]]></title>
            <link>https://medium.com/codezillas/pid-controllers-explained-without-maths-551faf251d4f?source=rss----255c770a4a8a---4</link>
            <guid isPermaLink="false">https://medium.com/p/551faf251d4f</guid>
            <category><![CDATA[control-system]]></category>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[pid-controller]]></category>
            <category><![CDATA[control]]></category>
            <category><![CDATA[system]]></category>
            <dc:creator><![CDATA[Dashanka De Silva]]></dc:creator>
            <pubDate>Sat, 27 Jun 2020 17:31:35 GMT</pubDate>
            <atom:updated>2020-09-26T08:56:36.735Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*2RhVR_CxtYbrRCZwFa4veg.png" /><figcaption>A basic illustration of a PID control loop(Image source: <a href="https://www.elprocus.com/the-working-of-a-pid-controller/">https://www.elprocus.com/the-working-of-a-pid-controller/</a>)</figcaption></figure><p>PID controller is an essential control tool and widely used in industry for many process automation applications. Therefore, it is important to understand the concept of a PID control with underlying mathematics if you are from an engineering or technology background. However, in this article, we skip the math and only discuss the concept of PID control for the ease of understanding.</p><p>The basic idea of a PID controller is to use closed-loop feedback to control the process variables. Therefore, we can start with a simple control system with a Plant and a Controller. For simplicity let us start with the Plant or the system we want to control or change the behaviour. In the right-hand side of figure 1, we have a simple plant or with the input feeding into the plant and the output coming out from the plant. The goal of control engineering is to develop an appropriate input signal to plant so we would get the desired output. Now let us focus on the controller in the middle. In the left end of figure 1, we have something called reference or what we want our system to do. Some often call this command variable as well. What we do here is we compare the output of the plant with the reference. The output of the system is fed back to compare and control the system. That is why this process is known as feedback control. The difference provides us with how the plant has deviated from the desired behaviour or the error of the system in other words. This so-called error would be zero if the output is exactly equal to the reference and that is the expected behaviour of the system. Therefore, we introduce the controller to fix that by taking the error and transform it into a suitable input signal to the plant so we get the desired output. However, this does not happen in one shot mostly but over time. As the controller generates input signals based on the estimated error such a way that the error goes down to zero.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/588/1*iDivTkbC30bxlfRA6nwA3Q.jpeg" /><figcaption>Figure 1: Feedback (closed-loop) control system illustration.</figcaption></figure><p>Let us take a practical example to understand how error can be used by the controller to generate input signals to the plant. We have a mobile robot that requires to move a desired distance. Now in terms of control systems, the plant is the robot, the input is the robot speed, the output is the distance from beginning, and the reference is the distance we want it to move. In the beginning, the error is equal to the reference. Let us say that the controller can decide the robot speed based on that error. We can set an arbitrary proportional gain as the multiplier term for the controller. Error multiplied by the proportional gain provides the controller output or the speed input to the robot. Therefore, the robot has the maximum speed due to the maximum error at the beginning. As it moves forward the error decreases and hence the speed as well. When it reaches very close to goal the speed would tend to zero and stops eventually. One can nicely plot how distance from starting point increase and how speed is gradually decreasing over time. Furthermore, changing the multiplier term, it can change the rate of change of speed. This type of controller is called a <strong>proportional controller</strong>.</p><p>However, this proportional controller does not solve everything. Let us clear this by thinking of an altitude controller of an aerial drone. To hover it at a desired altitude, the trust generated by drone propellers must equal to the weight. But first, the drone has to reach the desired altitude. We can use the same proportional controller to control the thrust of the drone propellers, which means the thrust is now proportional to the error. The error explains the difference between the current altitude and the target altitude. At the beginning due to maximum error, the propeller will create a significant thrust that the drone will lift off from the ground or altitude zero and go up while overcoming the weight. The drone will reach close to the desired hovering altitude as it rises.</p><p>Now the error will become very small or tend to zero and hence the thrust will also go to near zero. This would create a loss of trust to keep up the upward motion and start falling due to gravity. When it loses altitude the thrust would again start increasing as the error grows which sensed and reacted by the controller. This happens because the controller is only focused on the present error of the motion. The oscillating motion of going up and going down will damp until the error related thrust is equal to the weight of the drone and will not reach the desired altitude eventually. Even though we increase the proportional gain, the error term will always dominate the thrust and when the error is reaching zero, the thrust will also reach zero. One can argue that by introducing a bias term to the thrust equation to compensate the weight of the drone might solve the problem and it is also possible. But our goal is to create a stable controller. This particular error related to height is where the drone eventually hovers. However, this steady-state hovering altitude will always stay below the desired altitude. Furthermore, this particular error is known as the steady-state error.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*g8AclK6hgoEWi2ORJX7DOg.png" /><figcaption>Figure 2: Proportional integral controller</figcaption></figure><p>Now the goal is to avoid this steady-state error and this can be done by letting the controller use the past information of motion. To achieve this, we can introduce an additional <strong>integrator </strong>part to our proportional controller in parallel as shown in figure 2. As integrator keeps a running sum of the input over time, it enables us to keep a memory or an idea of what happened in the past. Now let us call our modified controller as a <strong>proportional integral controller</strong>. With this new controller, we can consider both past and present errors. When the drone reaches a steady state like discussed before. The integrator part will sum up that non zero error and as a result, the combined output from both parts will increase. As a result, the drone would keep going up towards the target hight. When the error becomes very small as it reaches the desired altitude, the contribution from the proportional part becomes very small or zero. But the integral part would not become zero as there is a non zero error sum and will not let the drone to lose altitude but to keep moving up, which is nice since we have not reached the target altitude yet. Eventually, the drone would hover at the desired altitude but after an oscillatory behaviour due to excess thrust generation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*oNiQazlrgyoHT78vfGBDdw.png" /><figcaption>Figure 3: Proportional integral derivative controller or PID controller</figcaption></figure><p>This is acceptable as the discussed controller fulfils our requirement. However, depending on how the drone has climbed up to the desired altitude, the integration part may have summed up the error that could generate an excess thrust. Due to this, the drone could fly over the desired altitude and create an overshoot. But the negative error from overshoot would again bring down the drone to by reducing the thrust. Eventually, such behaviour will create an oscillating like behaviour before hovering at the target altitude. The occurred overshooting and oscillating behaviour might not desirable since our effort is to develop a stable controller. This can overcome by introducing a <strong>derivative </strong>part to the controller parallel to the existing two parts as shown in figure 3. By introducing the derivative part, we can predict how fast the error would change while enabling us to predict how the motion will change. Now let us apply this future prediction part to the drone example. Let us say the drone is moving up towards the desired altitude and the error is reducing. Therefore, the rate of change of error would be a negative value. Now, this negative value, which is the output from the derivative controller part will make a negative effect on the overall controller output and hence lowers the thrust. The idea here is to use the rate change of error to determine whether the drone reaches the target altitude fast or slow. Furthermore, the size of the derivative controller output would determine how fast the drone accelerating or decelerating regardless of the sign. Importantly, the derivative controller part slows the drone down prematurely if it is moving too fast while preventing or minimizing undesirable overshoot.</p><p>Now our controller is upgraded to a <strong>proportional integral derivative</strong> controller or <strong>PID </strong>controller. It is capable of examining how the error has changed, how the error changes, and how the error would change based on the closed-loop feedback and then outputting appropriate control signals to the plant. One can decide how far these 3 sub-controllers should contribute to the overall output by introducing weights to each part. This is also known as <strong>tuning </strong>the controller and it is decided during the design phase of the PID controller. PID controllers are so popular because they can be used for a wide variety of real applications successfully such as industrial process control, robotics, mechatronics systems, and many more.</p><p>In this article, we have discussed how a PID controller works with a little bit of control theory without involving maths. It is important to understand the math behind and designing PID controllers. Popular tools such as MATLAB can be easily used to design PID controllers and to develop systems for real-world applications. I hope the used examples helped readers to clear the idea. Moreover, I look forward to discussing more on this and other control systems involved topics on later articles.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=551faf251d4f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codezillas/pid-controllers-explained-without-maths-551faf251d4f">PID Controllers explained without Maths</a> was originally published in <a href="https://medium.com/codezillas">Codezillas</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a RESTful API with Go (Part 1)]]></title>
            <link>https://medium.com/codezillas/building-a-restful-api-with-go-part-1-9e234774b14d?source=rss----255c770a4a8a---4</link>
            <guid isPermaLink="false">https://medium.com/p/9e234774b14d</guid>
            <category><![CDATA[servers]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[rest-api]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[mu-x]]></category>
            <dc:creator><![CDATA[John Eckert]]></dc:creator>
            <pubDate>Thu, 15 Aug 2019 13:32:57 GMT</pubDate>
            <atom:updated>2019-08-15T13:32:57.763Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*30dQdx5M04Evuyn30eFf2Q.png" /></figure><p>Last week I decided that I wanted to learn more about Go. Since it was lunch time, I figured I’d use Go to write a simple backend API for an app that lists a bunch of different sushi rolls and their ingredients. It seemed like it would be an excellent way to get used to the syntax of the language and also learn how to use it in a practical application. My first step was to watch a bunch of tutorials on youtube.</p><h3>Step 0 — Setup Your Environment</h3><p>Before doing anything, we need to have Go installed on our machine. I’m not going to go into detail about that here. If like me, you are starting from zero, you can check out the <a href="https://golang.org/">documentation</a> or <a href="https://youtu.be/5qI8z_lB5Lw">this tutorial</a> which I found helpful. It’s pretty easy, the only significant difference with Go is that all of your projects reside in the same folder on your computer which is called your Go Workspace.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FRb6unv0ddpPCo%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FRb6unv0ddpPCo%2Fgiphy.gif&amp;image=https%3A%2F%2Fmedia2.giphy.com%2Fmedia%2Fv1.Y2lkPTc5MGI3NjExc2V1OGR6MWw2cXM5cXF0Mm9hamhwaGhtcm1zajY0MHZpdG1zNThqeiZlcD12MV9naWZzX2dpZklkJmN0PWc%2FRb6unv0ddpPCo%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="305" frameborder="0" scrolling="no"><a href="https://medium.com/media/acc3388162683c9cb11170b72c876e82/href">https://medium.com/media/acc3388162683c9cb11170b72c876e82/href</a></iframe><h3>Step One — Basic Structure</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*McQQ9efunX_0JKb0-8DbMQ.png" /></figure><p>At the top, we have the package declaration. In this case, we are using main which tells the compiler that this will be an executable file. If we were making a library, we would declare a name for it here and this would be the package name we would use whenever we accessed the functions later. For example, if we wrote package maki and then wrote a function called AddWasabi(), we would call that function with maki.AddWasabi().</p><p>Next, we have the import keyword. This is where we list all of the libraries that we will use. The libraries’ names will go inside the parentheses as strings. For example, if we needed our AddWasabi() function, we would need to put &quot;maki&quot; here.</p><p>Finally, our main() function. Since this will be an executable file, main() is what will get called when our program runs. In other words, this is where the magic happens!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FF0uUtL7lmALCM%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FF0uUtL7lmALCM%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2FF0uUtL7lmALCM%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="228" frameborder="0" scrolling="no"><a href="https://medium.com/media/d4da823bee2da6417141c3b4b269d5ae/href">https://medium.com/media/d4da823bee2da6417141c3b4b269d5ae/href</a></iframe><h3>Step Two — Let’s Make a Model</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B4uS20rQ3dm5Gco8oWC1DQ.png" /></figure><p>Since we want to be able to render our data as json, we need to add &quot;encoding/json&quot; to our import statement.</p><p>Next, we need to create a model to represent our different sushi rolls. We will do this above our main function, and we will use a struct. A struct is used for Object Oriented programming in Go. It’s a user-defined data type that contains a collection of fields. It’s kind of like a class in ES6, in that it can have methods and properties that we define. To define a struct, we use the type keyword, an identifier for our new data type, and the struct keyword followed by curly braces containing the desired named properties.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QQvPs0R6c7FzG0WhALC5Ag.png" /></figure><p>For my sushi app, I created a struct named Roll with four properties: ID, ImageNumber, Name, and Ingredients. For simplicity’s sake, I made each property be of type string. We define a property by writing it’s identifier, then it’s data type, and finally it’s json identifier wrapped in back ticks.</p><p>Since we won’t be using a database for this little exercise, we need a variable to hold all of our sushi rolls. For that, we will use a data type called a slice.</p><p>This was a new concept for me. In Go, arrays have a specified length that cannot be changed, but there is another data type called a slice which is like an array but is of variable length.</p><p>We create our slice using the var keyword followed by the name of the variable. To specify that our new variable is a slice we use square brackets followed by the data type our slice will contain (in this case instances of our struct Roll.) Now we have a place to hold all that delicious sushi!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FAwF74xIsyoiKQ%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FAwF74xIsyoiKQ%2Fgiphy.gif&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FAwF74xIsyoiKQ%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="260" frameborder="0" scrolling="no"><a href="https://medium.com/media/d10bf1ad64fb8cb81d1330d19a6de2e1/href">https://medium.com/media/d10bf1ad64fb8cb81d1330d19a6de2e1/href</a></iframe><h3>Step Three — Implementing the Router</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XF85-QcUhSHdH575ZYOw4w.png" /></figure><p>Ok, time to start building our routes. Before we dive in, we are going to need to get a 3rd party library called <a href="https://github.com/gorilla/mux">mux</a>. To install 3rd party libraries, we use the go get command. So the first thing we need to do is run the following line in terminal.</p><pre>go get -u github.com/gorilla/mux</pre><p>If you look in your go path in Go/src/github.com/ you will see your new library fresh from the internet. Let’s add it to our import. To add 3rd party libraries we need to specify their paths starting inside the src folder. Also, while we’re there let’s add some more standard libraries:</p><ul><li>&quot;net/http&quot; which has some client/server functionality we will need.</li><li>&quot;log&quot; we will use this for a little error handling.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QHpjW7cRgqIntRXb0A3eOQ.png" /></figure><p>Now that we have all the libraries we need let’s make our router. This time we will be writing in the main function since this code will be executed whenever we run our executable file.</p><p>It’s time to use that <a href="https://github.com/gorilla/mux">mux</a> library we downloaded. The first thing we need to do is initialize our router, which I’m just going to call router. Mux has a function for this that we can call and assign the return value to a variable.</p><pre>router := mux.NewRouter()</pre><p>If you are new to Go like I am, you might be wondering about that := symbol. It’s pretty awesome, so I’m going to take a second and explain it. When you are inside a function in Go, you can use what is called s<em>hort variable declaration </em>by skipping the var keyword and using :=. When this syntax is used, Go will infer the variable type based on the value we are giving it.</p><p>Time to define our routes. We will create each endpoint by chaining a couple of methods onto our router.</p><ul><li>.HandleFunc() which takes two arguments, a string that defines our route and a function that handles that route.</li><li>.Method() which takes an argument of the http method.</li></ul><p>If we want full CRUD for our sushi, we will need the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1HvMfar8oqa7GJgKMJT1dg.png" /></figure><p>They won’t work yet since we haven’t written any of the handler functions, but it looks good so far! Before we write those functions, let’s set up the code to run our server. We will use the ListenAndServe() function from the <em>net/http</em> library. It takes two arguments, an address and a handler. For this little example, our address will be whatever port we want and for the handler, we will pass it the router variable we just defined.</p><pre>http.ListenAndServe(&quot;:5000&quot;, router)</pre><p>That looks great, but let’s do one last thing before we move on. Remember that log library we imported? Let’s wrap this whole line in log.Fatal() so it will throw an error if it fails.</p><pre>log.Fatal(http.ListenAndServe(&quot;:5000&quot;, handler))</pre><p>To test what we have so far, let’s create some empty function definitions for our route handling functions. Each of these functions needs to take two arguments a response w, and a request r. These use special data types from the http library.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*070pGaD0zd4nOTetHKup4w.png" /></figure><p>Now we can compile our program and test it. Save the file and in terminal, compile it with go build. Now if you look in the directory, you should see an executable file with the same name as the directory mine is <em>sushiAPI</em>. We will run that file from the command line to start our server.</p><pre>./sushiAPI</pre><p>Once you run your executable file, your terminal should hang because the server is running. Now if I go to<strong> localhost:5000/sushi</strong> in <a href="https://www.getpostman.com/">Postman</a>, I see… nothing. That’s ok, it’s because there’s nothing in the controller function. However, if I check the headers I can see a status code of 200!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fvmv47p4zksWDC%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fvmv47p4zksWDC%2Fgiphy.gif&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fvmv47p4zksWDC%2Fgiphy-downsized-large.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="236" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c9313f6b3337ae3f1d7fdeba2a3a6ec/href">https://medium.com/media/9c9313f6b3337ae3f1d7fdeba2a3a6ec/href</a></iframe><h3>Step 4 — Controller Methods</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7X6sJO7If0mU68W1Ajl2lA.png" /></figure><p>So far so good, now let’s make it do something. We need two more packages <em>math/rand</em> to generate random numbers <em>strconv </em>so that we can convert strings to other data types. Next, we will work through each of our functions one at a time.</p><h4>getRolls()</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TlU6-DEXp_pw2YaO9aqFoA.png" /></figure><p>This function is pretty easy to figure out. The first thing we do is set the headers on the response. Then we use the NewEncoder() and Encode() methods to render our <em>rolls</em> slice as json and send it to the response stream. Now our index page should be operational. At least, it would be if we had any data! Let’s add a dummy roll to our rolls slice by dropping this line at the top of our main function (We’ll look more closely at it later.)</p><pre>rolls = append(rolls, Roll{ID: &quot;1&quot;, ImageNumber: &quot;8&quot;, Name: &quot;Spicy Tuna Roll&quot;, Ingredients: &quot;Tuna, Chili sauce, Nori, Rice&quot;})</pre><p>Now let’s re-compile, restart our server, and check out our index endpoint in Postman.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fXk-EkjTkfrMM9AVMnwZeg.png" /><figcaption>Nice!!!</figcaption></figure><h4>getRoll()</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UbGYFyJhYaiKt5y0DakKow.png" /></figure><p>Ok, let’s look at the show function now. Right away we can see some similarities to the index function. We already know what that first line does.</p><p>The second line looks kind of new, but there is some familiar stuff here. We can tell we are using := to set params, so it’s pretty safe to assume that the mux.Vars() function is setting our params variable from the http response we are passing it.</p><p>Next, we have a for loop, which makes sense because we are going to have to iterate over our Rolls variable to find the one we want. The syntax looked a little weird to me the first time I saw it, so let’s break it down a little.</p><p>In Go, <em>for</em> is the only looping construct that exists, so there are multiple ways to use it. We can use it in the familiar way of initializing a counter, setting a condition and then incrementing.</p><pre>for i := 0; i &lt; 3; i++ {<br>  fmt.Println(i)<br>}</pre><p>We can also use it like we would a <em>while </em>loop in JavaScript, by only supplying it with a condition. This will continue to execute the block until the condition is no longer true.</p><pre>for x &gt; y {<br>  fmt.Println(x)<br>}</pre><p>Also, we can supply it with a <em>range clause </em>which allows it to iterate over a collection of data, our slice for example.</p><pre>for i, element := range myArr {<br>  fmt.Println(element)<br>}</pre><p>If we break the range clause down, it consists of a variable to hold the index of the current element, an identifier for the current element, our old friend :=, the range keyword, and the name of the variable that we are iterating over. Our code refines this one step further. Since we aren’t using the index variable, it can be omitted by putting an underscore in its place.</p><p>Ok, now that we are Go looping masters, let’s look inside the loop. That if statement is no big deal, when we find the item in our slice where the <em>ID</em> matches the <em>id</em> being sent through our params variable we should render it as json just like we did in the getRolls() function and then return.</p><p>Now if we check our show route in postman we see the single roll, just like we’d expect.</p><h4>createRoll()</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GUBkEBGoMvXex2VC6ZXVpg.png" /></figure><p>So far so good, now let’s figure out how to create a roll. This is our first POST request, so now we get to figure out how to deal with incoming data.</p><p>The first thing we need to do is create an instance of our Roll struct, as you can see above I’ve called it newRoll.</p><p>Now let’s get some data from the HTTP request and put it in there! To get the data out of our response, we need to head back to the json library that we imported. Much like we’ve been using .NewEncoder() and .Encode() to send our response; we will use .NewDecoder() and .Decode() to read data from our requests. We call json.NewDecoder() and pass it the body of our HTTP request. The decoder reads the data, and we call .Decode() on that passing it a pointer to our newRoll struct which allows it to match the json to the appropriate properties of the struct.</p><p>Then to simulate the database we assign it an id, by getting the length of the rolls slice and then calling strconv.Itoa() on it which converts it to a string.</p><p>Now that we have our newRoll assembled we need to add it to our slice. To do that we use append(). Append takes at least two arguments, the first is the slice we want to add items to and then we can add multiple arguments of the correct data type to add them to the slice.</p><p>Finally, we send a response back containing our new roll, just like before.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F3og0IunizV9M3xj4dO%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F3og0IunizV9M3xj4dO%2Fgiphy.gif&amp;image=https%3A%2F%2Fi.giphy.com%2Fmedia%2F3og0IunizV9M3xj4dO%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="326" frameborder="0" scrolling="no"><a href="https://medium.com/media/91a5197c9be6f60637a0b11999db5f75/href">https://medium.com/media/91a5197c9be6f60637a0b11999db5f75/href</a></iframe><p>I hope you find this walkthrough useful. In <a href="https://medium.com/@johnteckert/building-a-restful-api-with-go-part-2-22d99285c130">future posts</a>, I will go into writing the update and delete functions to round out our CRUD functionality and then maybe I’ll explore how to talk to a database.</p><ul><li><a href="https://medium.com/@johnteckert/building-a-restful-api-with-go-part-2-22d99285c130">Building a RESTful API with Go (Part 2)</a></li><li><a href="https://sushigo.herokuapp.com/">Sushi Go</a></li><li><a href="https://github.com/johneckert/blogpost">johneckert/blogpost</a></li><li><a href="https://golang.org/">The Go Programming Language</a></li><li><a href="https://github.com/gorilla/mux">GitHub - gorilla/mux: Package gorilla/mux is a powerful HTTP router and URL matcher for building Go web servers with 🦍</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9e234774b14d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codezillas/building-a-restful-api-with-go-part-1-9e234774b14d">Building a RESTful API with Go (Part 1)</a> was originally published in <a href="https://medium.com/codezillas">Codezillas</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Discovering Latent Clusters with K-means]]></title>
            <link>https://medium.com/codezillas/discovering-latent-clusters-with-k-means-f1670a1320e8?source=rss----255c770a4a8a---4</link>
            <guid isPermaLink="false">https://medium.com/p/f1670a1320e8</guid>
            <category><![CDATA[expectations]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[k-means]]></category>
            <dc:creator><![CDATA[Tyler Walker]]></dc:creator>
            <pubDate>Thu, 15 Aug 2019 13:32:48 GMT</pubDate>
            <atom:updated>2019-08-15T13:32:48.709Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E0FELrol5N81GrifI8KtKg.jpeg" /><figcaption>A really big cluster</figcaption></figure><p>As humans, we like to think things in the world fall neatly into discrete categories. These are bananas, those are papayas, these are apples, those are oranges. But even among categories that we consider virtually immutable, there can be discrepancies which violate its strictest definition.</p><p>I think it is safe to say that the state of nature is truly non-categorical, and that our categories are artificially imposed upon something which is not inherently binary or discrete. But that does not mean categories are meaningless. Categories tell you something about the statistical tendencies of it’s members, and help to organize otherwise indiscernible blobs of shape and color into identifiable collections.</p><p>Also as humans, we don’t know everything, so sometimes there may be patterns in the data that we don’t know about yet. K-means is about discovering those latent clusters: statistical groupings that suggest underlying forces at work.</p><h3>K Means</h3><p>The idea is really simple: start with some random categories. Then assign points to whichever category is closest. Finally recalculate the center to be the average of the points within the category. Repeat until either there is no change, or some other stopping criteria is met. The calculated center is called the <strong>centroid</strong> — in this example, just the average of the points.</p><p>K Means is really a form of <strong>Expectation Maximization</strong>: that is, like many Bayesian models, we are looking for the most likely model given the data. Which function has an expected value which most closely predicts the data?</p><p>The dataset I will use is one of drivers: the number of miles they drove, and the percentage of the time they spent over the speed limit. I use a Node.js library called csvtojson to load the csv file, then map over the rows, extracting the distance and % speeding:</p><pre><strong>const</strong> csv = require(&#39;csvtojson&#39;);</pre><pre><strong>let</strong> data = await csv().fromFile(drivers);</pre><pre>data = data.map(row <strong>=&gt;</strong> {<br>  <strong>const</strong> x = Number(row[&#39;Distance_Feature&#39;]);<br>  <strong>const</strong> y = Number(row[&#39;Speeding_Feature&#39;]);<br>  return { x, y };<br>});</pre><p>So <strong>data </strong>is an array of (x, y) pairs with x representing the <em>distance </em>and y representing the <em>% of drive spent speeding. </em>Given data in this form, I created some helper functions for equality and distance:</p><pre><strong>function</strong> distance(a, b) {<br>  return Math.sqrt((b.x - a.x)**2 + (b.y - a.y)**2);<br>}</pre><pre><strong>function</strong> equals(a, b) {<br>  return (a.x == b.x) &amp;&amp; (a.y == b.y);<br>}</pre><p>Whenever doing K-means, you need some sort of distance function for determining which centroid is closest. In this case we are doing Euclidian distance, but for Boolean attributes we could use the Hamming distance, or in 3-D the Manhattan distance. I also created this function for generating a random centroid, which takes into account the range for this particular dataset:</p><pre><strong>function</strong> randomCentroid() {<br>  <strong>const</strong> xLim = 250;<br>  <strong>const</strong> yLim = 100;<br>  <br>  return { x: Math.random() * xLim, y: Math.random() * yLim };<br>}</pre><p>Using this function, I will generate 2 random initial categories. This does not necessarily have to be random: you can also take a guess as to what the categories will be:</p><pre>let k = 2;<br><strong>const</strong> centroids = [];<br>for (<strong>let</strong> i = 0; i &lt; k; i += 1) {<br>  <strong>const</strong> centroid = randomCentroid();<br>  centroids.push(centroid);<br>}</pre><p>Now we are going to iterate over the samples, see which centroid is closest, and then assign the sample the corresponding category. This is sometimes called the <strong>Expectation </strong>step, although really this step has nothing to do with expectations. Rather, it is simply assigning data points to categories in a deterministic way:</p><pre><strong>const</strong> clusters = [];</pre><pre>for (<strong>let</strong> i = 0; i &lt; k; i += 1) {<br>  clusters[i] = []; // empty array to hold samples for the cluster<br>}</pre><pre>for (<strong>let</strong> sample of data) {<br>  <strong>const</strong> distances = [];<br>  for (<strong>let</strong> centroid of centroids) {<br>    <strong>const</strong> dist = distance(sample, centroid);<br>    distances.push(dist);<br>  }</pre><pre>  <strong>const</strong> min = Math.min(...distances);<br>  <br>  <strong>const</strong> idx = distances.indexOf(min);<br>  clusters[idx].push(sample);</pre><pre>}</pre><p>Next is the <strong>Maximization</strong> step, where we choose a new centroid that is most likely to have generated the points in its category. In this case the new centroid is just the average of the points in the category. By doing this, the chosen center maximizes the likelihood of seeing the given data. The following function averages the x and y values in the cluster:</p><pre><strong>function</strong> computeCentroid(cluster) {<br>  <strong>const</strong> x = cluster.reduce((acc, sample) <strong>=&gt;</strong> acc + sample.x, 0) / cluster.length;<br>  <strong>const</strong> y = cluster.reduce((acc, sample) <strong>=&gt;</strong> acc + sample.y, 0) / cluster.length;<br>  <br>  return { x, y };<br>}</pre><pre>for (<strong>let</strong> i = 0; i &lt; clusters.length; i += 1) {<br>  <strong>const</strong> cluster = clusters[i];<br>  <strong>const</strong> newCentroid = computeCentroid(cluster);<br>  if (equals(newCentroid, centroids[i])) {<br>    stop = true; // stop the algorithm if nothing changed<br>  }<br>  centroids[i] = newCentroid; //assign the centroid<br>}</pre><p>One last check to reinitialize a centroid if no points get assigned to it:</p><pre>for (<strong>let</strong> i = 0; i &lt; clusters.length; i += 1) {<br>  <strong>const</strong> cluster = clusters[i];<br>  if (cluster.length === 0) {<br>    console.log(&#39;None assigned to cluster: &#39;, centroids[i])<br>    centroids[i] = randomCentroid();<br>    console.log(&#39;New centroid: &#39;, centroids[i]);<br>  }<br>}</pre><p>Finally we’re ready to see some graphs. Here are the result of my running the algorithm with k=2. I graphed everything using Plotly:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/912/1*G3pPwd-Q63gD4NwX263mVw.gif" /><figcaption>K-Means algorithm with k=2</figcaption></figure><p>I drew one plot for the expectation step and one plot for the maximization step. You can see how the centroids are each immediately drawn to the nearest center of mass. If there are many samples around a point, K-means assumes that the source is likely nearby. It only took 4 iterations of the algorithm to rest into a stable position. It is possible that given other random starting points, some of those in between points could have been captured by the other group. So, initialization matters.</p><p>Although there do seem in fact to be two major categories, one of drivers who drove a relatively short distance, and then a discontinuous leap to drivers who drove relatively longer distances, we can also intuit that within these categories there seems to be additional clustering in the vertical direction. In both groups, there is a highly concentrated group of drivers who spent most of the time under the speed limit, while a sparser group of drivers spent a high percentage over the speed limit. Given these observations, let’s try with k = 4 and see what we get:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/913/1*9z0DH8f33qBrhgq24JVnmw.gif" /><figcaption>K-Means EM with k=4</figcaption></figure><p>I think it is interesting how the red centroid gradually captures more of the points from the yellow centroid. It is as if the clusters have electromagnetic charges that repel each other. It took 7 rounds for the centroids to rest in a stable equilibrium.</p><h3>Conclusions and Observations</h3><p>Like constellations, the groupings produced by K-means are not always inherently meaningful, outside of the psychological parameters that may make them intuitive. There are ways to help guarantee the most accurate groupings. One way is to iterate over values of K, and choose the one which minimizes the average distance between cluster members. A caveat to this is that increasing K will <strong>always</strong> reduce this metric, so the trick is to choose the point at which increasing K only trivially reduces the average distance.</p><p>It is also worth noting that the above method is easily generalizable to higher dimensional spaces: just average over all the dimensions to find the new centroid.</p><p>You can also try an <strong>Ensemble </strong>of K-means predictions, and then taking the top three hypotheses and applying majority rule for category membership. Again, K-means is used when we do not know whether there are underlying causal categories or not. This is akin to feeling around in the mud hoping to find a diamond. Sometimes the result will correspond with what we can see or understand intuitively. Other times it can highlight unexpected character in the data, or explores dimensions that are too complex to understand. K-means shines a light in places too dark or too deep for the ordinary senses to reach.</p><h3>References</h3><ol><li>Artificial Intelligence: A Modern Approach — <a href="https://www.amazon.com/Artificial-Intelligence-Modern-Approach-3rd/dp/0136042597">https://www.amazon.com/Artificial-Intelligence-Modern-Approach-3rd/dp/0136042597</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f1670a1320e8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codezillas/discovering-latent-clusters-with-k-means-f1670a1320e8">Discovering Latent Clusters with K-means</a> was originally published in <a href="https://medium.com/codezillas">Codezillas</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Uh-Oh’s in Go — Slice of Pointers]]></title>
            <link>https://medium.com/codezillas/uh-ohs-in-go-slice-of-pointers-c0a30669feee?source=rss----255c770a4a8a---4</link>
            <guid isPermaLink="false">https://medium.com/p/c0a30669feee</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[pointers]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[arrays]]></category>
            <dc:creator><![CDATA[Nitish Malhotra]]></dc:creator>
            <pubDate>Thu, 15 Aug 2019 13:31:33 GMT</pubDate>
            <atom:updated>2019-08-15T13:31:33.869Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*2LP80A-JSDJu8uPN0fsMvg.png" /><figcaption>Image courtesy — <a href="https://xkcd.com/138/">https://xkcd.com/138/</a></figcaption></figure><p>Go makes dealing with slices and other primitive data structures a complete cake walk. For those coming from the daunting world of pointers, in <strong>C</strong> and <strong>C++</strong>, working in a <strong>Go</strong> is a blessing (most of the time). For those that have adopted go after working in languages like <strong>JS/Python</strong>, there is not much of a difference, apart from the syntax.</p><p>However more often than not a <strong>JS/Python</strong> developer or any Go newbie would trip on pointers. Most likely it would be in the scenario presented below.</p><h3>Scenario</h3><p>Consider the case where you need to load a slice of string pointers, []*string{} with some data.</p><p>Let’s see some code -</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/271845b92354366e7ebdf7da0afef93f/href">https://medium.com/media/271845b92354366e7ebdf7da0afef93f/href</a></iframe><p>The code sample above, generates numbers from 0 to 9. For each number (int), we convert it, into its string representation using strconv.Itoa . Next we prefix the generated number string with the # symbol. And finally append it to the targeted slice.</p><p>Let’s look at the output from running that code snippet</p><pre>➜  sample go run main.go<br>#9<br>#9<br>#9<br>#9<br>#9<br>#9<br>#9<br>#9<br>#9<br>#9</pre><blockquote>What happened here ????</blockquote><blockquote>Why do I only see the final number `#9` being printed ??? I am pretty sure I added the other numbers to the list! Let’s add some debug to the sample program.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/937d30ee01ac1b7d289ab0f161e05d8f/href">https://medium.com/media/937d30ee01ac1b7d289ab0f161e05d8f/href</a></iframe><p>Output from the run with debug -</p><pre>sample go run main.go<br>Adding number #0 to the slice<br>Adding number #1 to the slice<br>Adding number #2 to the slice<br>Adding number #3 to the slice<br>Adding number #4 to the slice<br>Adding number #5 to the slice<br>Adding number #6 to the slice<br>Adding number #7 to the slice<br>Adding number #8 to the slice<br>Adding number #9 to the slice</pre><blockquote>I see them being added … *scratching head*</blockquote><blockquote>WHY IS THIS HAPPENING TO ME !!!????</blockquote><blockquote>$@#! My Life !!</blockquote><h4>Easy there cowboy ... Let’s look at what really happened, shall we …</h4><pre>var numberString string</pre><p>What happens here is that numberString gets allocated on the stack, at let’s say, memory address 0x3AF1D234 .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/260/1*15Rbt3dzl-U5BI57d1alSA.png" /><figcaption><strong>numberString</strong> on the stack at address 0x3AF1D234</figcaption></figure><pre>for i := 0; i &lt; 10; i++ {                         <br>    numberString = fmt.Sprintf(&quot;#%s&quot;, strconv.Itoa(i))<br>    listOfNumberStrings = append(listOfNumberStrings, &amp;numberString)                        }</pre><p>What we do now is we loop through from 0 to 9.</p><h4>First iteration <strong><em>[i = 0</em></strong>].</h4><p>Here we generate the string &quot;#0&quot; and store it in the variable numberString.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/302/1*0ti6HW7R57n057yHltMk9Q.png" /><figcaption>numberString stored at 0x3AF1D234 with content &quot;#0&quot;</figcaption></figure><p>Next we take the address of numberString (&amp;numberString), which equates to 0x3AF1D234 , and append it to the listOfNumberStrings slice.</p><blockquote>listOfNumberStrings now looks like :</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/191/1*lqhnW94y7EYO66gcAScLkA.png" /><figcaption>listOfNumberStrings slice with value 0x3AF1D234</figcaption></figure><h4>Second iteration [i = 1].</h4><blockquote>We follow the same steps as above.</blockquote><p>Here we generate a string &quot;#1&quot; and store it in the same variable numberString.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/302/1*Repod_CioITvZXA0QDGKqw.png" /><figcaption>numberString stored at 0x3AF1D234 with content &quot;#1&quot;</figcaption></figure><p>Next we take the address of numberString (&amp;numberString), which equates to 0x3AF1D234 , and append it to the listOfNumberStrings slice.</p><blockquote>listOfNumberStrings now looks like :</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/391/1*TXkH3liojhp49agMFMkWqg.png" /><figcaption>Two items in the slice BOTH with values 0x3AF1D234.</figcaption></figure><p>Hopefully it is now starting to become more obvious to you.</p><p>The slice now has two members. But both the members, at index 0 and 1, store the value 0x3AF1D234 (memory address of numberString) .</p><p>However, keep in mind, that the last string stored in numberString, at the end of the second iteration, was “#1”.</p><p>This above steps repeat till we reach the last iterations.</p><p>The final string stored in numberString, at the end of the last iteration, was “#9”.</p><p>Alright so lets look at what happens when we try to print the content of each element stored in the slice, by de-referencing it (the pointer) using the * operator.</p><pre>for _, n := range listOfNumberStrings {<br>    fmt.Printf(&quot;%s\n&quot;, *n)                        <br>}</pre><p>Since each element of the slice stores the value 0x3AF1D234 (as seen in the examples above) , de-referencing the element at that would give you the value stored at that memory address.</p><p>From the final iteration, we know that the last value to be stored was “#9” . Hence, the output looks like shown below.</p><p><strong><em>The last string stored in the variable </em></strong><strong><em>numberString =&gt; </em></strong><strong><em>&quot;#9&quot; .</em></strong></p><pre>➜  sample go run main.go<br>#9<br>#9<br>#9<br>#9<br>#9<br>#9<br>#9<br>#9<br>#9<br>#9</pre><h3>Solution</h3><p>There is a pretty easy way to resolve the issue. It would require you to modify the location of where you declare your variable numberString.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/004205507800e59204d4662dee894f59/href">https://medium.com/media/004205507800e59204d4662dee894f59/href</a></iframe><p>We now declare the variable inside our for loop.</p><p>What this accomplishes is, that at every iteration through the loop we are forced to <strong>re-declare</strong> the variable numberString on the stack, thereby giving it a new unique memory address.</p><p>We then update the variable with the generated string and append its address into the slice. The result is that every element of the slice, now stores a unique memory address.</p><p>The output from running the above snippet would be -</p><pre>➜  sample go run main.go<br>#0<br>#1<br>#2<br>#3<br>#4<br>#5<br>#6<br>#7<br>#8<br>#9</pre><p>I hope this post would help someone out there. I came up with the idea to write this post based on my experience with a Junior engineer at my workplace, where he hit this exact scenario and seemed to be completed baffled by it. It reminded me of the time I fell into the same trap, while working in C at my previous job, when I was a Junior engineer.</p><blockquote>PS : If you come from the wonderful world of pointers in C/C++ … Honestly, you have all made (and learned from) this mistake already ;) !</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c0a30669feee" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codezillas/uh-ohs-in-go-slice-of-pointers-c0a30669feee">Uh-Oh’s in Go — Slice of Pointers</a> was originally published in <a href="https://medium.com/codezillas">Codezillas</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a microservice framework in Golang]]></title>
            <link>https://medium.com/codezillas/building-a-microservice-framework-in-golang-dd3c9530dff9?source=rss----255c770a4a8a---4</link>
            <guid isPermaLink="false">https://medium.com/p/dd3c9530dff9</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[adopting-microservices]]></category>
            <category><![CDATA[framework]]></category>
            <dc:creator><![CDATA[Nitish Malhotra]]></dc:creator>
            <pubDate>Thu, 15 Aug 2019 13:31:21 GMT</pubDate>
            <atom:updated>2019-08-15T13:31:21.152Z</atom:updated>
            <content:encoded><![CDATA[<p>[UPDATE — 05/13/2018]</p><p>Added an gRPC based microservice example to the <a href="https://github.com/nitishm/go-micro-framework">github</a> repository. Check it out at <a href="https://github.com/nitishm/go-micro-framework/tree/master/examples/rpc-microservice">rpc-microservice</a>.</p><p>Start the gRPC server by running the /examples/rpc-microservice/main.go and invoke an RPC call using /examples/rpcclient.go</p><p>Added the code snippets on how the RPC server implements the Server interface.</p><p>Exactly one year ago I started my job at <a href="https://www.cspi.com/">CSP Inc.</a> to work on the <a href="https://www.cspi.com/aria-software-defined-security/">ARIA</a> product line. At the time we were a team of four engineers, all hailing from an embedded background. Having worked extensively in C/C++ in the past, we had little to no experience in Golang (heck I hadn’t even heard of Golang at that point). We were faced with a daunting task — to build the next biggest Software Defined Security product! The project had multiple components ranging from the web-service front-end to developing an agent software which would run on our Secure Intelligent Adapters (SIA).</p><p>I was tasked with designing and developing the Controlplane (which we refer to as the Software Defined Security Orchestrator — SDSo). The SDSo was to be developed as a collection of multiple microservices mostly written in Golang. Over the past year I have worked with multiple open-source libraries and implemented services using Kafka, gRPC, Redis, MySQL, HTTP, Celery, among several others. In this time, I have created over twenty microservices written exclusively in Golang. With the team growing from four to fifteen engineers working on the product, I have seen microservices being developed in a style as unique as the developer who worked on it. However, in principle, each one of these services are the same. Each one composed of one or more running servers, storage options and one or more upstream clients.</p><p>Due to the fast paced nature of our work, it was absolutely essential for me and my team of four engineers to focus on the business logic of our services. Rather than rehashing boilerplate code to setup our microservices such as initializing our server, database, clients etc., I came up with a simplified framework which I will be describing in this post.</p><h3>Microservice interface</h3><p>Each microservice should satisfy the Microservice interface. This is not imposed but only serves as a guideline.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/adec0c13cd63a04b21aaa51f0ac24700/href">https://medium.com/media/adec0c13cd63a04b21aaa51f0ac24700/href</a></iframe><p>An example of how we implement this interface would be :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bbcd0cfea2aeab6d016fe9d74039cb63/href">https://medium.com/media/bbcd0cfea2aeab6d016fe9d74039cb63/href</a></iframe><p>We will get to the init() seen in line 12 above in a few moment. (*)</p><h3>Server interface</h3><p>Each server would need to implement the server interface which is composed of the following :</p><p>AStart() method which would start the would start the server as a go routine.</p><p>A Stop() method which would gracefully stop the server by sending a message on its quit channel returned at the time of starting</p><p>A RegisterNamespace() and RegisterService() to register a namespace which allows for using the same channel/topic for sending messages destined for different endpoints identified by their service type string.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47b630f126fc6363931bd2e5eb6e6bbe/href">https://medium.com/media/47b630f126fc6363931bd2e5eb6e6bbe/href</a></iframe><p>An implementation of the server would look something like follows -</p><p>Dont get confused by the nomenclature [KafkaConsumer] below. At CSPi we use kafka not just as a pubsub framework but also as a server for our microservices. Typically used when the service being targeted needs to asychronous. (You may note that the response in the Handle() is not returned to the caller. Sending a response is an implementation detail when using kafka as a server).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/989ba29949fbe67b0356e9508ba77ea1/href">https://medium.com/media/989ba29949fbe67b0356e9508ba77ea1/href</a></iframe><h3>Endpoint interface</h3><p>The business logic of our service is encapsulated in the Endpoint interface within the Handle() method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac6799249063798b4485fcab78e6a990/href">https://medium.com/media/ac6799249063798b4485fcab78e6a990/href</a></iframe><p>An implementation of which would look something like this :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/568f01f84316affca52c4299b47389dc/href">https://medium.com/media/568f01f84316affca52c4299b47389dc/href</a></iframe><p>The users job is to register this endpoint as a handler for the service identified by a string &quot;hello&quot; registered under the &quot;greeter&quot; namespace. In kafka the namespace is synonymous with the topic name.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/adb89e4682951adf067ef3e0df784a1f/href">https://medium.com/media/adb89e4682951adf067ef3e0df784a1f/href</a></iframe><p>That wraps up my post about microservices.</p><p>Keep watching this space for more edits, completed with snippets and gifs to come.</p><h3>Doing this with an RPC Server instead of Kafka</h3><p>The Server interface implementation would like below :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/82ad2a06020a5db4ccf4b44d2d89ac47/href">https://medium.com/media/82ad2a06020a5db4ccf4b44d2d89ac47/href</a></iframe><p>I generated the RPC server using the protoc tool using the following protobuf specification file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e252e29b7039fba0d99e9e7208049f2b/href">https://medium.com/media/e252e29b7039fba0d99e9e7208049f2b/href</a></iframe><p>which can be used to generate the proto server using the protoc tool provided by gRPC.</p><pre>protoc --proto_path=./ rpc.proto --go_out=plugins=grpc:.</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f28d5ba786657dafee702e2ec3a668a/href">https://medium.com/media/8f28d5ba786657dafee702e2ec3a668a/href</a></iframe><p>You can find all the code hosted on github at <a href="https://github.com/nitishm/go-micro-framework/">https://github.com/nitishm/go-micro-framework/</a>.</p><p>To test this framework out I have provided an example directory completed with a simple microservice running kafka as a server which can found in the <strong>/examples/</strong> folder. Simple start the server by running the main.go in the <strong>/examples/kafka-microservice/</strong> directory.</p><p>To send a message over the listening kafka topic, use the producer.go program provided under the /examples directory.</p><p>[A docker-compose file is in the works for using the example microservice. I will update the page as soon as I have that ready. If you are curious you can always manually startup your kafka server using the Dockerfile provided at <a href="https://hub.docker.com/r/spotify/kafka/">https://hub.docker.com/r/spotify/kafka/</a>].</p><p>PS : If you looking for a more mature and independent framework, take a look at <a href="https://github.com/NYTimes/gizmo">gizmo</a>, <a href="https://github.com/go-kit/kit">go-kit </a>and <a href="https://github.com/micro/go-micro">go-micro</a>. The reason we didn’t use any of these framework at our workplace was because we found these frameworks to be more web-dev-centric and not as flexible for our diverse needs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dd3c9530dff9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codezillas/building-a-microservice-framework-in-golang-dd3c9530dff9">Building a microservice framework in Golang</a> was originally published in <a href="https://medium.com/codezillas">Codezillas</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Go Concurrency- A primer]]></title>
            <link>https://medium.com/codezillas/go-concurrency-a-primer-de4e8eefd16e?source=rss----255c770a4a8a---4</link>
            <guid isPermaLink="false">https://medium.com/p/de4e8eefd16e</guid>
            <category><![CDATA[goroutines]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[parallel-computing]]></category>
            <category><![CDATA[multithreading]]></category>
            <dc:creator><![CDATA[manoj s k]]></dc:creator>
            <pubDate>Thu, 15 Aug 2019 13:30:45 GMT</pubDate>
            <atom:updated>2019-08-15T13:30:50.660Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YWqrXXCLAePH_Hqb8TvZCQ.jpeg" /></figure><p>World around us is concurrent. At a given time, lot of things happen independent of each other. To solve real world problems, then, we need a way to model this concurrency in a program. Go, as a modern language, comes with concurrency support built along the language making us to avoid depending on third part libraries which are difficult to understand, port and maintain. In this article, I will delve into Go concurrency primitives and use them to design concurrent programs.</p><h3>Concurrency or Parallelism ?</h3><p>Definitions first:</p><ul><li><strong>Concurrency</strong>: is a way of composing independent tasks.</li><li><strong>Parallelism</strong>: is actually running multiple independent tasks at the same time.</li></ul><p>Say we have a single processor which is used to run our program. We design a way to split our single program into 2 subtasks which are independent of each other. This way of organizing is concurrent design.This code when run on the said machine is not parallely running because we have a single processor and each subtask must share the processor with other and at a time only one task occupies processor.</p><p>But say now we have a multi-processor system and we run the given program on that. Since tasks are independent and if we can pipeline them to different processors, we have a parallel computation happening. We converted a concurrent program to a parallel one just by adding another processor(not really, but kind of).</p><p>Thus, concurrency is a composition method while parallelism is actual execution of independent tasks at once. Concurrency is the <strong>ability</strong> to run independently.</p><h3>Go concurrency primitives</h3><p>Now lets look at concurrency specific to Golang.</p><h4>Goroutines</h4><p>Goroutine is an independently executing function. It is launched by go statement and has its own call stack which can grow or shrink dynamically and run time takes care of handling it. If we have a function foo to execute as independent goroutine, we just need to do go foo() and voila! we have a goroutine created. Now foo() runs in its own stack and the called function can proceed without blocking for it to return.</p><blockquote>Go concurrency is composition of independently executing goroutines.</blockquote><p>Goroutine is NOT a thread. Goroutines are multiplexed onto threads dynamically.Threads are created on the fly such that no goroutine ever blocks. Go runtime has an abstraction over OS threads with concepts like logical processors. OS scheduler schedules threads to run on physical processors while Go runtime scheduler runs goroutines against logical processors. Each logical processor is associated with one OS thread. Go default is to have a logical processor for every physical processor on the machine. These are used to run goroutines that are created.Even if we have a single processor, huge number of coroutines can be run concurrently.</p><p>The go runtime scheduler has a global run queue. As every goroutine is created it is put in this queue. Then they are assigned to a logical processor and put into the logical processors run queue. And then they wait for their turn to run on logical processor. When a processor is running a goroutine that makes a blocking call or explicitly calls runtime.Gosched(), it will be detached from the processor and next one in the queue is run. Once the blocking call returns, the goroutine is again placed to a local run queue.</p><p>For threads blocked on network IO calls, they are put in integrated network poller which will indicate when read or write operation is ready and the goroutine moves to a logical processor’s run queue.</p><p>A program can have unto 10000 operating system threads by default(can tinker the number in debug.SetMaxThreads function).If it exceeds this limit, program crashes. This is for OS threads, not goroutines. We can shoot goroutines on the fly but a new OS thread is created only when a goroutine is ready to run but all the existing threads are blocked in system calls or threads are locked to goroutines with runtime.LockOSThread calls.</p><pre>func foo(){<br>   fmt.Println(&quot;In foo&quot;)<br>}</pre><pre>func bar(n int){<br>   for i:=0;i&lt;n;i++{<br>       fmt.Printf(&quot;In bar for %dth time&quot;,i)<br>}</pre><pre>func main(){<br>    go foo()<br>    go bar(3)<br>    ....///do something else program may exit before goroutines run<br> }<br></pre><h4>Channels</h4><p>Just having many independent functions running and printing to console is not very useful. There should be a way for them to communicate with each other. In languages like Java, C++ the concurrency libraries are modeled around communicating between threads by sharing resource. This needed locking ,mutexes etc which were difficult to get right.</p><p>Go shares information between resources by communicating. And the primitive we have for communication is channel. Channels are conduits through which data is passed using channel operator(&lt;-). Channels can be used as shown in the block below:</p><pre>ch := make(chan int)/// channels must be typed<br>ch&lt;-1 /// send value to channel<br>var a int<br>a&lt;-ch ///get value from channel<br>///the arrow indicates the direction of data flow</pre><p>By default, reads and writes to channel block. i.e., if we try to read from a channel, we block till there is some value to read in it. Likewise write blocks till the receiver is ready.(Though we can overcome this by having buffered channels).Thus, channels provide a way to both communicate and synchronize.</p><p>Not that we don’t have ways to synchronize access to a global data. There are good old primitives like mutexes, atomic functions etc but Go has better way to compose concurrent functions around channels.</p><blockquote>Go way is to share by communicating instead of communicating by sharing as in C++ or Java.</blockquote><p>Let’s see the same using an example program</p><pre>package main<br>import(<br>    &quot;fmt&quot;<br>    &quot;time&quot;<br>)<br>func main(){<br>    ch := make(chan int)<br>    cnt := 5<br>    go foo(cnt,ch) ///pass channel as parameter<br>    fmt.Println(&quot;In main function\n&quot;)<br>    for i:=0; i&lt;3;i++{<br>        fmt.Printf(&quot;Value on channel is %d\n&quot;,&lt;-ch)//blocks on ch<br>    }<br>    fmt.Println(&quot;Done. Exiting main\n&quot;)<br>}</pre><pre>func foo(count int, ch chan int){<br>    for j:=0;;j++{                  /// runs forever.<br>        ch &lt;- j                     ///writes j to ch<br>        time.Sleep(time.Millisecond)<br>    }<br>}</pre><p>Output is:</p><pre>In main function</pre><pre>Value on channel is 0</pre><pre>Value on channel is 1</pre><pre>Value on channel is 2</pre><pre>Done. Exiting main</pre><p>Though the goroutine runs forever if it is let , main function’s for loop runs only for 3 iterations. If the channel ch on which the statement inside for loop is not yet written to, the line is blocked. Meanwhile, goroutine foo will run independently writing to channel ch.</p><ul><li>As soon as goroutine writes j to ch,the main’s for loop’s statement will unblock and execute after reading(kind of popping ) from the channel.</li><li>After sleeping for a millisecond, goroutine’s ch &lt;-j will be blocked till channel’s value is read in above step after which it unblocks and writes again to channel and then goes to sleep.</li><li>This cycle repeats for 3 iterations and after which main exits the for loop and prints the Exiting main message and exits. This also results in goroutine exiting.</li></ul><p>Though it is a very trivial example, it shows two basic concurrency primitives , goroutines and channels, in action and how channels help in communication and synchronization.</p><h4>select : a case for channels</h4><p>For those who come from C, select is similar to switch statement. But instead of integer values like in switch, select lets you wait on multiple channel operations. A select blocks until one of its cases can run.It then executes that particular case. If more than one are ready, it chooses one randomly. Once some channel is available to read and the execution completes, select will exit.</p><pre>package main</pre><pre>import (<br> &quot;fmt&quot;<br> &quot;time&quot;<br>)</pre><pre>func main() {<br> ch1 := make(chan int)<br> ch2 := make(chan string)<br> go foo(ch1)<br> go bar(ch2)<br> for i := 0; i &lt; 5; i++ {<br>  select {<br>  case v1 := &lt;-ch1:<br>   fmt.Printf(&quot;Channel 1 sent me %d\n&quot;, v1)<br>  case v2 := &lt;-ch2:<br>   fmt.Printf(&quot;Channel 2 send me %s\n&quot;, v2)<br>  }<br> }<br> fmt.Println(&quot;Exiting main\n&quot;)<br>}</pre><pre>func foo(ch chan int) {<br> for i := 0; ; i++ {<br>  ch &lt;- i<br>  time.Sleep(time.Second * 3)<br> }<br>}</pre><pre>func bar(ch chan string) {<br> for i := 0; ; i++ {<br>  ch &lt;- &quot;Cheers!&quot;<br>  time.Sleep(time.Second * 2)<br> }<br>}</pre><p>Output to above program is :</p><pre>Channel 2 send me Cheers!</pre><pre>Channel 1 sent me 0</pre><pre>Channel 2 send me Cheers!</pre><pre>Channel 1 sent me 1</pre><pre>Channel 2 send me Cheers!</pre><pre>Exiting main</pre><p>Each goroutine writes to appropriate channel at every 2 and 3 seconds respectively and then blocks. In main, select is run in a for loop for 5 iterations.</p><ul><li>At every iteration, select will execute the case that is ready and then goes back to waiting on them both.</li><li>Since we are timing such that only one channel has a value during a select run, we can see values from each channel printed alternatively.</li><li>After 5 runs, select exits and main returns.</li></ul><blockquote>select along with goroutines and channels can be used as a powerful tool for organizing code in a concurrent fashion with synchronization and communication acting as tools of concurrency instead of global shared resource and locks.</blockquote><h4>Mutexes and atomic operations</h4><p>If there is a need to protect some shared value between different goroutines, we can still use mutexes present in sync package. It is better to club mutex and the value to be protected as a single type and using lock and unlock to access the protected value. A very crude example is shown below:</p><pre>type Protect struct{<br>    crucial Message     /// a member of a composite type, Message<br>    mu sync.Mutex<br>}</pre><pre>var p Protect            ///declare a variable of type Protect</pre><pre><br>func foo(){<br>mu.Lock()<br>....                     ///whatever operation to be done to Message<br>mu.Unlock()              //Unlock, between lock and unlock<br>}                        // execution will be atomic</pre><p>There are also atomic operations in sync package which guarantee atomic execution. Like AddInt32(), CompareAndSwapInt64() etc there are many primitive functions that provide atomic operations for native types. But it is advised to use other sync primitives and channels for synchronization than using atomics operations.</p><h4>Race conditions</h4><p>When multiple goroutines are having access to non synchronized shared resource and they simultaneously try to modify the value of the resource, we get what is called race conditions. We saw above how to do operations atomically using Mutexes or construct code using channels. If we have not used them (?), we can still detect race conditions in the code using race detector flag while building. Yes, wow indeed !</p><pre>go build -race example.go<br>./example</pre><p>Yes it is that simple. And when we run the executable, clearly RACE warnings will be printed to console and we can check back the code to correct the issues.</p><h4>Conclusion</h4><p>While goroutines provide a very light weight threads maintained by Go runtime, channels provide a way to communicate between those goroutines in a synchronized way. Using the tools discussed above, we can model powerful concurrent solutions and ensure ease of understanding and maintenance of the code. Along with these CSP style primitives, we have mutexes, atomic functions and WaitGroups which provide good old, C style concurrency patterns. These features being built into the language along with powerful race detector, makes Golang useful in creating highly concurrent and efficient pieces of software without being bogged down by third party libraries.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=de4e8eefd16e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codezillas/go-concurrency-a-primer-de4e8eefd16e">Go Concurrency- A primer</a> was originally published in <a href="https://medium.com/codezillas">Codezillas</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[UX Designers should start participating in Hackathons]]></title>
            <link>https://medium.com/codezillas/ux-designers-should-start-participating-in-hackathons-7740dd41cf17?source=rss----255c770a4a8a---4</link>
            <guid isPermaLink="false">https://medium.com/p/7740dd41cf17</guid>
            <category><![CDATA[digital-design]]></category>
            <category><![CDATA[business]]></category>
            <category><![CDATA[ux]]></category>
            <category><![CDATA[startup]]></category>
            <category><![CDATA[tech]]></category>
            <dc:creator><![CDATA[Moses Ong]]></dc:creator>
            <pubDate>Thu, 15 Aug 2019 13:30:24 GMT</pubDate>
            <atom:updated>2022-03-22T09:22:49.921Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2L0Aq9wtrLBaefbkN0JfFQ.jpeg" /></figure><p>I’ve noticed that there’s a lack of UX designers participating in Hackathons here in Singapore which I believe is a huge waste of talent and expertise. Hackathons are designed to put participants under intense pressure to build a product or service that solves a real problem. So to raise more awareness on hackathon, I decided to write an article about it.</p><h3>Before I dwell more into the topic of Hackathon, let me first answer what is a Hackathon?</h3><p>Hackathon stems from the word Marathon (A long, or difficult activity accomplished in a short period)</p><p>A hackathon puts a group of strangers (<strong>Developers, Designers, Hustlers, Data Scientist, Engineers</strong>) together into a team setting to solve <strong>challenge statement </strong>posted by the company/organisers.</p><p>The team then goes into a <strong>24–48 hour</strong> (or more) marathon to code, design and develop meaningful solutions that solves the <strong>world’s more dire problems</strong>. Pizza is often served to prevent them from going hungry. Solutions are then pitched and winners get some prizes. It’s not only for technologists. As all solutions require nowadays, a good business brain — and in many cases a good designer too.</p><h3>Why UX Designers play such a vital role in Hackathons</h3><p>Most often that not, teams are only given 24–48 hours to deliver a demo. Hence, it’s almost impossible for developers to deliver a fully functional working app/website or digital product.</p><p>This is where UX designers come in to fill the gap. We translate ideas and insights discussed by the team into prototype, facilitate design thinking among the team to better solve problems and tighten communications and goals.</p><h3>Benefits of participating in a Hackathon</h3><h4>1. Meaningful network</h4><p>You meet people who aren’t only passionate about the same thing as you are, but you’re in an environment that everyone has chosen to be a part of. There is a natural high you get from networking and forming a team with others around a united project and goal. Everyone has something to offer, and from a networking perspective this is the most exciting thing.</p><h4>2. Time Pressure</h4><p>Hackathon forces you to work under pressure, and that is exactly what we require to be at our best. Time limit creates a uniquely productive atmosphere that forces participants to distil their visionary concepts down to actionable solutions that solves a real problem in a form of a prototype. All this increases the chance of finding innovative fixes to persistent problems. A clear direction, path is often required to drive momentum. This eliminates a lot of distractions which otherwise follows in a normal daily routine.</p><h4><strong>3. Watch Innovation as it happens</strong></h4><p>Hackathons are an innovative proving ground for new ideas. They stimulate the creative juices of participants and foster problem-solving and risk-taking in a casual environment. The <strong>diversity</strong> of participants guarantees a multitude of perspectives, example the demo of other teams and see how they built unimaginable things that cover a broad range of skills and technologies. That will give you a lot of insight and opportunities to learn about new cool things that other programmers and designers are using and/or exposition to new areas.</p><h4><strong>4. Discover yourself in the midst of diversity</strong></h4><p>One of the best way I put myself in uncomfortable situation is getting myself out there. Participating in hackathon means learning to work with new people from different disciplines and all walks of life to collectively collaborate on building a product that solves a real problem. The struggle and intensity to prototyping a solution with such a short time have helped me learn a lot about my shortcomings, my strength and contribution to the team and how I can manage my mental emotions under intense pressure.</p><h4><strong>5. Demonstrate what you’ve built</strong></h4><p>Presentation to the judges and the rest of the participants at the very end is really exciting and yet nerve-wrecking. You are only given (x) amount of time to demo your idea and if the judges have doubts about your idea, you will be questioned. I have to be honest with you, till date, I’m still battling that little nervous self in me whenever I go up the stage to present but on the flip side, I enjoyed every second of it as it has taught me to be a better listener, a better communicator and how to keep compose at stressful situation.</p><p>As a UXer, I believe what makes you a successful designer is that you’re able to sell your craft. Quoted by Marc Hemeon:</p><blockquote>Success = 80% sell, 20% design — Talking and selling your design work is just as valuable and important as the actual design work — Marc Hemeon</blockquote><h3><strong>Here are are some tips and resources for starters</strong></h3><h4>5 Best Practices</h4><ol><li><strong>Empathy <br></strong>business languages and goals are equally as important as understanding user goal. Listen carefully to their business concern to craft out the features that glues the required business model together with the user expectation</li><li><strong>Challenge Statement </strong><br>I can’t emphasis enough on the importance of listening carefully to the objective and what the judges are looking for in the competition. The challenge statement serves a “First instance” to guide all decision-making.<strong>.</strong></li></ol><p><strong>3. Ask Questions</strong></p><blockquote>“How to identify smart people: They ask you questions. When you answer, they ask you more. When you start using big words, they ask for clarification. When they can’t understand anything you’re saying, they ask for you to explain it to them as if they were a five-year-old. When you say something intriguing, they write it down in their notebook or phone. These people are not naturally more intelligent. They’re better learners which makes them more intelligent. Why do they learn better? They’re genuinely curious and ask questions from a humble standpoint. I’ve met highly successful people who’ve said, “explain it to me as if I were a five-year-old.” This simple phrase has changed my life when it comes to learning. It comes back to the famous Chinese proverb: “He who asks a question is a fool for five minutes; he who does not ask a question remains a fool forever.” As soon as you stop asking questions, you stop learning. As soon as you stop writing down ideas, you forget them. The hardest part of becoming intelligent is not bullshitting yourself about what you know; it’s being humble enough to ask questions. If you can do this, then people will see you as intelligent, too.” — Jeffrey Tiong</blockquote><p><strong>4. Avoid using Jargons<br> </strong>How well you will work in a team depends on your communication with your team and whether they understand what you do. Help your team understand your role and value you can bring to the table.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*esXIHz9VoD6QmxSQSxmsuQ.jpeg" /><figcaption>Design studio with my team at Startup Weekend 2016</figcaption></figure><p><strong>5. Minimum Viable Technology (MVT)</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ry5OKFaIa1tvS_XDBWLCpQ.jpeg" /></figure><p>Adapting the prototype mindset. The prototype is meant to answer questions, so keep it focused. You don’t need a fully functional product — You just need a real-looking facade to which customer can react.</p><p>But wait, as cliche as it sounds, what’s the definition of a prototype</p><blockquote>We define prototype as any representation of a design idea, regardless of medium — Stephanie Houde</blockquote><p>Fake it till you make it. Let’s be honestly here, it’s not so much about what you know, but what you show to the judges</p><p>Demonstrate a prototype (It’s about getting the idea validated before building a real one) so as long as your product in the hands of your customer, you will know whether your product has solved a real problem.</p><p>What can be more convincing then a quote by a Creative director from Google Venture. it goes as such</p><blockquote>The ideal prototype should be “Goldilocks quality.” If the quality is too low, people won’t believe the prototype is a real product. If the quality is too high, you’ll be working all night and you won’t finish. You need Goldilocks quality. Not too low, not too high, but just right. — <a href="https://medium.com/u/c6d7332a2978">Daniel Burka</a></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_7wDfnc_fJmu2oXbUcoCMQ.png" /><figcaption>Inspired by Christopher Nolans’ movie Prestige</figcaption></figure><blockquote>“Every great magic trick consists of three parts or acts. The first part is called “The Pledge”. The magician shows you something ordinary: a deck of cards, a bird or a man. He shows you this object. Perhaps he asks you to inspect it to see if it is indeed real, unaltered, normal. But of course… it probably isn’t. The second act is called “The Turn”. The magician takes the ordinary something and makes it do something extraordinary. Now you’re looking for the secret… but you won’t find it, because of course you’re not really looking. You don’t really want to know. You want to be fooled. But you wouldn’t clap yet. Because making something disappear isn’t enough; you have to bring it back. That’s why every magic trick has a third act, the hardest part, the part we call “The Prestige”. — Christopher Priest</blockquote><p><strong>Resources <br></strong>Some of the books I’ve read significantly helped me in sprints during Hackathons are as followed:</p><p>Here are some of the books I would recommend you to strongly read.</p><ol><li><a href="http://www.gv.com/sprint/">Sprint (Google Ventures)</a></li><li><a href="https://www.ideo.com/post/creative-confidence">Creative Confidence (IDEO)</a></li><li><a href="http://shop.oreilly.com/product/0636920021827.do">Lean UX</a></li></ol><h3>In conclusion</h3><p>Hackathons have enabled me to be more sensitive of the challenges the different domain of businesses face and through it all, it has definitely opened my eyes to different dimensions of how a problem can be solved. I wish you all the best in hackathons and I hope you’ve enjoyed the article as much as I love writing it.</p><p>Remembering your roots.</p><p>1. Creative freedom over the work I do. Most clients’ project I’ve taken on do not involve much creative and innovative thinking process which I think is still important today, It reminds me why I wanted to be a UX Designer and never to forget that solving a real problem is so important.</p><p>Here are some of the meaningful relationship I’ve made while participating in Hackathon</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EkxOSvjnxK0FfHIQuLrVgQ.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7740dd41cf17" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codezillas/ux-designers-should-start-participating-in-hackathons-7740dd41cf17">UX Designers should start participating in Hackathons</a> was originally published in <a href="https://medium.com/codezillas">Codezillas</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 Develop ERC20 Token Using Truffle]]></title>
            <link>https://medium.com/codezillas/how-to-develop-erc20-token-using-truffle-6e50703cbe87?source=rss----255c770a4a8a---4</link>
            <guid isPermaLink="false">https://medium.com/p/6e50703cbe87</guid>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[bitcoin]]></category>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[crypto]]></category>
            <category><![CDATA[cryptocurrency]]></category>
            <dc:creator><![CDATA[Navdeep Singh]]></dc:creator>
            <pubDate>Thu, 15 Aug 2019 13:30:13 GMT</pubDate>
            <atom:updated>2019-08-15T13:30:12.972Z</atom:updated>
            <content:encoded><![CDATA[<p><em>In this blog post we will begin with a short introduction to the </em><strong><em>Truffle framework</em></strong><em> and create an </em><strong><em>ERC20-compatible token</em></strong><em> using it.</em></p><h3>Truffle for Ethereum</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*dtGTQ6UNuicMePi6.png" /></figure><p>Truffle is a framework for development, testing, and asset pipelining for Ethereum. It also has an incorporated package specification using the <strong>ERC190</strong> standard. <strong>ERC190</strong> is a request for comments, which lays down a set of rules about package specifications for Ethereum smart contracts.</p><p>Asset pipelining for Ethereum is a concept that has been borrowed from Ruby on Rails. This feature provides a framework to concatenate, minify, or compress JavaScript, Solidity, and CSS assets in the required sequence of execution. This feature helps the developer to create compressed and pre-processed library-like modules that can be reused by other coding modules, which helps in improving productivity.</p><h3>Working on a Demo</h3><p><strong>Step 1:</strong> We will start by installing the Truffle framework. The installation is pretty straightforward and can be accomplished in the Git Bash window with the following command:</p><pre><strong>$ npm install -g truffle</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/714/0*qR0QopKKZIgN4I01.png" /><figcaption>Installation inside a new folder named Truffle</figcaption></figure><p><strong>Step 2:</strong> After the installation, we need to initiate the framework, which can be done as shown in the next figure.</p><p><em>If you run into any trouble during installation or initiation, please refer to the Ethereum stack exchange or Truffle documentation for the </em><a href="https://github.com/trufflesuite/truffle/issues"><em>known troubleshooting issues.</em></a></p><p>It is to be noted that, to initiate Truffle for a new project, we must always use the Truffle init command in a new folder with a custom project name. We cannot initiate Truffle in a project folder where the Truffle framework is already initiated, as this then throws an exception.</p><p>This is how it works:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/708/0*5HxifT-oNNyxnlx8.png" /><figcaption>Initiation of Truffle framework</figcaption></figure><p><strong>Step 3:</strong> Let’s now create a simple<strong> ERC20-compatible token</strong>. For this, we create a new folder, <strong>ERC20_token</strong>, inside the private-truffle-project folder and write a solidity file, <strong>ScratchToken.sol</strong>, inside it.</p><p>We place the <strong>solidity</strong> file, along with the <strong>Migration.sol</strong> file, inside the contract folder.</p><p>Now, we open another Git Bash window and type in <strong>testrpc</strong> to start the <strong>testnet</strong> :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/758/0*5P6MIjUelfYjBqZl.png" /><figcaption>Starting testrpc</figcaption></figure><p><strong>Step 4</strong>: In the old Git Bash window, we change the truffle.js file to point to our localhost:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/0*Y4dAHGg6kagkUyJT.png" /><figcaption>Truffle.js configuration changes</figcaption></figure><p><strong>Step 5</strong>: Compile the <strong>.sol</strong> files, as shown in the next figure; they can now be deployed using the command <strong>truffle.cmd</strong> deploy in the <strong>testnet</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/933/0*ECDpkaeGbL_pUf89.png" /><figcaption>Compiling ScratchToken.sol</figcaption></figure><p>This is how we deploy a basic <strong>ERC20-compliant</strong> token using the <strong>Truffle framework</strong>. We can add more functionality for incorporating other <strong>ERC20-compliant</strong> functions that are required for a full-fledged token with security enhancements.</p><h4>Functions and more info</h4><p>An <strong>ERC20-compliant</strong> token must have the following list of functions with the same naming convention:</p><blockquote><strong><em>totalSupply ()</em></strong><em>: This function has no arguments and gets back the total supply of tokens that is in circulation.</em></blockquote><p>For example, if we create an <strong>ERC20 token</strong> that imitates the bitcoin ideology, then the <strong>totalSupply()</strong> would fetch 21 million as a number,</p><blockquote><strong><em>balanceOf (owner)</em></strong><em>: This function has an argument inside the parenthesis, which is the address of the owner.</em></blockquote><p>Hence, this <strong>balanceOf </strong>function needs address of the owner to return back the balance value.</p><blockquote><strong><em>transfer (to, value): </em></strong><em>This function has two arguments, namely, the </em><strong>address of the to</strong><em>, that is, the recipient’s address, and the</em><strong> value</strong><em> of how many tokens needs to be sent to that address.</em></blockquote><blockquote><strong><em>transferFrom (from,to,value): </em></strong><em>This function has three arguments, namely, the sender’s address, the recipient’s address, and the value of the token that needs to be transferred.</em></blockquote><blockquote><strong><em>approve (spender,value): </em></strong><em>This function empowers the owner to approve a third-party entity to spend tokens up to a designated maximum value</em></blockquote><blockquote><strong><em>allowance (owner, spender): </em></strong><em>This function allows the owner to empower a third-party entity to spend tokens on their behalf, without enforcing any spending limit.</em></blockquote><p>In addition to the previous functions, such a token has two events:</p><blockquote><strong><em>event Transfer(from, to, tokens)</em></strong></blockquote><blockquote><strong><em>event Approval(tokenOwner, spender, tokens)</em></strong></blockquote><p>For example, if there is a token that has a <strong>totalSupplyInCirculation()</strong> function, which has the same functionality as <strong>totalSupply()</strong>, the token is <strong>ERC20-non-compliant</strong>, because the function name has changed.</p><p>Events, on the other hand, are broadcast throughout the network. For example, when a transfer occurs, a transfer event is broadcast throughout the network so that the network is aware of the transaction and the blocks are updated accordingly in the blockchain. Approval events are also broadcast in a similar manner to make the network aware of the change of authority of the spender’s address.</p><p><strong>Ethereum Smart Contract Development</strong></p><p><em>If you found this article an interesting read, you can try </em><a href="https://amzn.to/2IL04yJ"><em>Ethereum Smart Contract Development</em></a><em> to Become an Ethereum Blockchain developer using a blend of concepts and hands-on implementations. This book is your guide to blockchain and Ethereum smart contract development.</em></p><p><strong>Word of Caution</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/0*KuDZbAvAhdQsRMb3." /></figure><p>It is advised to store crypto tokens in a hardware wallet like the one shown in the image,</p><p><em>Storage Carrying Case for KeepKey Simple Cryptocurrency BTC Bitcoin Wallet Hardware by Aenllosi (Black)</em></p><p>For other updates you can follow me on Twitter on my twitter handle <a href="https://twitter.com/NavRudraSambyal">@NavRudraSambyal</a></p><p>Thanks for reading, please share it if you found it useful 🙂</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e50703cbe87" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codezillas/how-to-develop-erc20-token-using-truffle-6e50703cbe87">How to Develop ERC20 Token Using Truffle</a> was originally published in <a href="https://medium.com/codezillas">Codezillas</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Golang : Leaky goroutines and how to clean them]]></title>
            <link>https://medium.com/codezillas/golang-leaky-goroutines-and-how-to-clean-them-30b505417028?source=rss----255c770a4a8a---4</link>
            <guid isPermaLink="false">https://medium.com/p/30b505417028</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[goroutines]]></category>
            <category><![CDATA[design-patterns]]></category>
            <dc:creator><![CDATA[manoj s k]]></dc:creator>
            <pubDate>Thu, 15 Aug 2019 13:29:34 GMT</pubDate>
            <atom:updated>2019-08-15T13:29:34.216Z</atom:updated>
            <content:encoded><![CDATA[<h3>Golang patterns: Leaky goroutines and how to clean them</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QGyVCHQWYfsaRYxhZusRVw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@veverkolog?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Dušan Smetana</a> on <a href="https://unsplash.com/search/photos/gopher?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><h3>Introduction</h3><p>With intuitive concurrency primitives Go makes it easier to write concurrent programs and also makes it easier to make sense of such programs. I have covered concurrency support in <a href="https://medium.com/@manoj563125/go-concurrency-a-primer-de4e8eefd16e">Concurrency Primer</a>. We will be able to use the features in many different combinations to create various patterns that solve different problems we encounter with concurrent code. But with such simplicity also comes few caveats and we have to be aware of them while building large scale applications on Go. One such is leaking goroutines which I will cover in detail in this post.</p><h3>Goroutines: a quick recap</h3><p>Goroutines are created using <em>go </em>keyword appended before the function which is the entry-point to goroutine. It executes asynchronously to the calling goroutine and it is handled by runtime. Goroutine will be terminated by:</p><ul><li>when it completes whatever task it is doing.</li><li>when it errors out</li><li>it is explicitly stopped.</li></ul><h3>Go GC and goroutines</h3><p>Go comes with Garbage collector(GC) and it is a neat way to cleanup objects that are not needed and keep the memory/ run time tidy without programmer worrying about allocate-cleaning mess(C++).</p><p>But there is a nuance here. GC wont cleanup goroutines. Even if the goroutine is taking forever to complete, GC wont bother.</p><p>Take an example of following piece of code. Here since goroutine running poorFoo() receives nil values channel, it will forever be blocked and lies around in a vegetative state.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9242ba1feb2348e8eeea6857eae22e7/href">https://medium.com/media/f9242ba1feb2348e8eeea6857eae22e7/href</a></iframe><p>Though this is a very simplistic and dumb example, there are cases like a web client waiting for a response from some server which can theoretically take forever. We have said goroutines are lightweight, but they still cost in terms resources and we will not like huge number of such waste goroutines lying around. We need a way to signal goroutines to die in a convenient way by the main caller.</p><h3>Done channel</h3><p>A very common way to accomplish such signaling is to use done channels. Consider the same above example with a done channel passed to the goroutine as below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3aeb6385ba9bf54bb9076f423cd6d7d2/href">https://medium.com/media/3aeb6385ba9bf54bb9076f423cd6d7d2/href</a></iframe><p>All I have done is pass a done channel which will be used later to signal to return the goroutine. Select call blocks until either one of the channels become readable and in out case since dummy ch won’t be readable ever, when Maincaller() closes the done channel, that case will be unblocked and executes return. Thus the previously vehemently alive goroutine is now closed effectively.</p><p>We can have goroutine listening on multiple type of events: setting timeout on the goroutine, done event coming from the caller goroutine or some other eventing from very top caller.Usually in network applications, it makes sense to put a timeout on tasks so that they will not wait unnecessarily or the request parameters might have expired. Below is an example where a timeout signaling is also embedded into the goroutine.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e409ca397946c2257df93a72e8f9105/href">https://medium.com/media/3e409ca397946c2257df93a72e8f9105/href</a></iframe><p>In the above case, if the done or other channels do not signal within 1 second, the last case will be unblocked (time.After() will send time on the channel after elapsing the said time) and executes the statements. This is now getting neater and handling rogue goroutines is better this way.</p><p>But Go language itself provides these idioms in Context package.</p><h3>Go Contexts</h3><p>Contexts ,as the name suggests, are used to pass along contexts. It has ways to not just signal done or timeouts but in an idiomatic way while giving better information when signaling.</p><p>From context package, we get Context which is an interface type as below:</p><pre><strong>type</strong> Context <strong>interface</strong> {<br>    // ok is false if no deadline set. if true, deadline is time     //when work done to be cancelled.<br>    Deadline() (deadline time.Time, ok <strong>bool</strong>)<br><br>    <em>//returned channel will be closed when we need to cancel.</em><br>    Done() &lt;-<strong>chan</strong> <strong>struct</strong>{}<br><br>    <em>// Err returns a non-nil error value after Done is closed. Like</em><br>    <em>// Canceled if the context was canceled or</em><br>    <em>// DeadlineExceeded if the context&#39;s deadline passed. </em><br>    Err() <strong>error</strong><br><br>    <em>// Value returns the value associated with this context for key,</em><br>    <em>// or nil if no value is associated with key.</em><br>    Value(key <strong>interface</strong>{}) <strong>interface</strong>{}<br>}</pre><p>Along with these, context package itself has below functions exposed.</p><pre><strong>func</strong> WithCancel(parent Context) (ctx Context, cancel CancelFunc)<br><strong>func</strong> WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc)<br><strong>func</strong> WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)</pre><pre><strong>func</strong> Background() Context // returns empty context</pre><p>Basically context package will :</p><ul><li>provide a way of cancelling tasks on behalf of caller</li><li>provide a key value store to pass down the call hierarchy.</li></ul><p>In this post, we will focus on its utility in the first scenario- cancellation of tasks.</p><p>When using context, we pass a context object as argument to our function.</p><pre>ctx := context.Background()<br></pre><p>And we use functions of context package to use the empty context created above to create other contexts which are more useful.</p><ul><li>WithCancel() returns a new Context that closes its done channel when the returned cancel function is called. This wraps done channel with a neat sugar.</li></ul><pre>func Maincaller(){<br>  ctx := context.Background()<br>  cancelctx, cancel := context.WithCancel(ctx)<br>  <br>  go func(){<br>      ret := someIndependentOperation()<br>      if ret{<br>          globalchan&lt;- ret<br>      }<br>      else{<br>          cancel()<br>      }</pre><pre>go func(ctx context.Context) error{<br>    select {<br>    case &lt;-ctx.Done():<br>        return<strong> </strong>ctx.Err()</pre><pre>    case x&lt;- globalchan:<br>         return x<br>    }<br> }(cancelctx)</pre><p>As we can see, when we call context.WithCancel(), we get a new context called cancelctx and a cancelFunc called cancel. The downstream goroutines have access to this context as well as cancel function. When a module calls cancel(), context will close the Done channel associated with it and hence in the second goroutine the first case will unblock and return an error “<em>Canceled</em>”</p><ul><li>WithDeadline returns a Context that closes its done channel when the clock runs beyond the passed deadline</li><li>WithTimeout returns a new Context that closes its done channel after the given timeout duration.</li></ul><pre>func foo_timings(ctx context.Context) error{<br>  cctx, cancel := context.WithTimeout(ctx, 1*time.Second)<br>  err := someLongCalculation(cctx)<br>  if err{<br>    return err<br>  }<br>  //continue operation<br>}</pre><pre>func someLongCalculation(ctx context.Context) error{<br>    dl_ctx, ok := ctx.Deadline()<br>    if ok{<br>       // check if deadline passed, if yes no need to proceed //further<br>    }<br>    switch{<br>      case &lt;-ctx.Done():<br>           return ctx.Err()<br>      case &lt;-someslowchannel:<br>            return nil<br>   }<br>}</pre><p>In case the someslowchannel channel does not return within the given timeout of 1s, the done channel associated with context is closed and the error “Deadline exceeded” is returned.</p><p>We also see, the callee has access to newly created dl_ctx context which can be used to see if deadline is already passed and if yes any decisions to be taken. Only after this check is passed, we can go ahead with the select statements. Thus, both caller and callee using context can use the context package functions to appropriately take actions on cancellations of tasks.</p><h3>Conclusion</h3><p>We saw how to build up a done channel hierarchy using simple channels to signal goroutines to stop. We also saw neat syntactic sugar of context package to achieve the same with added features. This way we can cleanup rogue goroutines and with that covered can spin up safely a large number of goroutine tasks with associated timeouts, stop signaling etc associated.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=30b505417028" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codezillas/golang-leaky-goroutines-and-how-to-clean-them-30b505417028">Golang : Leaky goroutines and how to clean them</a> was originally published in <a href="https://medium.com/codezillas">Codezillas</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>