<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dani Lewandowski on Medium]]></title>
        <description><![CDATA[Stories by Dani Lewandowski on Medium]]></description>
        <link>https://medium.com/@webdevdani?source=rss-3e0f22d116f6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*q5AhTky6pCodzAKj0p7AEg.jpeg</url>
            <title>Stories by Dani Lewandowski on Medium</title>
            <link>https://medium.com/@webdevdani?source=rss-3e0f22d116f6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 00:37:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@webdevdani/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A Quick Intro to React Hooks with useEffect, useState, and useContext]]></title>
            <link>https://codeburst.io/quick-intro-to-react-hooks-6dd8ecb898ed?source=rss-3e0f22d116f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/6dd8ecb898ed</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[hooks]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Dani Lewandowski]]></dc:creator>
            <pubDate>Wed, 23 Jan 2019 19:30:55 GMT</pubDate>
            <atom:updated>2019-07-22T04:28:00.465Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CWQEn-btflBOJ2TPosUY9g.jpeg" /><figcaption>Hooks on cups on hooks. Original photo by <a href="https://unsplash.com/@chris_j_scott?utm_source=medium&amp;utm_medium=referral">Chris Scott</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a>, poor editing by me</figcaption></figure><p>At React Conference 2018, the React team first introduced the concept of hooks and my mind was blown. Both from excitement, <em>and</em> confusion. Since then, hooks have become available in React 16.8, and the community is <a href="https://medium.com/tag/hooks">abuzz</a>. At first, it was tricky for me to understand how to use the different hooks but since then I’ve started using them in a side project and am in love!</p><p>If you haven’t had the chance to look into them, nows a great time. They’re an excellent way to make code cleaner, make components smaller, and reuse logic.</p><h3>What are hooks?</h3><p>Hooks let us abstract different pieces of complexity from our functional components. They are regular functions that you import directly from the react library and call in your components. Different hooks do different things:</p><h4>useEffect</h4><p><a href="https://reactjs.org/docs/hooks-effect.html">useEffect</a> is a hook for encapsulating code that has ‘side effects<strong>,</strong>’ and is like a combination of componentDidMount, componentDidUpdate, and componentWillUnmount. Previously, functional components didn’t have access to the component life cycle, but with useEffect you can tap into it.</p><h4>useState</h4><p><a href="https://reactjs.org/docs/hooks-state.html">useState</a> is a hook that encapsulates local state management. Previously, functional components were called “stateless components,” but no longer! With useState, we can utilize what seems like local state for storing values. Actually, they’re more like “state variables” that React keeps track of by the order you declared them. useState saves us from having to create class-based components for state-related responsibilities, since it gives functional components the power and flexibility to handle it themselves.</p><h4>useContext</h4><p><a href="https://reactjs.org/docs/hooks-reference.html#usecontext">useContext</a> is a hook for getting broadly shared application data from <a href="https://reactjs.org/docs/context.html">context providers</a>. When they added the official context API to React in 16.3, I was really excited about the capabilities but hated working with the API. The need for functional rendering made sense, but I strongly disliked using it. Now with the<a href="https://reactjs.org/docs/hooks-reference.html#usecontext"> useContext hook</a>, we can utilize all that power without having to ugly up our code or the<a href="https://github.com/facebook/react-devtools"> React Developer Tools</a> component tree!</p><p>There are also other hooks that exist as of the 16.8-alpha, but they aren’t covered in this intro. You can find documentation on them here:</p><ul><li><a href="https://reactjs.org/docs/hooks-reference.html#usereducer">useReducer</a></li><li><a href="https://reactjs.org/docs/hooks-reference.html#usecallback">useCallback</a></li><li><a href="https://reactjs.org/docs/hooks-reference.html#usememo">useMemo</a></li><li><a href="https://reactjs.org/docs/hooks-reference.html#useref">useRef</a></li><li><a href="https://reactjs.org/docs/hooks-reference.html#useimperativehandle">useImperativeHandle</a></li><li><a href="https://reactjs.org/docs/hooks-reference.html#uselayouteffect">useLayoutEffect</a></li><li><a href="https://reactjs.org/docs/hooks-reference.html#usedebugvalue">useDebugValue</a></li></ul><h3>Learning by example</h3><p>Personally, I learn better by trying things out and seeing examples than I do by reading overviews and concepts. The <a href="https://reactjs.org/docs/hooks-intro.html">examples in the React documentation</a> are great but purposefully simple, so it was easier for me to grasp the different hooks through more realistic examples. So for this introduction, I’m going to run through some code samples and explain how the featured hook works.</p><blockquote><strong>Note</strong>: Hooks only work in functional components!</blockquote><h3>Using native event listeners</h3><p>This is a small functional component that renders an 𝗫 ‘close’ button for use in a modal. It only accepts one prop, and that’s used to close the modal on click of the button. However, in order to make the <a href="https://www.w3.org/TR/wai-aria-practices/examples/dialog-modal/dialog.html">modals more accessible</a> and provide a better user experience, it should also be able to close on press of the escape key.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05c4b29e0aa97a95acfe49e29bc2f662/href">https://medium.com/media/05c4b29e0aa97a95acfe49e29bc2f662/href</a></iframe><p>Let’s look at the hook used to make this possible:</p><pre>useEffect(() =&gt; {<br>    window.addEventListener(&#39;keydown&#39;, handleEscapeKeyDown);</pre><pre>    return () =&gt; {<br>        window.removeEventListener(&#39;keydown&#39;, handleEscapeKeyDown);<br>    };<br>}, []);</pre><p>useEffect accepts two arguments and doesn’t return anything. The first parameter is a function that holds the code you want to run. If you return a function from this function, it will be used for “clean-up.” This is where you would negate the effects of your effect if you need to. Here I use it to unbind the event listener.</p><p>The optional second parameter tells React when you want this hook to be called. If you don’t pass anything, your function will be called on mount and every rendered update. If you pass an array of values, like props or values from state, the function will be called on mount and whenever any of those values change. So if you want to trigger an effect purely when one or a few props change, you can! It’s a streamlined way of writing componentDidUpdate with a lot less effort. Or, if you pass an empty array like in the code sample, it will only be called on mount.</p><p>Our returned “clean-up” function is called before any repeated call of the effect function, and always on unmount. This is done in order to “ensure consistency by default and prevents bugs that are common in class components due to missing update logic.” <a href="https://reactjs.org/docs/hooks-effect.html#explanation-why-effects-run-on-each-update">(React Hooks docs)</a></p><h3>A form with controlled inputs</h3><p>This example is a very simple form for updating two fields. In order to use controlled inputs and simplify gathering the form data, I’m using useState to store the input values.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8349886ead06e68f508c54908cf4ebaa/href">https://medium.com/media/8349886ead06e68f508c54908cf4ebaa/href</a></iframe><p>Functional components that use useState don’t have access to a single state object like class-based components do. Instead, we use useState for each value we want to store in a state variable.</p><pre>const [name, setName] = useState(props.name);</pre><p>useState is a simple function that just accepts one optional argument. If you do pass an argument to useState, that will be the value’s initial state. In the form example, I’m passing the user’s already existing name and e-mail to be the default values.</p><p>Calling useState returns an array with two values. The first one is the current value of the state variable, and the second one is a setter function for you to use to update the state as needed. Here we’re using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#Spread_in_array_literals">array spreading</a> to easily access them. They can be named whatever you want since array spreading works by index. (If it’s still confusing, the <a href="https://reactjs.org/docs/hooks-state.html#tip-what-do-square-brackets-mean">React useState docs</a> explain it well.)</p><p>To keep the inputs in the example controlled, I use the setter function on change of the input and pass the state variable as its value:</p><pre>&lt;input                    <br>    name=&quot;name&quot;                    <br>    type=&quot;text&quot;                    <br>    value={name}                    <br>    onChange={(e) =&gt; setName(e.target.value)}        <br>    required<br>/&gt;</pre><p>useState is really simple, and will probably be the most used hook. It gets especially fun when you start combining it with other hooks!</p><h3>Accessing static user data</h3><p>Getting basic information about who is currently logged in is a great use case for React Context. It’s cumbersome to pass the user prop to every component in our tree, since it rarely changes. With useContext, it works the same and looks a lot neater:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e000b6d3d65de5c4cc19e1c721c6124/href">https://medium.com/media/0e000b6d3d65de5c4cc19e1c721c6124/href</a></iframe><p>Creating context providers remains the same, but now we don’t need to wrap children in functions to access those values! With useContext it’s as easy as calling a function and using the results of it to access our context values:</p><pre>const user = useContext(UserContext);</pre><p>useContext requires only one argument, and it’s the context object of the context you want to access. You can use useContext anywhere as long as it’s within the provider of that context. Even better yet, it will update the returned value whenever your context updates! It’s super helpful for avoiding prop-drilling without the use of libraries like Redux.</p><p>If you’re interested in a deeper dive into possible uses for hooks, I strongly recommend <a href="https://medium.com/u/162352c45b6e">Ryan Florence</a>’s talk from React Conf 2018. He spoke after <a href="https://medium.com/u/b610e3f3fee2">Sophie Alpert</a> and <a href="https://medium.com/u/a3a8af6addc1">Dan Abramov</a> <a href="https://www.youtube.com/watch?v=V-QO-KO90iQ">introduced hooks</a> for the first time. It was overwhelming but immensely exciting:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FwXLf18DsV-I%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DwXLf18DsV-I&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FwXLf18DsV-I%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/427aec0855dad0751a33c83769524b28/href">https://medium.com/media/427aec0855dad0751a33c83769524b28/href</a></iframe><p>Hooks are an immensely helpful feature that makes me excited about the future of React. The more you work with them, the more components you start to notice could be simplified with them.</p><p>If you want to test hooks, I recommend spinning up an app with <a href="https://github.com/facebook/create-react-app">Create React App</a> (CRA), bringing over some of your own existing components, and refactoring them with hooks. As you see your components shrink, I think you’ll fall in love with them too.</p><blockquote>𝓲 💖 👏’s<br> If people enjoy this article, I’ll follow up with how I’m organizing custom hooks in projects, how to reuse logic with them, and the power of combining multiple hooks.</blockquote><p><em>Many thanks for the copy-editing by </em><a href="https://medium.com/u/3eaead2336fe"><em>Bart Flaherty</em></a><em> and </em><a href="https://twitter.com/onietzschan"><em>Ash Dague</em></a><em>!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6dd8ecb898ed" width="1" height="1" alt=""><hr><p><a href="https://codeburst.io/quick-intro-to-react-hooks-6dd8ecb898ed">A Quick Intro to React Hooks with useEffect, useState, and useContext</a> was originally published in <a href="https://codeburst.io/">codeburst</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setting Global NPM Defaults for Quick-starting New Projects]]></title>
            <link>https://codeburst.io/setting-global-npm-defaults-for-quick-starting-new-projects-ed06ed22edb3?source=rss-3e0f22d116f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed06ed22edb3</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[npm]]></category>
            <dc:creator><![CDATA[Dani Lewandowski]]></dc:creator>
            <pubDate>Fri, 23 Feb 2018 15:57:27 GMT</pubDate>
            <atom:updated>2018-02-24T15:47:58.856Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*Aet7CQH9Ehti_s5E7zk_CQ.gif" /><figcaption>NPM Wombat. Because wombats.</figcaption></figure><p>Each time you start a new project that utilizes NPM (<a href="https://www.npmjs.com/">Node Package Manager</a>) as it’s dependency manager, you have to create a package.json file. This file contains crucial info about your project, the dependencies it relies on, and can even contain simple custom-made command line scripts. If you have a lot of side-projects or like to build prototypes, you’ll be creating these files often.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/503/1*jcVwEKfMFABJiyeETpCPoA.gif" /></figure><p>To create a package.json file, you run npm init in the terminal while in your new project’s directory. This takes you through a tiny quiz to build the file based on your answers. If you’re making several of these, or just don’t like writing your name like me, you can configure the default values that NPM offers in this init quiz.</p><p>To check all the properties in your global <a href="https://docs.npmjs.com/files/npmrc">NPM config</a> (npmrc) file, in the command line, run</p><blockquote>npm config ls -l</blockquote><p>This will list out <strong>everything</strong> it relies on but we’re only interested in modifying the init properties right now. If you scroll down the list, you’ll find a few “init” prefixed properties like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*GdLWKfS39AOBaLyyH4cU4g.png" /><figcaption>Default global NPM config file values</figcaption></figure><ul><li><a href="https://docs.npmjs.com/misc/config#init-author-email">init-author-email</a></li><li><a href="https://docs.npmjs.com/misc/config#init-author-name">init-author-name</a></li><li><a href="https://docs.npmjs.com/misc/config#init-author-url">init-author-url</a></li><li><a href="https://docs.npmjs.com/misc/config#init-license">init-license</a></li><li><a href="https://docs.npmjs.com/misc/config#init-module">init-module</a></li><li><a href="https://docs.npmjs.com/misc/config#init-version">init-version</a></li></ul><p>These values are what get suggested to us or are automatically filled in when we run npm init. Several of them are just empty strings by default but we can easily configure them to offer more helpful, personalized suggestions instead.</p><h3>Customizing your Global NPM Config</h3><p>Using the npm config <a href="https://docs.npmjs.com/cli/config">commands</a>, we can easily <em>configure </em>these different values<em>. </em>To globally set a value, we would use</p><blockquote>npm config set &lt;key&gt; &lt;value&gt; -g</blockquote><p>For example, if I want to set the author name to be my own, I would run</p><blockquote>npm config set init-author-name &quot;Dani Lewandowski&quot; -g</blockquote><p>If you’re setting a value that has spaces in it, make sure to wrap it in quotes or only the first word will be used. Otherwise the quotes can be omitted.</p><p>To check what the value is now set to, use</p><blockquote>npm config get &lt;key&gt;</blockquote><p>If we tried to see the global list with npm config ls -l again, it would only show that the defaults have been overwritten but not to what value. Use the get commands to find out what they’ve been overwritten to.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/756/1*vDNwRP6uZWNm2izsV09Eug.png" /><figcaption>Modified results of ‘npm config ls -l’</figcaption></figure><p>If you’re setting a different license version, make sure to use a valid SPDX license identifier. Read more about them <a href="https://docs.npmjs.com/files/package.json#license">here</a>.</p><p>If you’d like to edit these values in bulk and feel confident in not messing anything up, you can use npm config edit -g to edit your global config file directly. I recommend modifying one value with the set command first so you can see how the values should be formatted at the top of the file.</p><h3>Streamlined &#39;npm init&#39;</h3><p>After customizing whichever rules you’d like, creating package.json files will be even quicker. If you’ve set any of the asked question’s defaults to a different value, that will be suggested as the autofill value. If it’s not a question, it will simply set it for you automatically. Watch this npm init with updated defaults:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/506/1*ESze77i9MExYcDC6rOQYiA.gif" /><figcaption>Customized ‘npm init’ values</figcaption></figure><p>Although I’ve only set up my personal author information and changed the default license suggestion, it’s nice not having to write it each time. Laziness is a virtue when it comes to programming. If you don’t even want to have to hit enter each time, you can simply run npm init -y which will automatically accept all of the defaults and create the package.json from it. You can always update it directly or through the command line if you need to make changes.</p><h3>Need even more power?</h3><p>If you’d <strong><em>really</em></strong> like to customize the npm init flow, you can set up your own <a href="https://docs.npmjs.com/misc/config#init-module">init-module</a>. Note: This is overkill for most uses. Check the location of your init-module with npm config get init-module. From there, you can create and edit that file by <a href="https://docs.npmjs.com/getting-started/using-a-package.json#how-to-customize-the-packagejson-questionnaire">creating fields or prompts</a> to be asked instead. Keep in mind that this has to contain every question or field you want to be asked because it will replace the standard npm init flow.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed06ed22edb3" width="1" height="1" alt=""><hr><p><a href="https://codeburst.io/setting-global-npm-defaults-for-quick-starting-new-projects-ed06ed22edb3">Setting Global NPM Defaults for Quick-starting New Projects</a> was originally published in <a href="https://codeburst.io/">codeburst</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 Make Your Code Stand out to Interviewers]]></title>
            <link>https://medium.com/@webdevdani/how-to-make-your-code-stand-out-to-interviewers-ff2a1b4464c?source=rss-3e0f22d116f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/ff2a1b4464c</guid>
            <category><![CDATA[jobs]]></category>
            <category><![CDATA[code]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[learning-to-code]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Dani Lewandowski]]></dc:creator>
            <pubDate>Wed, 07 Feb 2018 19:59:11 GMT</pubDate>
            <atom:updated>2018-02-07T19:59:11.700Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wkYuJbRH4degUHBZu4l6jQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/Q3zvxHEm6B4?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Tobias Winkelmann</a> on <a href="https://unsplash.com/search/photos/beautiful-code?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>While reviewing the projects of job applicants, I’ve noticed a major difference in the code of the candidates we go forward with and the ones we don’t.</p><p>The code they write, to put it simply, is <em>beautiful</em>. And I don’t mean because it’s necessarily amazing code; I mean that it’s easy to look at and I want to keep reading it. It’s consistently styled, descriptive, documented, and I just can’t take my eyes off of it.</p><p>The world could certainly use more code like that. If you’re a new developer, having trouble getting bites on your applications, or just want to make your code more professional, here are some easy techniques you can begin implementing to make your code more desirable:</p><ul><li>Use version control more professionally</li><li>Use semantic naming for variables and functions</li><li>Include comments and documentation</li><li>Lint for consistent style</li><li>Write automated tests for your code</li></ul><p>Throughout this article, we’re going to use these suggestions to improve this purposefully bad code to show that even though you might not be a senior developer yet, they can vastly help improve the look, and quality, of anyone’s code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7834bf1f4b60aac7ca7785e12502d6be/href">https://medium.com/media/7834bf1f4b60aac7ca7785e12502d6be/href</a></iframe><blockquote>Although this article focuses on JavaScript, the concepts within are applicable to most, if not all, languages.</blockquote><h3>Version Control</h3><p>Do you have any publicly accessible code for potential employers to look at? It’s important to be able to show them that you know how to work in a project-structure that’s protected by version control. If you don’t use any type of version control hosting, like <a href="https://github.com/">GitHub</a> or <a href="https://bitbucket.org/">Bitbucket</a>, please start. Although employers might not spend too much time going through your public code, we do like to peruse to see what kinds of things you’re interested in making, and of what quality.</p><p>If you don’t know how to use git at all yet, here are some resources you can learn from. Keep in mind that no matter which article you read, you can still use any host you want. The concepts apply to any git system.</p><ul><li><a href="https://guides.github.com/activities/hello-world/">GitHub’s intro tutorial</a></li><li><a href="https://www.atlassian.com/git/tutorials/what-is-version-control">Bitbucket’s intro tutorials</a></li><li><a href="https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners">An Intro to Git and GitHub for Beginners</a></li></ul><p>When using git for your own projects, make sure to use branches instead of just pushing to master. This is more like how a proper team would work and shows that you’re smart enough to be careful.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Jm2Svh0qVQ-AmdvXSHWTbg.png" /><figcaption>The landing page of a repository on GitHub, showing some awful commits 😣</figcaption></figure><p>Learn to be aware of what your commit messages look like to other people. They aren’t just something you need to do to be able to push your code, they’re a crucial form of documentation in a code base. Also, when viewing a project repository on GitHub, the landing page shows the most recent commit message that affected that file. Interviewers definitely see them. If you want to learn how to write better commit messages, Chris Beam’s blog post on <a href="https://chris.beams.io/posts/git-commit/">how to write better commit messages</a> provides some fantastic patterns to follow.</p><h3>Semantic Naming</h3><p>You know what’s nice? When you can understand what code is doing at a glance. In programming, there’s an old adage that, “good code is self-documenting” and you should always write with that in mind. You should try to write your code in a way that it can almost be read like a sentence.</p><h4>Variable naming</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4UhkJztWnpkqVBeng8DNlA.png" /><figcaption>Short variable names vs. descriptive names</figcaption></figure><p>Looking at the example above, can you instantly tell which is easier to read? Please don’t make people do mental jiu-jitsu to understand what your code is doing. If you name variables by what they’re meant to represent without unnecessarily trying to shorten them, your code reads more like a story with a purpose than a cipher without a key. And at the very least it avoids readers having to scroll up to remember what in the world t stands for.</p><p>Abbreviated names were originally used to save memory in older programming languages. Thankfully in JavaScript, we don’t have to worry about that so you can get as descriptive as you want with your variable names. If you do want to save space, <a href="https://en.wikipedia.org/wiki/Minification_(programming)">minify</a> and <a href="https://en.wikipedia.org/wiki/Obfuscation_(software)">obfuscate</a> your code for production instead of making the rest of us suffer by trying to do it yourself.</p><p>Writing descriptive variable names and following some common stylized naming conventions will further help people understand what a variable is. These naming conventions focus on the type the variable is. Some examples are PascalCase for classes/constructor functions, state-prefixed roots for booleans, UPPERCASE_SNAKE_CASE for important, higher-scoped constants, or a $ sign preceding jQuery objects. Although not everyone follows these conventions, they’re well enough known that experienced developers will recognize the purpose they serve.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/806/1*LIukEOG27X9nfm78JsoJng.png" /></figure><h4>Function Names</h4><p>Function names should start with verbs to denote that they perform an action. For example, getters and setters on an object should be get[PropertyName] or set[PropertyName]. A function that makes an AJAX request for getting product details could be named fetchProductDetails.</p><p>However you name your function, try to be as clear as possible about its use and intention. If you’re having trouble naming your function because it doesn’t just do one thing, that’s usually a sign you should refactor it into multiple, smaller, functions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be9bfc162041d90ff60031d2c1f9a289/href">https://medium.com/media/be9bfc162041d90ff60031d2c1f9a289/href</a></iframe><p>In our code example, I’ve renamed all the variables to full words, renamed our order function, and even made a new one. Our new little function getProductPrice is pretty simple, but it’s a helpful little bit that we can reuse. It’s also self-documenting by name alone because it tells us exactly what it’s doing. Our original function, order, was renamed to calculateTotalPrice in order to be more descriptive of what it’s actually doing.</p><h3>Comments &amp; Documentation</h3><p>Sometimes semantic naming can only get us so far. Other developers could spend hours reading through and trying to understand your code base, but do they have the time and desire to? Probably not. What if you could just <em>tell them</em> what your code does and save them some time?</p><p>If you don’t write comments in your code because you ‘understand what your code is doing,’ clearly you’ve never looked back on your code after a few weeks of not working on it. If you’re having trouble understanding your code, other developers definitely will. So do everyone, <em>including yourself</em>, a favor by adding comments to your code as you go.</p><p>However, with great comments comes great responsibility. Be wary of over-commenting and making your code more cluttered. Comments shouldn’t explain every little thing that you’re doing. That’s what semantic naming is supposed to help with. Other developers should be able to follow along well enough but they might not know <em>why </em>you wrote certain blocks. Try to use inline comments for explaining your intent.</p><h4>Documentation Comments</h4><p>For functions, it’s helpful to write a small explainer above them. This serves as a small form of documentation. Depending on what style you write your comments in, they can even be used to create actual documentation!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/519/1*C4hy4nIFbP6_pwsdmg2KjQ.gif" /><figcaption>Using Docblockr to write JSDoc function documentation in Sublime</figcaption></figure><p>One popular style for writing comment docs is <a href="http://usejsdoc.org/about-getting-started.html">JSDoc</a>. Through plugins like <a href="https://github.com/spadgos/sublime-jsdocs">Docblockr</a> (for Sublime), you can easily add auto-filled comments in this style. If you then decide to create documentation from them, there are tools that can do that automatically. Check them out in the <a href="https://github.com/jsdoc3/jsdoc#templates-and-tools">JSDoc Github repo</a>.</p><p>Writing comments like this can be another helpful way to realize you might need to refactor a function. If you can’t sum up what a function is doing in one sentence, it’s probably time to split it up into several smaller functions that can be.</p><h4>README, please</h4><p>READMEs are markdown files that source code hosting sites like GitHub and BitBucket will display as formatted text when viewed in your repo. Every project should have <em>at least</em> one README.md. This is usually the first introduction to your project. If no one can understand what your project’s intent is, how to get it started, or can’t find any info about it, they probably won’t care about it.</p><p>For an in-depth guide on what to put in a README, check out the <a href="https://github.com/noffle/art-of-readme">Art of README</a>. If you don’t have that much information to write, don’t feel bad! Just put in what you can. Use the READMEs from repositories you like as examples. And if you’re like me and constantly need to reference how to write markdown, here’s a handy <a href="https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet">markdown cheatsheet</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca112742c83e45a3396e4c6c387067e9/href">https://medium.com/media/ca112742c83e45a3396e4c6c387067e9/href</a></iframe><p>Here we’ve added little comments showing what our functions do, what types their parameters are expected to be, and what value our functions might return. In JSDoc notation, string[] means an array of strings, and number|undefined means number or undefined. These are helpful for seeing how to work with a function at a glance.</p><h3>Linting</h3><p>Sure, maybe you’ve got a good eye. Maybe you even have your own personal style of writing code that you think looks nice. Or maybe you’re a spaghetti cowboy who thinks ‘no errors, no problems.’ Truth is, either style can benefit immensely from a linter.</p><p>A linter is an extension in your text editor or IDE that enforces style rules. So instead of you trying to manage yourself, it will highlight and show you exactly what rule you’re breaking. It’s super distracting to see the highlighted errors and incredibly therapeutic to fix them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/966/1*8V_Q5yUfe4tK5GOTBMNyBg.png" /><figcaption>Linter lighting up like a Christmas tree</figcaption></figure><p>In the screenshot above, it’s warning about preferring dot notation over bracket notation, using let or const depending on if the variable gets reassigned, missing or extraneous semi-colons, and odd, or inconsistent, spacing.</p><p>Linters are for more than just style though. Many code style-guides include rules for things to avoid and reminders of best practices. For example, there are rules like no-unused-vars that error when you declare but never use a variable, or no-global-assign that yells when you try to reassign properties on native objects or read-only global variables. These can catch bugs before you push them and save you from some debugging hell in the future. It’s also a great way to learn some new best practices while you’re writing.</p><h4>Setting up a linter</h4><p>For JavaScript, the current most popular linter is ESLint. It’s easily configurable and integrates into most text editors like <a href="https://github.com/SublimeLinter/SublimeLinter-eslint">Sublime</a>, <a href="https://atom.io/packages/linter-eslint">Atom</a>, <a href="https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint">VS Code</a>, and a <a href="https://eslint.org/docs/user-guide/integrations">ton of others</a>. It comes with a set of <a href="https://eslint.org/docs/rules/">default recommended rules</a> you can easily enable, or you can set up your own custom configuration. ESLint also allows you to extend off of outside style-guides. <a href="https://medium.com/u/e9031892baf5">Brandon Morelli</a>’s <a href="https://codeburst.io/5-javascript-style-guides-including-airbnb-github-google-88cbc6b2b7aa">article on style-guides</a> shows off some popular ones. Personally, I like to extend off of <a href="https://github.com/airbnb/javascript">Airbnb’s Javascript Style Guide</a> and customize a few rules from there. (Four space indentation is better than two. Sorry, not sorry. 😉)</p><p>If you’ve never used it before though or maybe just need a refresher, there are tons of great tutorials on learning how to get ESLint set-up. And here are a few to get you started:</p><ul><li><a href="https://eslint.org/docs/user-guide/getting-started">ESLint’s official documentation</a></li><li><a href="https://medium.com/the-node-js-collection/why-and-how-to-use-eslint-in-your-project-742d0bc61ed7">Why (and how) to use eslint in your project</a> by <a href="https://medium.com/u/96cd9a1fb56">Node.js Foundation</a></li><li><a href="https://www.sitepoint.com/up-and-running-with-eslint-the-pluggable-javascript-linter/">Up and Running with ESLint — the Pluggable JavaScript Linter</a></li></ul><p>If you prefer something simpler with less complex configuration options, <a href="http://jshint.com/">JSHint</a> is also great. <a href="https://medium.com/u/fb8d78e7d205">Treehouse</a> has a nice, quick, <a href="http://blog.teamtreehouse.com/improving-code-quality-jshint">blog post on setting it up</a>.</p><h4>Introducing linting to your engineering organization</h4><p>If your company doesn’t use a linter yet, try to get them on board as soon as possible! The quality of everyone’s code will improve by eliminating simple bugs early on, and you’ll be able to read through pull requests much faster when everyone’s style is consistent. First setting up a linter can be a difficult process because you need to have everyone on board with what rules to include. You know the whole spaces vs. tabs fiasco? Now imagine that with spaces vs. no spaces around if statement parentheses. The easiest route would be to go with one of the popular, extendable style-guides I mentioned before and then lint files the next time you’re working in them.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/84c1730c19315655951b3278b2bf2415/href">https://medium.com/media/84c1730c19315655951b3278b2bf2415/href</a></iframe><h3>Testing</h3><p>Now that your functions are smaller and serve a singular purpose, they’re much easier to test! If you don’t know how to test your code, or simply haven’t been adding tests to your personal projects, <strong>right now</strong> is the time to start. It makes your code look immensely more professional and gives you peace of mind when making future changes to your code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cjxNyb27ZSlpeVGHWjJHlQ.png" /><figcaption>Example of a test written using Jasmine</figcaption></figure><p>Writing tests is a great way to show how your function should behave. If across your code you’re using a function that is expected to return a number, and then someone goes and makes a change to it so it now returns undefined in some cases, they’ve just introduced a possible JavaScript error. However, if you had tests, it would’ve been caught beforehand.</p><p>Test your methods for the expected return values and how your function handles incorrect arguments passed to it. <a href="https://medium.com/u/c359511de780">Eric Elliott</a> outlines the things you should make sure to test for in his article “<a href="https://medium.com/javascript-scene/what-every-unit-test-needs-f6cd34d9836d">5 Questions Every Unit Test Must Answer</a>.”</p><p>While writing tests you might notice improvements you should make to increase the resiliency of your code. For example, while writing this simple little test for calculateTotalPrice, I realized that it would break if it was called without any arguments. Now I know to go back and add a type check for productList being defined and being an array. Then I can go and add tests to check it works in those cases to make sure I don’t have to worry about it.</p><h4>Adding testing to your workflow</h4><p><a href="https://medium.com/u/f708ee7ee97b">Vitali Zaidman</a> wrote a great write-up of the <a href="https://medium.com/powtoon-engineering/a-complete-guide-to-testing-javascript-in-2017-a217b4cd5a2a">state of current JavaScript testing</a> that I recommend as a good starting point for research on how to get started with testing. I also recommend James Sinclair’s <a href="https://jrsinclair.com/articles/2016/gentle-introduction-to-javascript-tdd-intro/">Gentle Introduction to JavaScript Test Driven Development</a>.</p><p>If you enjoy the extra sanity tests can give you, try considering switching to a test-driven development (TDD) style. TDD is when you write your tests first before writing the actual code. Of course, they’ll all fail initially, but then as you’re writing, you make them pass. It’s like an outline of what you need to write. And SitePoint has a nice <a href="https://www.sitepoint.com/learning-javascript-test-driven-development-by-example/">intro to TDD through examples</a>.</p><p>If we had started with a TDD workflow, we would’ve had safety nets up for any potential flaws in our code to begin with. It would’ve saved us from having to go back and amend the code to add those type safety-checks because we would have known in advance to watch out for that.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/100b5eafddb0fa69fdbe1b1e48bbde3c/href">https://medium.com/media/100b5eafddb0fa69fdbe1b1e48bbde3c/href</a></iframe><h3>Lookin’ Good</h3><p>What do you think of the before and after of our coffeeShoppe example? The functionality stayed about the same but it looks a lot more professional now. Which development style would you rather work with? If you start using these suggestions in your workflow, it will make a noticeable difference to anyone reviewing your code.</p><p>A crucial part of these changes is that you show the interviewer you don’t need to be taught these things. They’ll see that you’ll be more likely to be able to contribute in a meaningful manner sooner than another candidate because they won’t have to waste time teaching you as much. Adding these techniques in your workflow will boost your potential value as an employee which can increase your chances of being hired. Plus, improving your code is a reward in and of itself. 😉</p><p><em>We’re hiring! </em>If you already follow all of these practices and you’re looking for a new team, check out <a href="https://splashthat.com/careers">careers at Splash</a>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ff2a1b4464c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Stop Making Your Components Able to Do Everything]]></title>
            <link>https://codeburst.io/stop-making-your-components-able-to-do-everything-9dbad62b1c0?source=rss-3e0f22d116f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/9dbad62b1c0</guid>
            <category><![CDATA[design-systems]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[vuejs]]></category>
            <dc:creator><![CDATA[Dani Lewandowski]]></dc:creator>
            <pubDate>Tue, 16 Jan 2018 00:45:29 GMT</pubDate>
            <atom:updated>2018-02-21T00:27:17.031Z</atom:updated>
            <content:encoded><![CDATA[<p>Not every component needs to be a javascript-army knife.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*iUf-Dkz29doiu01CI9vntg.jpeg" /></figure><p>A few weeks ago I got into a conversation with some coworkers on one of my pull requests for a simple React search bar component. They liked that it encapsulated a common pattern we use in several places for searching but had some suggestions for small improvements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/530/1*yWzN2rcVOMreKnwT_73o-w.png" /><figcaption>Pretty straight-forward search input, right?</figcaption></figure><p>“<em>We should make it possible to pass in a different icon.</em>” — Different than the standard magnifying glass we use everywhere?</p><p>“<em>Maybe triggering the search on pressing </em><em>enter should be decided from a boolean prop.</em>” — Wouldn’t that be confusing for the user if one place worked that way and another didn’t?</p><p>“<em>I think the number of milliseconds for debouncing the search should be customizable.</em>” — Wait, wait, what? What use case would justify needing a millisecond difference?!</p><p>Sure, we can make everything a customizable prop, but <em>should</em> we? We need to ask ourselves if we’re building a consistent product or an element library.</p><p>If you <strong>are</strong> building an element library for use by many different developers with many different needs, please go ahead and make everything as extendable and customizable as possible. Thank you for your hard work! For the rest of us who want to build a stellar user-facing product, stay tuned.</p><h3>Aren’t components supposed to make our lives easier?</h3><p>The benefit of components is being able to quickly use them in multiple places throughout your website. Just a few lines of code and you can quickly build an on-brand UI. It’s the closest to plug-and-play we can get while writing code. However, the more props you have to configure for each use, the more work you’ll have to do, and more importantly, the more likely you’re going to have implementation discrepancies across your site.</p><p>A product’s design isn’t purely how it looks, it’s also how it feels. Users learn behavior patterns from previous interactions on your site. Quoting the Director of Product Design at Intercom, Emmet Connolly, from this <a href="https://www.invisionapp.com/blog/guide-to-design-systems/">Invision blog post about design systems</a>,</p><blockquote>Your product is more than just a pile of reusable UI elements. It has structure and meaning. It’s not a generic web page, it’s the embodiment of a system of concepts.</blockquote><p>If in one place a piece of UI behaves differently than one encountered earlier on the site, user’s will have a less cohesive experience, and may even perceive it as broken.</p><p>To avoid this, designers and developers need to be on the same page. Make sure to keep each other in check while building new features/interfaces. If a design requires a slight tweak that would need developers to extend a commonly used component, politely ask the designer if it’s necessary, or if they’d like that change made across the product. Depending on your company structure, you’d be surprised at how open designers are to discourse!</p><p>An easy way to collaborate between teams is to have a live style guide of your components. If you’re using React, I recommend <a href="https://react-styleguidist.js.org/">Styleguidist</a> to make an interactive style guide for everyone to be able to reference. The code examples from Styleguidist are also immensely helpful for bigger dev teams and for onboarding new people to the codebase. (If you’re using Vue, there is a fan-made Vue version of Styleguidist available <a href="https://github.com/vue-styleguidist/vue-styleguidist">here</a>. Disclaimer, I have not personally tried it yet.)</p><h3>Using this concept in code</h3><p>While building, consider not making a feature customizable until it truly needs to be. Sure it might need to change in the future, but you can wait until then to change it. How many times have you expected something to be done later only to have product designers change their mind? Now your code is more complicated than it needs to be for no reason. Laziness can be beneficial.</p><p>Not only is this great practice for component-based code, but whenever you’re writing anything. <a href="https://medium.com/u/b49fae4639c2">Sandi Metz</a> warns about premature extensibility in <a href="https://www.sandimetz.com/blog/2014/05/28/betting-on-wrong">a great blog post</a> as it relates to cost for your team:</p><blockquote>Code is read many more times than it is written. Abstractions add changeability but increase cognitive load. The ones that are actually needed save money, those that aren’t increase costs every time anyone looks at the code.</blockquote><p>In the cases where you do need to extend your own components, make good use of default property values. Don’t just set them to null so your linter stops yelling at you. Set the value to the most common denominator to lessen configuration in most places.</p><p>If you’re using an external component library like <a href="http://www.material-ui.com/#/">Material UI</a> or <a href="https://react.semantic-ui.com/introduction">Semantic UI</a>, consider making wrapper components around them that automatically set the props you need to make the component personalized for your product’s design system. One way to do this is by using a props proxy higher order component as outlined in <a href="https://medium.com/u/bbc4c2a3a7ae">franleplant</a>’s article on <a href="https://medium.com/@franleplant/react-higher-order-components-in-depth-cf9032ee6c3e">higher order components</a>. With this method, you can have components that automatically have set props; saving you the trouble of having to configure them for every single use.</p><p>Another way to do this is to simply wrap the imported component with your own and let it pass through the props while manipulating or adding new ones on the way down:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/77469a602f456fb598177f8a0baf27e5/href">https://medium.com/media/77469a602f456fb598177f8a0baf27e5/href</a></iframe><p>Although this is a simple example of this pattern, proper utilization of it will prove incredibly beneficial in the long run. You can create custom ways of interacting with this component that don’t require using bizarre prop names from the library, set custom default values, and make it significantly easier to migrate versions, switch libraries, or implement your own in the future. Better to change it in one place rather than everywhere you’ve used the component.</p><p>If you keep your product’s UI components as opinionated as possible, it will prove simpler and less time consuming for everyone to develop with them. Don’t allow fellow developers to easily stray from the predesigned uses and behaviors of components by restricting how they can be customized. Restrict how components can be customized to prevent fellow developers from straying from the predesigned uses and behaviors.</p><p><strong>tl;dr</strong>: Mo’ props, mo’ problems.</p><p><em>With thanks to </em><a href="https://medium.com/u/b0a676339359"><em>James Klein</em></a><em> for his review and feedback.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9dbad62b1c0" width="1" height="1" alt=""><hr><p><a href="https://codeburst.io/stop-making-your-components-able-to-do-everything-9dbad62b1c0">Stop Making Your Components Able to Do Everything</a> was originally published in <a href="https://codeburst.io/">codeburst</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>