<?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[ACM@UCR - Medium]]></title>
        <description><![CDATA[ACM@UCR is a collection of articles contributed to by it’s members on topics ranging web development to security to machine learning. - Medium]]></description>
        <link>https://medium.com/acm-ucr?source=rss----abe431afdbc5---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>ACM@UCR - Medium</title>
            <link>https://medium.com/acm-ucr?source=rss----abe431afdbc5---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 14:43:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/acm-ucr" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Data Structures and Algorithms: 0 to 60]]></title>
            <link>https://medium.com/acm-ucr/data-structures-and-algorithms-0-to-60-8413dc3b33b1?source=rss----abe431afdbc5---4</link>
            <guid isPermaLink="false">https://medium.com/p/8413dc3b33b1</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[data-structures]]></category>
            <dc:creator><![CDATA[Aaroh Mankad]]></dc:creator>
            <pubDate>Tue, 16 Jan 2018 22:25:26 GMT</pubDate>
            <atom:updated>2018-01-17T00:42:21.167Z</atom:updated>
            <content:encoded><![CDATA[<p>Whether for homework or interviews, Data Structures and Algorithms will be <strong>foundational</strong> to your early career. This article is to help you go from nothing to practical knowledge in this crucial area of Computer Science.</p><h3>Quick Links</h3><ul><li><a href="#c632">Linked Lists</a></li><li><a href="#b698">Stacks/Queues</a></li><li><a href="#91fe">Trees</a></li><li><a href="#96e0">Maps</a></li><li><a href="#855c">Graphs</a></li><li><a href="#5ae7">Appendix and Further Resources</a></li></ul><h3>Linked Lists</h3><p><strong>A Linked List</strong> is, as the name suggests, a list of items. Each item points to the next node in the list. Think of a linked list as a scavenger hunt.</p><p>You start with a clue (the head of the list), and each clue leads you to the next clue (the next pointer), until you’re at the end (the tail of the list). Can you think of a list implementation?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/730/1*gVHT0d3SFpvYjiwNj9FcJg.png" /><figcaption>Every Node is the start of its own Linked List!</figcaption></figure><p>Some common reasons to use Linked Lists include:</p><ul><li><strong>Inserting elements in the middle of your data</strong>: You can insert anywhere in your list as long as you know the Node before the insertion index. O(n) in a Vector, O(1) in a Linked List.</li><li><strong>Quicker at expanding space for your data:</strong> Because Linked Lists focus on this idea of using pointers, you don’t have to allocate contiguous blocks of memory for your data. This saves you O(n) time compared to a naive implementation of a Vector.</li></ul><p>One major drawback of Linked Lists is that <strong>you don’t have random access of an element</strong> in the list by its index. For example: If you wanted to retrieve the 5th element in a list, you would have to iterate through the list until you ran into it.</p><h3>Stacks/Queues</h3><p>A Stack and a Queue are pretty much the same data structure, the only difference being how they order their information. We can implement both with our Node struct from earlier!</p><p><strong>A Stack</strong> operates last in, first out (LIFO). Think of a stack of cafeteria trays. The last tray you put on the stack will be the first one you pick up off the stack. A stack exposes three main functions: one to see the top, one to push to the top, and one to pop off the top.</p><p><strong>A Queue</strong> operates first in, first out (FIFO). Think of a queue of people waiting in line. If you were first in line, you would be helped first. Similarly, if you’re last in line, you will be helped last. A queue exposes the same functions as a stack, with the implementation of push being slightly different. (We want to push to the <em>end </em>of a queue.)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2rSzCebGeD2Hk0bdCvEvMw.png" /><figcaption>Basic Stack and Queue implementations based off our Node from earlier</figcaption></figure><p>Stacks and Queues are great for <strong>maintaining order based on insertion time:</strong> Use a Stack if you want to keep track of the most recent item. Use a Queue if you want to keep track of the oldest item.</p><p>Don’t use either if you care about anything other than the most recent or oldest items.</p><h3>Trees</h3><p><strong>A Tree</strong> is a data structure that is very suited to parent-child relationships in data. It can be implemented by a simple Node class as we defined above, with the exception of having multiple children pointers instead of one next pointer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/730/1*0-7UYg7UUY79Zl6NGp300Q.png" /><figcaption>A simple implementation of a tree. Note how every node is the “start” of it’s own subtree!</figcaption></figure><p>Trees require a different type of thinking than Vectors, Linked Lists, or even Stacks/Queues. They introduce a concept of <strong>traversing a tree. </strong>Because there is no set way to go from a parent to a child, you can choose between three main traversal methods:</p><h4>Pre-Order Traversal</h4><p>Use a Pre-Order Traversal if you want to calculate something on the parent before doing the same on the children.</p><p>Given our tree representation from above:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*682N3EB9dBWURYPvHlegBA.png" /></figure><h4>In-Order Traversal</h4><p>Use an In-Order Traversal if you want to calculate something on the left child, then the parent, and finally the right child. This is typically only used for binary trees, trees with only a max of two children per Node.</p><p>Given our tree representation from above:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*jxPrzsO0IpZxRhPB5sf20g.png" /></figure><h4>Post-Order Traversal</h4><p>Use a Post-Order Traversal if you want to calculate something on the children before doing the same on the parent.</p><p>Given our tree representation from above:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*WNe5ibhftL0ltNluyIAaWA.png" /></figure><h3>Maps</h3><p><strong>A Map</strong> is a way to represent data in terms of a key and a value. A proper implementation offers very quick insertion, lookup, and removal times.</p><p>The base of a good map implementation is it’s hash function. The purpose of this hash is to:</p><ul><li>Provide a hashed, or encoded, output given any valid input</li><li>Always return the same output given the same input</li><li>Minimize collisions, when two inputs returns the same output</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/856/1*4y-A-qQZyD6EzcmIaOrT2Q.png" /></figure><p>Maps are incredibly powerful because of how quickly you can manipulate them. (O(1) for insertion, deletion, and lookup!) This speed lends itself very well for most interview questions, to the point of making them trivial.</p><p>Get to know Maps very well.</p><h3>Graphs</h3><p><strong>A Graph</strong> is a flexible data structure that can be used to represent data with complex relationships between Nodes. It is most similar to a Linked List, but every Node can have n number of pointers to other Nodes. A Graph is also not guaranteed to be free of cycles. (A cycle is when you can start and end at the same Node while traversing the graph.)</p><p>A Graph implementation is actually quite simple:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/730/1*7q-5bKXDNySNL2JpWvGPug.png" /><figcaption>Notice how you can get to a OR c from b, but not necessarily the other way around!</figcaption></figure><p>We can model a lot of situations using graphs, such as: friendships between users, airplane routes, etc. Graphs can make any problem with complex relationships quite simple, and going from one Node to another is as simple as traversing through its neighbors. (Examples are mentioned in the Appendix: Path-finding and Dijkstra’s.)</p><h3>Appendix</h3><p>These were some of the basic data structures that can be found in course curricula and interview questions. If you want to expand your knowledge further (and go from 60 to 120!), here are some advanced Data Structures and Algorithms:</p><ul><li><a href="https://blog.ostermiller.org/find-loop-singly-linked-list">Cycles in Linked Lists</a></li><li><a href="http://faculty.cs.niu.edu/~hutchins/csci241/eval.htm">Using a Stack to evaluate mathematical expressions</a></li><li><a href="https://www.topcoder.com/community/data-science/data-science-tutorials/using-tries/">Tries: a fancy tree that allows efficient path lookup</a></li><li><a href="https://en.wikipedia.org/wiki/Set_(abstract_data_type)">Sets: when you only want to remember if you’ve seen an element before </a>(great replacement for map&lt;[anything], bool&gt;)</li><li>Path-finding algorithms (<a href="http://blog.hackerearth.com/kruskal-algorithm-minimum-spanning-tree-example">Kruskal’s</a> and <a href="https://www.tutorialspoint.com/data_structures_algorithms/prims_spanning_tree_algorithm.htm">Prim’s</a>), for when you want to find a path between two nodes in a graph</li><li><a href="https://medium.com/basecs/finding-the-shortest-path-with-a-little-help-from-dijkstra-613149fbdc8e">Dijkstra’s Algorithm: find the path of least resistance between two nodes</a></li></ul><h3>Further Resources</h3><p>If you prefer books over blog posts, I highly recommend (in order of readability):</p><ul><li><a href="http://amzn.to/2D0owJL">Algorithms to Live By: The Computer Science of Human Decisions</a>: A great read on how you can apply algorithms in your daily life. Not a very academic read.</li><li><a href="http://amzn.to/2FvQ5fB">Grokking Algorithms</a>: An incredible book for not only its simple explanations and relation to real world examples, but also PICTURES!</li><li><a href="http://amzn.to/2CYIPHh">Introduction to Algorithms, 3rd Edition</a>: A great reference book, used for Intermediate Data Structures and Algorithms at UCR.</li><li><a href="http://amzn.to/2Ds0NDo">Algorithms, 4th Edition</a>: Written by the legendary Robert Sedgewick, super informative. (Sedgewick has been teaching algorithms since the peak of Fortran!)</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8413dc3b33b1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/acm-ucr/data-structures-and-algorithms-0-to-60-8413dc3b33b1">Data Structures and Algorithms: 0 to 60</a> was originally published in <a href="https://medium.com/acm-ucr">ACM@UCR</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Crash Course for Alternative Cryptocurrencies]]></title>
            <link>https://medium.com/acm-ucr/a-crash-course-for-alternative-cryptocurrencies-6c9ca12a3b53?source=rss----abe431afdbc5---4</link>
            <guid isPermaLink="false">https://medium.com/p/6c9ca12a3b53</guid>
            <category><![CDATA[bitcoin]]></category>
            <category><![CDATA[coinbase]]></category>
            <category><![CDATA[altcoins]]></category>
            <category><![CDATA[ethereum]]></category>
            <dc:creator><![CDATA[John Pham]]></dc:creator>
            <pubDate>Wed, 03 Jan 2018 03:20:45 GMT</pubDate>
            <atom:updated>2018-01-03T03:28:30.889Z</atom:updated>
            <content:encoded><![CDATA[<p>By now you’ve probably heard of Bitcoin, Ethereum, and Litecoin. Those 3 coins are not the only ones out there. Outside of those 3 coins, we can call everything else Bitcoin Alternatives (alts). This guide will get you up to speed on why you should care about alts and how you can start using them.</p><p>Skip to the bottom if you’d like a tl;dr.</p><h3>What are alts?</h3><p>The underlying technology for all cryptocurrencies is the blockchain. If you understand linked-lists and hash tables, you’re already 90% finished with learning how blockchains work.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FbBC-nXj3Ng4%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DbBC-nXj3Ng4&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FbBC-nXj3Ng4%2Fhqdefault.jpg&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ef6abd10257b9f27834e3b1ca1dcef0/href">https://medium.com/media/4ef6abd10257b9f27834e3b1ca1dcef0/href</a></iframe><p>Alts are called Bitcoin Alternatives because the new coin improves or adds features to the original Bitcoin technology. This can be in the form of privacy, speed, or scalability. The original functionality of Bitcoin can be found in its <a href="https://bitcoin.org/bitcoin.pdf">whitepaper</a>.</p><h3>Why should I care?</h3><p>Cryptocurrencies are a great place to park your investing money. Due to their volatile nature, it could be a good or bad investment depending when you enter. Take a look at how Bitcoin has grown over the years.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6XMiAkBPqWDuiqY3DVMV3A.png" /><figcaption>Bitcoin’s Growth</figcaption></figure><p>Alts share similar growth and depression patterns as Bitcoin. Take a look at Monero (XMR).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kvCDYYlInXgOZYkX7_r2Zg.jpeg" /><figcaption><a href="https://coinmarketcap.com/currencies/monero/">Monero’s Growth</a></figcaption></figure><p>So if they share similar patterns why should you put money in alts? The growth deltas will never be the same. At one point BTC may increase 2% compared to an alt that increased 80%.</p><p>The difference in deltas can be attributed to real world events or tech changes (coin burns).</p><h3>How do I get started?</h3><p>So how do you convert your fiat to crypto? Unlike BTC, ETH, and LTC, you will find very few options for doing a direct exchange between fiat and your coin. Most exchanges that have alts allow you to trade BTC or ETH to an alt. Because of this relationship, you should be wary that if BTC’s or ETH’s value goes down, the alts may also go down.</p><p><a href="https://www.coinbase.com/join/52a6a8fc348c70cd0e000094">Coinbase</a> is a popular currency exchange. They allow you to link your bank account and exchange directly for BTC/ETH/LTC. <strong>Don’t forget to enable 2FA.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l7QLIxH6jVb0NaC6oxWiIA.png" /><figcaption>Coinbase’s Exchange Interface</figcaption></figure><p>Once you’ve exchanged your fiat for BTC, you can send your BTC from the Coinbase wallet to another exchange’s wallet. The exchange I would recommend for trading with alts is <a href="https://www.binance.com/?ref=16696825">Binance</a>. Binance has over 100 alts with more being added everyday.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bZ5C8Ndhk5LQGR3rK96wfA.png" /><figcaption>Binance’s Exchange View</figcaption></figure><p>The first column tells you that you can buy x for y coin (x/y). We are able to exchange BTC for any of the alts.</p><p>The second columns tells you what’s the last traded price for a single alt coin. Don’t be wary of whether it’s green, red, or black since this is on a per-trade basis and not the aggregate.</p><p>The third column tells you the delta for the past 24 hours. Don’t be scared if you see all red, it could just by a correction.</p><p>The fourth and fifth tells you the high and low price for a single alt within the past 24 hours.</p><p>The last column tells you how much of the alt has been traded in the past 24 hours.</p><h3>Research before you buy</h3><p>As with any investment, you need to know the reasons on why you think a investment will go up. A great place to do research is on <a href="https://coinmarketcap.com/">coinmarketcap</a> which has trading hitory, news, and other functions to help you make decisions.</p><p>When you have a sound argument on why you think an alt will go up, then it’s time to buy. On Binance, you’ll simply exchange BTC for the alt.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0ClxErwo8VHdBjAHjvl-Nw.png" /><figcaption>Buying 500 BCC at 0.169599 BTC each with 84.7995 BTC</figcaption></figure><h3>tl;dr version</h3><ol><li>Exchange fiat to BTC on <a href="https://www.coinbase.com/join/52a6a8fc348c70cd0e000094">Coinbase</a></li><li>Send BTC from Coinbase to <a href="https://www.binance.com/?ref=16696825">Binance</a></li><li>Do alt coin research on <a href="https://coinmarketcap.com/">coinmarketcap</a></li><li>Buy the alt coin with BTC</li><li>Sell the alt coin when you’ve made your profit</li></ol><h3>Glossary</h3><p><strong>Alt Coin</strong>: Bitcoin alternative, a new cryptocurrency based off of Bitcoin that has added new features</p><p><strong>Exchange/Market:</strong> A central hub to exchange fiat to crypto, crypto to crypto, or crypto to fiat.</p><p><strong>Wallet: </strong>Just like your physical wallet, it’s a place to store your crypto. Unlike your physical wallet that can store a variety of coins, each wallet can only store a unique coin. You’ll have a Bitcoin wallet, an Ethereum Wallet, etc.</p><p><strong>Dump: </strong>When there is a huge influx of coins being sold. This usually causes the value to go down.</p><p><strong>Pump:</strong> When there is a huge influx of coins being bought. This usually causes the value to go up.</p><p><strong>Hold/Hodl: </strong>You continue to hold onto a coin despite a coin decreasing in value because you believe it will go back up.</p><p><strong>Stop-Limit Order: </strong>These types of orders allows an event (buy/sell) to take place once a certain price has been met.</p><p>Given this stop-limit sell order: stop is set to 50, the limit is set to 55, and amount set to 2000.</p><p>When the value of the coin reaches to 50 or above, it will sell 2000 coins at 55 each.</p><p>A general rule when investing in alts is to always set a stop-limit sell order at -10% of the price you bought the coin. This can prevent you from losing large amounts while staying in a tolerable decrease range.</p><p><em>Special thanks to Aaroh Mankad for helping me proof my first article!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6c9ca12a3b53" width="1" height="1" alt=""><hr><p><a href="https://medium.com/acm-ucr/a-crash-course-for-alternative-cryptocurrencies-6c9ca12a3b53">A Crash Course for Alternative Cryptocurrencies</a> was originally published in <a href="https://medium.com/acm-ucr">ACM@UCR</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cleaning Up Your Code]]></title>
            <link>https://medium.com/acm-ucr/cleaning-up-your-code-8c2a88cb47fc?source=rss----abe431afdbc5---4</link>
            <guid isPermaLink="false">https://medium.com/p/8c2a88cb47fc</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[best-practices]]></category>
            <category><![CDATA[clean-code]]></category>
            <dc:creator><![CDATA[Aaroh Mankad]]></dc:creator>
            <pubDate>Sun, 10 Dec 2017 02:01:09 GMT</pubDate>
            <atom:updated>2017-12-10T18:58:57.962Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’ve been programming for any period of time, you’ve probably seen bad code. The kind that you’re scared of touching for fear of breaking the program, the kind that employs some trickery that is beyond any documentation found online, the kind that <em>slows you down</em>.</p><blockquote>So what makes clean code, and how can we tackle the disease of bad code?</blockquote><p>Luckily, you can follow the same principles to write good code and to refactor bad code. Here are some attributes I’ve noticed in good code:</p><ul><li><a href="#3663">Good Names</a></li><li><a href="#8f37">Abstracting Reused Code</a></li><li><a href="#f777">Helpful Comments</a></li><li><a href="#26bd">Easy-to-read Formatting</a></li><li><a href="#a479">Using Intuitive Data Structures</a></li><li><a href="#7c8e">Handling Errors Well</a></li><li><a href="#1df0">Testing Your Code</a></li></ul><h3>Good Names</h3><p>Programming is not a math equation. The vast majority of the programs you write will not be simple. They will have to interact with the rest of the program, forming complex relationships along the way, and therefore have to be read by other programmers. Someone with no idea of your contribution should be able to quickly understand what your code does.</p><p>Let’s look at an example, which code snippet is easier to grasp?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/31a938bc95735a24a4d43b17d2bbe2ee/href">https://medium.com/media/31a938bc95735a24a4d43b17d2bbe2ee/href</a></iframe><p>If you’d like to start using good names throughout your codebase, here are some general rules to follow:</p><ul><li><strong>Expose meaning through names</strong>: Similar to the above example, the name of a variable should provide some meaning towards code functionality.</li><li><strong>Use pronounceable names</strong>: Software Development involves talking through your code with other developers. It will help a lot if you can actually talk through your code.</li><li><strong>Use searchable names</strong>: There will be many times you want to track a variable or function through your codebase. In this case, the variable should be unique enough that it can be searched for with few duplicates.</li></ul><h3>Abstracting Reused Code</h3><p>If you’ve found pieces of code in your codebase that are heavily duplicated or rely on the same logic, they are probably great contenders for abstraction.</p><p>Abstracting code into a separate function or class can help reduce code complexity as well. If your algorithm related to that block of code changes, you only have to change it in one place versus changing it in multiple places across your codebase.</p><p>What does a good abstraction look like? Well let’s take our example from earlier. The good_names.cpp file is really easy to read! However, we would have to rewrite that code block if we ever wanted to find a cell based on a different FLAG_CODE. So let’s move that code into an isolated function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/37198bbf489cef492a37e6461f23b5d1/href">https://medium.com/media/37198bbf489cef492a37e6461f23b5d1/href</a></iframe><p>Before you decide to start splitting your code apart, here are some things to keep in mind:</p><ul><li><strong>Keep your functions small</strong>: A small function is easy to read, but also very easily reused. Your function should be able to be used for a wide variety of use cases, not just the one case it used to be in.</li><li><strong>Limit the functionality</strong>: If your code does more than one exact thing, it will be difficult to use it across your codebase. Continue splitting your code until your functions have only one purpose each.Then you can implement a function to call these smaller functions in any way you wish!</li><li><strong>Ensure no side effects</strong>: A function should be thought of as a walled off piece of code that you control. It should <em>purely </em>accept input and return output. A <em>pure</em> function is a function that does not affect any code it does not have direct access to. A simple example would be an impure add function that adds two variables outside of it’s scope. This is very hard to keep track of and should be avoided in most situations.</li></ul><h3>Helpful Comments</h3><p>There will come a time when you have to write some code that is <em>so</em> convoluted that it just can’t be fixed with good naming or any number of levels of abstraction. Or you just have some code that requires some “inside” knowledge to understand.</p><p>In this case you have two options: you can either write that bit of code and continue on your merry way, or you can leave a comment for yourself and other programmers. Leaving a comment requires very little effort on your part because you’ve already written the code!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eec22b5086c84c1735f275723ba859d7/href">https://medium.com/media/eec22b5086c84c1735f275723ba859d7/href</a></iframe><p>Some questions to consider when writing your comments:</p><ul><li><strong>What does this code do?</strong></li><li><strong>Where in the codebase is this code called?</strong></li><li><strong>Why is this the best way to accomplish the problem?</strong></li></ul><h3>Easy-to-read Formatting</h3><p>An age-old debate: tabs or spaces? Braces on the same line or new line?</p><p>I’m not going to suggest any specific formatting, but I do want to expose you to some guidelines on deciding formatting for your project. First and foremost, once you’ve found a method of formatting to use, <strong>stick to it.</strong></p><p>There’s two steps to getting most of the way to a perfect style guide for your project.</p><ol><li><strong>Pick a popular style guide for your language.</strong> Examples are included below.</li><li><strong>Make slight variations to it for your specific use cases.</strong> If a certain point in your chosen style guide doesn’t make sense for your use case, document it. (Ideally in a CONTRIBUTING.md file!)</li></ol><ul><li><a href="https://github.com/airbnb/javascript">Airbnb Style Guide for Javascript</a></li><li><a href="https://google.github.io/styleguide/cppguide.html">Google Style Guide for C++</a></li><li><a href="https://www.python.org/dev/peps/pep-0008/">PEP8 Style Guide for Python</a></li></ul><h3>Using Intuitive Data Structures</h3><p>Programming is hard enough as it is, don’t make it harder for yourself by choosing poor data structures.</p><p>Here are some examples I’ve seen in real code. I’m defining a “good” data structure as one that most easily maps to the real-world representation of the data and can be picked up quickly by a colleague.</p><p>Can you think of a better data structure than the one initially presented? (Try to find one before you read my explanation!)</p><h4>Using a list as a way to store a Bingo board</h4><p>This is the “worst” of all the scenarios I’ll cover. While a list may seem easier to traverse with a simple for-loop, it doesn’t represent the real-world board very well. (If you have a sharp eye, you may have noticed my previous examples with gameBoard used this data structure.)</p><p>How would you go “down 2 spaces” if the board were stored as a list? Instead of increasing the row index by two, you would increase your index by 2 * NUM_COLUMNS. You can’t do that unless you know how big the board is, which would mean passing your board dimensions to every function that accesses it.</p><p>A Bingo board should be stored in a more intuitive two-dimensional vector (a matrix).</p><h4>Using a string to store an arithmetic equation</h4><p>A string was likely used because the input from the user was in the form of a string. However, this approach lends itself very easily to out-of-bounds errors or incorrect results.</p><p>Evaluating the equation would involve keeping an answer variable that is either NULL or the result of the last operation. Then you iterate through the string, and if you find an operator either execute the operator on the previous and next values or the answer variable and next value if the answer is not NULL. You would also have to check to see if you’ve ever written to the answer variable because NULL = 0 and having an equation such as 2 — 2 would return the answer variable to the NULL state. The string method also can’t be extended to support precedence through parenthesis.</p><p>A better method would be to store your equation as an <a href="https://en.wikipedia.org/wiki/Binary_expression_tree">Binary Expression Tree</a>. Evaluating the function would simply be an in-order traversal of this tree. While building the initial tree would require a bit of work, it will make the rest of the program trivial.</p><h4>Using an array to store pictures posted by a user</h4><p>This is the most subtle error out of the three scenarios. If I wanted to find all the photos a user has posted, why shouldn’t it be stored in a list? It seems the most user-friendly method.</p><p>Well to see the error in this situation, we have to account for the situation we want to <em>find</em> a single photo. This now becomes an O(n) operation, testing every photo against some unique id.</p><p>Instead, we may want to store the photos in a map, with the unique id as the key and the photo data as the value. This is okay because the id should not change throughout the lifecycle of the photo. Now when we want to find a specific photo, we’ve reduced our complexity to O(1), while not affecting the use case for listing out all of the users photos.</p><h3>Handling Errors Well</h3><p>A great program is nothing if it can’t handle edge cases or non-optimistic cases. Always make sure to test your code with some bad cases, such as:</p><ul><li>NULL input</li><li>Vectors, Lists, or Maps that are empty</li><li>Negative input when you expect it to be positive</li></ul><p>Let’s make our findCells function from earlier more robust by handling these edge cases. (Note that the check I added isn’t necessary, and is used only to prove a point.)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4292ae060b242d0997d1ef4b4a557493/href">https://medium.com/media/4292ae060b242d0997d1ef4b4a557493/href</a></iframe><p>There are also some changes we could make given some assumptions.</p><ul><li><strong>What if </strong><strong>FLAG_CODE should always be positive? </strong>You can either make the FLAG_CODE argument of type unsigned or you can add a check for that.</li><li><strong>What if </strong><strong>FLAG_CODE is out of the range of the expected </strong><strong>FLAG_CODE range? </strong>You can store an enum for the FLAG_CODE and then let C++ handle the type checking for you!</li></ul><p>The main point of this section is to make sure your code either doesn’t break (not likely), or breaks in a predictable way. How can we test for that? Read the next section!</p><h3>Testing Your Code</h3><p>Before I start explaining the specifics of testing in certain languages, I want to start by explaining the concept of <em>Test Driven Development</em>. You may have heard the term before but thought that your project wasn’t big enough to adopt this method of developing.</p><p>Test Driven Development (TDD) is a method of developing in which you write tests before you write <em>any</em> code. Why would you want to do this?</p><p>TDD actually enforces writing tests for your code! Many times you’ll write a program, run some examples and call it done. However, you’ve likely missed several bugs in your code by testing only the optimal cases. If you did manage to find some bugs and fix them, it is more work to make sure your fix did not break any previous cases. For this reason, tests serve as a method of documentation for all the cases you’ve checked so far and as a way to force you to think of ways to break your code.</p><p>So what makes a good test? Follow the F.I.R.S.T method!</p><ul><li><strong>Fast: </strong>If a test is slow, your approach to the task is probably not optimal. If the test is slow for a sample size of one, imagine how that would scale to a system of a million users!</li><li><strong>Independent: </strong>A test should not depend on the outcome of another test. In the case that tests were <em>dependent</em>, then your tests would fall in a domino effect, hiding bugs in a different test until you fix the first bug. (Not ideal for when you’re working in teams asynchronously.)</li><li><strong>Repeatable:</strong> A test should not leave anything to chance. Every time you run a test, assuming no changes to the code have been made, you should expect an identical outcome. If the test cannot be repeated, you will eventually fall into a “It works on my machine!” attitude which is not beneficial to your team or your users.</li><li><strong>Self-Validating:</strong> A test should return a boolean, true or false. This allows us to automate the job of testing our code, checking if all the tests return true or not. If you have to check some printed output to see if it matches the expected output, you could instead do a diff on the expected string and the returned string. (This changes our test from a string return type to a bool return type!)</li><li><strong>Timely</strong>: This is the self-fulfilling prophecy in F.I.R.S.T. If you write your tests in a timely manner, your testing will go smoothly. Do not put off writing tests in favor of extending the functionality of your program.</li></ul><p>Some housekeeping notes, but keep your tests separate from your actual program logic, ideally in a /tests directory.</p><p>Testing can be very domain-specific in its scope. My experience is primarily in Javascript and C++, which I feel represent two very different sides of programming. (Hopefully your language of choice falls somewhere on that spectrum!)</p><h4>Javascript</h4><p>Before you start thinking that tests have to be this fancy concept of your code that can only take a significant portion of your development time, remember that a test can be as simple as checking the output of a function to the expected output!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c072963033549c5f37207cbc0dc3f292/href">https://medium.com/media/c072963033549c5f37207cbc0dc3f292/href</a></iframe><p>There are more robust ways to write tests in Javascript, mostly due to the vibrant developer community. Most commonly used is the <em>test runner</em>. A test runner will automatically run all your tests and present them in a very clean format. It is also much more extensible in the use case that you have databases of objects and want to mock some test data for your tests.</p><p>The test runner that I’m going to recommend is <strong>Jest</strong>. Jest is used by Facebook to test all their JavaScript code including their React applications. To me, Jest has a lot of reputation from being backed by Facebook and having a team dedicated to its active development.</p><p>To learn how to start using Jest, I highly recommend Fun Fun Function:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fpdx2HjFRaJY%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dpdx2HjFRaJY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fpdx2HjFRaJY%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/3e1c4396ae02c55618885249077d3930/href">https://medium.com/media/3e1c4396ae02c55618885249077d3930/href</a></iframe><h4>C++</h4><p>As in Javascript, sometimes the easiest way to test your code in C++ is with some assert statements.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/31363205804b742d49ea0486f42bed3a/href">https://medium.com/media/31363205804b742d49ea0486f42bed3a/href</a></iframe><p>When you want to get into some more complex testing, such as checking for memory leaks, you may want to start using <em>static analyzers</em>. First, use <a href="http://cppcheck.sourceforge.net/">cppcheck</a> to see if you even have a memory leak in the first place. cppcheck can also offer advice for better performance or cleaner code style.</p><p>If you do end up having a memory leak or segmentation fault, one option is to compile your program with the -g flag for debugging support. Then you can run your program with <a href="https://www.gnu.org/software/gdb/">GDB</a> (GNU DeBugger). GDB allows you to set breakpoints in your code, step through functions, inspect values at each step, and locate segmentation faults.</p><p>Fun fact: You can also use GDB as a disassembler for executables!</p><h3>Further Resources</h3><p>If you want to take your code to the next level, I highly recommend:</p><ul><li><a href="https://www.amazon.com/gp/product/0132350882/ref=as_li_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0132350882&amp;linkCode=as2&amp;tag=aarohmankad-20&amp;linkId=6660374c4ee5a6a10573a4bd43ef09ba">Clean Code: A Handbook of Agile Software Craftsmanship</a>: Clean Code was my inspiration behind this article, and goes into much more depth regarding all the sections I wrote about!</li><li><a href="http://amzn.to/2AoDlZ9">Clean Architecture: A Craftsman’s Guide to Software Structure and Design</a>: Also by Robert Martin of Clean Code, this book specifically cover clean code in a large architecture. If you’re dealing with large projects, this would be a great book to look into.</li><li><a href="http://amzn.to/2kKJJDc">The Pragmatic Programmer: From Journeyman to Master</a>: Pragmatic Programmer is example-driven, with stories to back up every rule that Andrew Hunt recommends. A great insight into best practices in the industry.</li></ul><p><em>Special thanks to Daniel Stinson-Diess for helping me proofread the article and helping me with the C++ Testing portion!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c2a88cb47fc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/acm-ucr/cleaning-up-your-code-8c2a88cb47fc">Cleaning Up Your Code</a> was originally published in <a href="https://medium.com/acm-ucr">ACM@UCR</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>