<?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 John Riccardi on Medium]]></title>
        <description><![CDATA[Stories by John Riccardi on Medium]]></description>
        <link>https://medium.com/@riccjohn?source=rss-54ff4d7c439b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Jy56toS8W7IowHk9s8MwHg.jpeg</url>
            <title>Stories by John Riccardi on Medium</title>
            <link>https://medium.com/@riccjohn?source=rss-54ff4d7c439b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 20:51:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@riccjohn/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[Inheritance vs Composition in Swift]]></title>
            <link>https://medium.com/@riccjohn/inheritance-vs-composition-in-swift-9b35d7b15e44?source=rss-54ff4d7c439b------2</link>
            <guid isPermaLink="false">https://medium.com/p/9b35d7b15e44</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[inheritance]]></category>
            <category><![CDATA[composition]]></category>
            <category><![CDATA[development]]></category>
            <dc:creator><![CDATA[John Riccardi]]></dc:creator>
            <pubDate>Wed, 20 Nov 2019 17:48:57 GMT</pubDate>
            <atom:updated>2019-12-03T15:13:29.898Z</atom:updated>
            <content:encoded><![CDATA[<h3>Inheritance vs Composition</h3><h4>in iOS and Swift</h4><figure><img alt="A pile of different colored lego blocks" src="https://cdn-images-1.medium.com/max/1024/1*y4vyJqSID96wL_IRj1ad1Q.jpeg" /></figure><h3>What does inheritance vs composition even mean?</h3><h4>Inheritance</h4><p>Let’s say you’re asked to create an application where you’ll need some dogs and some robots. Dogs need to be able to bark and eat, and robots will need to be able to clean. Simple enough! Let’s create some classes to handle this functionality for us.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/567b68315374bac1f6477a0534ec2418/href">https://medium.com/media/567b68315374bac1f6477a0534ec2418/href</a></iframe><p>The next day, you’re asked to add cats to the game. Cats should be able to eat, but they’ll meow instead of bark. To achieve this, we could change the Dog class to inherit from a more generic Animal class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9b2b1d5ef611414be821aec6ef1c0b2/href">https://medium.com/media/f9b2b1d5ef611414be821aec6ef1c0b2/href</a></iframe><p>The next day, you pick up a story that’s asking you to add a RobotDog to the game. RobotDogs will clean and bark, but since they’re robots, they don’t need to eat. There’s no easy way to do this with the current method of inheriting from more generic classes.</p><figure><img alt="Flow chart representation of the inheritance hierarchy" src="https://cdn-images-1.medium.com/max/981/1*bvbm5n1GzHt59ps9_llMkw.png" /></figure><h4>Composition</h4><p>Instead of using inheritance, we could have chosen to use composition and built smaller blocks of code that could then be assembled together for each use-case. What we’re trying to do here is create some simple classes or types that other classes make use of rather than inheriting everything from a large, hierarchical class structure.</p><p>In Swift, we’d first create some small classes that define the different things our characters should be able to do.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b018f89e012c2f2c091666924b92725b/href">https://medium.com/media/b018f89e012c2f2c091666924b92725b/href</a></iframe><p>Now that we have that set up, we can <em>compose</em> some of those simple class methods together to create our main character classes. Each character class will create an instance of the class that it needs, and then utilize the necessary methods on that class instance.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc4b5e1785ca5e07bc5ba266691c77b7/href">https://medium.com/media/bc4b5e1785ca5e07bc5ba266691c77b7/href</a></iframe><p>By creating classes that define the actions that our objects should have, we’ve simplified the overall design and made it more extensible. Instead of predicting what features each class will need, we’re making small classes that we can then use to build new classes as if we were using lego blocks. This also allows us to control what variables and methods we want to expose with our new classes’ API. For example, we’re only exposing the Dog classes’ bark method, but not how the whole BarkingSkill.bark method. In this simple case, that might not mean much, but in a more complex class, the BarkingSkill.bark method may have implementation details that we don’t actually need to care about if we’re just calling a Dog’s .bark method. All we care about is that when we create a new dog and call dog.bark() , it barks!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/1*EtfkrFR0Sw3ZUlB_pU7y9Q.png" /></figure><h3>Trade-offs &amp; Real World Examples</h3><p>You’ll often hear people saying that one way to tell whether you should use composition or inheritance is whether something <strong><em>is a</em></strong> thing or <strong><em>has a</em></strong> thing. For example, a dog <strong><em>is a(n)</em></strong> animal, whereas a car <strong><em>has a(n)</em></strong> engine. Things are rarely this cut and dry, and a lot of examples could be interpreted in both ways. Sometimes, you’ll actually want to use both inheritance and composition to build a relationship between classes or types. Inheritance usually requires you to predict what you’ll need in your application, since you’re making assumptions on how general you can make the class at the top of your hierarchy. These kind of predictions are usually wrong, and once you see an exception to the rules you’ve just made, that should be the first sign that the design needs to change.</p><p>Because of it’s more linear, hierarchical nature, inheritance can sometimes lead to simpler code if you really do just need to create classes that add functionality to parent classes. However, it requires you to commit to a certain structure early on and try to solve a more general problem before solving the smaller components of a problem first. Composition allows you to tackle the small parts of a problem first (ex. create the functionality that would allow something to bark before having to think about the entire structure of a Dog class). It also leaves your code more flexible to change, and doesn’t require you to plan too far ahead or abstract out your functionality too early. This also means you’ll be doing a bit more manual set up and maybe have some ‘boilerplate-y code’ that you wouldn’t need with inheritance, but that’s because you’re only getting what you need and not grabbing everything from another class.</p><h4>Real World Trade-offs</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1877c4acf719b87f4c7e42caf7947fd7/href">https://medium.com/media/1877c4acf719b87f4c7e42caf7947fd7/href</a></iframe><p>Above is some code for an iOS app that deals with drawing a table inside a View. In this View Controller class, we’re inheriting from UIKit’s UIViewController. We could have inherited from UITableViewController and had the data source and delegate come with it, but we’d be getting a bunch of other methods and instance variables that we don’t need right now. Instead, we’re inheriting from a the simpler UIViewController and then using using composition to add the data source and handle viewWillAppear ourselves. This allows us to be more flexible with the class and doesn’t restrict us to only showing a table on the screen like a UITableViewController would.</p><p>Here’s what it could look like if we had inherited from UITableViewController instead:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f892778223528721ba5a2db8ee0edc73/href">https://medium.com/media/f892778223528721ba5a2db8ee0edc73/href</a></iframe><p>Previously, we were using a separate class to act as the dataSource for us and to handle figuring out what should be in each cell. Now that we’re inheriting everything from UITableViewController directly, this class needs to handle everything itself, increasing the complexity of the class and adding even more responsibilities. Ideally, this class should only have a <a href="https://en.wikipedia.org/wiki/Single_responsibility_principle">single responsibility</a> — setting the data source of the table view. Now, it IS the data source and has to deal with managing the data, drawing each cell, and displaying the table.</p><h4>Trade-offs in Testing</h4><p>With inheritance, since you’re getting all the class methods and instance variables from a parent class, everything in the parent class will be contained within your class already. This means you’ll likely have a single test file for this entire module.</p><p>With composition, you’re pulling small pieces from other classes and may even reorganize them into multiple modules. For example, in the UIViewController, we add the data source with via composition, but then use a separate module to deal with how the data source is actually implemented. Doing this allows us to write smaller tests that are likely easier to write too. However, the trade-off here is that we then need to test the connections between these pieces now that there are more seams than we’d have with inheritance.</p><p>Neither inheritance or composition is a <em>better</em> method than the other, but composition is usually a safer bet since it’s more likely to be <em>appropriate</em> in most cases. However, if you find that your class <em>is</em> a proper subtype of another class, your class mainly adds functionality to another class, and/or one class’ implementation is a requirement for another class, inheritance may be the way to go. These are both just tools that you’ll be using in your code to solve problems in different ways just like anything else. Thinking through the ‘why’ for your reasoning to use one over the other is the most important part.</p><p>References:</p><ul><li><a href="https://medium.com/humans-create-software/composition-over-inheritance-cb6f88070205">Composition over Inheritance</a></li><li><a href="https://www.thoughtworks.com/insights/blog/composition-vs-inheritance-how-choose">Composition vs. Inheritance: How to Choose?</a></li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FdYUZiJEy0JE%3Ffeature%3Doembed&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DdYUZiJEy0JE&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FdYUZiJEy0JE%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/92606723d4728c0949ce5f884cd2efd4/href">https://medium.com/media/92606723d4728c0949ce5f884cd2efd4/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9b35d7b15e44" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding and building your own tiny blockchain in JavaScript]]></title>
            <link>https://medium.com/coinmonks/understanding-and-building-your-own-tiny-blockchain-in-javascript-a16f2137cfec?source=rss-54ff4d7c439b------2</link>
            <guid isPermaLink="false">https://medium.com/p/a16f2137cfec</guid>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[node]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[tutorial]]></category>
            <dc:creator><![CDATA[John Riccardi]]></dc:creator>
            <pubDate>Sat, 22 Sep 2018 23:19:08 GMT</pubDate>
            <atom:updated>2022-06-25T07:29:23.803Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eEvuhJkBYY1_ocvONk5Z8g.jpeg" /></figure><p>The ideas and technology behind blockchains has always fascinated me. Beyond the social, financial, and technological disruptions that blockchain technology has the power to create, the simple idea that a piece of code could be behind all this has always amazed me.</p><p>Despite blockchain tech usually having whitepapers freely available and more forums, discord and IRC channels, and developer groups than we know what to do with, the basic structure of a blockchain can be pretty opaque. Let’s take a look at how we’d build a <em>very</em> simple block blockchain to help visualize how transactions are added.</p><p><strong>Requirements:</strong> I built this in <a href="https://nodejs.org/en/">Node.js</a>, but you can easily build it on <a href="https://repl.it/">Repl.it</a> if you prefer. We’ll also be making use of the sha256 npm package to hash the blocks.</p><p>If you’re new to Node.js, first what you’ll need to do is create a new directory, create an index.js file, and then from your terminal, run npm init -y from within your new directory. This creates a package.json file, which will allow us to install the sha256 package. Run npm i sha256 to install the sha256 library. If you’re on Repl.it, create a new Nodejs repl, install thejs-sha256 package, and require it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3b568e267498c75efd63917fa5029ba/href">https://medium.com/media/f3b568e267498c75efd63917fa5029ba/href</a></iframe><p>Now that we’re set up, it’s time to create a new class so we have a template that we’ll use to build each block. Each block on our simple blockchain will contain 5 pieces of data: the block’s index, the precise time it was created, some arbitrary data (for bitcoin, this data would be the addresses of the sender and receiver of bitcoin and the amount of the transaction), the hash value of the block preceding it, and a hash of itself. A hash is a digital fingerprint of a piece of data. We’ll see later how this part of each block makes it extremely difficult for anyone to alter the blockchain once a block has been confirmed.</p><p>To create our Block, I’m using a JavaScript class, but a constructor function would work just as well since that’s really all a class is under the hood. I set the constructor function to take all of our data except for the hash as parameters. The block will then look at the index, timestamp, data, and previous hash, and generate its own hash or fingerprint.</p><p>You may have noticed that since each block requires the hash of the previous block, we have a problem of how to create the first block. The first block, known as the Genesis Block, will get handed zeros as the previous hash, so we’ll have to write a function to create that single block.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/48b57640956fb421d081bb1250540e44/href">https://medium.com/media/48b57640956fb421d081bb1250540e44/href</a></iframe><p>On line 15, I use an arrow function to create a new block based on the class we wrote earlier. I hand it a 0 for the index, the current time, a string stating that it’s the Genesis Block, and a previous hash of 0. We now have our first block, and we’re almost done! The genesis block is special, but every other block will use the same formula going forward.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d8f4cb4faa5940b935b06d0b3971bbc/href">https://medium.com/media/9d8f4cb4faa5940b935b06d0b3971bbc/href</a></iframe><p>Here, I write a nextBlock function that takes only the last block, and the new block’s data as parameters. The function then creates a new block and sets the index of the new block to whatever the previous block’s index was and adds 1, grabs the current time, and then grabs the previous block’s hash and sets it as prevHash on the new block. This last step, while only a tiny bit of code, is extremely important. This links each block to it’s previous block through a hash. Let’s say someone wanted to alter the bitcoin blockchain so that it looked like they had received more bitcoin than they actually did, or to make it look like they spent less of their bitcoin. If they altered a block, the hash, which serves as a snapshot of the data the block contained when it was created, would no longer match. To remedy this, they’d have to rehash the block. Since all the blocks are linked together and point back to the previous block’s hash, this person would also have to alter all the blocks on the chain following their target block in order to make it look legitimate.</p><p>While modern computers could conceivably alter a block, generate new hash, and then generate new hashes for all the following blocks so that the altered block was seen as legitimate, bitcoin in particular implements a method called ‘Proof of Work’ to slow down the confirmation of new blocks, making it nearly impossible. Also, due to the distributed network of ledgers that a blockchain relies on, a nefarious actor would have to control more than 50% of the network in order for their counterfeit blockchain to be accepted. Our blockchain won’t be distributed or utilize proof of work, but it’s an extremely clever solution to the problem and those topics warrant their own explanations.</p><p>So now we have everything we need to generate our tiny, simple blockchain! Let’s write a function that will run as many times as we want, adding new blocks, and then we’ll take a look at what it’s created.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa49d2a44749f6b99ac8983a6cd57c9e/href">https://medium.com/media/fa49d2a44749f6b99ac8983a6cd57c9e/href</a></iframe><p>On line 20, i create a new function that will create a new blockchain at any length you specify. I create a new blockchain that we’ll store in an array, and I place the genesis block as the first element in the array. Next, I create each block one at a time, handing it the data it requires from the previous block, and then I place that new block into our array. Once we’re done, I log out the result. Let’s take a look at the first few blocks to see what we’ve created.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/885bcc8a257208d1cc42f21ba4840f58/href">https://medium.com/media/885bcc8a257208d1cc42f21ba4840f58/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D9Q0D6QZqLB7EToKiluHGA.png" /></figure><p>Here’s the first 4 blocks in our chain. Notice how each block’s prevHash value matches the thisHash value of the block preceding it. This links all the blocks together in something similar to a linked-list, and ensures that tampering with the blockchain is extremely difficult.</p><p>There’s a lot of information I’ve omitted from this example, but the main idea is there. Until I was able to visualize these ephemeral blocks and blockchains I’d heard so much about, it was all just magic to me. Now, using a few dozen lines of code, we’ve created our own blockchain!</p><p><a href="https://repl.it/@riccjohn/Tiny-Blockchain-node">Here’s a repl</a> of all the code for the simple blockchain if you want to play around with it.</p><blockquote>Join Coinmonks<a href="https://t.me/coincodecap"> Telegram Channel</a> and<a href="https://www.youtube.com/c/coinmonks/videos"> Youtube Channel</a> get daily <a href="http://coincodecap.com/">Crypto News</a></blockquote><h4>Also, Read</h4><ul><li><a href="https://medium.com/coinmonks/top-10-crypto-copy-trading-platforms-for-beginners-d0c37c7d698c">Copy Trading</a> | <a href="https://medium.com/coinmonks/crypto-tax-software-ed4b4810e338">Crypto Tax Software</a></li><li><a href="https://coincodecap.com/grid-trading">Grid Trading</a> | <a href="https://medium.com/coinmonks/the-best-cryptocurrency-hardware-wallets-of-2020-e28b1c124069">Crypto Hardware Wallet</a></li><li><a href="http://Top 4 Telegram Channels for Crypto Traders">Crypto Telegram Signals</a> | <a href="https://medium.com/coinmonks/crypto-trading-bot-c2ffce8acb2a">Crypto Trading Bot</a></li><li><a href="https://medium.com/coinmonks/crypto-exchange-dd2f9d6f3769">Best Crypto Exchange</a> | <a href="https://medium.com/coinmonks/bitcoin-exchange-in-india-7f1fe79715c9">Best Crypto Exchange in India</a></li><li><a href="https://medium.com/coinmonks/best-crypto-apis-for-developers-5efe3a597a9f">Best Crypto APIs</a> for Developers</li><li>Best <a href="https://medium.com/coinmonks/top-5-crypto-lending-platforms-in-2020-that-you-need-to-know-a1b675cec3fa">Crypto Lending Platform</a></li><li>An ultimate guide to <a href="https://medium.com/coinmonks/leveraged-token-3f5257808b22">Leveraged Token</a></li><li><a href="https://coincodecap.com/zero-fee-crypto-exchanges">7 Best Zero Fee Crypto Exchange Platforms</a></li><li><a href="https://coincodecap.com/best-online-casinos">Best Online Casinos</a> |<a href="https://medium.com/coinmonks/futures-trading-bots-5a282ccee3f5"> Futures Trading Bots</a></li><li><a href="https://coincodecap.com/what-are-decentralized-exchanges">Decentralized Exchanges</a> |<a href="https://coincodecap.com/bitbns-fip"> Bitbns FIP</a></li><li><a href="https://coincodecap.com/buy-crypto-with-credit-card">10 Best Places to Buy Crypto with Credit Card</a></li><li><a href="https://coincodecap.com/5-best-crypto-trading-bots-in-canada">Best Crypto Trading bots in Canada</a> |<a href="https://coincodecap.com/bybit-binance-moonxbt"> Bybit vs Binance</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a16f2137cfec" width="1" height="1" alt=""><hr><p><a href="https://medium.com/coinmonks/understanding-and-building-your-own-tiny-blockchain-in-javascript-a16f2137cfec">Understanding and building your own tiny blockchain in JavaScript</a> was originally published in <a href="https://medium.com/coinmonks">Coinmonks</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>