<?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"><channel><title><![CDATA[LionGuest Studios]]></title><description><![CDATA[LionGuest Studios]]></description><link>https://liongueststudios.com</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1735744336745/2acc6fee-387a-458e-bb98-daca8bd9fc45.png</url><title>LionGuest Studios</title><link>https://liongueststudios.com</link></image><generator>RSS for Node</generator><lastBuildDate>Tue, 07 Apr 2026 20:09:24 GMT</lastBuildDate><atom:link href="https://liongueststudios.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Tariffs, Tech, and Tomorrow: A Candid Chat on Geopolitics and India's Future]]></title><description><![CDATA[Introduction
In this candid conversation, Prometheus (Yash) and Sahil dive into the implications of Trump's proposed 26% tariffs on India, touching on broader geopolitical shifts, India’s role on the global stage, the future of homegrown innovation, ...]]></description><link>https://liongueststudios.com/tariffs-tech-and-tomorrow-a-candid-chat-on-geopolitics-and-indias-future</link><guid isPermaLink="true">https://liongueststudios.com/tariffs-tech-and-tomorrow-a-candid-chat-on-geopolitics-and-indias-future</guid><category><![CDATA[Geopolitics]]></category><category><![CDATA[politics]]></category><dc:creator><![CDATA[Sahil Bhosale]]></dc:creator><pubDate>Sat, 05 Apr 2025 10:15:57 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1743850441375/2b1ae254-aa91-401d-88b8-9eb697a84933.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction</h2>
<p>In this candid conversation, Prometheus (Yash) and Sahil dive into the implications of Trump's proposed 26% tariffs on India, touching on broader geopolitical shifts, India’s role on the global stage, the future of homegrown innovation, and how automation might reshape global power dynamics. It's a thought-provoking exchange blending realism, speculation, and a deep curiosity about the road ahead.</p>
<h2 id="heading-tariffs-tech-and-tomorrow">Tariffs, Tech, and Tomorrow</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1743849255500/820c54dc-09eb-4685-ab58-dc052a0ac105.webp" alt class="image--center mx-auto" /></p>
<div class="hn-embed-widget" id="sahil-yash-conv"></div><p> </p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>As the conversation winds down, both agree that while predictions may vary, the world is undoubtedly entering a period of rapid change. With shifting alliances, emerging technologies, and rising national ambitions, India finds itself at a pivotal moment. The dialogue ends with cautious optimism and a shared hope that whatever unfolds, people and nations prioritize stability, resilience, and long-term vision.</p>
]]></content:encoded></item><item><title><![CDATA["Designing Data-Intensive Applications" by Martin Kleppmann - A comprehensive summary]]></title><description><![CDATA[I’m diving into the book “Designing Data-Intensive Applications” by Martin Kleppmann, a must-read for anyone interested in building data intensive distributed systems. Over the next few weeks, I’ll be sharing my key takeaways and insights from this f...]]></description><link>https://liongueststudios.com/designing-data-intensive-applications-by-martin-kleppmann-a-comprehensive-summary</link><guid isPermaLink="true">https://liongueststudios.com/designing-data-intensive-applications-by-martin-kleppmann-a-comprehensive-summary</guid><category><![CDATA[design patterns]]></category><category><![CDATA[design principles]]></category><category><![CDATA[System Design]]></category><category><![CDATA[System Architecture]]></category><category><![CDATA[software architecture]]></category><category><![CDATA[Software Engineering]]></category><category><![CDATA[software development]]></category><category><![CDATA[design and architecture]]></category><dc:creator><![CDATA[Sahil Bhosale]]></dc:creator><pubDate>Tue, 14 Jan 2025 15:14:30 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1736870929107/834889b6-7f0f-4426-b8f2-b0ed164e0d1c.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>I’m diving into the book “Designing Data-Intensive Applications” by Martin Kleppmann, a must-read for anyone interested in building data intensive distributed systems. Over the next few weeks, I’ll be sharing my key takeaways and insights from this fantastic book on Linkedin. 🚀</p>
<p>I’ll be breaking my learnings into bite-sized posts. If you’re interested in leveling up your knowledge, follow my posts, and let’s learn together! Feel free to connect to me on LinkedIn.</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://www.linkedin.com/posts/sahilbhosale63_softwarearchitecture-webapplications-systemdesign-activity-7282430702086733824-b5z-?utm_source=share&amp;utm_medium=member_desktop">https://www.linkedin.com/posts/sahilbhosale63_softwarearchitecture-webapplications-systemdesign-activity-7282430702086733824-b5z-?utm_source=share&amp;utm_medium=member_desktop</a></div>
<p> </p>
<h2 id="heading-chapter-1-reliable-scalable-and-maintainable-applications">Chapter 1: Reliable, Scalable, and Maintainable Applications</h2>
<p>In this chapter, we will be focusing on three concerns, Reliability, Scalability and Maintainability that are important in most software systems.</p>
<ol>
<li><h3 id="heading-reliability">Reliability</h3>
</li>
</ol>
<p>Reliability is one of the most critical aspects of any data-intensive application. A reliable system continues to function correctly, delivering the desired performance, even when faced with challenges like hardware or software faults, or human errors.<br />However, reliability can be compromised by faults and failures in the system. Understanding the difference between the two is crucial:</p>
<ul>
<li><p>𝗙𝗮𝘂𝗹𝘁: A fault occurs when a component deviates from its expected behavior (e.g., hardware issues, bugs, or human mistakes).</p>
</li>
<li><p>𝗙𝗮𝗶𝗹𝘂𝗿𝗲: A failure happens when the system as a whole stops delivering the required service to users, impacting overall functionality.</p>
</li>
</ul>
<ol start="2">
<li><h3 id="heading-scalability">Scalability</h3>
</li>
</ol>
<p>Scalability is the key to ensuring your system can handle growth in data volume, traffic, or complexity. A scalable system describe a system’s ability to cope with increased load.</p>
<h4 id="heading-describing-load">Describing Load</h4>
<p>Before we can think about scaling, we need to define the current load on the system. Only then can we explore what happens when the load increases. 𝗟𝗼𝗮𝗱 𝗽𝗮𝗿𝗮𝗺𝗲𝘁𝗲𝗿𝘀 help describe this. The choice of parameters depends on your system’s architecture. Examples include:</p>
<ul>
<li><p>Requests per second to a web server</p>
</li>
<li><p>Read/write ratio in a database</p>
</li>
<li><p>Number of simultaneously active users</p>
</li>
<li><p>Cache hit rate, etc</p>
</li>
</ul>
<h4 id="heading-describing-performance">Describing Performance</h4>
<p>Once the load is defined, it's important to consider how performance changes as the load increases:</p>
<ul>
<li><p>What happens to performance if you increase load (e.g., double the traffic) without changing system resources like CPU, memory, and bandwidth?</p>
</li>
<li><p>How much more resources (CPU, memory, etc.) are needed to maintain performance when the load increases?</p>
</li>
</ul>
<p>Scalability ensures that your system grows efficiently without compromising performance.</p>
<h4 id="heading-response-time">Response Time</h4>
<p>Response time is the total time a user waits after making a request until they get a response. It includes:</p>
<ul>
<li><p>The time it takes for the system to actually process the request (called service time).</p>
</li>
<li><p>Any delays caused by the network.</p>
</li>
<li><p>Time spent waiting in line if other requests are being processed (queueing delays).</p>
</li>
</ul>
<p>Random additional latency could be introduced by a context switch to a background process, the loss of a network packet and TCP retransmission, a garbage collection pause, a page fault forcing a read from disk, mechanical vibrations in the server rack, or many other causes.</p>
<h4 id="heading-latency">Latency</h4>
<p>Latency is specifically the time a request spends waiting to be processed. It's the "idle" time before the actual work on the request starts.<br />𝗜𝗻 𝘀𝗵𝗼𝗿𝘁,</p>
<pre><code class="lang-markdown">Response time = Processing time + Network delays + Queueing time.
Latency = Waiting time before processing starts.
</code></pre>
<p>Key points for understanding response time and their impact on user experience:</p>
<ul>
<li><p><strong>Average Response Time</strong>: Commonly reported but not always accurate in reflecting typical user experience. It’s often calculated as the arithmetic mean.</p>
</li>
<li><p><strong>Mean vs. Percentiles</strong>: The mean doesn’t show how many users experience a particular delay. Percentiles are better for understanding typical and worst-case response times.</p>
</li>
<li><p><strong>Median (50th Percentile)</strong>: The median response time is a good measure of typical user experience, where half the requests are faster and half are slower.</p>
</li>
<li><p><strong>Higher Percentiles (p95, p99, p999)</strong>: These show the response times for the slowest 5%, 1%, and 0.1% of requests, respectively, and are crucial for understanding the "worst-case" scenarios (tail latencies).</p>
</li>
<li><p><strong>Impact on User Experience</strong>: High percentile response times affect user satisfaction and business outcomes. For instance, Amazon found that slower responses reduce sales and customer satisfaction.</p>
</li>
<li><p><strong>Challenges with High Percentiles</strong>: Optimizing extremely high percentiles (e.g., 99.99th) is costly and yields diminishing returns, often influenced by random external factors.</p>
</li>
<li><p><strong>Service Level Agreements (SLAs)</strong>: Percentiles are used in SLAs to define acceptable service performance and availability, often including specific percentile thresholds for response times.</p>
</li>
<li><p><strong>Queueing Delays and Scalability Testing</strong>: Queueing delays contribute significantly to high percentile response times. When testing scalability, ensure load generation is independent of response time to get accurate measurements.</p>
</li>
</ul>
<ol start="3">
<li><h3 id="heading-maintainability">Maintainability</h3>
</li>
</ol>
<p>Over time, various people will work on the system (both in engineering and operations), maintaining its current behavior and adapting it to new use cases.</p>
<p>The 3 key principles to minimize maintenance issues and avoid creating outdated (legacy) systems:</p>
<ol>
<li><p><strong>𝗢𝗽𝗲𝗿𝗮𝗯𝗶𝗹𝗶𝘁𝘆</strong>: Ensure the system is easy for operations teams to keep running smoothly.</p>
</li>
<li><p><strong>𝗦𝗶𝗺𝗽𝗹𝗶𝗰𝗶𝘁𝘆</strong>: Make the system easy for new engineers to understand by removing unnecessary complexity.</p>
</li>
<li><p><strong>𝗘𝘃𝗼𝗹𝘃𝗮𝗯𝗶𝗹𝗶𝘁𝘆</strong>: Facilitate future changes to the system, allowing it to adapt to new, unforeseen use cases as requirements evolve.</p>
</li>
</ol>
<p>Maintainability covers many aspects, but ultimately, it’s about making life easier for the engineering and operations teams working with the system.</p>
<h2 id="heading-chapter-2-data-models-and-query-languages">Chapter 2: Data Models and Query Languages</h2>
<p>This chapter discusses various data models and query languages in database design.</p>
<h3 id="heading-object-relational-mismatch">Object-Relational Mismatch</h3>
<p>A common criticism of the SQL data model is that when data is stored in relational tables, an awkward translation layer is needed between the objects in the application code and the database model of tables, rows, and columns. This disconnect between the models is often referred to as an impedance mismatch.</p>
<p>Object-relational mapping (ORM) frameworks like ActiveRecord, Hibernate and many others help minimize the boilerplate code needed for this translation layer, but they cannot entirely conceal the differences between the two models.</p>
<h3 id="heading-relationships-in-database">Relationships in Database</h3>
<ul>
<li><p><strong>1 : 𝗠𝗮𝗻𝘆 𝗿𝗲𝗹𝗮𝘁𝗶𝗼𝗻𝘀𝗵𝗶𝗽</strong>: When one entity participates in many relationships and the other entity participates in one relationship then this is called 1 : Many relationship. Ex: A user can be stay in single city but that city can be used by many other users.</p>
</li>
<li><p><strong>𝗠𝗮𝗻𝘆 : 𝗠𝗮𝗻𝘆 𝗿𝗲𝗹𝗮𝘁𝗶𝗼𝗻𝘀𝗵𝗶𝗽</strong>: When both entities participates in many relationships then this is called Many : Many relationship. Ex: A user can buy many courses and one course can be bought by many users.</p>
</li>
<li><p><strong>1 : 1 𝗿𝗲𝗹𝗮𝘁𝗶𝗼𝗻𝘀𝗵𝗶𝗽</strong>: When both entities participates in single relationship then this is called 1 : 1 relationship. Ex: employee can have single id card associated and that id card will also be assigned to single entity.</p>
</li>
</ul>
<h3 id="heading-choosing-the-right-data-model-for-your-application">Choosing the Right Data Model for Your Application</h3>
<ul>
<li><p><strong>Relational model</strong>: If your application does use many-to-many relationships then use relational model instead of document model.</p>
</li>
<li><p><strong>Document model</strong>: Ideal for applications with one-to-many relationships (tree-structured data) or where records have minimal relationships.</p>
</li>
<li><p><strong>Graph model</strong>: When your data has highly interconnected relationships and many-to-many connections are complex, graph models become a good choice.</p>
</li>
</ul>
<p>However, the final choice might depend on specific application requirements, such as the need for scalability, flexibility, or performance considerations.</p>
<h2 id="heading-chapter-3-storage-and-retrieval">Chapter 3: Storage and Retrieval</h2>
<h3 id="heading-index-in-db">Index in DB:</h3>
<ul>
<li><p>An index is an additional structure (that acts as metadata) that is derived from the primary data in DB.</p>
</li>
<li><p>Well chosen indexes speed up read queries.</p>
</li>
<li><p>Index usually slows down writes, because the index also needs to be updated every time data is written.</p>
</li>
</ul>
<h3 id="heading-underlying-data-structures-used-to-implement-indexing">Underlying data structures used to implement indexing:</h3>
<p><strong>1. 𝗸𝗲𝘆 𝘃𝗮𝗹𝘂𝗲 𝗯𝗮𝘀𝗲𝗱 𝗶𝗻𝗱𝗲𝘅𝗶𝗻𝗴</strong></p>
<ul>
<li>To avoid running out of disk space we use compaction (Compaction means throwing away duplicate keys in the log, and keeping only the most recent update for each key.)</li>
</ul>
<p><strong>2. 𝗦𝗦𝗧𝗮𝗯𝗹𝗲𝘀 𝗮𝗻𝗱 𝗟𝗦𝗠-𝗧𝗿𝗲𝗲𝘀</strong></p>
<ul>
<li><p><strong>SSTables</strong> (Sorted String Tables) store key-value pairs that allows efficient lookups and range scans (retrieving records within specific range).</p>
</li>
<li><p><strong>LSM-Trees</strong> (Log-Structured Merge-Trees) are optimized for write-heavy operations and does periodical merging the data using compaction.</p>
</li>
<li><p>Both store data in sorted order and on disk.</p>
</li>
</ul>
<p><strong>3. 𝗜𝗻𝗱𝗲𝘅𝗶𝗻𝗴 𝗶𝗻 𝗗𝗕 𝘄𝗶𝘁𝗵 𝗕 𝘁𝗿𝗲𝗲</strong></p>
<ul>
<li><p>B-trees store key-value pairs in sorted order, enabling efficient lookups and range queries.</p>
</li>
<li><p>Data is organized into fixed-size blocks (called pages), each page is typically 4 KB, with one page read or written at a time.</p>
</li>
<li><p>The branching factor refers to the number of child page references in a single B-tree page.</p>
</li>
<li><p>Most databases use B-trees with 3–4 levels, minimizing page references. A 4-level tree with 4 KB pages and a branching factor of 500 can store up to 256 TB.</p>
</li>
</ul>
<h3 id="heading-types-of-indexing-in-db">Types of indexing in DB.</h3>
<ol>
<li><p>𝗣𝗿𝗶𝗺𝗮𝗿𝘆 𝗜𝗻𝗱𝗲𝘅<br /> The primary index is created automatically on primary key column. Only one primary index is allowed per table.</p>
</li>
<li><p>𝗖𝗹𝘂𝘀𝘁𝗲𝗿𝗲𝗱 𝗶𝗻𝗱𝗲𝘅<br /> A clustered index stores all row data within the index.</p>
</li>
<li><p>𝗡𝗼𝗻-𝗰𝗹𝘂𝘀𝘁𝗲𝗿𝗲𝗱 𝗶𝗻𝗱𝗲𝘅<br /> A non-clustered index stores only references to the data within the index.</p>
</li>
<li><p>𝗠𝘂𝗹𝘁𝗶 𝗰𝗼𝗹𝘂𝗺𝗻 𝗶𝗻𝗱𝗲𝘅𝗲𝘀<br /> If we need to query multiple columns of a table then we use multi column indexes</p>
</li>
<li><p>𝗙𝘂𝗹𝗹-𝘁𝗲𝘅𝘁 𝘀𝗲𝗮𝗿𝗰𝗵 𝗮𝗻𝗱 𝗳𝘂𝘇𝘇𝘆 𝗶𝗻𝗱𝗲𝘅𝗲𝘀<br /> All the above indexes are used to query exact data. In contrast, a full-text search index retrieves relevant records by analyzing the search query, including synonyms, ignoring grammatical variations, and finding words that appear near each other in a document.</p>
</li>
</ol>
<h2 id="heading-chapter-4-encoding-and-evolution">Chapter 4: Encoding and Evolution</h2>
<p>Programs store data in memory in the form of data structures like objects, array, trees, graphs, etc. However, if you want to send this data over the network or write it to a file then its not directly possible. You first have to convert this data into a sequence of bytes.</p>
<p>This translation from the in-memory representation to a byte sequence is called encoding (serialization or marshalling), and the reverse is called decoding (parsing, deserialization, unmarshalling).</p>
<p>When it comes to encoding, there are two formats in which we represent data:<br />1. 𝗧𝗲𝘅𝘁-𝗯𝗮𝘀𝗲𝗱 𝗲𝗻𝗰𝗼𝗱𝗶𝗻𝗴: JSON, XML, CSV<br />2. 𝗕𝗶𝗻𝗮𝗿𝘆 𝗲𝗻𝗰𝗼𝗱𝗶𝗻𝗴: Protobuf, Avro</p>
<h3 id="heading-compatibility-in-binary-encoding">Compatibility in Binary Encoding</h3>
<p>Compatibility refers to the relationship between a process that encodes data and another that decodes it. To ensure smooth data exchange, the format or schema used for encoding and decoding must remain synchronized, even as the schema evolves.</p>
<p><strong>Types of Compatibility</strong></p>
<ol>
<li><p><strong>Backward Compatibility</strong>: Newer versions of the system can read data written by older versions.</p>
</li>
<li><p><strong>Forward Compatibility</strong>: Older versions of the system can read data written by newer versions.</p>
</li>
</ol>
<h3 id="heading-challenges-with-rpc">Challenges with RPC:</h3>
<p>RPC helps the client (someone who request a resource) to directly execute a function written on the server (who can serve that request).</p>
<ul>
<li><p><strong>Unpredictability</strong>: Unlike local function calls, RPCs can fail due to network issues or remote machine unavailability, which are outside your control.</p>
</li>
<li><p><strong>Timeouts and Unknown Outcomes</strong>: RPC requests may return without a result due to timeouts, leaving uncertainty about whether the request was processed.</p>
</li>
<li><p><strong>Retry Issues</strong>: Retrying failed requests can lead to duplicate actions if responses are lost but requests succeed.</p>
</li>
<li><p><strong>Latency Variability</strong>: Network latency is variable and significantly slower than local function calls.</p>
</li>
<li><p><strong>Data Encoding Challenges</strong>: Parameters must be encoded for network transmission, which can be problematic with complex data types.</p>
</li>
<li><p><strong>Interoperability Issues</strong>: Different programming languages require datatype translations, which can be complex and error-prone.</p>
</li>
</ul>
<p>Even after these challenges, RPC remains a valuable tool in distributed systems due to its ability to simplify the development of remote interactions.</p>
<h2 id="heading-chapter-5-replication">Chapter 5: Replication</h2>
<p>Replication involves creating multiple copies of the same data and storing them across different nodes to enhance data availability and fault tolerance.</p>
<h3 id="heading-types-of-replication">Types of Replication</h3>
<ol>
<li><strong>Leader and Followers Replication</strong></li>
</ol>
<ul>
<li><p>In this model, a leader node handles both read and write requests, while follower nodes can only accept read requests.</p>
</li>
<li><p>The leader is often referred to as the master or primary node, while followers may be called read replicas, slaves, secondaries, or hot standbys.</p>
</li>
<li><p>This type of replication is typically configured to be completely asynchronous.</p>
</li>
</ul>
<ol start="2">
<li><strong>Synchronous Replication</strong></li>
</ol>
<ul>
<li><p>Data is written to both the primary and secondary storage systems simultaneously.</p>
</li>
<li><p>This method prioritizes high availability and aims for zero data loss.</p>
</li>
</ul>
<ol start="3">
<li><strong>Asynchronous Replication</strong></li>
</ol>
<ul>
<li><p>In this approach, data is first written to the primary storage and then replicated to the secondary storage after a delay (may be due to network delay, etc).</p>
</li>
<li><p>While this method focuses on speed and efficiency, it carries a risk of data loss if the primary site fails before the latest changes are replicated.</p>
</li>
</ul>
<p>___________________________________________________</p>
<p>REMAINING CONTENT COMING EVERY WEEK…</p>
<p>___________________________________________________</p>
<h2 id="heading-noteworthy-points-wip">Noteworthy Points (WIP)</h2>
<p>I will continue adding more points to the list below as I progress through the book.</p>
<ul>
<li><p><strong>Horizontal scaling</strong>: Distributing load across multiple machines is also known as a shared-nothing architecture.</p>
</li>
<li><p>Some systems are elastic, meaning that they can automatically add computing resources when they detect a load increase, whereas other systems are scaled manually (a human analyzes the capacity and decides to add more machines to the system). An elastic system can be useful if load is highly unpredictable, but manually scaled systems are simpler and may have fewer operational surprises.</p>
</li>
<li><p><strong>Polyglot persistence</strong> is a hybrid approach that uses different data storage technologies to handle different data storage needs within a software application.</p>
</li>
<li><p>In both relational and document DBs, the related item is referenced by a unique identifier, which is called a <code>foreign key</code> in the relational model and a <code>document reference</code> in the document model.</p>
</li>
<li><p>Document databases are sometimes called schemaless, but that’s misleading, as the code that reads the data usually assumes some kind of structure—i.e., there is an implicit schema, but it is not enforced by the database.</p>
</li>
<li><p>In <code>schema-on-read</code> (the structure of the data is implicit, and only interpreted when the data is read), in contrast with <code>schema-on-write</code> (the traditional approach of relational databases, where the schema is explicit and the database ensures all written data conforms to it).</p>
</li>
<li><p>Schema-on-read is similar to dynamic (runtime) type checking in programming languages, whereas schema-on-write is similar to static (compile-time) type checking.</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[System Design: How to Scale from Zero to a Million Users]]></title><description><![CDATA[Scalability plays a major role in building today’s software systems. Why, you may ask? The simple answer is to manage a large number of users simultaneously with low latency and high response rates.
Building and managing such systems at scale used to...]]></description><link>https://liongueststudios.com/system-design-how-to-scale-from-zero-to-a-million-users</link><guid isPermaLink="true">https://liongueststudios.com/system-design-how-to-scale-from-zero-to-a-million-users</guid><category><![CDATA[System Design]]></category><category><![CDATA[design patterns]]></category><category><![CDATA[scalability]]></category><category><![CDATA[Scalable web applications]]></category><category><![CDATA[design principles]]></category><category><![CDATA[design and architecture]]></category><category><![CDATA[System Architecture]]></category><category><![CDATA[Software Engineering]]></category><category><![CDATA[software architecture]]></category><category><![CDATA[interview]]></category><dc:creator><![CDATA[Sahil Bhosale]]></dc:creator><pubDate>Tue, 31 Dec 2024 18:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1735828264210/09765a29-22d2-4610-bc70-98be05ac9fee.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>Scalability</strong> plays a major role in building today’s software systems. Why, you may ask? The simple answer is to manage a large number of users simultaneously with low latency and high response rates.</p>
<p>Building and managing such systems at scale used to require a high budget and advanced technical skills. However, alternatives like AWS, Google Cloud, and Azure are now available, allowing you to avoid building your infrastructure from scratch.</p>
<p>If you are a software developer, applying for a software engineering or architecture position at MAANG companies, or simply looking to upskill, knowledge of designing scalable and distributed systems is crucial. In this article, we will explore one such concept. We’ll start from a monolithic system and see how to scale it into a distributed system capable of handling millions of users.</p>
<h3 id="heading-single-server-architecture">Single Server Architecture</h3>
<p>A single server architecture consists of a client and a server. A client can be anything — a mobile app, web application, etc. — that is trying to make requests to the server for data. This type of architecture is also known as <strong>monolithic architecture</strong> because all the code (frontend, backend, database) is kept on a single server.</p>
<p>Monolithic systems have their own advantages. However, in today’s world, performance and efficiency play a major role, and that’s where monolithic systems fall short after a certain point.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735829431186/b5811f2b-0921-4a21-8feb-c45b1943b0b9.jpeg" alt class="image--center mx-auto" /></p>
<p>The drawback of such systems is that they are not scalable. Since all the code is on a single server, each server has its own hardware limitations — such as RAM, CPU, and storage — beyond which it becomes very difficult to scale. The process of increasing system resources to their limits is called <strong>vertical scaling</strong>. Additionally, such systems can handle only a specific number of users (traffic) at any given time. For example, if a large number of users (say 10K requests) try to request resources and the system can handle only (say 1K requests), the system will crash.</p>
<p>This is the type of system we currently have which is shown in above image. There are various steps that we will follow throughout this article to scale our system. These are general steps to build scalable systems, though real-life systems are quite complex, and their architecture and components will vary from this example. Each system will have different architecture based on its requirements.</p>
<p>Let’s now see how we can start scaling this system.</p>
<ol>
<li><h3 id="heading-application-and-db-separation">Application and DB separation</h3>
</li>
</ol>
<p>The first step we can take is to separate the database from the application code (both frontend and backend). This separation keeps the business logic and data source distinct. This approach is a form of <strong>horizontal scaling</strong>, where different components are placed on separate servers.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735829447034/bb7548a7-81a9-4307-9b9c-1a4a08d98bc5.jpeg" alt class="image--center mx-auto" /></p>
<p>By doing this, we reduce the load on the application server, improving efficiency while also enhancing data security.</p>
<ol start="2">
<li><h3 id="heading-adding-multiple-application-servers">Adding multiple application servers</h3>
</li>
</ol>
<p>The next step is to separate the application code based on its functionality. For example, if you’re building an e-commerce site, you could separate the frontend code, payment processing code, user management code, etc., onto individual servers, as shown below.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735829529925/5e12cc72-2abd-4227-addc-2b499ea8e166.jpeg" alt class="image--center mx-auto" /></p>
<p>As illustrated in the diagram, this approach isolates the code into multiple application servers (AP), making it more modular, loosely coupled, and secure. The main advantage of dividing the application across multiple servers is that if one server goes down, it won’t break the entire application — other parts will continue to function. However, if there’s an API call from one server to another that has gone down, that specific API call will fail.</p>
<p>It’s important to note that dividing the application into multiple servers does not necessarily mean adopting a microservices architecture.</p>
<ol start="3">
<li><h3 id="heading-load-balancers">Load balancers</h3>
</li>
</ol>
<p>The architecture we’ve built so far has become quite efficient. However, since we’ve added multiple application servers, a new challenge arises: how do we determine which server should handle each request from the client? There needs to be a component that manages client requests (load/traffic) and directs them to the appropriate application server based on specific logic. This is where <strong>load balancers</strong> come into play.</p>
<p><strong>For example:</strong> If we have four application servers, we could use the <strong>round-robin</strong> load balancing algorithm to distribute the load. This algorithm sends one request to each application server (AS) in sequence, ensuring the load is evenly distributed across all servers.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735829539386/2687183a-83f2-4707-9563-46bef6214e2a.jpeg" alt class="image--center mx-auto" /></p>
<p>At this stage, various <strong>load balancing algorithms</strong> can be implemented, including:</p>
<ol>
<li><p>Round Robin</p>
</li>
<li><p>Weighted Round Robin</p>
</li>
<li><p>IP Hash</p>
</li>
<li><p>Least Connection</p>
</li>
<li><p>Weighted Least Connection</p>
</li>
<li><p>Least Response Time</p>
</li>
</ol>
<p>Having a good understanding of these algorithms and knowing when to use each one can be highly beneficial in practice.</p>
<p><strong>Subscribed</strong></p>
<ol start="4">
<li><h3 id="heading-database-replication-master-slave-concept">Database replication (master / slave concept)</h3>
</li>
</ol>
<p>In our current architecture, we have only one database, which handles both read and write operations. This increases the overall latency of the system. Additionally, if the database goes down, our application won’t be able to retrieve any data. To address this issue, we implement <strong>database replication</strong>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735829573330/4a1dba78-4489-4d90-b666-947287eb12b0.jpeg" alt class="image--center mx-auto" /></p>
<p>Database replication involves adding multiple slave databases and copying all data from the current database (which acts as the master) to these slave databases. The <strong>slave</strong> databases are used solely for <strong>reading</strong> data, while the <strong>master</strong> database is responsible for <strong>writing</strong> data. Whenever we write data to the master, it is replicated across all slave databases to ensure consistency.</p>
<ol start="5">
<li><h3 id="heading-cache">Cache</h3>
</li>
</ol>
<p>To further enhance the performance of our system, we can implement a <strong>cache server</strong>. A cache server helps retrieve data quickly without needing to query the database or recompute the data. Typically, we cache data that doesn’t change frequently and is often requested by the frontend or client.</p>
<p>The cache server is located alongside the application server. When a request reaches the application server, it first checks if the data is available in the cache. If the data is present, it’s sent directly from the cache to the client. If not, the application server (AS) will query the database to retrieve the data.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735829588982/42664803-aa38-4c05-9e23-f8bcb173ab01.jpeg" alt class="image--center mx-auto" /></p>
<p>We should always aim to minimize database calls by storing and retrieving data from the cache whenever possible, as database queries are costly and significantly contribute to increased latency.</p>
<p>When cached data becomes outdated due to updates in the database, we refer to this as <code>stale</code> data, meaning the cache needs to be updated. There are various cache invalidation techniques to refresh the cache, such as setting expiry times or clearing the cache completely.</p>
<ol start="6">
<li><h3 id="heading-cdn">CDN</h3>
</li>
</ol>
<p>CDN stands for <strong>Content Delivery Network</strong>. At this stage, we will separate all static files from the application server and store them on a CDN server. The CDN is designed to store static files like images, fonts, assets, and other resources that do not change frequently.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735829729250/82c1d4b5-a726-4b47-9fa4-c58bd325ed7e.jpeg" alt class="image--center mx-auto" /></p>
<p>Clients can make direct requests for these files to the CDN server instead of routing them through the application server.</p>
<ol start="7">
<li><h3 id="heading-data-centers">Data Centers</h3>
</li>
</ol>
<p>The system architecture we have designed so far can be hosted in a single country. If we replicate this architecture across multiple regions or countries, we create a data center that handles geo-specific requests.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735829619939/498ff2d8-fef1-4071-aba3-32e6e3ec23d8.jpeg" alt class="image--center mx-auto" /></p>
<ol start="8">
<li><h3 id="heading-database-scaling">Database scaling</h3>
</li>
</ol>
<p>As discussed, we can leverage database replication to help our system handle millions of users. Another technique to further boost database performance is <strong>database scaling</strong>, which can be implemented at the database level. There are two types of database scaling:</p>
<ol>
<li><p><strong>Vertical Scaling:</strong> In this approach, we increase system resources, such as RAM or CPU, for our database.</p>
</li>
<li><p><strong>Horizontal Scaling:</strong> Here, we add multiple database nodes, typically using a method called <strong>sharding</strong>. Sharding can also be categorized into two types:</p>
</li>
</ol>
<ul>
<li><p><strong>Vertical Sharding:</strong> This involves dividing a table into multiple tables by rows.</p>
</li>
<li><p><strong>Horizontal Sharding:</strong> This approach divides a table into multiple tables by columns and is the more common method. To join these tables, we perform denormalization.</p>
</li>
</ul>
<p>The <strong>consistent hashing</strong> technique is used to shard tables that are already sharded.</p>
<p>In addition to all these strategies, you can also incorporate a messaging queue into your architecture, such as RabbitMQ or Kafka, to handle asynchronous messaging.</p>
<p>By progressing step by step, we can achieve scalability, enabling our systems to handle millions of users while maintaining performance efficiency.</p>
<p>I hope this article has helped you learn some new concepts related to building scalable distributed systems and that you found it worthwhile. Thank you for reading! Please make sure to like and share this article on your social media platforms.</p>
]]></content:encoded></item><item><title><![CDATA[Tidy First: A Systematic Approach to Improve Code Readability and Maintainability]]></title><description><![CDATA[Key Concepts and Practices
✦ Tidyings
Beck outlines a series of tidyings that can be applied to improve code structure and readability:

Guard Clauses: Use guard clauses to exit early from functions when conditions aren't met, reducing nested code an...]]></description><link>https://liongueststudios.com/tidy-first-a-systematic-approach-to-improve-code-readability-and-maintainability</link><guid isPermaLink="true">https://liongueststudios.com/tidy-first-a-systematic-approach-to-improve-code-readability-and-maintainability</guid><category><![CDATA[design patterns]]></category><category><![CDATA[design principles]]></category><category><![CDATA[design and architecture]]></category><category><![CDATA[software architecture]]></category><category><![CDATA[Software Engineering]]></category><category><![CDATA[software development]]></category><dc:creator><![CDATA[Sahil Bhosale]]></dc:creator><pubDate>Sun, 01 Dec 2024 06:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1735736582601/71c10d72-30a2-4a60-b5eb-26622e6b000a.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-key-concepts-and-practices"><strong>Key Concepts and Practices</strong></h2>
<h3 id="heading-tidyings"><strong>✦ Tidyings</strong></h3>
<p>Beck outlines a series of tidyings that can be applied to improve code structure and readability:</p>
<ol>
<li><p><strong>Guard Clauses:</strong> Use guard clauses to exit early from functions when conditions aren't met, reducing nested code and improving clarity.</p>
</li>
<li><p><strong>Dead Code Removal:</strong> Identify and eliminate unused code segments to reduce clutter and potential confusion.</p>
</li>
<li><p><strong>Normalize Symmetries:</strong> Enforce consistent code patterns and formatting to promote readability and collaboration.</p>
</li>
<li><p><strong>New Interface, Old Implementation:</strong> Create simplified interfaces to interact with existing code, making modifications easier without disrupting core functionality.</p>
</li>
<li><p><strong>Reading Order:</strong> Arrange code elements logically for better comprehension, considering language-specific conventions.</p>
</li>
<li><p><strong>Cohesion Order:</strong> Enhance code cohesion by grouping related elements together and managing coupling between dependencies.</p>
</li>
<li><p><strong>Variable and Constant Naming:</strong> Use descriptive names for variables and constants to self-document their purpose.</p>
</li>
<li><p><strong>Explicit Parameters:</strong> Make function inputs explicit to clarify data flow and dependencies.</p>
</li>
<li><p><strong>Chunk Statements:</strong> Break down large code blocks into smaller, more manageable sections for improved readability.</p>
</li>
<li><p><strong>One Pile:</strong> When code is overly fragmented, consider consolidating it into a single view to facilitate understanding.</p>
</li>
<li><p><strong>Explanatory Comments:</strong> Add comments to elucidate complex logic or non-obvious code constructs.</p>
</li>
<li><p><strong>Redundant Comment Removal:</strong> Eliminate comments that merely restate the code's functionality.</p>
</li>
</ol>
<h3 id="heading-managing-tidyings"><strong>✦ Managing Tidyings</strong></h3>
<p>Beck emphasizes the importance of managing tidyings within the development process:</p>
<ol start="13">
<li><strong>Separate Tidying Pull Requests:</strong> Isolate code structure changes from functional changes in code reviews to ensure clarity and focus.</li>
</ol>
<h3 id="heading-theory-of-tidying"><strong>✦ Theory of Tidying</strong></h3>
<p>The book delves into theoretical considerations for when to apply tidyings:</p>
<ol start="14">
<li><p><strong>Reversible Structure Changes:</strong> Prioritize reversible design modifications to maintain flexibility and minimize risks.</p>
</li>
<li><p><strong>Coupling and Cascading Changes:</strong> Understand the implications of coupling between code elements and strategies to mitigate cascading effects, such as automated tools for multi-file updates.</p>
</li>
</ol>
<h3 id="heading-conclusion"><strong>Conclusion</strong></h3>
<p>By embracing the principles of "tidy first," developers can create code that is not only functional but also clear, maintainable, and adaptable to future changes. This approach fosters a more sustainable and efficient development process, ultimately leading to more robust and well-structured software systems.</p>
]]></content:encoded></item><item><title><![CDATA[Learn Git and GitHub in 2025: A Practical Tutorial for Beginners]]></title><description><![CDATA[What is Git and Github?
Git is a distributed version control system that is used to track the changes (different versions of the software) made by many different people (distributed) to the repository (where the software is stored) of the software. I...]]></description><link>https://liongueststudios.com/learn-git-and-github-in-2025-a-practical-tutorial-for-beginners</link><guid isPermaLink="true">https://liongueststudios.com/learn-git-and-github-in-2025-a-practical-tutorial-for-beginners</guid><category><![CDATA[Git]]></category><category><![CDATA[GitHub]]></category><category><![CDATA[version control]]></category><category><![CDATA[Gitcommands]]></category><dc:creator><![CDATA[Sahil Bhosale]]></dc:creator><pubDate>Tue, 05 Nov 2024 06:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1735661787909/7ba8351d-d445-4432-a21e-83195e48704c.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-what-is-git-and-github">What is Git and Github?</h2>
<p>Git is a distributed version control system that is used to track the changes (different versions of the software) made by many different people (distributed) to the repository (where the software is stored) of the software. It helps developers to easily submit those changes which are made to the source code just by executing a few git commands. Whenever a developer makes any changes to the code and wants them to be merged with the official repository of the project then he/she has to make a pull request which will eventually submit those changes to the repository.</p>
<p>At a time, thousands of people push changes to the same repository and keeping track of all these changes is very difficult. For this, we need a version control system. In this post, we will discuss the various git commands which we can use for creating a branch, checking the status, pushing the changes, updating the project and much more.</p>
<p>For doing all of this we will be using a website called GitHub where we can host our open source projects and is based on git version control system.</p>
<p>Before diving deep lets first understand all the git commands which we will be using in this blog post.</p>
<h2 id="heading-git-commands"><strong>Git commands:</strong></h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Command</td><td>Description</td></tr>
</thead>
<tbody>
<tr>
<td>git clone</td><td>To clone the repository from GitHub</td></tr>
<tr>
<td>git init</td><td>Initialize the git repository</td></tr>
<tr>
<td>git add .</td><td>To add files to the staging area</td></tr>
<tr>
<td>git status</td><td>Check files in the staging area</td></tr>
<tr>
<td>git commit -m “message”</td><td>Committing changes with a commit message</td></tr>
<tr>
<td>git checkout</td><td>Used to switch between branches</td></tr>
<tr>
<td>git merge</td><td>To merge your own branch with the master branch</td></tr>
<tr>
<td>git remote add origin</td><td>Adding a remote repository</td></tr>
<tr>
<td>git push</td><td>Pushing local changes to the remote server i.e on GitHub</td></tr>
</tbody>
</table>
</div><p>There are even more git commands which you can go through which are available <a target="_blank" href="https://github.com/joshnh/Git-Commands#basic-snapshotting">here</a>.</p>
<h2 id="heading-recommended-books-to-learn-git-amp-github"><strong>Recommended Books to learn Git &amp; GitHub</strong></h2>
<ol>
<li><p><a target="_blank" href="https://amzn.to/40niX3y">Git: The Ultimate Guide for Beginners: Learn Git Version Control</a></p>
</li>
<li><p><a target="_blank" href="https://amzn.to/3MwEzVw">Pragmatic Version Control Using Git: 01</a></p>
</li>
</ol>
<h2 id="heading-using-git-commands-on-github"><strong>Using Git Commands on GitHub</strong></h2>
<p>Firstly, to use git commands with GitHub you will need a GitHub account, if you don’t have one then you can create it <a target="_blank" href="https://github.com/">here</a>.</p>
<p>After creating an account login and click on “+” icon at the top-right and select “New repository” to create a new GitHub repository. You will see the below screen where you have to enter the details related to your repository like the name of the repository, description, choose whether you want to make your repository public or private and lastly, you can choose whether to initialize your repository with a README.md file or not. After that, you can select a license for your repository and then create the repository.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735661346110/132511e7-b33a-499b-af6a-074c4187fb5e.png" alt class="image--center mx-auto" /></p>
<p>If you want to run git commands on Windows or Mac systems then you have to install Git through this <a target="_blank" href="https://git-scm.com/download/">link. An</a>d if you are using Linux you can run this command inside your terminal: <code>sudo apt-get install git</code> (for Debian) or <code>sudo yum install git</code> (for Fedora).</p>
<p>Before you can make changes or add files to the repository you first have to clone the repository from G<a target="_blank" href="https://git-scm.com/download/">itHu</a>b on to your local machine by pressing the button “clone or download” from your repository page or just by running the command which is shown below.</p>
<pre><code class="lang-bash">git <span class="hljs-built_in">clone</span> https://github.com/sahilbhosale63/FirstRepo.git
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735661389692/b7f02aca-7be9-47a0-9c19-608dbb95c894.png" alt class="image--center mx-auto" /></p>
<p>Here in the place of “sahilbhosale63”, you have to put your own username and in place of “FirstRepo.git” you have to add your own GitHub repository name. Make sure that your repository name mentioned here matches your actual repository name. If this is not the case then you will get an error.</p>
<p>Now that you have the project locally available on your system you can make changes to this project locally and then you can follow the steps below to push this project onto your GitHub repository.</p>
<p>The changes can vary depending upon the needs of what exactly you want to do like fixing an issue or writing some snippets of documentation, etc. It doesn’t matter what exactly you want to do the commands used to push the changes to the remote server remains the same.</p>
<p>Initially, go inside the folder (in this case FirstRepo) from your terminal and then you have to initialize the repository. This is the primary step that you have to do when you are initially pushing changes to the remote server i.e on your GitHub repository.</p>
<pre><code class="lang-bash">git init
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735661447349/3740efe1-972c-4c39-9eb7-2b09837b2f82.png" alt class="image--center mx-auto" /></p>
<p>After that, you have to tell git that which files you want to upload on to the staging area, all the files which are available locally or only the files in which you have made the changes.</p>
<p>To add all of the files inside the folder to the staging area you have to run <code>git add .</code> or for a specific file use <code>git add [file-name.txt]</code></p>
<p>In this case, we will be creating a file called “sample” using <code>gedit sample</code> which is a text file and will be adding this file to the staging area.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735661489885/9ac0f431-59fa-4cca-a9d3-bd3bf1ccc995.png" alt class="image--center mx-auto" /></p>
<p>Before you can do anything else you have to add your name and email address using the following command.</p>
<pre><code class="lang-bash">git config --global user.name <span class="hljs-string">'Your Name'</span> git config --global user.email <span class="hljs-string">'youremail@domain.com'</span>
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735661550241/097b1afd-7929-4cd0-be58-1afec571b966.png" alt class="image--center mx-auto" /></p>
<p>To check what’s in the staging area we can use the command below.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735661625015/af922782-86ab-4180-94ad-dc97df526680.png" alt class="image--center mx-auto" /></p>
<p>Since we have previously added the “sample” file to the staging area therefore when we run <code>git status</code> command we can see that files which we have added in the staging area.If you want to remove any file from the staging area you can run the command below. Here, the “sample” is the name of a file.</p>
<pre><code class="lang-bash">git rm --cached sample
</code></pre>
<p>Now after you have done with adding files to the staging area you can now commit those changes for that you have to run the following command.</p>
<pre><code class="lang-bash">git commit -m <span class="hljs-string">"Initial Commit"</span>
</code></pre>
<p>Since we have previously added the “sample” file to the staging area therefore when we run <code>git status</code> command we can see that files which we have added in the staging area.If you want to remove any file from the staging area you can run the command below. Here, the “sample” is the name of a file.</p>
<pre><code class="lang-bash">git rm --cached sample
</code></pre>
<p>Now after you have done with adding files to the staging area you can now commit those changes for that you have to run the following command.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735661646135/7730ab56-74e9-45eb-bca5-356434110c81.png" alt class="image--center mx-auto" /></p>
<p>Here, the “-m” stands for a message where you have to enter your commit message meaning you have to write a message about what changes you have made so that whenever you push these changes on the GitHub other people working on that project can easily recognize these changes.</p>
<p>In this case, we will enter “Initial Commit” as our commit message. You can put whatever you want to until and unless it is related to the changes which you have made.</p>
<p>Now that we have committed these changes lets talk about the concept of branches in git.Till now whatever we were doing was directly onto the master branch (our project on the remote location). So if you have not created any branch then whenever you commit changes it will by default apply to the master branch. But this is not a proper way of committing changes instead we can create a separate branch and make our changes on that branch.</p>
<p>To create a new branch.</p>
<pre><code class="lang-bash">git branch FirstBranch
</code></pre>
<p>You can give whatever name you want to your branch here we have given “FirstBranch”.</p>
<p>But if you run the <code>git status</code> command then it will tell you that you are still on the master branch. So to switch to the “FirstBranch” branch which we have created run the following command.</p>
<pre><code class="lang-bash">git checkout FirstBranch
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735661677062/c2bb5591-eb7e-40e6-99a7-e13997d624fb.png" alt class="image--center mx-auto" /></p>
<p>Let’s create a new file called login.html using <code>touch login.html</code> command and add it to the staging area by using <code>git add .</code> and then commit it using <code>git commit -m 'Login File'</code> through the “FirstBranch” branch.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735661697678/7a9be272-4f8f-4331-b32a-b54646327494.png" alt class="image--center mx-auto" /></p>
<p>Now to merge the changes which we have made in the “FirstBranch” with the “master” branch then we have to first switch to the master branch by <code>git checkout master</code> command. And then merge those changes using <code>git merge Firstbranch</code> command.</p>
<p>Finally, to push all of these changes to the remote repository on GitHub you have to run the following two commands.</p>
<pre><code class="lang-bash">//To add origin
git remote add origin 
https://github.com/sahilbhosale63/FirstRepo.git
//To push changes
git push -u origin master
</code></pre>
<p>Here it will ask you to enter your GitHub credentials, so enter your username and password and after that, all your local changes will be pushed onto your GitHub repository. You can go to your repository page on GitHub where you will see your changes live.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735661736017/1cd1d753-942e-49b6-9730-98785ec380b1.png" alt class="image--center mx-auto" /></p>
<p>Here it will ask you to enter your GitHub credentials, so enter your username and password and after that, all your local changes will be pushed onto your GitHub repository. You can go to your repository page on GitHub where you will see your changes live.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735661752490/72011e07-3b9a-462e-9316-ed427809b967.png" alt class="image--center mx-auto" /></p>
<p>Thanks for reading and if you like the content then support us on <a target="_blank" href="https://www.patreon.com/liongueststudios">Patreon. Your</a> support will surely help us in writing more of such content.</p>
<p>If you have any questions regarding Git and GitHub you can comment them d<a target="_blank" href="https://www.patreon.com/liongueststudios">own bel</a>ow. And don’t forget to share this blog post with others on social platforms.</p>
]]></content:encoded></item><item><title><![CDATA[Artificial Intelligence – Power of making machines intelligent]]></title><description><![CDATA[What is Artificial Intelligence?
“Providing intelligence to a machine” is what we call artificial intelligence. This is the most simple, appropriate, and logical one-line definition of Artificial Intelligence (AI).
Artificial means a system that has ...]]></description><link>https://liongueststudios.com/artificial-intelligence-power-of-making-machines-intelligent</link><guid isPermaLink="true">https://liongueststudios.com/artificial-intelligence-power-of-making-machines-intelligent</guid><category><![CDATA[Machine Learning]]></category><category><![CDATA[AI]]></category><category><![CDATA[Data Science]]></category><category><![CDATA[Unsupervised learning]]></category><category><![CDATA[Supervised learning]]></category><category><![CDATA[ML algorithm]]></category><category><![CDATA[machine learning models]]></category><category><![CDATA[Machine Learning algorithm]]></category><dc:creator><![CDATA[Sahil Bhosale]]></dc:creator><pubDate>Sat, 02 Nov 2024 06:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1735735654351/7e18652b-747b-445f-a10c-160e732d7af1.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-what-is-artificial-intelligence"><strong>What is Artificial Intelligence?</strong></h2>
<p>“Providing intelligence to a machine” is what we call artificial intelligence. This is the most simple, appropriate, and logical one-line definition of <strong>Artificial Intelligence (AI)</strong>.</p>
<p>Artificial means a system that has been created or developed by humans and intelligence means an ability to think. The main goal here is to make machines think like humans and make them do all those things that humans are capable of doing. Now the question is how to make a machine intelligent and make them do the things which humans can do? To make artificial systems think like humans we need some knowledge or data.</p>
<p>So you might be thinking why do we require data or knowledge to make a machine intelligent? To understand this thing you first have to understand how the human brain works and how people think in general. We humans think or predict based on the knowledge or the data which we have gained from the surrounding, books, peers, etc and the most important thing which is learning from our own mistakes and based on this we make further decisions and act accordingly in the environment (the world).</p>
<p>The same thing goes with artificial systems. They also take the data, process it, make mistakes, and learns from those mistakes, and then they take much more accurate decisions through experience along the way.</p>
<p>We have now understood why we need data so let’s address another question which is where to use this data or how to feed it into a machine to make it intelligent? For this we create models and we called them Machine Learning (ML) models. The model works the same way as the human brain. We need a lot of data so that our ML models can make accurate decisions. And this collection of large amounts of data is called a dataset.</p>
<p>This dataset is further broken down into 2 parts one is called trained data (will be used to train the ML model) and another is called test data (will be used to test the model after training). We don’t do this manually we have tools in python to do this. And this division of dataset into 2 parts is not an exact 50-50% breakup its approximately 50%.</p>
<h2 id="heading-artificial-intelligence-and-its-subfields"><strong>Artificial intelligence and its subfields</strong></h2>
<p>Artificial Intelligence has various subfields but out of them, the most popular ones are Natural Language Processing (NLP), Machine Learning (ML), &amp; Computer Vision.</p>
<ol>
<li><h4 id="heading-natural-language-processing-nlphttps1442029123categoryartificial-intelligence"><strong>Natural Language Processing</strong> <a target="_blank" href="https://144.202.9.123/category/artificial-intelligence/"><strong>(NLP)</strong></a></h4>
<p> Natural Language Processing (NLP) deals with the processing of human language like English and making it understandable to a machine so that it will know how to respond when humans speak to them. Applications of NLP would be Amazon Alexa, Apple’s Siri, or Google Assistant.</p>
<p> You just have to speak or provide tasks to these devices by communicating with them by asking questions such as what is the weather? or turning on the lights, etc through your language and then they will process your language and provide responses accordingly.</p>
</li>
<li><h4 id="heading-computer-vision"><strong>Computer Vision</strong></h4>
<p> Computer Vision deals with identifying the objects in an image or in a video stream. The most popular example of computer vision would be self-driving cars.</p>
</li>
<li><h4 id="heading-machine-learning"><strong>Machine Learning</strong></h4>
<p> Another most popular subfield of artificial intelligence is Machine Learning(ML). Machine Learning provides an ability to a machine to think, learn, and to predict. By using Machine Learning we don’t have to explicitly program a machine to do a particular task by writing a lot of if and else statements.</p>
<p> Deep Learning is a subfield of Machine Learning in artificial intelligence (AI) which uses neural networks to make even accurate decisions like that of the human brain.</p>
</li>
</ol>
<h2 id="heading-types-of-learning-in-ml"><strong>Types of learning in ML</strong></h2>
<p>There are different ways in which we can teach a machine how to learn. The main ingredient for making machines capable of learning is the data or the dataset which we use. We take this dataset and apply a learning technique (from the ones which have mentioned below) and accordingly feed the data to a machine learning model so to make machines understand things. Each of these techniques works differently and this is what we will see below.</p>
<ol>
<li><h4 id="heading-supervised-learning"><strong>Supervised Learning</strong></h4>
<p> In Supervised Learning, we have the dataset and each data point (single unit of information) in that dataset is associated with a label. So, whenever we pass this dataset as an input to an ML model we also specific beforehand what will be the output for all the data points in that particular dataset.</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735735717431/d75a21b1-bcbb-420c-888a-4d0b56b3a7e2.png" alt class="image--center mx-auto" /></p>
<p>Let’s say we have images of a cat and a dog as the input data as shown above. Here, we also tell the model what each of these images represents, either it is a cat or it is a dog. The names which are used to represent these images are known as labels.</p>
<p>Supervised Learning Algorithms:</p>
<ul>
<li><p>Linear Regression</p>
</li>
<li><p>Logistic Regression</p>
</li>
<li><p>K-Nearest Neighbors</p>
</li>
<li><p>Decision Tree</p>
</li>
<li><p>Random Forest</p>
</li>
<li><p>Support Vector Machines</p>
</li>
</ul>
<ol start="2">
<li><h4 id="heading-unsupervised-learning"><strong>Unsupervised Learning</strong></h4>
<p> In Unsupervised Learning, unlike in supervised learning where we know the input as well as the output beforehand here we only know the input and we have to predict the output. Now to predict the output we use a concept called as clustering meaning grouping of the data.</p>
<p> In the above plot, we have plotted all of our data points from the dataset on to a graph, and these data points represent 3 shapes, triangles, rectangles, and circles. After identifying the shapes we have drawn 2 lines to make groups or to cluster those data points based on their properties. Here, the property could be the shape of the object.</p>
<p> After grouping, we can easily predict the output that the first group represents triangles, the second one represents rectangles and the last one as circles.</p>
<p> <strong>Types of Unsupervised Learning:</strong></p>
<p> - Clustering</p>
<p> - Association</p>
</li>
</ol>
<ol start="3">
<li><h4 id="heading-reinforcement-learning"><strong>Reinforcement Learning</strong></h4>
<p> Reinforcement Learning is all about learning from the environment. In Reinforcement Learning, there is an agent that acts in the environment who takes decisions. Learning from the environment means, as the agent comes across new data which the agent as never seen before and then it tries to predict what that new data is all about and based on his decision the agent is either rewarded or being punished (penalty is imposed).</p>
</li>
</ol>
<h2 id="heading-getting-started-with-data-science"><strong>Getting started with Data Science</strong></h2>
<ol>
<li><p><strong>Picking up a Programming languages</strong></p>
<ul>
<li><p>Python: An easy to learn programming language which is one of the most popular languages when it comes to the field of Data Science.</p>
</li>
<li><p>R language: The R programming language is also great and is specifically designed for statistical computing and data analysis.</p>
</li>
</ul>
</li>
</ol>
<ol start="2">
<li><p><strong>Tools and Libraries</strong></p>
<ul>
<li><p><strong>Numpy</strong>: Numpy is a python library used for working with large multi-dimensional arrays and matrices.</p>
</li>
<li><p><strong>Pandas</strong>: Pandas is a tool used for data manipulation and data analysis written in the python programming language.</p>
</li>
<li><p><strong>Mathplotlib</strong>: Mathplotlib is a python library used for creating static, animated, and interactive visualizations.</p>
</li>
<li><p><strong>Scikit-Learn</strong>: Scikit-Learn is a machine learning library for python and is used for data analysis like classification, regression, clustering, preprocessing, etc.</p>
</li>
<li><p><strong>Tensorflow</strong>: TensorFlow is a software library for dataflow and differentiable programming which is developed by Google.</p>
</li>
<li><p><strong>Keras</strong>: Keras is a neural network library written in Python which is capable of running on top of TensorFlow, Microsoft Cognitive Toolkit, R, Theano, or PlaidML.</p>
</li>
</ul>
</li>
</ol>
<ol start="3">
<li><p><strong>Mathematics for Machine Learning (ML)</strong></p>
<ul>
<li>Linear Algebra, Calculus, Statistics and Probability.</li>
</ul>
</li>
</ol>
<ol start="4">
<li><p><strong>IDEs</strong></p>
<ul>
<li><p>Jupyter Notebook: Jupyter Notebook is used to write the code, equations, visualizations, and narrative text inside a web browser.</p>
</li>
<li><p>R Studio: R Studio is an Integrated Development Environment where you can write code in R programming language.</p>
</li>
<li><p>PyCharm: Pycharm is an IDE developed by JetBrains specifically for writing code in the python programming language.</p>
</li>
</ul>
</li>
</ol>
<ol start="5">
<li><p><strong>Reading Research Papers of Artificial Intelligence &amp; Machine Learning</strong></p>
<p> There are various software companies that do a lot of research in the field of artificial intelligence / data science and publish research papers. One of the great companies in this field is DeepMind which is now acquired by Google.</p>
<p> <strong>DeepMind</strong>: <a target="_blank" href="https://deepmind.com/research">https://deepmind.com/research</a></p>
<p> These papers might be confusing to understand but don’t worry these are complex research papers and its ok if you don’t completely understand them. You will get used to it as you move along in your journey.</p>
<p> These are some of the things which you can learn to get started with data science. Apart from this, there are a hell lot of things (tools &amp; libraries) which are out there but the things which I have mentioned above are the basics and the building blocks for learning and getting staring into this field.</p>
<p> To progress in the field of data science or software development you always have to keep your self updated via reading books, blogs, videos, etc. As technology is changing rapidly the things which we use today might get deprecated or may not be in use tomorrow.</p>
</li>
</ol>
<h4 id="heading-practicing-what-you-have-learned-so-far"><strong>Practicing What You Have Learned so far</strong></h4>
<p>Just learning things will not help you also have to practice or implement what you have learned in the artificial intelligence field. For this, there is a great platform out there called <a target="_blank" href="https://www.kaggle.com/">Kaggle</a> which you can use to practice the concepts of data science by taking part in the competitions.</p>
<p>While doing so you can also make some money if you perform well in those competitions. But don’t do it for money, your primary focus should be on learning.</p>
<h2 id="heading-free-resources-for-learning-data-science"><strong>Free Resources for Learning Data Science</strong></h2>
<ol>
<li><p><a target="_blank" href="https://www.udacity.com/course/intro-to-artificial-intelligence--cs271">Intro to Artificial Intelligence</a> by Udacity</p>
</li>
<li><p><a target="_blank" href="https://towardsdatascience.com/">Towards Data Science</a> Publication on Medium</p>
</li>
<li><p><a target="_blank" href="https://ocw.mit.edu/courses/res-18-010-a-2020-vision-of-linear-algebra-spring-2020/">A 2020 Vision of Linear Algebra</a> by Gilbert Strang</p>
</li>
</ol>
<p>Thanks for reading and if you like the content then support us on <a target="_blank" href="https://www.patreon.com/liongueststudios">Patreon</a>. Your support will surely help us in writing more of such content.</p>
]]></content:encoded></item><item><title><![CDATA[Tree Data Structure in C: An Overview]]></title><description><![CDATA[This blog post will give you an overview of the tree data structure in C programming language, types of tree data structure, and tree traversal techniques on a tree.
Introduction of Tree Data Structure
A tree is a hierarchical data structure (top to ...]]></description><link>https://liongueststudios.com/tree-data-structure-in-c-an-overview</link><guid isPermaLink="true">https://liongueststudios.com/tree-data-structure-in-c-an-overview</guid><category><![CDATA[Tree]]></category><category><![CDATA[TreeTraversals]]></category><category><![CDATA[Trees]]></category><category><![CDATA[data structures]]></category><category><![CDATA[data structure and algorithms ]]></category><category><![CDATA[algorithms]]></category><category><![CDATA[C]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[programming languages]]></category><category><![CDATA[programming]]></category><dc:creator><![CDATA[Sahil Bhosale]]></dc:creator><pubDate>Sat, 14 Sep 2024 18:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1735748067315/402f8834-801e-49e2-b4db-856586962649.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>This blog post will give you an overview of the tree data structure in C programming language, types of tree data structure, and tree traversal techniques on a tree.</p>
<h2 id="heading-introduction-of-tree-data-structure"><strong>Introduction of Tree Data Structure</strong></h2>
<p>A tree is a hierarchical data structure (top to bottom and left to right) which consists of nodes and edges. Nodes are where we store all of our data. The data can be anything it can be integers, strings, characters, etc. The starting node or the first node in a tree is called a root node. And the nodes which don’t have any children are called the leaf node. The path which connects two nodes is called an edge.</p>
<p>Every node in a tree can have multiple children’s and based on the number of children’s a tree have we give that tree a unique name. Example: If every node in a tree has two children then that tree will be called as a binary tree, if every node in a tree has three children’s then it is called as a ternary tree and so on.</p>
<p>Below is a diagram which illustrates a binary tree.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735748119691/e1299ec4-b3cb-4ce9-ae32-11357f16d5ad.png" alt class="image--center mx-auto" /></p>
<p>To construct a tree, we can use linked lists.</p>
<p>There are some concepts in trees one is called traversing and another one is searching. Don’t get confused between traversing and searching they both are two different things. Traversing means visiting each and every node in a tree at least once. In searching, we search for a particular node in a tree and after finding that element we stop.</p>
<h2 id="heading-traversing-a-tree-data-structure"><strong>Traversing a tree data structure</strong></h2>
<p>There are 3 tree traversal techniques. And in all those techniques, the order in which we traverse the root node changes. But when it comes to the left child and right child of a tree we traverse them in the same sequence i.e we traverse the left child first and then the right child in every traversal technique.</p>
<ol>
<li><p><strong>Preorder Traversal (Root – Left – Right)</strong></p>
<p> In Preorder, the root node is printed first and then the left and right child. Therefore it is called preorder traversal.</p>
</li>
</ol>
<pre><code class="lang-c"><span class="hljs-comment">//Creating a node with two pointer left and right</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span>{</span>
  <span class="hljs-keyword">char</span> data;
  <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> *<span class="hljs-title">left</span>, *<span class="hljs-title">right</span>;</span>
}

<span class="hljs-comment">//Preorder Traversal on a Binary Tree</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Preorder</span><span class="hljs-params">(struct node *t)</span></span>{

  <span class="hljs-comment">//Checking if the tree is Null or not.</span>
  <span class="hljs-keyword">if</span>(t != <span class="hljs-literal">NULL</span>){

    <span class="hljs-comment">//Print value inside the node</span>
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%c"</span>, t-&gt;data);

    <span class="hljs-comment">//visit the left subtree</span>
    Preorder(t-&gt;left);

    <span class="hljs-comment">//visit the right subtree</span>
    Preorder(t-&gt;right);
  }
}
</code></pre>
<ol start="2">
<li><strong>Inorder Traversal (Left – Root – Right)</strong></li>
</ol>
<pre><code class="lang-c"><span class="hljs-comment">//Creating a node with two pointer left and right</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span>{</span>
  <span class="hljs-keyword">char</span> data;
  <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> *<span class="hljs-title">left</span>, *<span class="hljs-title">right</span>;</span>
}

<span class="hljs-comment">//Inorder Traversal on a Binary Tree</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Inorder</span><span class="hljs-params">(struct node *t)</span></span>{

  <span class="hljs-comment">//Checking if the tree is Null or not.</span>
  <span class="hljs-keyword">if</span>(t != <span class="hljs-literal">NULL</span>){ 

    <span class="hljs-comment">//visit the left subtree</span>
    Inorder(t-&gt;left);

    <span class="hljs-comment">//Print value inside the node</span>
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%c"</span>, t-&gt;data);

    <span class="hljs-comment">//visit the right subtree</span>
    Inorder(t-&gt;right);
  }
}
</code></pre>
<ol start="3">
<li><p><strong>Postorder Traversal (Left – Right – Root)</strong></p>
<p> In Postorder, the left and right child are printed first and then the root node. Therefore it is called Postorder traversal.</p>
</li>
</ol>
<pre><code class="lang-c"><span class="hljs-comment">//Creating a node with two pointer left and right</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span>{</span>
  <span class="hljs-keyword">char</span> data;
  <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> *<span class="hljs-title">left</span>, *<span class="hljs-title">right</span>;</span>
}

<span class="hljs-comment">//Postorder Traversal on a Binary Tree</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">Postorder</span><span class="hljs-params">(struct node *t)</span></span>{

  <span class="hljs-comment">//Checking if the tree is Null or not.</span>
  <span class="hljs-keyword">if</span>(t != <span class="hljs-literal">NULL</span>){ 

    <span class="hljs-comment">//visit the left subtree</span>
    Postorder(t-&gt;left);

    <span class="hljs-comment">//visit the right subtree</span>
    Postorder(t-&gt;right);

    <span class="hljs-comment">//Print value inside the node</span>
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%c"</span>, t-&gt;data);
  }
}
</code></pre>
<p>All the above 3 tree traversal techniques will take the same time complexity and space complexity of O(n).</p>
<h2 id="heading-types-of-trees"><strong>Types of trees</strong></h2>
<ol>
<li><h4 id="heading-binary-tree"><strong>Binary Tree</strong></h4>
<p> Binary tree is a tree in which every node consist of either two children’s or zero children’s. Except the leaf node all the other nodes will have two children’s.</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735748212372/6cc96a14-2c7f-4d83-902a-fbdd476627f0.png" alt class="image--center mx-auto" /></p>
<ol start="2">
<li><h4 id="heading-binary-search-tree"><strong>Binary Search Tree</strong></h4>
<p> As the name suggests it’s a binary tree (having two or zero children) but in this case here we have a condition and that is the elements present in the left subtree must be lesser than the value of the root element and the elements which are present at the right subtree must be greater than the value of the root element. This condition is applicable to each and every node in the binary search tree because each node will have either a left subtree or a right subtree or both.</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735748238102/6e04684f-542e-4a0c-96ab-1197bc79df61.png" alt class="image--center mx-auto" /></p>
<ol start="3">
<li><h4 id="heading-balanced-binary-search-tree-avl-tree"><strong>Balanced Binary Search Tree (AVL Tree)</strong></h4>
<p> Now to check if the tree is balanced or not there is a concept called Balanced Factor (BF). The Balanced Factor for a particular node in an AVL tree can be either -1, 0, or 1 (which means the node is balanced). If the balanced factor of the node is other than these numbers say 2 or -2 then we have to balance that particular node. Below is the formula to find the balanced factor.</p>
<p> <code>Balanced Factor (BF) = Height of Left Subtree (LST) – Height of Right Subtree (RST)</code></p>
<p> Every time we insert a node into the balanced binary search tree (AVL tree) we have to check whether we have any kind of imbalance in a tree or not. If there is any imbalance then we have to balance it using the below imbalance techniques.</p>
</li>
</ol>
<p>There are 4 types of imbalances possible in AVL tree.</p>
<p><strong>a. Left-Left Imbalance (LL imbalance)</strong></p>
<p>From the below figure we can see that the tree has LL imbalance. So we have to rotate the tree in a clockwise direction to make it balanced.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735748302277/76f6bb93-6e98-474c-8988-fb33e910a085.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735748309390/468730e1-3ce0-4c78-acec-e669ecd1277c.png" alt class="image--center mx-auto" /></p>
<p><strong>b. Right-Right Imbalance (RR imbalance)</strong></p>
<p>If we have RR imbalance in a tree then we have to rotate the tree in a anticlockwise direction to make it balanced.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735748357534/defd5ddb-3c6a-4270-86e2-5c08ead21363.png" alt class="image--center mx-auto" /></p>
<p><strong>c. Left-Right Imbalance (LR imbalance)</strong></p>
<p>If we have an LR imbalance in a tree then we have to perform 2 rotations. First rotate anticlockwise (RAC) and second rotate clockwise (RCW).</p>
<p><strong>d. Right-Left Imbalance (RL imbalance)</strong></p>
<p>If we have an RL imbalance in a tree then also we have to perform 2 rotations. First rotate clockwise (RCW) and second rotate anticlockwise (RAC).</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735748399810/390df12c-bf2c-4165-abd5-bf9caddac3bd.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-what-should-be-the-approach-for-solving-a-tree-problem"><strong>What should be the approach for solving a tree problem?</strong></h2>
<p>Any Data Structure can be implemented either iteratively (using loops) or recursively (using recursion functions). And the same goes with the tree data structure. But implementing a tree data structure could be quite confusing at first.</p>
<p>To solve a tree data structure, you should have a good understanding of how recursion works. Because most of the time we will be using recursion to implement a tree data structure. By using recursion we can solve a tree problem in fewer lines of code.</p>
<p>But if you are using an iterative way to solve a tree problem then you might have to write a lot of code and it might be really confusing to understand it. It’s totally up to you which approach you want to follow while implementing a tree.</p>
<p>Thanks for reading and if you like the content then support us on <a target="_blank" href="https://www.patreon.com/liongueststudios">Patreon</a>. Your support will surely help us in writing more of such content.</p>
]]></content:encoded></item><item><title><![CDATA[Ensemble Method in Machine Learning]]></title><description><![CDATA[Ensemble method is a technique in which we take a group of the #ml models and then we combine the predictions of those models to produce optimal predictive model. Ensemble means a group.
Steps:

Use the ensemble and make prediction

Then calculate a ...]]></description><link>https://liongueststudios.com/ensemble-method-in-machine-learning</link><guid isPermaLink="true">https://liongueststudios.com/ensemble-method-in-machine-learning</guid><category><![CDATA[Data Science]]></category><category><![CDATA[Machine Learning]]></category><category><![CDATA[AI]]></category><category><![CDATA[data analysis]]></category><dc:creator><![CDATA[Sahil Bhosale]]></dc:creator><pubDate>Wed, 28 Aug 2024 06:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1735735507825/9c502d70-60a5-4193-bfec-797765e45a6e.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Ensemble method is a technique in which we take a group of the <a target="_blank" href="https://www.linkedin.com/feed/hashtag/?keywords=ml&amp;highlightedUpdateUrns=urn%3Ali%3Aactivity%3A7069010356215398400">#ml</a> models and then we combine the predictions of those models to produce optimal predictive model. Ensemble means a group.</p>
<h2 id="heading-steps"><strong>Steps:</strong></h2>
<ul>
<li><p>Use the ensemble and make prediction</p>
</li>
<li><p>Then calculate a loss function eg: mean squared error</p>
</li>
<li><p>Fit a new model using the loss function</p>
</li>
<li><p>Add this new model in ensemble</p>
</li>
<li><p>Repeat</p>
</li>
</ul>
<h2 id="heading-types-of-ensemble-methods"><strong>Types of ensemble methods:</strong></h2>
<ul>
<li><p>Voting</p>
</li>
<li><p>Averaging</p>
</li>
<li><p>Stacking</p>
</li>
<li><p>Bootstrap</p>
</li>
<li><p>Aggregating (Bagging)</p>
</li>
<li><p>Boosting</p>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Cross-validation in Machine Learning]]></title><description><![CDATA[Enhancing model quality with cross-validation in Machine Learning 🤖
Cross-validation is a modelling process where the data is divided into multiple folds (pieces) and then we perform experiments by taking each fold at a time and considering it as a ...]]></description><link>https://liongueststudios.com/cross-validation-in-machine-learning</link><guid isPermaLink="true">https://liongueststudios.com/cross-validation-in-machine-learning</guid><category><![CDATA[Data Science]]></category><category><![CDATA[AI]]></category><category><![CDATA[Machine Learning]]></category><category><![CDATA[Python]]></category><category><![CDATA[cross-validation]]></category><dc:creator><![CDATA[Sahil Bhosale]]></dc:creator><pubDate>Wed, 21 Aug 2024 06:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1735735287515/49514422-8a6a-4c2d-9b99-1a4883fd7769.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Enhancing model quality with cross-validation in Machine Learning 🤖</p>
<p>Cross-validation is a modelling process where the data is divided into multiple folds (pieces) and then we perform experiments by taking each fold at a time and considering it as a validation set and all the other folds combined as a training set.</p>
<p>The data can be divided into any number of folds. Say we have divided the data into three folds, then,</p>
<ul>
<li><p><strong>Experiment 1:</strong> Fold 1 – validation set, Fold 2,3 – training set</p>
</li>
<li><p><strong>Experiment 2:</strong> Fold 2 – validation set, Fold 1,3 – training set</p>
</li>
<li><p><strong>Experiment 3:</strong> Fold 3 – validation set, Fold 1,2 – training set</p>
</li>
</ul>
<p>After performing all experiments on the #mlmodels, we get scores (model quality/model accuracy) for each experiment. We can take the average of it and consider the overall score.</p>
<p>This process is resource heavy and so it might not produce instant results for larger datasets as compared to smaller datasets. Overall it helps increase the model quality.</p>
]]></content:encoded></item><item><title><![CDATA[3 ways to handle missing values in Machine Learning]]></title><description><![CDATA[Steps to handle missing values:

Drop the columns that has missing values

Here, we drop the columns that have missing values.

With this approach, there is a chance that we may lose access to lot of useful information.

You should use this approach ...]]></description><link>https://liongueststudios.com/3-ways-to-handle-missing-values-in-machine-learning</link><guid isPermaLink="true">https://liongueststudios.com/3-ways-to-handle-missing-values-in-machine-learning</guid><category><![CDATA[Machine Learning]]></category><category><![CDATA[machine learning models]]></category><category><![CDATA[Python]]></category><category><![CDATA[Data Science]]></category><category><![CDATA[AI]]></category><dc:creator><![CDATA[Sahil Bhosale]]></dc:creator><pubDate>Wed, 14 Aug 2024 06:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1735735146887/a0890e04-b6b2-4b41-bbc3-5df65e03094f.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-steps-to-handle-missing-values">Steps to handle missing values:</h2>
<ol>
<li><p><strong>Drop the columns that has missing values</strong></p>
<ul>
<li><p>Here, we drop the columns that have missing values.</p>
</li>
<li><p>With this approach, there is a chance that we may lose access to lot of useful information.</p>
</li>
<li><p>You should use this approach only if most of the values in a column are missing and not otherwise.</p>
</li>
</ul>
</li>
</ol>
<ol start="2">
<li><p><strong>Imputation</strong></p>
<ul>
<li><p>Here, we replace all the missing values in a column with the average value of that column. For example, if column has 3, 2, 1 as values then we take average of this column that is 2 and replace it with missing values.</p>
</li>
<li><p>It’s a standard approach.</p>
</li>
<li><p>The imputed value is not always right, however, it results in good accuracy as compared to the previous way.</p>
</li>
</ul>
</li>
</ol>
<ol start="3">
<li><p><strong>Extension to Imputation</strong></p>
<ul>
<li><p>Here, we follow the process of imputation and then for each column with missing value we also create a new column.</p>
</li>
<li><p>The location at which the value is imputed we mark an entry in the newly created column to specify that this location has imputed value.</p>
</li>
<li><p>This may increase the accuracy of prediction, but in some cases it can also make no difference.</p>
</li>
</ul>
</li>
</ol>
]]></content:encoded></item><item><title><![CDATA[Splitting the dataset in Machine Learning]]></title><description><![CDATA[Do you know how to split the #datasets in Machine Learning? 🤔
If you want to become a data scientist 👨‍💻, then you must be good at working with datasets. Being familiar with the dataset is the first step towards building ML models.
Before working ...]]></description><link>https://liongueststudios.com/splitting-the-dataset-in-machine-learning</link><guid isPermaLink="true">https://liongueststudios.com/splitting-the-dataset-in-machine-learning</guid><category><![CDATA[Machine Learning]]></category><category><![CDATA[dataset]]></category><category><![CDATA[Data Science]]></category><category><![CDATA[AI]]></category><dc:creator><![CDATA[Sahil Bhosale]]></dc:creator><pubDate>Thu, 08 Aug 2024 06:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1735734670584/7df3dc25-3b6b-48c2-8569-ba3b7e572ffb.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Do you know how to split the <a target="_blank" href="https://www.linkedin.com/feed/hashtag/?keywords=datasets&amp;highlightedUpdateUrns=urn%3Ali%3Aactivity%3A7050464383780405248">#datasets</a> <a target="_blank" href="https://www.linkedin.com/feed/hashtag/?keywords=datasets&amp;highlightedUpdateUrns=urn%3Ali%3Aactivity%3A7050464383780405248">in Machi</a>ne Learning? 🤔</p>
<p>If you want to become a data scientist <a target="_blank" href="https://www.linkedin.com/feed/hashtag/?keywords=datasets&amp;highlightedUpdateUrns=urn%3Ali%3Aactivity%3A7050464383780405248">👨‍💻, t</a>hen you must be good at working with datasets. Being familiar with the dataset is the first step towards building ML models.</p>
<p>Before working with the data from the <a target="_blank" href="https://www.linkedin.com/feed/hashtag/?keywords=datasets&amp;highlightedUpdateUrns=urn%3Ali%3Aactivity%3A7050464383780405248">dataset i</a>t’s important to split the dataset into three parts.</p>
<ul>
<li><p>Training set: Used for training the mo<a target="_blank" href="https://www.linkedin.com/feed/hashtag/?keywords=datasets&amp;highlightedUpdateUrns=urn%3Ali%3Aactivity%3A7050464383780405248">del.</a></p>
</li>
<li><p><a target="_blank" href="https://www.linkedin.com/feed/hashtag/?keywords=datasets&amp;highlightedUpdateUrns=urn%3Ali%3Aactivity%3A7050464383780405248">V</a>alidation set: Used to pick the best <a target="_blank" href="https://www.linkedin.com/feed/hashtag/?keywords=datasets&amp;highlightedUpdateUrns=urn%3Ali%3Aactivity%3A7050464383780405248">model for</a> prediction. Once you have trained and tested various models you have to decide which model makes accurate predictions based on the validation set.</p>
</li>
<li><p>Testing set: Once you have finalized t<a target="_blank" href="https://www.linkedin.com/feed/hashtag/?keywords=datasets&amp;highlightedUpdateUrns=urn%3Ali%3Aactivity%3A7050464383780405248">he model</a> you can then test the model with a testing set.</p>
</li>
</ul>
<p>The ratio in which you divide the data <a target="_blank" href="https://www.linkedin.com/feed/hashtag/?keywords=datasets&amp;highlightedUpdateUrns=urn%3Ali%3Aactivity%3A7050464383780405248">depends</a> on the size of the dataset and there is no specified number on how to split the data. However, you can generally split the dataset into 60% training set, 20% validation set and 20% testing set.</p>
<p>That’s all about splitting the dataset <a target="_blank" href="https://www.linkedin.com/feed/hashtag/?keywords=datasets&amp;highlightedUpdateUrns=urn%3Ali%3Aactivity%3A7050464383780405248">in Machi</a>ne Learning. In the next post, we will see how to explore the dataset with Pandas.</p>
]]></content:encoded></item><item><title><![CDATA[Prediction target and Features in Machine Learning]]></title><description><![CDATA[Prediction Target

It represents the output of an ML model.

For example, In a supervised learning algorithm, if you are developing a model for predicting house prices then your dataset will have a “Price” column, so this column will be your predicti...]]></description><link>https://liongueststudios.com/prediction-target-and-features-in-machine-learning</link><guid isPermaLink="true">https://liongueststudios.com/prediction-target-and-features-in-machine-learning</guid><category><![CDATA[AI]]></category><category><![CDATA[Machine Learning]]></category><category><![CDATA[Data Science]]></category><dc:creator><![CDATA[Sahil Bhosale]]></dc:creator><pubDate>Wed, 07 Aug 2024 06:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1735734925008/cca3de14-94e3-4d40-bcea-5127a49f6da2.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-prediction-target"><strong>Prediction Target</strong></h2>
<ul>
<li><p>It represents the output of an ML model.</p>
</li>
<li><p>For example, In a supervised learning algorithm, if you are developing a model for predicting house prices then your dataset will have a “Price” column, so this column will be your prediction target.• Only one column can be the prediction target.</p>
</li>
<li><p>Represented as “y”.</p>
</li>
<li><p>It can be accessed with a dot notation approach as follows.y = my_dataset.Price</p>
</li>
</ul>
<h2 id="heading-features"><strong>Features</strong></h2>
<ul>
<li><p>Features are nothing but the inputs that are provided to a model.</p>
</li>
<li><p>The feature can be any number of columns from the dataset, except for the prediction target.</p>
</li>
<li><p>Based on the selected input columns (features) the output will be predicted by the model.</p>
</li>
<li><p>Represented as “X” as follows.</p>
</li>
</ul>
<pre><code class="lang-markdown">features = [‘TotalRooms’, ‘Floors’, ‘Landsize’]X = my<span class="hljs-emphasis">_dataset[melbourne_</span>features]]
</code></pre>
]]></content:encoded></item><item><title><![CDATA[List in Python – Data Structure]]></title><description><![CDATA[Understanding the Data structures is the most crucial and important thing when it comes to learning and working with any programming language. Python too has some of the most popular data structures using which you can organize the data in memory. Li...]]></description><link>https://liongueststudios.com/list-in-python-data-structure</link><guid isPermaLink="true">https://liongueststudios.com/list-in-python-data-structure</guid><category><![CDATA[Python]]></category><category><![CDATA[data structures]]></category><category><![CDATA[Python 3]]></category><category><![CDATA[python beginner]]></category><dc:creator><![CDATA[Sahil Bhosale]]></dc:creator><pubDate>Fri, 05 Jul 2024 06:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1735736854290/fbba3644-849f-450f-802a-4d58b7d58b59.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Understanding the <a target="_blank" href="https://en.wikipedia.org/wiki/Data_structure">Data structures</a> is the most crucial and important thing when it comes to learning and working with any programming language. <a target="_blank" href="https://en.wikipedia.org/wiki/Python_\(programming_language\)">Python</a> too has some of the most popular data structures using which you can organize the data in memory. List, Tuples, Sets, and Dictionary are some of the widely used data structures in python. We will discuss the List in python data structure in detail in this blog post. Later we will see some of the other data structures in python in detail.</p>
<p>For more blogs visit <a target="_blank" href="https://liongueststudios.com">LionGuest Studios.com</a></p>
<h2 id="heading-list-in-pythonhttpsdocspythonorg3tutorialdatastructureshtml"><a target="_blank" href="https://docs.python.org/3/tutorial/datastructures.html"><strong>List in python</strong></a></h2>
<ul>
<li><p>The List is a data structure in python which stores data linearly or sequentially one after the other. It is somewhat similar to arrays in other programming languages. The only difference between an array and a List is the arrays can store data of the same type whereas Python’s List can store data with multiple data types (a combination of integer, string, floating-point, etc).</p>
</li>
<li><p>Lists are represented using square brackets [ ]. The initial item in a list starts with an index number ‘0’.</p>
</li>
<li><p>The list is a mutable data structure which simply means that once it has created the items inside which can be changed later.</p>
</li>
</ul>
<h2 id="heading-creating-a-list-in-pythonhttpswwwgeeksforgeeksorgpython-listcl"><a target="_blank" href="https://www.geeksforgeeks.org/python-list/#cl"><strong>Creating a List in python</strong></a></h2>
<p>Lists can be created by putting values inside the square brackets and assigning it to a variable.</p>
<pre><code class="lang-python">empty_list = [ ]
</code></pre>
<p>Here, empty_list is the name of the variable which will contain a list as represented by square brackets. And inside the square brackets, you don’t have any elements which simply means that the list is empty.</p>
<p>Examples:</p>
<pre><code class="lang-python">list_of_numbers = [ <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>] 
list_of_Strings = [<span class="hljs-string">'Ana'</span>, <span class="hljs-string">'Pat'</span>, <span class="hljs-string">'Simon'</span>, <span class="hljs-string">'Amanda'</span>]
</code></pre>
<p>Note that the strings inside the list are represented by quotes (single or double quotes anything will work) and numbers have no quotation mark.</p>
<pre><code class="lang-python">list_with_different_data_types =  [ <span class="hljs-string">'Micheal'</span>, <span class="hljs-number">15</span>, <span class="hljs-number">20.5</span>, [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]  ]
</code></pre>
<p>This is a list that contains multiple data types. Also, the lists can contain another list too. In this case, element number 3 is an element of a list which is a list in itself.</p>
<h2 id="heading-adding-elements-to-a-list-in-pythonhttpswwww3schoolscompythonpythonlistsaddasp"><a target="_blank" href="https://www.w3schools.com/python/python_lists_add.asp"><strong>Adding elements to a List in python</strong></a></h2>
<ol>
<li><code>append()</code> <strong>function:</strong></li>
</ol>
<p>If you want to insert an element at the end of the list then you can use the append function. Inside this function, you have to pass the value or the element that needs to be inserted in a list.</p>
<pre><code class="lang-python">original_lst = [<span class="hljs-string">'A'</span>, <span class="hljs-string">'B'</span>, <span class="hljs-string">'C'</span>, <span class="hljs-string">'D'</span>, <span class="hljs-string">'E'</span>]
original_lst.append(<span class="hljs-string">'F'</span>) <span class="hljs-comment">#This will insert element ‘F’ at the end of the list</span>

print(original_lst)
</code></pre>
<pre><code class="lang-bash">Output:
[<span class="hljs-string">'A'</span>, <span class="hljs-string">'B'</span>, <span class="hljs-string">'C'</span>, <span class="hljs-string">'D'</span>, <span class="hljs-string">'E'</span>, <span class="hljs-string">'F'</span>]
</code></pre>
<p>You can even insert an entire list to an already created list with the append() function.</p>
<pre><code class="lang-python">lst = [<span class="hljs-string">'A'</span>, <span class="hljs-string">'B'</span>, <span class="hljs-string">'C'</span>, <span class="hljs-string">'D'</span>, <span class="hljs-string">'E'</span>]
new_lst = [<span class="hljs-string">'F'</span>, <span class="hljs-string">'G'</span>, <span class="hljs-string">'1'</span>, <span class="hljs-string">'2'</span>, <span class="hljs-string">'3'</span>]

lst.append(new_lst)

print(lst)
</code></pre>
<pre><code class="lang-bash">Output:
[<span class="hljs-string">'A'</span>, <span class="hljs-string">'B'</span>, <span class="hljs-string">'C'</span>, <span class="hljs-string">'D'</span>, <span class="hljs-string">'E'</span>, [<span class="hljs-string">'F'</span>, <span class="hljs-string">'G'</span>, <span class="hljs-string">'1'</span>, <span class="hljs-string">'2'</span>, <span class="hljs-string">'3'</span>]]
</code></pre>
<p>Note: If you want to add a single element to a list then use append() and if you want to add multiple elements into a list then use extend(). Go to <a target="_blank" href="https://3.235.222.184/list-in-python-data-structure/#extend">extend() function</a>.</p>
<ol start="2">
<li><p><code>insert()</code> <strong>function:</strong></p>
<p> If you want to insert an element at a specified location in a list then you can use the insert(location, element_to_insert) function which takes two parameters. In the first parameter, you have to pass the location at which the element is to be inserted and in the second parameter, you have to pass the value of the element.</p>
</li>
</ol>
<pre><code class="lang-python">my_list = [<span class="hljs-string">'one'</span>, <span class="hljs-string">'three'</span>]
</code></pre>
<p>Now let’s add element ‘two’ to the above list at index 1 for that you have to use the insert function.</p>
<pre><code class="lang-python">my_list( <span class="hljs-number">1</span>, <span class="hljs-string">'two'</span> )
</code></pre>
<pre><code class="lang-bash">Output:
[<span class="hljs-string">'one'</span>, <span class="hljs-string">'two'</span>, <span class="hljs-string">'three'</span>]
</code></pre>
<h2 id="heading-deleting-elements-from-a-list-in-pythonhttpswwww3schoolscompythonpythonlistsremoveasp"><a target="_blank" href="https://www.w3schools.com/python/python_lists_remove.asp"><strong>Deleting elements from a List in python</strong></a></h2>
<ol>
<li><p><code>remove()</code> <strong>function:</strong></p>
<p> If you want to delete a particular element from a list using its value then you can use the remove() function. For example, if you want to remove element ‘B’ from the list then you have to pass this as an argument to the remove() function.</p>
</li>
</ol>
<pre><code class="lang-python">list = [<span class="hljs-string">'A'</span>, <span class="hljs-string">'B'</span>, <span class="hljs-string">'C'</span>, <span class="hljs-string">'D'</span>, <span class="hljs-string">'E'</span>]

list.remove(<span class="hljs-string">'B'</span>)
print(list)
</code></pre>
<pre><code class="lang-bash">Output:
[<span class="hljs-string">'A'</span>, <span class="hljs-string">'C'</span>, <span class="hljs-string">'D'</span>, <span class="hljs-string">'E'</span>]
</code></pre>
<ol start="2">
<li><p><code>del</code> <strong>keyword:</strong></p>
<p> If you want to delete a particular element from a list using its index then you can use the del keyword. Let say you want to delete ‘Marry’ from the below list then you can delete that element using its index no.</p>
</li>
</ol>
<pre><code class="lang-python">list = [<span class="hljs-string">'Merry'</span>, <span class="hljs-string">'Adam'</span>, <span class="hljs-string">'Watson'</span>, <span class="hljs-string">'Anna'</span>]

<span class="hljs-keyword">del</span> list[<span class="hljs-number">0</span>] <span class="hljs-comment">#passing index 0 because Marry is present at the index  </span>
print(list)
</code></pre>
<pre><code class="lang-bash">Output:
[<span class="hljs-string">'Adam'</span>, <span class="hljs-string">'Watson'</span>, <span class="hljs-string">'Anna'</span>]
</code></pre>
<ol start="3">
<li><p><code>pop()</code> <strong>function:</strong></p>
<p> The pop() function works the same way as the del keyword but the only difference is that the pop() function will also return the element which has been deleted from the list.</p>
</li>
</ol>
<pre><code class="lang-python">list = [<span class="hljs-string">'Merry'</span>, <span class="hljs-string">'Adam'</span>, <span class="hljs-string">'Watson'</span>, <span class="hljs-string">'Anna'</span>]
item_popped = list.pop(<span class="hljs-number">0</span>) 

print(item_popped) 
print(list)
</code></pre>
<p>So, If you want to delete an element from a list in python using the value then use the remove() function. And if you want to delete an element using index no then use the del keyword or pop() function. In case if you also want to get the item that you have deleted then use the pop() function which will return you that item.</p>
<h2 id="heading-accessing-elements-inside-a-list-indexing-in-pythonhttpswwww3schoolscompythonpythonlistsaccessasp"><a target="_blank" href="https://www.w3schools.com/python/python_lists_access.asp"><strong>Accessing elements inside a List (indexing) in python</strong></a></h2>
<p>Each element in a list as an index no starting with zero. To find an element inside the list you have to use its index no. Accessing an element using its index no is called indexing. This can be done by passing the index number inside the square brackets.</p>
<pre><code class="lang-python">list[indexNo]
</code></pre>
<p>In the below list, if you want to access the individual elements inside a list then you can pass the index as follows.</p>
<pre><code class="lang-python">my_list = [<span class="hljs-string">'one'</span>, <span class="hljs-string">'two'</span>,<span class="hljs-string">'three'</span>]

print(my_list[<span class="hljs-number">0</span>])    <span class="hljs-comment"># accessing element ‘one’</span>
print(my_list[<span class="hljs-number">1</span>])    <span class="hljs-comment"># accessing element ‘two’</span>
print(my_list[<span class="hljs-number">2</span>])    <span class="hljs-comment"># accessing element ‘three’</span>
</code></pre>
<pre><code class="lang-bash">Output:
one
two
three
</code></pre>
<h2 id="heading-slicing-a-list-in-pythonhttpsrailswarecomblogpython-for-machine-learning-indexing-and-slicing-for-lists-tuples-strings-and-other-sequential-typesslicenotation"><a target="_blank" href="https://railsware.com/blog/python-for-machine-learning-indexing-and-slicing-for-lists-tuples-strings-and-other-sequential-types/#Slice_Notation"><strong>Slicing a list in python</strong></a></h2>
<p>Slicing is nothing but dividing a list. In case if you want to print some part of the list and not the entire list then you can use slicing.</p>
<p>Slicing is done using “:” colon symbol. It has 3 parts list[ start : end : stepSize ]. Step size is optional.</p>
<p>Before the colon, you have to specify the start index of the list (i.e the index from which you want to slice a list) and it is denoted by start. After the colon, you have to specify the end index of the list (till what index do you want to slice the list) and it is denoted by the end. Here, note that the value which you specify for the end part will not be included in the final list after slicing.</p>
<p>Let’s take an example and understand this.</p>
<pre><code class="lang-python">slice_this_list = [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>, <span class="hljs-number">50</span>, <span class="hljs-number">60</span>]
</code></pre>
<p>So, to slice them you have to use the index number of both the start and the end elements. The index of starting element 20 is 1 and the index of ending element 50 is 4. You will use these index nos in order to slice the list.</p>
<pre><code class="lang-python">print(slice_this_list[<span class="hljs-number">1</span> : <span class="hljs-number">4</span>])
</code></pre>
<p>What do you think it will print? It will print 20, 30, 40 only 3 elements. But we also want to print 50, right? The last element is not included in the slicing because python will exclude the end and will stop at one index before the end i.e in this case index 3. </p>
<p>You have specified end as 4, so it will stop at one element before index 4 which is index 3. And the element which we have at index 3 is 40. Therefore, it is slicing till element 40. </p>
<p>Now, if you also want to get the element 50 then you have to increase our end index to 5.</p>
<pre><code class="lang-python">print(slice_this_list [ <span class="hljs-number">1</span> : <span class="hljs-number">5</span> ])
</code></pre>
<p>Here, you are slicing the list in sequential order (one after the other) but if you want to skip some elements in the middle then you can use step size. By default step size is 1 which will slice the elements in sequential order. You can increase it to any value, in this case, let’s increase it to 2.</p>
<pre><code class="lang-python">print(slice_this_list [ <span class="hljs-number">1</span> : <span class="hljs-number">5</span> : <span class="hljs-number">2</span> ])

<span class="hljs-comment"># Here,</span>
<span class="hljs-comment"># 1 represents - start</span>
<span class="hljs-comment"># 5 represents - end</span>
<span class="hljs-comment"># 2 - represents - step size</span>
</code></pre>
<p>This will print the following as output.</p>
<pre><code class="lang-bash">Output:
20 40
</code></pre>
<p>In case if you want to start from a particular element and then print all the elements then don’t specify the end part keep it blank.</p>
<pre><code class="lang-python">my_list = [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>, <span class="hljs-number">50</span>, <span class="hljs-number">60</span>, <span class="hljs-number">70</span>]
</code></pre>
<p>Here, Let’s start printing from the element 30 to the last element i.e 70.</p>
<pre><code class="lang-bash">Output:
[30, 40, 50, 60, 70]
</code></pre>
<p>The same can be done if you want to print all the elements from the staring index to the desired element. Let’s slice the above myList till the index 5. For that keep the start part blank.</p>
<pre><code class="lang-python">print(my_list[  : <span class="hljs-number">5</span> ]) <span class="hljs-comment">#end part is kept blank to print all the in the list after</span>
</code></pre>
<pre><code class="lang-bash">Output:
[10, 20, 30, 40, 50]
</code></pre>
<p>Note that it will slice till one element before the index 4.</p>
<h2 id="heading-sorting-a-list-in-pythonhttpswwww3schoolscompythonpythonlistssortasp"><a target="_blank" href="https://www.w3schools.com/python/python_lists_sort.asp"><strong>Sorting a List in python</strong></a></h2>
<p>Sorting a list is arranging the elements inside the list in either ascending or descending order. There are two ways to sort a list in python.</p>
<ol>
<li><p><code>sorted()</code> <strong>function:</strong></p>
<p> <strong>sorted()</strong> function takes a list as an input and returns a sorted list in ascending order. The original list remains unchanged. Now if you want to sort the list in descending order then you can even pass reverse = True as a second argument (which is optional) to the sorted() function. </p>
</li>
</ol>
<pre><code class="lang-python">original_list  = [<span class="hljs-number">5</span>, <span class="hljs-number">2</span> , <span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">3</span>]

sorted_list_ascending_order = sorted(original_list)
sorted_list_descending_order = sorted(original_list, reverse= <span class="hljs-literal">True</span>)

print(original_list)
print(sorted_list_ascending_order)
print(sorted_list_descending_order)
</code></pre>
<pre><code class="lang-bash">Output:
[5, 2 , 1, 4, 3]
[1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]
</code></pre>
<ol start="2">
<li><p><code>sort()</code> <strong>function:</strong></p>
<p> Instead of creating a new variable and storing the sorted list inside that variable, you can also sort the list and store it inside the same variable using the sort() function.</p>
</li>
</ol>
<pre><code class="lang-bash">Output:
[1, 2, 3, 4, 5]
</code></pre>
<p>Note that if you have a list with different data types then you cannot sort that list with any of the above functions as it is not possible. </p>
<h2 id="heading-printing-all-the-elements-inside-a-list-in-pythonhttpswwww3schoolscompythonpythonlistsloopasp"><a target="_blank" href="https://www.w3schools.com/python/python_lists_loop.asp"><strong>Printing all the elements inside a list in python</strong></a></h2>
<p>In order to print all the elements inside a list in python you can use a for loop with the “in” keyword.</p>
<pre><code class="lang-python">list = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]

<span class="hljs-keyword">for</span> element <span class="hljs-keyword">in</span> list:
    print(element)
</code></pre>
<pre><code class="lang-bash">Output:
1
2
3
4
5
</code></pre>
<h2 id="heading-other-operations-performed-on-a-listhttpsdocspythonorg3tutorialdatastructureshtml"><a target="_blank" href="https://docs.python.org/3/tutorial/datastructures.html"><strong>Other operations performed on a List</strong></a></h2>
<ol>
<li><h3 id="heading-reversing-a-list-in-python"><strong>Reversing a List in python</strong></h3>
<p> <strong>list.reverse()</strong>: Reversing a list in python is very easy. In order to reverse a list, you have to use the reverse() function in python which will reverse all the elements inside that list.</p>
</li>
</ol>
<pre><code class="lang-python">reverse_this_list = [<span class="hljs-number">5</span>, <span class="hljs-number">10</span>, <span class="hljs-number">15</span>, <span class="hljs-number">20</span>, <span class="hljs-number">25</span>, <span class="hljs-number">30</span>]
print(reverse_this_list.reverse());
</code></pre>
<pre><code class="lang-bash">Output:
[30, 25, 20, 15, 10, 5]
</code></pre>
<ol start="2">
<li><h3 id="heading-finding-length-of-a-list"><strong>Finding length of a list</strong></h3>
<p> <strong>len(List)</strong>: If you want to know the total no of elements inside a list then you can use a len() function which will return the total elements present inside the list.</p>
</li>
</ol>
<pre><code class="lang-bash">Output:
5
</code></pre>
<ol start="3">
<li><h3 id="heading-splitting-a-string-into-a-list-in-python"><strong>Splitting a string into a list in python</strong></h3>
<p> <strong>string.split()</strong>: If you have a string say “Welcome to LionGuest Studios” and want to split the entire string in such a way that each word in a string is represented as an element inside a list. For that, you have to use the split() function. If you don’t specify any parameter then it will split the string based on white space or a tab.</p>
</li>
</ol>
<pre><code class="lang-python">string_to_list = <span class="hljs-string">"Welcome to LionGuest Studios"</span>
string_to_list.split()
</code></pre>
<pre><code class="lang-bash">Output:
[<span class="hljs-string">'Welcome'</span>, <span class="hljs-string">'to'</span>, <span class="hljs-string">'LionGuest'</span>, <span class="hljs-string">'Studios'</span>]
</code></pre>
<ol start="4">
<li><h3 id="heading-joining-two-or-more-list-in-python"><strong>Joining two or more List in python</strong></h3>
<p> <strong>a.</strong> <code>extend()</code> <strong>function:</strong></p>
<p> If you want to add elements at the end of the list or if you want to join two lists then you can use the extend function. This will add elements of list2 to list1 without affecting list2.</p>
</li>
</ol>
<pre><code class="lang-python"><span class="hljs-comment">#example with "extend"</span>

list1 = [<span class="hljs-number">1</span>, <span class="hljs-string">"Mac"</span>, <span class="hljs-number">20.5</span>]
list2 = [<span class="hljs-string">"Mongo"</span>, <span class="hljs-number">50</span>, <span class="hljs-number">40.5</span>]

list1.extend(list2)

print(list1)
print(list2)
</code></pre>
<pre><code class="lang-bash">Output:
[1, <span class="hljs-string">'Mac'</span>, 20.5, <span class="hljs-string">'Mongo'</span>, 50, 40.5]
[<span class="hljs-string">'Mongo'</span>, 50, 40.5]
</code></pre>
<p><strong>Note</strong>: If you want to add a single element to a list then use append() and if you want to add multiple elements into a list then use extend().</p>
<p><strong>b.</strong> <code>+</code> <strong>operator</strong></p>
<p>To join two or more lists together we can use the “+” operator. This works the same way as the extend function but here it will not modify the given lists. You have to store the results of this operation to a new variable so that you can use it later.</p>
<pre><code class="lang-bash">Output:
[1, <span class="hljs-string">'Mac'</span>, 20.5]
 [<span class="hljs-string">'Mongo'</span>, 50, 40.5]
 [<span class="hljs-string">'X'</span>, <span class="hljs-string">'Y'</span>, <span class="hljs-string">'Z'</span>]
 [1, <span class="hljs-string">'Mac'</span>, 20.5, <span class="hljs-string">'Mongo'</span>, 50, 40.5, <span class="hljs-string">'X'</span>, <span class="hljs-string">'Y'</span>, <span class="hljs-string">'Z'</span>]
</code></pre>
<ol start="5">
<li><h3 id="heading-counting-occurrence-of-an-item-in-a-list"><strong>Counting occurrence of an item in a list</strong></h3>
<p> <strong>list.count()</strong>: In order to count the total occurrence (frequency) of a particular element in a list we can use the count function.</p>
</li>
</ol>
<pre><code class="lang-python">this_list = [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">20</span>, <span class="hljs-number">10</span>, <span class="hljs-number">50</span> ,<span class="hljs-number">20</span> ,<span class="hljs-number">20</span>]

<span class="hljs-comment"># To count the occurrence of number 20</span>
count_of_no = this_list.count(<span class="hljs-number">20</span>)
print(<span class="hljs-string">"Total number of times the no. 20 has occured in a list is:"</span>, count_of_no)
</code></pre>
<pre><code class="lang-bash">Output:
Total number of <span class="hljs-built_in">times</span> the no. 20 has occured <span class="hljs-keyword">in</span> a list is: 4
</code></pre>
<p>That’s all it is. Thanks for the read. If you like the content then support us on <a target="_blank" href="https://www.patreon.com/liongueststudios">Patreon</a>. Your support will surely help us in writing more of such content.</p>
]]></content:encoded></item><item><title><![CDATA[4 pillars of OOP: Object Oriented Programming Paradigms in Java - Part 2/2]]></title><description><![CDATA[This is Part 2 of the object oriented programming series where we will be discussing the four object oriented programming paradigms in Java (4 pillars of oop). In our first part, we have discussed the basics of OOPs concepts like classes, objects, co...]]></description><link>https://liongueststudios.com/4-pillars-of-oop-object-oriented-programming-paradigms-in-java</link><guid isPermaLink="true">https://liongueststudios.com/4-pillars-of-oop-object-oriented-programming-paradigms-in-java</guid><category><![CDATA[Java]]></category><category><![CDATA[oop]]></category><category><![CDATA[OOPS]]></category><category><![CDATA[OOP Design Principles]]></category><category><![CDATA[design patterns]]></category><category><![CDATA[design principles]]></category><category><![CDATA[design and architecture]]></category><category><![CDATA[System Design]]></category><category><![CDATA[System Architecture]]></category><category><![CDATA[software development]]></category><category><![CDATA[Software Engineering]]></category><dc:creator><![CDATA[Sahil Bhosale]]></dc:creator><pubDate>Sun, 02 Jun 2024 18:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1735746908029/39b03cdb-f22a-4279-9762-cbf64f82257f.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>This is Part 2 of the object oriented programming series where we will be discussing the four object oriented programming paradigms in Java (4 pillars of oop). In our first part, we have discussed the basics of OOPs concepts like classes, objects, constructors, and many more. If you haven’t read that post then it’s recommended to do so as it will help you in understanding the concepts which we will be discussed in this blog post. The link to that first blog post can be found here 👉 (<a target="_blank" href="https://liongueststudios.com/object-oriented-programming-oops-concepts-in-java">Object Oriented Programming (OOPs) concepts in Java Part 1/2</a>).</p>
<p>This blog post will not be a structured kind of blog. We will explore various concepts of object oriented programming paradigm as we move forward while following a proper flow of information. Explaining these concepts individually will not make much sense since each of these concepts are interrelated to each other.</p>
<p>Before diving deep into the 4 pillars of oop lets first understand want are access modifiers in Java.</p>
<h2 id="heading-access-modifiers-in-java"><strong>Access Modifiers in Java</strong></h2>
<p>The Access modifiers are reserved keywords in Java that can be used with variables, methods, constructors, and classes. They are used to restrict the scope of the variables, methods, constructors, and classes within classes and packages (In general, packages are also known as directory or a folder). The Access modifiers will help us in writing quality code as well as in enhancing the security of the software. There are four access modifiers in Java each of them are explained briefly.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735747233202/70c88549-17a7-47dc-a9cf-afd8b8c87272.webp" alt class="image--center mx-auto" /></p>
<ol>
<li><p><code>public</code> – Whenever you use a public keyword before any variable or a method then that variable or a method can be accessed inside any class or packages.</p>
</li>
<li><p><code>default</code> – By default, if you don’t use any access modifier before variables, methods, constructors, or classes then in that case java will consider them as default access modifiers. The default access modifier can be accessed only inside a particular package in which it’s declared as well as inside the sub-packages if present.</p>
</li>
<li><p><code>private</code> – If the variables, methods or constructors are declared as private then they can be accessed only inside that class where they are declared. When using a private keyword with classes, only make inner classes or <a target="_blank" href="https://stackoverflow.com/questions/14692708/private-class-declaration">nested classes as private</a> and not the outer class. If we declare the inner class as private then it can be accessed from the outer class, but if the outer class itself is made private then this makes no sense since you will not be able to access any elements present inside that class.</p>
</li>
<li><p><code>protected</code> – If the variables, methods, or constructors are made protected then they can only be accessed within the package they are defined as well as inside the subclasses (with the help of inheritance). The classes cannot be made protected.</p>
</li>
</ol>
<h2 id="heading-the-4-pillars-of-oop-object-oriented-programming-paradigms-in-java"><strong>The 4 Pillars of OOP (object oriented programming) Paradigms in Java</strong></h2>
<p>Polymorphism, Inheritance, Encapsulation, and Abstraction are the 4 pillars of OOP (object oriented programming). Let’s understand each of them with an example. Imagine that we have a car manufacturing company and we want to design two cars, one is an SUV car and another one is a sports car. As we know, every car has various things in common and they have some differences as well. So, we will try to represent all of these things in object oriented way such that we can get a proper understanding of each of the 4 paradigms of the OOP.</p>
<p>First, we will create 4 classes, Main class, SUV class, Sports class, and Car class. The Car class will contain all the common properties of both the SUV and Sports cars. So, the Car class will be the parent of both SUV and Sports classes. The SUV and Sports classes will contain car-specific properties for SUV cars and Sports cars respectively. Inside the Main class, we will create the objects of these two car types.</p>
<p>Initially, this is how each of these classes will look like. You can create separate files for each class or you can also specify all these classes in a single file as well. The recommended way is to create separate files for each class.</p>
<pre><code class="lang-java">SUV.java

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SUV</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Car</span></span>{

}
</code></pre>
<pre><code class="lang-java">Sports.java

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Sports</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Car</span></span>{

}
</code></pre>
<pre><code class="lang-java">Car.java

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span></span>{

}
</code></pre>
<ol>
<li><h2 id="heading-polymorphism-in-java"><strong>Polymorphism in Java</strong></h2>
<p> Whenever people buy a car they are more interested in knowing the speed of the car. So, as a car manufacture company we have to make an engine for both our SUV as well as for our sports car that could take our car’s speed to the next level. To add an engine to our car, we will create a method called “engine()” which will take a single parameter of type string called “engineType”. The value of the “engineType” variable will be “SUV Engine” or “Sports Engine”. You can pass in any values, this is just for the sake of this example.</p>
<p> The engine() method will go under our Car class because it doesn’t matter what type of car your building it will always have an engine. And depending upon the type of the car which we are building we will tell the engine() method to create an engine for that type of car. For example, if we are building SUV car then we will tell the engine() to create an engine for SUV car by passing the value “SUV Engine” to the “engineType” parameter inside the method.</p>
<p> We can also make this method “public” so that we can access it inside the “Main.java” as that’s where we will be creating all of our objects.</p>
</li>
</ol>
<pre><code class="lang-java">Car.java

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span></span>{

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">engine</span><span class="hljs-params">(String engineType)</span></span>{
        System.out.println(<span class="hljs-string">"Create Engine: "</span> + engineType);
    }
}
</code></pre>
<p>In the case of Sports car, people are also interested in knowing how many seconds the car’s engine will take to go from 0 to 100 kmph. So, for this, we can also pass another parameter called “secondsToReachHundred” to the engine() method so that we can know in how many seconds our engine can reach 100 kmph. But if we pass this parameter to the above engine() method then it can cause a problem. Since whenever we will create the engine for our SUV class by calling the engine() method then during that time also we have to pass the value to the second parameter “secondsToReachHundred” as well since the engine() takes two parameters as input which we don’t want, Right.</p>
<p>To solve this, we can create another method specifically for our Sports car, so that whenever we will create the engine then we can call that method as shown below.</p>
<pre><code class="lang-java">Car.java

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span></span>{

    <span class="hljs-comment">// For the SUV and other car types</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">engine</span><span class="hljs-params">(String engineType)</span></span>{
        System.out.println(<span class="hljs-string">"Create Engine: "</span> + engineType);
    }

    <span class="hljs-comment">// For the sports car</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">engine</span><span class="hljs-params">(String engineType, <span class="hljs-keyword">int</span> secondsToReachHundred)</span></span>{
        System.out.println(<span class="hljs-string">"Create Engine: "</span> + engineType + secondsToReachHundred);
    }
}
</code></pre>
<p>As you can see above, we have created another method called engine() which takes two parameters “engineType” and “secondsToReachHundred”. This method can be called by any car that wants to know much time it will take for the engine to reach a hundred, but more specifically by the Sports car.</p>
<p>This is called polymorphism which is the first pillar out of the 4 pillars of OOP. In polymorphism, we can have various methods with the same name (in this case, engine) but each method will take different signatures or parameters. You can also create a method with the name “engine()” which can take no parameters as well. In this case, we don’t need a method with no parameters and that’s the reason we have not created such a method.</p>
<p>A Polymorphism is of two types:</p>
<h3 id="heading-a-compile-time-polymorphism"><strong>a. Compile time polymorphism</strong></h3>
<p>In this type of polymorphism, Java can decide during the compile-time which of these methods needs to be called since all the methods have the same name. The example which we have seen above is the compile-time polymorphism as Java can decide which engine() method needs to be called based on the number of parameters passed to the method while calling the method. This is also known as static polymorphism.</p>
<h3 id="heading-b-runtime-polymorphism"><strong>b. Runtime polymorphism</strong></h3>
<p>Whenever we have a method present inside the parent class and we override that method in our child class then this is called as runtime polymorphism or dynamic polymorphism.</p>
<p>Overriding a method simply means that you already have a method defined in the parent class, but for some reason you don’t want to use that method, instead want to create your own so that you can define your own code into that method. In such cases, the method name and its parameters should remain same as that of the method present inside parent class while defining your own method in child class.</p>
<p>In our above example, we have engine(String engineType) method in out Car class which only prints a message to the output window. Now let’s override this method into the Sports class which is a child of the Car class. While doing so we will also create a boolean variable called “isElectric” whose value will be currently set to false which represents that the car engine doesn’t work on electric, as shown below.</p>
<pre><code class="lang-java">Sports.java

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Sports</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Car</span></span>{

    <span class="hljs-keyword">boolean</span> isElectric = <span class="hljs-keyword">false</span>;

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">engine</span><span class="hljs-params">(String engineType)</span></span>{
        <span class="hljs-keyword">if</span>(!isElectric){
             System.out.printf(<span class="hljs-string">"The %s does not work on electric, it works on petrol. \n"</span>, engineType);
        }   
    }
}
</code></pre>
<pre><code class="lang-java">Main.java

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Main</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        Sports lamborghini = <span class="hljs-keyword">new</span> Sports();
        lamborghini.engine(<span class="hljs-string">"Sports Engine"</span>);
    }
}
</code></pre>
<p>In this case, while compilation the Java compiler will not know which engine() method you are exactly calling, either of the Sports class or of the Car class, since both of these classes have these methods present with the same name as well as with the same parameters or signature. This decision can only be taken during runtime only and therefore this is called runtime polymorphism or dynamic polymorphism.</p>
<p>If you run the above program at this stage then the output will be as follows.</p>
<pre><code class="lang-bash">Output:The Sports Engine does not work on electric, it works on petrol.
</code></pre>
<ol start="2">
<li><h2 id="heading-inheritance-in-java"><strong>Inheritance in Java</strong></h2>
<p> In our example, the SUV class and the Sports class both are children of the Car class since both can inherit and access the properties (variables and methods) present inside the Car class into their class. The Car class is the parent. This is called inheritance which is the second pillar out of the 4 pillars of OOP.</p>
</li>
<li><h2 id="heading-encapsulation-in-java"><strong>Encapsulation in Java</strong></h2>
<p> As we know, the variables which are made private cannot be accessed outside the class. This is good for security purposes because we don’t want all the classes to access these variables. But what if some of the classes want to access these variables?</p>
<p> To achieve this, we can use something called getter and setter methods. With the help of these methods, we can indirectly access the private variables of the classes. This helps us in achieving Encapsulation as we are enclosing the private variable with getter and setter methods because of which we cannot directly manipulate the private variable.</p>
<p> Let’s now create two private variables inside our SUV class. The first variable is the “navigationSystemSupported” which will tell whether the car supports a geographic navigation system or not and the second variable will “noOfSeats” which as the name suggests will tell the total number of seats the SUV car will have.</p>
<p> To create getter and setter methods for these two variables, on your IDE (I’m using Intellij IDEA) right-click and select “Generate”.</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735747296331/d524f7cf-cb63-4fa6-9580-5e6344e31695.png" alt class="image--center mx-auto" /></p>
<p>This will generate the below boiler plate code for you.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735747306571/dabb0ff6-474c-4124-ba22-c8bbed1debb2.png" alt class="image--center mx-auto" /></p>
<p>This will generate the below boiler plate code for you.</p>
<pre><code class="lang-java">SUV.java

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SUV</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Car</span></span>{

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> navigationSystemSupported;
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> noOfSeats;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">isNavigationSystemSupported</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> navigationSystemSupported;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setNavigationSystemSupported</span><span class="hljs-params">(<span class="hljs-keyword">boolean</span> navigationSystemSupported)</span> </span>{
        <span class="hljs-keyword">this</span>.navigationSystemSupported = navigationSystemSupported;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getNoOfSeats</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> noOfSeats;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setNoOfSeats</span><span class="hljs-params">(<span class="hljs-keyword">int</span> noOfSeats)</span> </span>{
        <span class="hljs-keyword">this</span>.noOfSeats = noOfSeats;
    }
}
</code></pre>
<p>Now let’s access these variables from our Main class. To do this, we will first create the object of the SUV class using the new keyword. Then using the setter methods “setNavigationSystemSupported” and “setNoOfSeats” we will set the values of the variables navigationSystemSupported and noOfSeats as shown below.</p>
<pre><code class="lang-java">SUV rangeRover = <span class="hljs-keyword">new</span> SUV();
rangeRover.setNavigationSystemSupported(<span class="hljs-keyword">true</span>);
rangeRover.setNoOfSeats(<span class="hljs-number">9</span>);
</code></pre>
<pre><code class="lang-java">Main.java

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Main</span> </span>{

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{

        Sports lamborghini = <span class="hljs-keyword">new</span> Sports();
        lamborghini.engine(<span class="hljs-string">"Sports Engine"</span>);

        SUV rangeRover = <span class="hljs-keyword">new</span> SUV();
        rangeRover.setNavigationSystemSupported(<span class="hljs-keyword">true</span>);
        rangeRover.setNoOfSeats(<span class="hljs-number">9</span>);

        <span class="hljs-keyword">if</span>(rangeRover.isNavigationSystemSupported())
            System.out.printf(<span class="hljs-string">"The SUV has %d seats and supports navigation system. \n"</span>, rangeRover.getNoOfSeats());
        <span class="hljs-keyword">else</span>
            System.out.printf(<span class="hljs-string">"The SUV has %d seats and does not supports navigation system. \n"</span>, rangeRover.getNoOfSeats());
    }
}
</code></pre>
<p>In the end, we are printing the values of the private variables using getter methods “isNavigationSystemSupported” and “getNoOfSeats” as shown above.</p>
<pre><code class="lang-bash">Output:The Sports Engine does not work on electric, it works on petrol. 
The SUV has 9 seats and supports navigation system.
</code></pre>
<p>This is all about encapsulation which is the third pillar out of the 4 pillars of OOP.</p>
<ol start="4">
<li><h2 id="heading-abstraction-in-java"><strong>Abstraction in Java</strong></h2>
<p> Abstraction is the last pillar out of the 4 pillars of OOP in Java. There is usually confusion between people when it comes to encapsulation and abstraction. Encapsulation, as we have already seen above is used to hide or encapsulate the data so that our data could remain secure.</p>
<p> In Abstraction, you might have seen this definition that “Abstraction is used to hide the implementation details”. But what exactly does it mean? The definition itself seems confusing 😂. So, let me clear this out of you.</p>
<p> In Abstraction, we usually make the class abstract and some or all of its methods abstract as well (we will see how to make a class or method abstract in few moments). This means that we don’t write the implementation or the body of the methods which are abstract. We only write the method name and specify the number of parameters it will take and then we put a semicolon as shown below.</p>
</li>
</ol>
<pre><code class="lang-java">Example.java

# This is an <span class="hljs-keyword">abstract</span> <span class="hljs-class"><span class="hljs-keyword">class</span>
<span class="hljs-title">abstract</span> <span class="hljs-title">class</span> <span class="hljs-title">MyClassName</span></span>{

    # This is an <span class="hljs-keyword">abstract</span> method that doesn<span class="hljs-string">'t have a body
    abstract int newMethod();
}</span>
</code></pre>
<p>Now whichever class will inherit this abstract class, that class will always have to define or implement these methods in its class. So, here what’s happening is that the abstract method doesn’t have the implementation of the methods present in it. The implementation is present in some other classes which will inherit this abstract class. Therefore, the definition of abstraction states that it hides the implementation.</p>
<p>Abstraction can be achieved with the help of abstract keyword or by using interfaces</p>
<h3 id="heading-a-abstract-keyword"><strong>a. abstract keyword</strong></h3>
<ul>
<li><p>A method or a class can be made abstract with the help of the “abstract” keyword.</p>
</li>
<li><p>An abstract class can contain both abstract methods as well as normal methods.</p>
</li>
<li><p>An Abstract method doesn’t have implementation.</p>
</li>
<li><p>An abstract class cannot be instantiated with the new keyword (i.e we can’t create objects of the abstract class).</p>
</li>
<li><p>To use the abstract class or its methods you can inherit this class using the “extend” keyword.</p>
</li>
</ul>
<p>Also, we will define one abstract method called “carChassisAvailable” that will tell how many car chassis are available in the factory for both SUV and sports car. This abstract method will take two parameters “chassisSUV” (total chassis of SUV car available) and “chassisSports” (total chassis of sports car available) and the sum of both these values will be returned as an integer value.</p>
<pre><code class="lang-java">CarFactory.java

<span class="hljs-comment">// Abstract class</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CarFactory</span> </span>{

    <span class="hljs-comment">// Normal method</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">totalCarsManufactured</span><span class="hljs-params">()</span></span>{
        System.out.println(<span class="hljs-string">"A total of 50 cars are manufactured to date."</span>);
    }

    <span class="hljs-comment">// Abstract method with no implementation</span>
    <span class="hljs-function"><span class="hljs-keyword">abstract</span> <span class="hljs-keyword">int</span> <span class="hljs-title">carChassisAvailable</span><span class="hljs-params">(<span class="hljs-keyword">int</span> chassisSUV, <span class="hljs-keyword">int</span> chassisSports)</span></span>;

}
</code></pre>
<p>We will write the implementation for the above abstract method inside the Car class as follows. Notice that we are extending the CarFactory class using extend keyword.</p>
<pre><code class="lang-java">Car.java

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">CarFactory</span></span>{

    <span class="hljs-comment">// For the SUV and other car types</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">engine</span><span class="hljs-params">(String engineType)</span></span>{
        System.out.println(<span class="hljs-string">"Create Engine: "</span> + engineType);
    }

    <span class="hljs-comment">// For the sports car</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">engine</span><span class="hljs-params">(String engineType, <span class="hljs-keyword">int</span> secondsToReachHundred)</span></span>{
        System.out.println(<span class="hljs-string">"Create Engine: "</span> + engineType + secondsToReachHundred);
    }

    <span class="hljs-comment">// Implementation for the abstract method present in CarFactory</span>
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">carChassisAvailable</span><span class="hljs-params">(<span class="hljs-keyword">int</span> chassisSUV, <span class="hljs-keyword">int</span> chassisSports)</span> </span>{
        <span class="hljs-keyword">return</span> chassisSUV + chassisSports;
    }
}
</code></pre>
<p>Now we can call the totalCarsManufactured() and carChassisAvailable() methods by creating an object of the Car class inside the Main class.</p>
<pre><code class="lang-java">Main.class

Car totalCars = <span class="hljs-keyword">new</span> Car();
totalCars.totalCarsManufactured();
<span class="hljs-keyword">int</span> chassis = totalCars.carChassisAvailable(<span class="hljs-number">40</span>, <span class="hljs-number">50</span>);
System.out.println(<span class="hljs-string">"Total Car Chassis Available: "</span>+chassis);
</code></pre>
<p>The output of this will be look like this.</p>
<pre><code class="lang-bash">Output:A total of 50 cars are manufactured to date.
Total Car Chassis Available: 90
</code></pre>
<h3 id="heading-b-interfaces"><strong>b. Interfaces</strong></h3>
<p>Another way of achieving abstraction is by using interfaces.</p>
<ul>
<li><p>To declare interface we use the “interface” keyword.</p>
</li>
<li><p>To use the interface we use the “implements” keyword.</p>
</li>
<li><p>An interface doesn’t allow normal methods as the abstract class does. It only allows abstract methods which provide total abstraction.</p>
</li>
<li><p>All methods present inside the interface have no implementation (methods without body).</p>
</li>
<li><p>No need to specify an abstract keyword before a method. By default, all the methods specified inside the interface are abstract.</p>
</li>
<li><p>Java doesn’t support multiple inheritance. But using interfaces we can achieve it.</p>
</li>
</ul>
<p>Now here we will create a new file for our interface and will create an abstract method but this time using an interface.</p>
<p>The interface will also have a name, here we will give “CarFactoryInterface” as a name for our interface. Then we will define a “noOfAssemblyLines()” method that illustrates the number of assembly lines in the car factory and the implementation of this method will be written inside the Car class.</p>
<p>To use the interface in the Car class we have to use the implements keyword. Then we will define the noOfAssemblyLines() method inside the Car class.</p>
<pre><code class="lang-java">Car.java

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">CarFactory</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">CarFactoryInterface</span></span>{

    <span class="hljs-comment">// For the SUV and other car types</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">engine</span><span class="hljs-params">(String engineType)</span></span>{
        System.out.println(<span class="hljs-string">"Create Engine: "</span> + engineType);
    }

    <span class="hljs-comment">// For the sports car</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">engine</span><span class="hljs-params">(String engineType, <span class="hljs-keyword">int</span> secondsToReachHundred)</span></span>{
        System.out.println(<span class="hljs-string">"Create Engine: "</span> + engineType + secondsToReachHundred);
    }

    <span class="hljs-comment">// Implementation for the abstract method present in CarFactory</span>
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">carChassisAvailable</span><span class="hljs-params">(<span class="hljs-keyword">int</span> chassisSUV, <span class="hljs-keyword">int</span> chassisSports)</span> </span>{
        <span class="hljs-keyword">return</span> chassisSUV + chassisSports;
    }

    <span class="hljs-comment">// Implementation for the abstract method present in CarFactoryInterface</span>
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">noOfAssemblyLines</span><span class="hljs-params">()</span> </span>{
        System.out.println(<span class="hljs-string">"There are 2 assembly lines, one for SUV and another for Sports car."</span>);
    }
}
</code></pre>
<p>If you noticed clearly then you will see we are accessing methods from two different classes CarFactory and CarFactoryInterface at the same time This is a kind of multiple inheritance. You can create any number of interfaces as you want and implement them using commas like “public class Example implements A, B, C”. But a class can only inherit from a single parent class using the extend keyword.</p>
<p>At the end, you can call the noOfAssemblyLines using the Car object inside the Main class.</p>
<pre><code class="lang-java">Main.class

<span class="hljs-keyword">package</span> com.company;

<span class="hljs-keyword">public</span> class Main {

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{

        Sports lamborghini = <span class="hljs-keyword">new</span> Sports();
        lamborghini.engine(<span class="hljs-string">"Sports Engine"</span>);

        SUV rangeRover = <span class="hljs-keyword">new</span> SUV();
        rangeRover.setNavigationSystemSupported(<span class="hljs-keyword">true</span>);
        rangeRover.setNoOfSeats(<span class="hljs-number">9</span>);

        <span class="hljs-keyword">if</span>(rangeRover.isNavigationSystemSupported())
            System.out.printf(<span class="hljs-string">"The SUV has %d seats and supports navigation system. \n"</span>, rangeRover.getNoOfSeats());
        <span class="hljs-keyword">else</span>
            System.out.printf(<span class="hljs-string">"The SUV has %d seats and does not supports navigation system. \n"</span>, rangeRover.getNoOfSeats());


        Car totalCars = <span class="hljs-keyword">new</span> Car();
        totalCars.totalCarsManufactured();
        <span class="hljs-keyword">int</span> chassis = totalCars.carChassisAvailable(<span class="hljs-number">40</span>, <span class="hljs-number">50</span>);
        System.out.println(<span class="hljs-string">"Total Car Chassis Available: "</span>+chassis);

        totalCars.noOfAssemblyLines();
    }
}
</code></pre>
<p>Finally, this is what you will see as an output.</p>
<pre><code class="lang-bash">Output:The Sports Engine does not work on electric, it works on petrol. 
The SUV has 9 seats and supports navigation system. 
A total of 50 cars are manufactured to date.
Total Car Chassis Available: 90
There are 2 assembly lines, one <span class="hljs-keyword">for</span> SUV and another <span class="hljs-keyword">for</span> Sports car.
</code></pre>
<p>That’s all for this blog post 4 pillars of OOP (Object Oriented Programming) Paradigms in Java. Thanks for the read. If you like the content then support us on <a target="_blank" href="https://www.patreon.com/liongueststudios">Patreon</a>. Your support will surely help us in writing more of such content.</p>
]]></content:encoded></item><item><title><![CDATA[Object Oriented Programming (OOPs) concepts in Java - Part 1/2]]></title><description><![CDATA[What is Object Oriented Programming?
An Object Oriented Programming (OOP) is one of the styles of writing robust, modular, reusable, and maintainable code. A programming style that revolves around objects knows as object oriented programming (OOP). I...]]></description><link>https://liongueststudios.com/object-oriented-programming-oops-concepts-in-java</link><guid isPermaLink="true">https://liongueststudios.com/object-oriented-programming-oops-concepts-in-java</guid><category><![CDATA[Java]]></category><category><![CDATA[oop]]></category><category><![CDATA[OOP Design Principles]]></category><category><![CDATA[OOPS]]></category><category><![CDATA[Object Oriented Programming]]></category><category><![CDATA[design patterns]]></category><dc:creator><![CDATA[Sahil Bhosale]]></dc:creator><pubDate>Thu, 16 May 2024 18:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1735746400658/3a02b495-5ca3-49ba-8efc-c40f6f2407fb.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-what-is-object-oriented-programming">What is Object Oriented Programming?</h2>
<p>An Object Oriented Programming (OOP) is one of the styles of writing robust, modular, reusable, and maintainable code. A programming style that revolves around objects knows as object oriented programming (OOP). It follows the principle of software engineering called <a target="_blank" href="https://liongueststudios.com/essential-software-engineering-principles-to-follow">DRY “Don’t Repeat Yourself”</a> which helps in minimizing the repetition of code.</p>
<p>Let’s understand the object oriented programming concepts in java by combining it with a real word example.</p>
<p>The part 2 of the Object Oriented Programming series can be found here: <a target="_blank" href="https://liongueststudios.com/4-pillars-of-oop-object-oriented-programming-paradigms-in-java">4 pillars of OOP: Object Oriented Programming Paradigms</a></p>
<h2 id="heading-classes-and-objects-in-object-oriented-programminghttpsenwikibooksorgwikijavaprogrammingclassesobjectsandtypes"><a target="_blank" href="https://en.wikibooks.org/wiki/Java_Programming/Classes,_Objects_and_Types"><strong>Classes and Objects in Object Oriented Programming</strong></a></h2>
<p>Class in java allows us to define all the variables and methods at one place related to a particular entity. For example, Let’s say we want to build a car (in this case car is the entity) then for that we first have to create a blueprint or a design of that car and that will contain the car’s dimensions like width &amp; height, car’s name, model no, car’s speed, braking system, auto or manual mode, etc. These are the things that we first have to figure out related to the car, and based on this data or blueprint we can create the actual physical car.</p>
<p>In OOP, the class is where we define all of such data and because of that class is also called a blueprint. The data which is stored inside a class is in a form of variables and methods. As just after creating a blueprint or a design of a car the physical car is not created, the same way whenever we define a class in java no object will be created which means no car will be created.</p>
<p>To create a car in java we first have to create an object of that class. Creating an object simply means allocating memory to the class which we have created. This is done with the help of a new keyword in Java. After allocating the memory, the object will now have all the data (variables and methods) which we have defined inside our class. Using this object, we can manipulate the data which is present inside that object. You can create as many car objects as you want from the Car class provide different properties to every object. Refer to the figure shown below which will provide you with a pictorial view of how the classes and objects work.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735746410129/a3136738-2d95-4974-ba29-fa47e29f7d72.png" alt class="image--center mx-auto" /></p>
<p>The above image contains a Car class and using this class we are creating multiple car objects. This Car class contains two variables, company (the company who manufactured the car), speed (speed of the car) and lastly, we have a method called as getSpeed() which prints the speed of the car.</p>
<p>To create a physical car we first have to create an object of this class with the help of a new keyword in Java. So, here we have created 3 objects from this class, the first one represents the Honda Car which has a speed of 100 km/hr, the second object represents the Jeep car with a speed of 500 km/hr and the third object represents the BMW car with the speed of 800 km/hr.</p>
<p>The above example of the class and objects is representation in the code below.</p>
<pre><code class="lang-java"><span class="hljs-keyword">import</span> java.io.*;

 <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MainClass</span> </span>{

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{   

            Car honda = <span class="hljs-keyword">new</span> Car();     
            honda.company = <span class="hljs-string">"Honda"</span>;     
            honda.speed = <span class="hljs-number">100</span>;     
            honda.getSpeed();    

            Car jeep = <span class="hljs-keyword">new</span> Car();     
            jeep.company = <span class="hljs-string">"Jeep"</span>;     
            jeep.speed = <span class="hljs-number">500</span>;     
            jeep.getSpeed();     

            Car bmw = <span class="hljs-keyword">new</span> Car();     
            bmw.company = <span class="hljs-string">"BMW"</span>;     
            bmw.speed = <span class="hljs-number">800</span>;     
            bmw.getSpeed(); 
    }
}

 <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span></span>{
     String company;
     <span class="hljs-keyword">int</span> speed;

    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">getSpeed</span><span class="hljs-params">()</span></span>{ 
        System.out.println(company+<span class="hljs-string">" car's speed is "</span>+ speed+ <span class="hljs-string">" Km/hr"</span>); 
    }
 }
</code></pre>
<pre><code class="lang-bash">Output:
Honda car<span class="hljs-string">'s speed is 100 Km/hr
Jeep car'</span>s speed is 500 Km/hr
BMW car<span class="hljs-string">'s speed is 800 Km/hr</span>
</code></pre>
<p>Don’t worry if you didn’t understand each and everything which is written inside the above code. As we move along, everything will make sense.</p>
<p>A constructor tells us how much space to allocate to an object in memory. Every class in java has a constructor inside it and this constructor is called the default constructor. This default constructor is created by java and will be used to allocate memory to the object. By default, while creating an object a default constructor will be called. The reason why java calls a default constructor is that the constructor knows how much memory will be required to create an object of that class. You can also create your own constructors and a class can support any number of constructors.</p>
<p>A job of a constructor is to allocate space in memory for an object and to initialize the variables at the time of object creation. A constructor is also called a member method because it is present inside the class, it has the same name as the class name and it is represented by round brackets ‘( )‘. A constructor will always have a public access modifier because it can be called from inside any class.</p>
<pre><code class="lang-java"><span class="hljs-comment">// Constructor Example</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span></span>{

    <span class="hljs-comment">// This is a default constructor</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Car</span><span class="hljs-params">()</span></span>{

    }
}
</code></pre>
<p>Whenever you will create a class you will not see the above code for the default constructor. But it will be there and while compiling the code the java compiler will call this default constructor if the compiler doesn’t find any other constructor.</p>
<p>Now with the default constructor, you manually have to initialize your variables after the object is created which is quite hectic at times. For, example, as we have seen above, whenever we were creating the object of the Car class at that time we were assigned the company name and the speed to the Car object manually inside our code. But we can also initialize any of these parameters at the time of object creation. To achieve this you can create your own constructors. The advantage of creating your own constructor is that you can initialize any number of variables present inside the class at the time of object creation.</p>
<pre><code class="lang-java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span></span>{

    <span class="hljs-keyword">int</span> speed;
    String engineType;

    <span class="hljs-comment">// This is a default constructor</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Car</span><span class="hljs-params">()</span></span>{

    }

    <span class="hljs-comment">// Creating your own constructors</span>

    <span class="hljs-comment">// Constructor which takes one parameter</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Car</span><span class="hljs-params">(String speed)</span></span>{
          <span class="hljs-keyword">this</span>.speed= speed;
    }

    <span class="hljs-comment">// Constructor which takes two parameter</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Car</span><span class="hljs-params">(<span class="hljs-keyword">int</span> speed, String engineType)</span></span>{
         <span class="hljs-keyword">this</span>.speed= speed;
         <span class="hljs-keyword">this</span>.engineType = engineType;
    }

    <span class="hljs-comment">// You can call any one of the above constructors </span>

}
</code></pre>
<p>Here, The constructor which you will be creating will look the exact same as the default constructor, the only difference is the parameters which it will take. You can create any number of constructors as you want but the only condition is that it should take different numbers of parameters.</p>
<h3 id="heading-calling-a-constructor-in-java"><strong>Calling a constructor in Java</strong></h3>
<p>In order to create an object, we use a new keyword in Java. By using this keyword Java compiler understands that you want to create an object. But just using the new keyword will not create an object for you as you also need to know how much space you need in memory to allocate for the object.</p>
<p>For that, you have to call the constructor. So, after the new keyword, you also have to write the constructor name (either the default constructor or the constructor which you have created) by passing appropriate parameters. This will call the constructor that will then tell the Java compiler how much space the object will require and the new keyword will allocate that much space and then the entire object will be stored inside a variable.</p>
<p>Below diagram illustrates this process.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735746441144/1fb4b5ca-9ed9-4cac-b819-4b25b304e305.png" alt class="image--center mx-auto" /></p>
<p>Here, our aim is to simply create a Mercedes physical car using the Car class (blueprint). For that, we first need to know how much space we will need to store our Mercedes car object in memory (this is nothing but creating a physical car in the real world). To get the space needed to store the object we have to call the Car constructor. In this case, we are calling the Car constructor which we have created that only takes a single parameter called speed. Since we want to initialize the speed variable at the time of object creation we will call the constructor which we have created and pass in the value 100 which represents speed to the constructor.</p>
<pre><code class="lang-java"><span class="hljs-comment">// Calling constructor and creating an object</span>
<span class="hljs-keyword">import</span> java.io.*;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MainClass</span> </span>{

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{       

      <span class="hljs-comment">// Calling our custom constructor &amp; Creating the object      </span>
      Car mercedes = <span class="hljs-keyword">new</span> Car(<span class="hljs-number">100</span>);     
        mercedes.company = <span class="hljs-string">"Mercedes-Benz"</span>;     
         mercedes.getSpeed(); 
     }
 }

 <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span></span>{
     String company;
     <span class="hljs-keyword">int</span> speed;

     <span class="hljs-comment">// Creating our own custom constructor </span>
    <span class="hljs-comment">// which takes speed as a parameter </span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Car</span><span class="hljs-params">(<span class="hljs-keyword">int</span> speed)</span></span>{     
    <span class="hljs-keyword">this</span>.speed = speed; 
    } 

    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">getSpeed</span><span class="hljs-params">()</span></span>{      
    System.out.println(company+<span class="hljs-string">" car's speed is "</span>+ speed+ <span class="hljs-string">" Km/hr"</span>); 
    } 
}
</code></pre>
<pre><code class="lang-bash">Output:
Mercedes-Benz car<span class="hljs-string">'s speed is 100 Km/hr</span>
</code></pre>
<h3 id="heading-keys-points-to-remember-about-a-constructor-in-object-oriented-programming"><strong>Keys points to remember about a Constructor in object oriented programming</strong></h3>
<ul>
<li><p>A constructor is a member method.</p>
</li>
<li><p>It has the same name as the class name.</p>
</li>
<li><p>It will never return anything.</p>
</li>
<li><p>It tells us how much space in memory needs to be allocated for an object.</p>
</li>
</ul>
<h2 id="heading-keywords-used-in-object-oriented-programminghttpsenwikipediaorgwikilistofjavakeywords"><a target="_blank" href="https://en.wikipedia.org/wiki/List_of_Java_keywords"><strong>Keywords used in object oriented programming</strong></a></h2>
<p>Since the Java programming language follows the object oriented programming style of writing code, it supports various keywords like static, this, super, final, which can make our life much easier in writing and maintaining our Java code and while working with object oriented programming style. These Java keywords are explained in detail below.</p>
<h2 id="heading-static-keyword-in-java"><strong>Static Keyword in Java</strong></h2>
<p>Whenever you want to call any method in java we first have to create an object of that method and then only we can call that method. Now when will the object be created? When your program execution will start. From where the program execution starts in java? From the main method, Right. But here is a catch, main() is also a method itself, Right? Yes, then who creates an object of the main() method? The answer is no one creates an object of the main() method.</p>
<p>But we have seen that to call a method we have to create an object of that method and here we are saying we don’t create an object of the main() method. Here, the problem is main() is the starting point from where our program execution will start, so before starting the program execution how can we can an object which is not possible. This is a deadlock condition.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735746465397/894d1a6a-fe17-4ef4-a61a-e684a66f73c0.png" alt class="image--center mx-auto" /></p>
<p>To solve this deadlock, java provides us with a keyword called static. Using the static keyword you can directly call a method without creating an object in Java. That’s the reason why we use a static keyword before the main() method in our program. You can use this keyword not just for main() but for any method which you want to call without creating an object of it.</p>
<h2 id="heading-this-keyword-in-java"><strong>‘this’ keyword in Java</strong></h2>
<p>Whenever you have the same name for instance variable and for local variable then in that case we use this keyword. We have already seen this keyword in practice while we created our own constructor and passed the value of the speed variable at the time of object created.</p>
<p>Inside this class, we have declared a variable speed which is known as an instance variable. When we are creating our own constructor by passing in a variable speed as a parameter then this variable is called a local variable. Our aim here is to assign the value of local variable speed to the instance variable speed. Since both of these variables have the same name then how would you differentiate between them? This can be done with the help of this keyword.</p>
<p>Whenever we use this keyword on a variable it will tell the Java compiler that this variable is an instance variable and the value of the local variable needs to be assigned to this instance variable.</p>
<h2 id="heading-super-keyword-in-java"><strong>Super keyword in Java</strong></h2>
<p>The super keyword is present inside each and every constructor. You will not explicitly find the super() written inside the constructor but by default, it will be there. During inheritance, the super keyword will be used to call the default constructor of the parent class from the child class. If you want to call a constructor other than the default constructor of the parent class then you have to write the super keyword and pass in the parameters accordingly based on which constructor of the parent class you want to call from the child class.</p>
<pre><code class="lang-java"><span class="hljs-comment">//static keyword example</span>

<span class="hljs-keyword">import</span> java.io.*;

 <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Vehicle</span></span>{
     <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Vehicle</span><span class="hljs-params">()</span></span>{ 

    } 

    <span class="hljs-function"><span class="hljs-keyword">public</span>  <span class="hljs-title">Vehicle</span><span class="hljs-params">(<span class="hljs-keyword">int</span> i)</span></span>{     
        System.out.println(<span class="hljs-string">"You called a constructor with 1 parameter of Parent class."</span>);     
    }
 }

 <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Vehicle</span></span>{
     <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Car</span><span class="hljs-params">()</span></span>{ 

    } 

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Car</span><span class="hljs-params">(<span class="hljs-keyword">int</span> i)</span></span>{     
        <span class="hljs-comment">//This will call the second constructor of Vehicle class     </span>
        <span class="hljs-keyword">super</span>(i);     
        System.out.println(<span class="hljs-string">"Calling vehicle class constructor"</span>); }
     }

 <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MainClass</span></span>{
     <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span></span>{
         Car myCar = <span class="hljs-keyword">new</span> Car(<span class="hljs-number">10</span>);
     }
 }
</code></pre>
<pre><code class="lang-bash">Output:
You called a constructor with 1 parameter of Parent class. 
Calling vehicle class constructor
</code></pre>
<h2 id="heading-final-keyword-in-java"><strong>final keyword in Java</strong></h2>
<p>The final keyword can be used with a variable, method, or class.</p>
<p>final keyword with variable</p>
<p>Whenever you want to keep the value of the variable constant throughout the execution of the program then in that case you can use the final keyword. For example, every car will always be having 4 wheels. No matter what happens this value will never change. So what we can do is we can make the wheels variable as final in our program and give it a value of 4. Now, this value will remain the same you cannot change it later.</p>
<pre><code class="lang-java"><span class="hljs-keyword">final</span> keyword examplefinal <span class="hljs-keyword">int</span> wheels = <span class="hljs-number">4</span>;
</code></pre>
<p>final keyword with a class</p>
<p>Now let’s see why we even need to make our class as final. Let’s say, we have 3 classes, first is the Main class, second is the Car class (child class) and third is the Vehicle class (It is a parent class. The vehicles can represent cars, bikes, trucks, etc).</p>
<pre><code class="lang-java"><span class="hljs-keyword">final</span> keyword example

<span class="hljs-keyword">import</span> java.io.*;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Vehicle</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">accelerate</span><span class="hljs-params">()</span></span>{
        System.out.println(<span class="hljs-string">"move..."</span>);
    }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Vehicle</span></span>{

}

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MainClass</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span></span>{
        Car myCar = <span class="hljs-keyword">new</span> Car();
        myCar.accelerate();
    }
}
</code></pre>
<pre><code class="lang-bash">Output:
move...
</code></pre>
<p>In the above code, notice that after creating an object of the Car class even though the accelerate() method is not present inside the Car class still, we are able to access it with the help of inheritance. Now to avoid using any methods or variables of a class by another class (i.e to disable inheritance) we can make that class has final. In this case, we will make the Vehicle class final using the final keyword.</p>
<pre><code class="lang-java"><span class="hljs-comment">// final keyword example</span>

<span class="hljs-keyword">final</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Vehicle</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">accelerate</span><span class="hljs-params">()</span></span>{
        System.out.println(<span class="hljs-string">"move..."</span>);
    }
}
</code></pre>
<p>Now no other class will be able to access the methods of this class.</p>
<p>final keyword with a method</p>
<p>We will take the same example which we have used for understanding the final keyword with class, but this time we will have another accelerate() method inside our Car class as well.</p>
<pre><code class="lang-java"><span class="hljs-comment">// final keyword example</span>

<span class="hljs-keyword">import</span> java.io.*;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Vehicle</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">accelerate</span><span class="hljs-params">()</span></span>{
        System.out.println(<span class="hljs-string">"move..."</span>);
    }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Vehicle</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">accelerate</span><span class="hljs-params">()</span></span>{
        System.out.println(<span class="hljs-string">"move my car..."</span>);
    }
}

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MainClass</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String args[])</span></span>{
        Car myCar = <span class="hljs-keyword">new</span> Car();
        myCar.accelerate();
    }
}
</code></pre>
<pre><code class="lang-bash">Output:
move my car...
</code></pre>
<p>In this case, we are overriding the accelerate() method inside our Car class. The overriding a method simply means that the method is already present inside a parent class (in this case Vehicle class) and then you are also creating a method with the same name and signature inside the child class (in this case Car class). In this way, you can override the contents inside the accelerate() method of the parent class with the accelerate() method of the child class. To avoid this we can make the method present inside the Vehicle class (parent class) as final so that no other class can override its methods.</p>
<pre><code class="lang-java"><span class="hljs-comment">// final keyword example</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Vehicle</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">final</span> <span class="hljs-keyword">void</span> <span class="hljs-title">accelerate</span><span class="hljs-params">()</span></span>{
        System.out.println(<span class="hljs-string">"move..."</span>);
    }
}
</code></pre>
<p>That’s all for this blog post. In the upcoming blogs of the object oriented programming series in Java, we will see the <a target="_blank" href="https://liongueststudios.com/4-pillars-of-oop-object-oriented-programming-paradigms-in-java">4 pillars of object oriented programming paradigms</a> i.e Polymorphism, Inheritance, Encapsulation, and Abstraction of the object oriented programming concepts in java.</p>
<p>Thanks for the read. If you like the content then support us on <a target="_blank" href="https://www.patreon.com/liongueststudios">Patreon</a>. Your support will surely help us in writing more of such content.</p>
]]></content:encoded></item><item><title><![CDATA[Essential Software Engineering Principles to Follow]]></title><description><![CDATA[Software development principles are a set of specific rules and recommendations that engineers should follow during program implementation if they want to write beautiful, clear, and maintainable code. There is no magic wand that can turn a mishmash ...]]></description><link>https://liongueststudios.com/essential-software-engineering-principles-to-follow</link><guid isPermaLink="true">https://liongueststudios.com/essential-software-engineering-principles-to-follow</guid><category><![CDATA[System Design]]></category><category><![CDATA[Software Engineering]]></category><category><![CDATA[software engineer]]></category><category><![CDATA[design patterns]]></category><category><![CDATA[design principles]]></category><category><![CDATA[design and architecture]]></category><dc:creator><![CDATA[Sahil Bhosale]]></dc:creator><pubDate>Fri, 15 Mar 2024 06:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1735745365074/a59c94f3-7ac5-4ba3-a5db-e918526ace0f.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><a target="_blank" href="https://144.202.9.123/category/software-development/">Software development principles</a> are a set of specific rules and recommendations that engineers should follow during program implementation if they want to write beautiful, clear, and maintainable code. There is no magic wand that can turn a mishmash of variables, classes, and functions into perfect code, but there are a few tips and hints that can help an engineer determine if he is doing the right thing.</p>
<p>Let’s take a look at these basic recommendations. Some of the principles below are Python-specific, but most are not.</p>
<h2 id="heading-measure-twice-and-cut-once"><strong>Measure twice and cut once</strong></h2>
<p>I think that’s the most important principle of all. If you learn only one principle from this post, it should be this one. We, developers /architects/ managers people struggle with lack of attention, stupid mistakes and misprints, personal problems, bad moods, and cold coffee. None of this is relevant — the problem has to be solved. For me as an engineer, this principle means choosing the right solution to the problem, choosing the right approach to the problem, choosing the right tools to solve the problem, confidence in the built solution.</p>
<p>Choosing here means putting some thought into it, find the necessary resources, put together the right team, thinking about design, thinking about the approach, set tasks, control the result, and bear responsibility for this. This is “Engineering as is”. I think that I myself am not ready to describe it with correct words.</p>
<h2 id="heading-dont-repeat-yourself-dryhttpsenwikipediaorgwikidon27trepeatyourself"><strong>Don’t Repeat Yourself (</strong><a target="_blank" href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself"><strong>DRY</strong></a><strong>)</strong></h2>
<p>It’s a rather simple but very useful principle which says that repeating the same thing in different places is a bad idea. First of all, it is related to the necessity of further support and modification of the code. If some code fragment is duplicated in several places inside a program, there is a high probability of two catastrophic situations:</p>
<ul>
<li><p>When making even small changes to the source code, you need to change the same code in several places. It will require additional time, effort, and attention(often it is not easy).</p>
</li>
<li><p>The first item follows the second one. You or another developer from your team may accidentally miss one of the changes(it can happen simply by merging branches in vcs) and face the subsequent bugs in the application. These bugs can be frustrating to you because you have heard that such a bug has already been fixed.</p>
</li>
</ul>
<p>In this regard, there is a recommendation — if any code is found in the listing more than twice, it should be placed in a separate way. This is a general recommendation. In fact, you should think about creating a separate method even if you encounter a repetition a second time.</p>
<h2 id="heading-occams-razor"><strong>Occam’s Razor</strong></h2>
<p>It’s a very common idea, which came to programming from philosophy. The principle got its name from the English monk William of Oakham. This <a target="_blank" href="https://en.wikipedia.org/wiki/Occam%27s_razor">principle says</a>: “Entities are not to be multiplied without necessity”. In engineering, this principle is interpreted as follows: there is no need to create unnecessary entities without necessity.</p>
<p>Thus, it is always a good idea to think first about the benefits of adding another method/class/tool/process, etc. After all, if you add another method/class/tool/process etc. and you don’t get any advantages other than increased complexity, what is the point?</p>
<h2 id="heading-keep-it-simple-stupid-kisshttpsenwikipediaorgwikikissprinciple"><strong>Keep It Simple Stupid (</strong><a target="_blank" href="https://en.wikipedia.org/wiki/KISS_principle"><strong>KISS</strong></a><strong>)</strong></h2>
<p>This is a very similar principle to the above, but it has a slightly different meaning. This principle says that the code must be as simple as possible without complex structures, otherwise, it will complicate debugging and maintenance of the code.</p>
<h2 id="heading-you-arent-gonna-need-it-yagnihttpsenwikipediaorgwikiyouaren27tgonnaneedit"><strong>You Aren’t Gonna Need It (</strong><a target="_blank" href="https://en.wikipedia.org/wiki/You_aren%27t_gonna_need_it"><strong>YAGNI</strong></a><strong>)</strong></h2>
<p>A problem that many programmers suffer from. The desire to implement at once all the necessary (and sometimes even unnecessary) functionality from the very beginning of the project. That is, when a developer adds all the possible methods to the class from the very beginning and implements them, and may even never use them in the future. Thus, according to this recommendation, first of all, implement only what you need, and later, if necessary, extend the functionality. This way, you will save effort, time, and nerves on debugging the code <a target="_blank" href="https://en.wikipedia.org/wiki/Worse_is_better">which is not really needed</a>.</p>
<h2 id="heading-big-design-up-front"><strong>Big Design Up Front</strong></h2>
<p>Before starting to develop functionality, you should first think about <a target="_blank" href="https://luminousmen.com/post/basic-architecture-post">application architecture</a> and design the entire system to sufficiently small details, and only then proceed to implementation according to a predefined plan. The <a target="_blank" href="https://en.wikipedia.org/wiki/Big_Design_Up_Front">Principle</a> has a right to exist, but lately, there has been quite a lot of criticism of it. It is first of all connected with the obsolescence of the plan during designing and working out. In this connection, it is necessary to make the subsequent changes still. But it also has undeniable advantages, at correct designing, it is possible to reduce considerably cost of further debugging and correction of errors. Besides, such information systems, as a rule, are more laconic and architecturally correct.</p>
<h2 id="heading-avoid-premature-optimization"><strong>Avoid Premature Optimization</strong></h2>
<blockquote>
<p>"Premature optimization is the root of all evil (or at least most of it) in programming"</p>
<p>- Donald Knuth</p>
</blockquote>
<p>Optimization is a very correct and necessary process to speed up the program as well as to reduce system resource consumption. But everything has its own time. If optimization is carried out at the early stages of development, it can do more harm than good. First of all, it is connected with the fact that the development of an optimized code requires more time and effort for development and support. In this case, you quite often have to check the correctness of the chosen development approach at first. That’s why at first it is more profitable to use a simple but not the most optimal approach.</p>
<p>And later on, when estimating how much this approach slows down the work of an application, move on to a faster or less resource-intensive algorithm. In addition, for as long as you initially implement the most optimal algorithm, the requirements may change and the code will go to the garbage. So there is no need to waste time on premature optimization.</p>
<h2 id="heading-principle-of-least-astonishment"><strong>Principle Of Least Astonishment</strong></h2>
<p>This principle means that your code should be intuitive and obvious, and not surprise another developer when reviewing the code. For example, if the method is called “making cookies” but you get potatoes as a result, that code is bad (obviously). Besides, you should try to avoid side effects and document them if you cannot avoid them.</p>
<h2 id="heading-solid"><strong>S.O.L.I.D.</strong></h2>
<p>“<a target="_blank" href="https://en.wikipedia.org/wiki/SOLID">SOLID</a>” is actually a group of object-oriented design principles. Each letter in “SOLID” represents one of the principles, which are:</p>
<ul>
<li><p>Single responsibility states that every module or class should have responsibility for a single part of the functionality provided by the software and that responsibility should be entirely encapsulated by the class;</p>
</li>
<li><p>Open-closed states that software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification;</p>
</li>
<li><p>Liskov substitution states that the inherited class should complement, not replace, the behavior of the base class;</p>
</li>
<li><p>Interface segregation states that no client should be forced to depend on methods it does not use;</p>
</li>
<li><p>Dependency inversion says that programmers should work at the interface level and not at the implementation level.</p>
</li>
</ul>
<h2 id="heading-law-of-demeter"><strong>Law of Demeter</strong></h2>
<p>The basic idea of this principle is to divide the areas of responsibility between classes and encapsulate the logic within a class, method, or structure. Several recommendations can be distinguished from this principle:</p>
<ul>
<li><p>The classes or entities should be independent</p>
</li>
<li><p>You should try to reduce the number of connections between different classes (so-called coupling).</p>
</li>
<li><p>The associated classes must be in one module/package/directory (also known as cohesion.</p>
</li>
</ul>
<p>Following those principles, the application becomes more flexible, understandable and easy to maintain.</p>
<h2 id="heading-conclusion"><strong>Conclusion</strong></h2>
<p>Fellow developers let’s be engineers! Let’s think about design and build robust and well-implemented systems, rather than growing organic monsters. Listed principles are highly correlated and connected in their essence. Of course, I didn’t create them, but a small reminder does not hurt, at least my memory is definitely not perfect.</p>
<p>This Article is written by Kirill B and originally published at <a target="_blank" href="https://luminousmen.com/">luminousmen.com</a>. We thank Kirill for allowing us to publish this article on LionGuestStudios.com.</p>
<p>That’s all it is. Thanks for the read. If you like the best software engineering principles which we discussed then support us on <a target="_blank" href="https://www.patreon.com/liongueststudios">Patreon</a>. Your support will surely help us in writing more of such content.</p>
]]></content:encoded></item><item><title><![CDATA[Mastering Stacks: A Step-by-Step Guide to Data Structure Learning]]></title><description><![CDATA[In this blog post, we will be learning, implementing, and having a thorough introduction to stacks data structure using the C programming language.
Before understanding the stacks data structure, you should have a good understanding of arrays and lin...]]></description><link>https://liongueststudios.com/mastering-stacks-a-step-by-step-guide-to-data-structure-learning</link><guid isPermaLink="true">https://liongueststudios.com/mastering-stacks-a-step-by-step-guide-to-data-structure-learning</guid><category><![CDATA[data structures]]></category><category><![CDATA[data structure and algorithms ]]></category><category><![CDATA[algorithms]]></category><category><![CDATA[C]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[programming languages]]></category><category><![CDATA[DSA]]></category><dc:creator><![CDATA[Sahil Bhosale]]></dc:creator><pubDate>Fri, 15 Mar 2024 06:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1735825394994/12a54fd2-1dda-4f31-bf37-49f197432d77.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In this blog post, we will be learning, implementing, and having a thorough introduction to stacks data structure using the C programming language.</p>
<p>Before understanding the stacks data structure, you should have a good understanding of arrays and linked lists because only with the help of these two data structures can we implement stacks.</p>
<p>In general, for implementing any data structure, whether it is stacks, queues, trees, etc., we have to use either arrays or a <a target="_blank" href="https://liongueststudios.com/understanding-linked-lists-in-c-a-complete-walkthrough">linked list</a>.</p>
<p>If you can predict the size of the data structure (how much memory it will need) then you can use an array, otherwise, you can dynamically allocate the memory using linked lists.</p>
<h2 id="heading-concept-of-stacks-data-structure"><strong>Concept of Stacks Data structure</strong></h2>
<p>Consider a stack as a series of plates kept one above the other, vertically, from bottom to top.</p>
<p>Let’s say there are five plates, P1, P2, P3, P4, and P5, and you want to organize them in the form of a stack (i.e. putting them one above the other), then how would you do it?</p>
<p>Obviously, you will take the first plate, P1, and on top of it, you will put the plate P2, and then the next plate, and so on, until you put the last plate on, i.e. P5.</p>
<p>This whole thing forms a stack of plates which is illustrated by the following diagram.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735825222668/c742f826-ca81-49b0-b15c-9ce596b032f7.png" alt class="image--center mx-auto" /></p>
<p>And, whenever you want to access or take out a particular plate, you have to first see if the plate you want to access is at the top or not.</p>
<p>A stacks data structure is also known as last-in-first-out (LIFO) meaning the element which is inserted last into the stack will be removed first from the stack.</p>
<h2 id="heading-operations-on-the-stack"><strong>Operations on the Stack</strong></h2>
<ul>
<li><p>Push: To insert an element on to the stack.</p>
</li>
<li><p>Pop: To delete an element from the stack.</p>
</li>
</ul>
<h2 id="heading-implementation-of-a-stack"><strong>Implementation of a Stack</strong></h2>
<p>There are two ways in which we can implement the stacks data structure.</p>
<ol>
<li><h4 id="heading-using-an-array"><strong>Using an array</strong></h4>
</li>
</ol>
<p>In this representation, we will be implementing the stack using an array whose index starts with <code>0</code>.</p>
<p>Before inserting or deleting an element, there are two conditions that we have to check, the first is the overflow condition and the second is the underflow condition.</p>
<p>The overflow condition simply checks whether the stack is full or not. If it is already full, then you can’t insert a new element on to the stack.</p>
<p>The underflow condition checks whether the stack is empty or not because if the stack is already empty and if we are trying to delete an element that does not even exist as the stack is already empty, we will definitely get an error.</p>
<p>These are some corner case conditions that we have to check to make sure that our program works perfectly in any situation without any errors.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735825246566/2b663814-16df-4bc9-981f-2eaa1497a0b3.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-c"><span class="hljs-comment">// Checking the Overflow condition &amp; pushing the element onto the stack </span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">push</span> <span class="hljs-params">(<span class="hljs-keyword">int</span> item)</span> </span>{

    <span class="hljs-comment">// MAX – 1 represents the top of the stack</span>
    <span class="hljs-comment">// Checks if stack full or not    </span>
    <span class="hljs-keyword">if</span> (top == MAX - <span class="hljs-number">1</span>) {
        <span class="hljs-built_in">printf</span>(“Overflow! The Stack is already full.”);
    }
    <span class="hljs-keyword">else</span> {
        top = top + <span class="hljs-number">1</span>;
        Stack[top] = item;
    }
}


<span class="hljs-comment">// Checking the Underflow condition &amp; deleting the elements from the stack</span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">pop</span> <span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">int</span> temp;

    <span class="hljs-comment">// Checks if stack is empty or not</span>
    <span class="hljs-keyword">if</span> (top == <span class="hljs-number">-1</span>) {
        <span class="hljs-built_in">printf</span>(“Underflow! The <span class="hljs-built_in">stack</span> is already empty.”);
        <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>;
    }
<span class="hljs-keyword">else</span> {
    temp = <span class="hljs-built_in">stack</span>[top]; <span class="hljs-comment">// Storing current value of stack in temp</span>
    top = top – <span class="hljs-number">1</span>; <span class="hljs-comment">// Decrementing the pointer</span>
    }  
<span class="hljs-keyword">return</span> temp;
}
</code></pre>
<p>In the stack, when we pop an element, we are not actually deleting the element from the stack as we do in a linked list. Here, we are only decrementing the top pointer and the value inside that particular stack block will still be there.</p>
<p>The time complexity for both insertion and deletion of an element in an array takes <strong>O(1)</strong>, i.e. constant time.</p>
<ol start="2">
<li><h4 id="heading-using-the-linked-list"><strong>Using the linked list</strong></h4>
</li>
</ol>
<p>In this representation, we will be implementing the stack data structure using a linked list.</p>
<p>As we know, the linked list consists of various nodes and each node has two things. One is the data and the other is the pointer that points to the next node.</p>
<p>But, before inserting an element, we first have to create a new node and then add that node to a linked list. Here, also, we have to check the overflow and underflow conditions.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735825306567/46d3057c-1f8d-4aee-9f71-e8d40157306f.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735825328052/b95fbcb8-d582-4f44-aecd-357a47ca22cf.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735825336703/52467472-8913-4116-9973-e781e72ab667.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735825348343/f737045d-3e8b-49b7-8b09-b01edc82dec6.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735825361731/1c2ee974-5ec6-4fe3-b09b-6ebfd80e1e5d.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-c"><span class="hljs-comment">// Struct of a single node in a linked list </span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> {</span> 
  <span class="hljs-keyword">int</span> i; 
  <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> *<span class="hljs-title">link</span>;</span> 
}; 

<span class="hljs-comment">// Inserting an element </span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">push</span> <span class="hljs-params">(<span class="hljs-keyword">int</span> item)</span> </span>{

  <span class="hljs-comment">//Creating a single node </span>
  <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> *<span class="hljs-title">p</span> = (<span class="hljs-title">struct</span> <span class="hljs-title">node</span> *) <span class="hljs-title">malloc</span>(<span class="hljs-title">sizeOf</span>(<span class="hljs-title">struct</span> <span class="hljs-title">node</span>));</span> 

  <span class="hljs-comment">// Checking for Overflow condition </span>
  <span class="hljs-keyword">if</span> (p == <span class="hljs-literal">NULL</span>) { 
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Error of malloc."</span>); 
    <span class="hljs-keyword">return</span>; 
  }

  p -&gt; data = item; 
  p -&gt; link = head; 
  head = p; 
} 

<span class="hljs-comment">// Deleting an element </span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">pop</span> <span class="hljs-params">()</span> </span>{ 
  <span class="hljs-keyword">int</span> item; 
  <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> *<span class="hljs-title">p</span>;</span> 

  <span class="hljs-comment">// Checking the Underflow condition </span>
  <span class="hljs-keyword">if</span> (head == <span class="hljs-literal">NULL</span>) { 
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"Underflow"</span>); 
    <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>; 
  }

  item = head -&gt; i; 
  p = head; 
  head = head -&gt; next; 
  <span class="hljs-built_in">free</span>(p); 
}
</code></pre>
<p>The time complexity for both insertion and deletion of an element in a linked list also takes <strong>O(1)</strong>, i.e. constant time.</p>
]]></content:encoded></item><item><title><![CDATA[Understanding Linked Lists in C: A Complete Walkthrough]]></title><description><![CDATA[A linked list data structure is a collection of nodes arranged in sequential order. A data structure is nothing but how we organize and store the data in memory.
A linked list data structure consists of various nodes, and each node contains two thing...]]></description><link>https://liongueststudios.com/understanding-linked-lists-in-c-a-complete-walkthrough</link><guid isPermaLink="true">https://liongueststudios.com/understanding-linked-lists-in-c-a-complete-walkthrough</guid><category><![CDATA[data structures]]></category><category><![CDATA[data structure and algorithms ]]></category><category><![CDATA[algorithms]]></category><category><![CDATA[algorithm]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[programming languages]]></category><category><![CDATA[programming]]></category><category><![CDATA[C]]></category><dc:creator><![CDATA[Sahil Bhosale]]></dc:creator><pubDate>Thu, 04 Jan 2024 18:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1735825661433/f85a7c25-b1c0-4c79-abbf-4da2d72b0efc.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>A linked list data structure is a collection of nodes arranged in sequential order. A data structure is nothing but how we organize and store the data in memory.</p>
<p>A linked list data structure consists of various nodes, and each node contains two things: One is the data, and the other is the pointer, which will point to the next node. A linked list basically can have n nodes, and each node is connected to its next node with the help of a pointer. There are two types of linked lists: Singly linked lists (SLL) and Doubly linked lists (DLL).</p>
<p>In a Single linked list, we have a single pointer that’ll point to the next node and, therefore, it is called a singly linked list. In a DLL, we have two pointers: One pointer will point to the next node, and another will point to the previous node. That’s why we call it a doubly linked list.</p>
<p>In C programming, we use structures to create a linked list. The structure is a data type inside which we can define variables with different data types (e.g., <code>int</code>, <code>char</code>, <code>pointer</code>, etc.).</p>
<p>For creating a linked list, we’ll define the structure of our linked list (using the <code>struct</code> data type), which will represent what a single node in a linked list will look like. And then we’ll actually create a linked list by assigning memory to use using the <code>malloc()</code> function.</p>
<h2 id="heading-structure-of-a-linked-list-data-structure"><strong>Structure of a linked list Data Structure:</strong></h2>
<pre><code class="lang-c"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span>
{</span>
    <span class="hljs-keyword">int</span> data;
    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> *<span class="hljs-title">p</span>;</span>
};
</code></pre>
<p>The above code represents the structure for our linked list data structure which contains two things. First is a variable, <code>data</code>, which will hold the integer value inside it. And the second is <code>struct node *p</code>, which is a pointer that’ll point to the next node in a linked list. Here, <code>struct</code> represents the structure data type, and <code>node</code> is the name of that structure. You can give whatever name you want to the structure.</p>
<p>Now, you may be having a question: Why does the pointer have the same data type as the structure? The reason behind this is the nodes in a linked list are self-referential, which simply means the pointer in a node will point to another node of the same type (in this case, the <code>struct</code> node).</p>
<p>Note that after writing the above code, the linked list data structure won’t be created — nor will any memory be assigned to it. It’s just a template that’ll define the structure of our linked list (how our linked list will look like).</p>
<p>Now, let’s write a code to create a linked list by assigning it some memory using the <code>malloc()</code> function.</p>
<pre><code class="lang-c"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> *<span class="hljs-title">new</span> = (<span class="hljs-title">struct</span> <span class="hljs-title">node</span> *) <span class="hljs-title">malloc</span>(<span class="hljs-title">sizeof</span>(<span class="hljs-title">struct</span> <span class="hljs-title">node</span>));</span>
</code></pre>
<p>The above line of code will create a node in memory followed by a pointer called <code>new</code>, which will be pointing to the newly created node.</p>
<p>Previously, while creating a template for our node, we have given its name as a <code>struct node</code>. Thus, to allocate the memory, first we have to find out the size of that data type and then pass it to the malloc function to allocate memory for it.</p>
<p>This is how it’ll look like.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735825724871/38efe2ea-7970-4efa-9f1c-5bdf6b501fe4.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-operations-performed-on-an-sll"><strong>Operations Performed on an SLL</strong></h2>
<ol>
<li><h3 id="heading-insert-an-element-in-the-linked-list"><strong>Insert an element in the linked list.</strong></h3>
</li>
</ol>
<ul>
<li><p>Before inserting a node (element) into a linked list, you first have to create a node by using malloc.</p>
</li>
<li><p>Also, before inserting a node (element), you first have to check whether the head is pointing to the NULLor not — otherwise, it will give a segmentation fault.</p>
</li>
</ul>
<pre><code class="lang-c"><span class="hljs-keyword">if</span> (head == <span class="hljs-literal">NULL</span>) {
    <span class="hljs-keyword">return</span>;
}
</code></pre>
<ul>
<li>Consider the following example:</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735825751827/fd97da00-b6b3-4d7b-962c-17c2b556b52a.png" alt class="image--center mx-auto" /></p>
<ul>
<li>Now, there are three ways in which we can insert this node in the above linked list data structure.</li>
</ul>
<p><strong>a. Insert node at the beginning</strong></p>
<p>For inserting a node that we’ve created that’s being pointed by a pointer called <code>new</code> at the beginning of the linked list, we have to write the following code.</p>
<pre><code class="lang-c"><span class="hljs-keyword">new</span> -&gt;link = head; 
head = <span class="hljs-keyword">new</span>;
</code></pre>
<p>What this will do is: It’ll first store the starting address of the node that’s being pointed by the <code>pointer head</code> into the node that’s being pointed by <code>pointer new</code>.And then it’ll assign the address of the <code>new pointer</code> to the <code>head pointer</code>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735826664541/6a445064-a38d-46bf-8862-1cc191e3123e.webp" alt class="image--center mx-auto" /></p>
<p>If you directly assign the address of <code>new pointer</code> to the <code>head pointer</code> to insert the node at the beginning before storing the address of the <code>head pointer</code> to the <code>new pointer</code>, then the entire linked list will be lost because, in this case, there’s no pointer that’ll be pointing to that memory location where the starting node of our linked list is present.</p>
<p>This might be somewhat confusing at first to understand, so I suggest you read it a number of times and try to understand it visually.</p>
<p><strong>b. Insert node in the middle</strong></p>
<p>Now to insert the node in the middle, we first have to think about where we’ll be inserting the new node.</p>
<p>Let say we want to insert the newly created node after the second node in the above linked list. For that, we have to reach the second node and then store whatever the second node is pointing too into the <code>new-&gt;link</code>. And then, we’ll insert the node at the location.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735825810266/4eda513e-6195-4e3d-a04f-e6acd9788546.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735825824080/762afa3d-8d1f-4f1a-827b-16578cf5d3ad.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735825841410/6dec3d5f-154e-45da-a755-329c2b69ae13.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735825855078/7289d0c9-3622-4dae-990b-5d74de7d1de7.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-c"><span class="hljs-comment">//created a new pointer 't'</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> *<span class="hljs-title">t</span> = <span class="hljs-title">head</span>;</span>

<span class="hljs-comment">//Loop through till we reach the node which has value 2 in it.</span>
<span class="hljs-keyword">while</span>(t-&gt;data != <span class="hljs-number">2</span>) {
    t = t-&gt;next;
}

<span class="hljs-comment">//After finding 2 in data store whatever t is pointing too to new-&gt;link</span>
<span class="hljs-keyword">new</span>-&gt;link = t-&gt;link;

<span class="hljs-comment">//Then insert node pointed by new at the position</span>
t-&gt;link = <span class="hljs-keyword">new</span>;
</code></pre>
<p><strong>c. Insert node at the end</strong></p>
<p>For inserting a node at the end, we first have to find the end of the linked list data structure. The ending node or the last node in a linked list will always have a <code>NULL</code>(also represented as <code>\0</code>) in this next part. From the above linked list, we can say the node that has a value of 5 is the last node because its next is <code>NULL</code>.</p>
<p>To reach the end, we first have to check whether <code>t-&gt;next</code> is null of not. If null, then we can insert the node at the position. Otherwise, keep moving forward until we reach <code>t-&gt; next</code> is null.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735826713374/f6b9b30a-fe45-4489-8e42-d32a943fe8cf.webp" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735826717716/29f91382-d984-4360-a214-ed1fb29acc24.webp" alt class="image--center mx-auto" /></p>
<pre><code class="lang-c"><span class="hljs-keyword">while</span>(t-&gt;next != <span class="hljs-literal">NULL</span>) {
    t = t-&gt;next;
}

<span class="hljs-comment">// Insert newly created node at the end</span>
t-&gt;next = <span class="hljs-keyword">new</span>;

<span class="hljs-comment">// Then change its next part to '\0' which represents the end of the list.</span>
<span class="hljs-keyword">new</span>-&gt;next = <span class="hljs-string">"\0"</span>;
</code></pre>
<ol start="2">
<li><h3 id="heading-traversing-a-linked-list-data-structure"><strong>Traversing a linked list data structure</strong></h3>
</li>
</ol>
<ul>
<li><p>Traversing means visiting every node in a linked list.</p>
</li>
<li><p>For traversing, instead of using the actual pointer, which is currently pointing to the linked list, we have to create another pointer (say, t). By using that, we can traverse the list. Because if we move the current pointer, then there’s a chance we may lose some of the nodes in a linked list.</p>
</li>
<li><p>Let’s understand this with an example. Say we have a linked list, as shown below. So let’s traverse it.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735825978995/f0e324fd-db51-4ee9-b887-18a8b55a8289.png" alt class="image--center mx-auto" /></p>
<p>Example of a linked list</p>
<p><strong>Code for traversing the above linked list:</strong></p>
<pre><code class="lang-c"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> {</span>
    <span class="hljs-keyword">int</span> data;
    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> *<span class="hljs-title">next</span>;</span>
};

<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> *<span class="hljs-title">t</span>;</span>
    t = head;

    <span class="hljs-comment">//while 't' is pointing to something run this loop</span>
    <span class="hljs-keyword">while</span> (t) {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"%d"</span>\n, t-&gt;data);
        t = t-&gt;next; <span class="hljs-comment">//point to the next node.</span>
    }    
}
</code></pre>
<p><strong>Output:</strong></p>
<pre><code class="lang-bash">1
2
3
4
</code></pre>
<p>Here, first, we have a template for our node which defines what we have in our linked list data structure. Secondly, we have the main function inside, for which we’ve created a new pointer <code>t</code> of type <code>struct node</code> (a pointer that we’ll use to traverse the list) and a while loop which will print the data inside the list and then increment the pointer.</p>
<p>In this, we are now touching the <code>head</code> pointer. If we do this, then we may lose some of the nodes inside our linked list as we have no pointer at the starting position of the list.</p>
<p>After creating another pointer — say, <code>t</code> —you have a choice. You can either move <code>t</code> or <code>head</code>. it doesn’t matter which because now you have two pointers at the starting position of the linked list.</p>
<ol start="3">
<li><h3 id="heading-delete-an-element-from-the-linked-list"><strong>Delete an element from the linked list</strong></h3>
</li>
</ol>
<ul>
<li><p>Before deleting a node (element), you first have to check whether the head is pointing to the NULL or not. Otherwise, it’ll give a segmentation fault.</p>
</li>
<li><p>Whenever you’re deleting an element from the linked list that you’ve created using the malloc() function, you also have to free it up by using a free() function. It’s not compulsory to free the space after deleting an element, but it’s a good practice to do so.</p>
</li>
<li><p>Also, create a temporary pointer — say, t — to hold the node that you want to delete, and then do the other operations. Otherwise, the node that you want to delete will get lost.</p>
</li>
</ul>
<p><strong>Conditions to check:</strong></p>
<pre><code class="lang-c"><span class="hljs-keyword">if</span> (head == <span class="hljs-literal">NULL</span>){
    <span class="hljs-keyword">return</span>;
} 

<span class="hljs-keyword">if</span> (head-&gt; next == <span class="hljs-literal">NULL</span>){
    <span class="hljs-built_in">free</span>(head);
    head =<span class="hljs-literal">NULL</span>;
    <span class="hljs-keyword">return</span>;
}
</code></pre>
<p>There are three ways to delete a node in the same way as insertion in a linked list data structure.</p>
<p><strong>a. Delete the node at the beginning</strong></p>
<p>To delete the node at the beginning, create a new pointer <code>t</code>, and make it point to the head. Now, we have to move the position of the head pointer one step ahead using <code>head = head-&gt;next</code>. Then free up the <code>t</code> to delete the first element in a linked list.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735826007240/30579781-4b20-4c29-b4de-9d9bf2d1e28e.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735826015838/449b4296-ec3a-4f3b-aa5f-8c875e70dd2f.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-c"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> *<span class="hljs-title">t</span> = <span class="hljs-title">head</span>;</span>
head = head-&gt;next;
<span class="hljs-built_in">free</span>(t);
</code></pre>
<p><strong>b. Delete the node in the middle</strong></p>
<p>Here, we have to identify which node we want to delete. After that, create a new pointer, and make it point to the position where the head pointer is pointing.</p>
<p>Let say we want to delete a node whose data part has the value 3. Then, check if <code>t-&gt;next-&gt;data</code> is <code>3</code>or not. If the data part, has a value of 3, then create another pointer — say, <code>p</code> —to hold the next part of the node whose value is 3. After that, free the location that’s currently pointed by the <code>t</code> pointer, and then assign the address of pointer <code>p</code> to <code>t-&gt;next</code>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735826076567/545f27f5-0710-4100-bc04-428fcea4d042.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735826080604/7108ee6e-c14e-4495-9d57-cd90b094b6a1.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-c"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> *<span class="hljs-title">t</span> = <span class="hljs-title">head</span>;</span>

<span class="hljs-comment">// loop through till we get the value of the data part as 3 </span>
<span class="hljs-keyword">while</span> (t-&gt;next-&gt;data != <span class="hljs-number">3</span>) {
    t = t-&gt;next;
}

<span class="hljs-comment">//create another pointer which will hold the next part of the list</span>
<span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">node</span> *<span class="hljs-title">p</span> = <span class="hljs-title">t</span>-&gt;<span class="hljs-title">next</span>-&gt;<span class="hljs-title">next</span>;</span>
<span class="hljs-built_in">free</span>(t-&gt;next);
t-&gt;next = p;
</code></pre>
<p><strong>c. Delete the node at the end</strong></p>
<p>When we’re deleting a node at the end, then we have to stand two steps back and check whether the second element is null or not.</p>
<pre><code class="lang-c"><span class="hljs-keyword">while</span> ( t-&gt;next-&gt;next != <span class="hljs-literal">NULL</span>) {
    t = t-&gt;next;
}

<span class="hljs-built_in">free</span>(t-&gt;next);
t-&gt;next = <span class="hljs-literal">NULL</span>;
</code></pre>
<p>The figure below explains the above code:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735826250388/bdc52b43-c1a4-492d-b9e8-f10ea7a2a155.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735826255813/28d8f299-c339-4467-a647-e052767015bf.png" alt class="image--center mx-auto" /></p>
<p>Searching for an element in a linked list takes a long time <strong>O(n)</strong> because in the worst case, we have to traverse ‘<strong>n</strong>’ elements. And in the best case, we can find the element in the first attempt, so time complexity will be <strong>O(1)</strong>.</p>
<p>When searching for an element in an array, it takes <strong>O(1)</strong> times (considering the array is sorted); otherwise, it’ll take <strong>O(n)</strong> times only.</p>
<p>Thanks for reading and if you like the content then support us on <a target="_blank" href="https://www.patreon.com/liongueststudios">Patreon</a>. Your support will surely help us in writing more of such content.</p>
]]></content:encoded></item><item><title><![CDATA[Google’s new Fuchsia OS: Download and Build from source code]]></title><description><![CDATA[About Google Fuchsia
Google is currently working on a new operating system, code name Fuchsia OS. The project was started in 2016 and its source code was made available on the Fuchsia official website in 2019 as well as the documentation. The Fuchsia...]]></description><link>https://liongueststudios.com/googles-new-fuchsia-os-download-and-build-from-source-code</link><guid isPermaLink="true">https://liongueststudios.com/googles-new-fuchsia-os-download-and-build-from-source-code</guid><category><![CDATA[fuchsia]]></category><category><![CDATA[Google]]></category><category><![CDATA[Open Source]]></category><category><![CDATA[aosp]]></category><category><![CDATA[Android]]></category><category><![CDATA[embedded]]></category><category><![CDATA[embedded systems]]></category><dc:creator><![CDATA[Sahil Bhosale]]></dc:creator><pubDate>Sun, 31 Dec 2023 18:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1735745620269/df686d8d-4017-45cc-b818-2d784fbdc15a.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-about-google-fuchsia">About Google Fuchsia</h2>
<p>Google is currently working on a new operating system, code name Fuchsia OS. The project was started in 2016 and its source code was made available on the <a target="_blank" href="https://fuchsia.dev/">Fuchsia</a> official website in 2019 as well as the documentation. The Fuchsia project is built on top of the <a target="_blank" href="https://fuchsia.dev/fuchsia-src/concepts/kernel">Zircon</a> kernel.</p>
<p>The Google Nest Hub is the first product that runs on Fuchsia OS. The source code of Fuchsia is available to download for the public (more specifically for the developer’s community).</p>
<p>In this article, we will talk about the Fuchsia operating system, why Google is developing it?, will it replace Android? and the step-by-step process of downloading and building google’s new fuchsia operating system using the source code. The reason behind writing this article is to help developers set up the development environment for Fuchsia correctly so that they can play with the operating system as well as if the project feels interesting to them then they can make some meaningful contributions towards the growth of the project.</p>
<p>There is no doubt that the Fuchsia documentation itself is easy to get through. But there are some things in the documentation which might be confusing or difficult to understand for beginners developers and that’s where this article will come into the picture. You can perform the steps mentioned below either on a 64-bit Linux machine (any distribution will work) or on a 64-bit MAC. Windows and ARM64 Linux are not supported. I have used a Linux machine (Ubuntu) for downloading, building, and flashing the Fuchsia OS.</p>
<p>Previously, I have written an article about the <a target="_blank" href="https://liongueststudios.com/how-to-download-and-build-aosp-android-11-custom-rom-from-scratch">Android open source project (AOSP) Android</a> 11, If you are interested in that do check it out.</p>
<h2 id="heading-will-fuchsia-os-replace-android-os"><strong>Will Fuchsia OS replace android oS?</strong></h2>
<p>The Android OS is a popular and widely used OS that runs on billions of devices. But the problem with Android is that lots of manufacturers do not provide the latest updates to their previously manufactured devices (&lt; a year old). This creates a huge security issue. Also, the Linux kernel (even though it’s optimized for low-powered devices), causes some performance issues.</p>
<p>Since the Android OS itself is improving in terms of features, bug fixes, and security patches but these things are not reaching the end-users. And very few devices are able to take advantage of these things. To resolve all such issues, Google is trying to build another operating system that can provide both security as well as performance. But this time they are not just targeting phones or tablets they are also focusing on all the other types of devices like Desktops, Laptops, and MAC’s as well.</p>
<p>Ultimately, Google is a business-driven company, profits are always on the higher priority. If a product can’t generate the profits as expected then they literally scrap those products. And this is not just about Google, almost every product-based company does this. This doesn’t mean that the Android OS is going away anytime soon. The Fuchsia project is still at the early stages of its journey and only time will tell whether it will replace Android or not.</p>
<h2 id="heading-layers-of-fuchsia-os"><strong>Layers of Fuchsia OS</strong></h2>
<p>The Fuchsia OS consists of 4 layers. At the bottom, we have the kernel itself but this time Google has developed its own kernel in-house which is known as the <a target="_blank" href="https://fuchsia.dev/fuchsia-src/concepts/kernel">Zircon</a> kernel. The Zircon kernel is made open source (<a target="_blank" href="https://cs.opensource.google/fuchsia/fuchsia/+/main:/zircon/kernel">/zircon/kernel</a>).</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735745694352/1ee56b63-3247-443e-9542-0e97492d13c0.png" alt class="image--center mx-auto" /></p>
<p>On top of the zircon layer, we have the Garnet layer which contains the device drivers (networking, graphics, etc) and the software installer. This layer also contains Escher (graphics renderer), Amber (software updater), and Xi code (Text and code editor).</p>
<p>The Peridot is the third layer that handles Fuchsia’s modular application design. It also has a Ledger that is used to sync files across devices. This layer also provides artificial intelligence support called Maxwell which dynamically optimizes the performance of your device based on how you use your phone by enabling or disabling some of its features. At the top, we have the Topaz layer which supports the UI which is designed with Flutter. Armadillo is the name of the Flutter main UI.</p>
<p>To build the Fuchsia OS you will need to download 3 packages, curl, unzip, and git. You can easily download and install these packages by running the following command inside the terminal.</p>
<pre><code class="lang-bash">sudo apt-get install curl git unzip
</code></pre>
<p>Before starting out, first, we have to check whether the system on which we will be building the Fuchsia source code will be able to successfully build the Fuchsia source code or not. For that, Fuchsia provides us with a tool called ffx platform preflight. The below command will download and run the tool.</p>
<pre><code class="lang-bash">curl -sO https://storage.googleapis.com/fuchsia-ffx/ffx-linux-x64 &amp;&amp; chmod +x ffx-linux-x64 &amp;&amp; ./ffx-linux-x64 platform preflight
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735745708567/6e6b9851-5f4c-4f56-9167-d60aaa97d24c.png" alt class="image--center mx-auto" /></p>
<p>In my case, everything is in green except the 2nd and the 3rd warning. If you get the third warning you can run the two commands which are shown below the warning and it will fix that warning. After that, you can again run the above command to check the status. You can ignore the second warning, here it’s unable to detect the Nvidia graphics card but it is able to detect Intel’s integrated graphics which is fine.</p>
<h2 id="heading-download-the-fuchsia-os-source-code"><strong>Download the Fuchsia OS source code</strong></h2>
<p>Since the Fuchsia OS is not that large in size, therefore the process of downloading the Fuchsia source code will hardly take around 40 – 60 min. It’s not like Android source code which is large in size that takes almost half of the day for the download process to complete.</p>
<p>To download the Fuchsia source code, you can create a new directory anywhere on your system and then go inside that directory as shown below.</p>
<pre><code class="lang-bash">mkdir fuchsia_source &amp;&amp; <span class="hljs-built_in">cd</span> fuchsia_source
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735745741714/91321822-f261-4322-a6f3-b89c7dec3120.png" alt class="image--center mx-auto" /></p>
<p>And then run the below command which will create a directory called fuchsia and inside this directory, it will download the entire source code of the Fuchsia OS. Make sure you have around 60GB of free space.</p>
<pre><code class="lang-bash">curl -s <span class="hljs-string">"https://fuchsia.googlesource.com/fuchsia/+/HEAD/scripts/bootstrap?format=TEXT"</span> | base64 --decode | bash
</code></pre>
<p>The download process is now complete. Now you have to add the bin directory to the PATH. For that, go inside your bash file (gedit ~/.bashrc) and paste the below two lines at the bottom of the bash file.</p>
<pre><code class="lang-bash"><span class="hljs-built_in">export</span> PATH=<span class="hljs-string">"~/fuchsia_source/fuchsia/.jiri_root/bin:<span class="hljs-variable">$PATH</span>"</span>
<span class="hljs-built_in">source</span> ~/fuchsia_source/fuchsia/scripts/fx-env.sh
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735745769776/c31ee850-3db1-4554-a0b4-55ca9221ec44.png" alt class="image--center mx-auto" /></p>
<p>After that, update the environment variables using source.</p>
<pre><code class="lang-bash"><span class="hljs-built_in">source</span> ~/.bashrc
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735745823548/857b29a0-b6f2-4503-bf1c-5ca404a146bd.png" alt class="image--center mx-auto" /></p>
<p>To check if everything is set up correctly, go inside the fuchsia directory using cd and run the below commands. If you get no errors which mean everything has been set up correctly.</p>
<pre><code class="lang-bash">// Run below commands inside fuchsia directory
jiri <span class="hljs-built_in">help</span>
fx <span class="hljs-built_in">help</span>
</code></pre>
<h2 id="heading-configure-and-build-the-fuchsia-os-source-code"><strong>Configure and Build the Fuchsia OS source code</strong></h2>
<p>For building the source code, we first have to select the product (core or workstation) and the board (x64 or qemu-x64). The core is a product that has minimum features were as the workstation product will have all the features. You can select any one of them, for this tutorial we will use the workstation product with the qemu-x64 board. To set the workstation and the qemu-64 as a target run the following command.</p>
<pre><code class="lang-bash">fx <span class="hljs-built_in">set</span> workstation.qemu-x64
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735745928901/754c6f51-5287-41bb-8cfe-c41fdb8e0b44.png" alt class="image--center mx-auto" /></p>
<p>After selecting the target device, we can start building the Fuchsia source code as follows.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735745970412/63571857-c1f9-40ba-8314-b3849d34fedc.png" alt class="image--center mx-auto" /></p>
<p>The building process will take around 1-2hrs to complete.</p>
<h2 id="heading-flashing-fuchsia-os-on-the-fuchsia-emulator-femu"><strong>Flashing Fuchsia OS on the Fuchsia Emulator (FEMU)</strong></h2>
<p>We have now reached the final stage where we will be flashing and running the Fuchsia OS on the emulator. The emulator which we will be using is known as Fuchsia Emulator (FEMU). This emulator is available inside the source code itself.</p>
<p>If you don’t want to connect the fuchsia emulator with the external network then you can simply run the following command. Make sure that you are inside the fuchsia directory while running the below command.</p>
<pre><code class="lang-bash">fx vdl start -N
</code></pre>
<p>If you want to provide internet access to the fuchsia emulator then you can run the below command.</p>
<pre><code class="lang-bash">fx vdl start -N -u scripts/start-unsecure-internet.sh
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735746035714/d7d578e3-f3f3-4b4f-8452-4589a60d760c.png" alt class="image--center mx-auto" /></p>
<p>After the operating system boots up, this is how it will look like.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735746032352/dd80a343-8f93-4605-a167-70a17a6daa60.png" alt class="image--center mx-auto" /></p>
<p>You can play with the terminal by running the Fuchsia command and access any website using the built-in web browser.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735746062160/ee3474f0-ea34-431b-b1fa-b017bd7ba24d.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735746079360/00464db6-b176-45dc-8932-6c16e0ce5f89.png" alt class="image--center mx-auto" /></p>
<p>To learn more about the Fuchsia commands and how it works you can visit <a target="_blank" href="https://fuchsia.dev/fuchsia-src/get-started/explore_fuchsia">Explore Fuchsia</a> on fuchsia.dev website.</p>
<p>This was the entire process of downloading, building, and flashing Fuchsia OS on the emulator. Thanks for the read. If you like the content then support us on <a target="_blank" href="https://www.patreon.com/liongueststudios">Patreon</a>. Your support will surely help us in writing more of such content.</p>
]]></content:encoded></item><item><title><![CDATA[How to Download and Build AOSP Android 11 custom ROM from scratch]]></title><description><![CDATA[What is AOSP?
Android Open Source Project (AOSP) is a project maintained by Google from where we can get the source code for the Android operating system. Since it is an open-source project anyone can download the android source code, compile it onto...]]></description><link>https://liongueststudios.com/how-to-download-and-build-aosp-android-11-custom-rom-from-scratch</link><guid isPermaLink="true">https://liongueststudios.com/how-to-download-and-build-aosp-android-11-custom-rom-from-scratch</guid><category><![CDATA[embedded android]]></category><category><![CDATA[Open Source]]></category><category><![CDATA[Android]]></category><category><![CDATA[android app development]]></category><category><![CDATA[aosp]]></category><category><![CDATA[embedded]]></category><category><![CDATA[embedded systems]]></category><dc:creator><![CDATA[Sahil Bhosale]]></dc:creator><pubDate>Sun, 24 Dec 2023 18:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1735742855520/cc2806c6-b54a-4b8c-8efd-cb801fbef8ec.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-what-is-aosp">What is AOSP?</h2>
<p><a target="_blank" href="https://source.android.com/setup/develop">Android Open Source Project (AOSP)</a> <a target="_blank" href="https://source.android.com/setup/develop">is a project maintained by Google</a> from where we can get the source code for the Android operating system. Since it is an open-source project anyone can download the android source code, compile it onto their machine, and can easily make some modifications to the source code. Currently, the latest version of Android is Android R (also known as Android 11 or Red Velvet Cake). You can get the source code for any version of android from the AOSP like Android R, Q, P, etc.</p>
<p><strong>Video 1:</strong> Download and Build AOSP Android 11 custom ROM from AOSP on Linux from scratch</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://youtu.be/2zVARyqTFtE">https://youtu.be/2zVARyqTFtE</a></div>
<p> </p>
<p>In this blog post, we will see how we can download the source code for Android 11, how to build or compile it and run it on the emulator or on the physical device using the Android Open Source Project.</p>
<p>There might be few reasons why you may need to download the android 11 source:</p>
<ul>
<li><p>To Flash custom ROM on to your phone: If your phone’s manufacturer is not providing the latest android released for your phone and you are eager to use and test the latest feature provided by the android platform then in that case you may need to download the android source code.</p>
</li>
<li><p>Contributing to Android Open Source Project (AOSP): If you are a developer and looking to contribute code to the AOSP, then in that case you get the android source code.</p>
</li>
<li><p>Creating your own version of android: In case, if you want to make your own version of Android like Cyanogenmod or something like that then also you can get the source code.</p>
</li>
</ul>
<h3 id="heading-system-requirements"><strong>System Requirements:</strong></h3>
<p>To download and build the AOSP project you should have a 64-bit version of either Linux (18.04 or greater) or Mac OS installed. Here, RAM plays a very crucial role when it comes to building / compiling the android source code. </p>
<p>According to the official docs, it is recommended that you should have at least 16GB of RAM. If you want to build an older version of android then 8GB may work for you. I tried android 10 (android-10.0.0_r20) with 8GB of RAM but I faced “java heap out of memory error” but it got fixed when I increased the Java heap size.</p>
<p>But when I was building for Android 11 (android-11.0.0_r17) with 8GB of RAM at that time also I faced the same memory-related error. So, I tried many different things but the issue still exists. Then I increased the RAM to 16GB and the build was successful.</p>
<ul>
<li><p>OS: A 64-bit Linux or Mac, Windows is not supported.</p>
</li>
<li><p>Ram: 16 GB preferred </p>
</li>
<li><p>Free Disk Space: 250 – 300GB free space</p>
</li>
</ul>
<p>The Open JDK comes prebuilt with AOSP, so no need to install it separately.</p>
<h3 id="heading-installing-required-packages"><strong>Installing Required packages:</strong></h3>
<p>Just copy the below command and paste it into your terminal. This will install all these packages which are required for working with android source code.</p>
<pre><code class="lang-bash">sudo apt-get install git-core gnupg flex bison build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z1-dev libgl1-mesa-dev libxml2-utils xsltproc unzip fontconfig
sudo apt update 
sudo apt upgrade
</code></pre>
<h1 id="heading-downloading-the-android-11-source-code"><strong>Downloading the Android 11 Source Code</strong></h1>
<p>The process of <a target="_blank" href="https://source.android.com/setup/build/downloading">downloading the Android Open Source Project (AOS</a><a target="_blank" href="https://source.android.com/setup/build/downloading">P) source code takes quite a while to complete, so make sure that you have a good internet</a> connection so that you can finish it in less time. On average it should take you around <strong>7–10 hrs</strong> to download the android source code.</p>
<p>Let’s first create a directory with the name <strong>aosp11</strong> inside which you can download the android 11 source <a target="_blank" href="https://source.android.com/setup/build/downloading">code.</a></p>
<pre><code class="lang-bash">//Created a new directory and <span class="hljs-built_in">cd</span> into it 
mkdir aosp11 &amp;&amp; <span class="hljs-built_in">cd</span> aosp11
</code></pre>
<p>You are now inside the <strong>aosp11</strong> directory. Now you have to configure the <strong>name</strong> and <strong>email address</strong> associate with your account by running the following commands. Here, replace words inside the quotation with your real git accounts details.</p>
<pre><code class="lang-bash">git config --global user.name <span class="hljs-string">"Your Name"</span> 
git config --global user.email <span class="hljs-string">"you@example.com"</span>
</code></pre>
<p>Let’s now download the repo tool. From your <strong>home directory</strong> run the following commands one by one.</p>
<pre><code class="lang-bash"><span class="hljs-built_in">cd</span> .. //Currently, you are <span class="hljs-keyword">in</span> aosp11 dir, this will take you back to home dir 
mkdir ~/bin 
PATH=~/bin:<span class="hljs-variable">$PATH</span> 
curl https://storage.googleapis.com/git-repo-downloads/repo &gt; ~/bin/repo 
chmod a+x ~/bin/repo
</code></pre>
<p>To get the Android 11 source code we first have to initialize the <a target="_blank" href="https://stackoverflow.com/questions/25046570/what-does-repo-init-and-repo-sync-actually-do">repo tool</a> inside our current working directory. This will create a <strong>.repo</strong> subdirectory inside our current working directory.</p>
<p>Here, you can even specify the branch which you want to download using the <strong>-b</strong> tag (this is optional). If you don’t specify the branch then it will by default take the <strong>master</strong> branch.</p>
<p>But in this case, we want to get the Android 11 source code so we will specify the <strong>android 11</strong> branch using the <strong>-b</strong> tag and initialize the repo.</p>
<p>Now to get the tag for android 11, we have to visit the <a target="_blank" href="https://source.android.com/setup/start/build-numbers#source-code-tags-and-builds">source code tags and builds</a> page available on android official docs. This is where you choose which android version to download. In this case, we will download android 11, you can choose whatever version of android you want to by choosing the appropriate tag from the above page.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735743430010/829ecf49-d76b-47b9-ad42-14efc6bbff75.webp" alt class="image--center mx-auto" /></p>
<pre><code class="lang-bash">repo init -u https://android.googlesource.com/platform/manifest -b android-11.0.0_r17
</code></pre>
<p>The <strong>android-11.0.0_r17</strong> is the latest version of Android 11 available for download. In the future, if some other versions of android are released (android S or android 12, etc) then this is the command where you have to make changes by selecting the appropriate tag and then initialize the repo with the above command.</p>
<p>During repo init, if you face any python related issues like <strong>/usr/bin/env: ‘python’: No such file or directory</strong> then to fix it run the following command.</p>
<pre><code class="lang-bash">sudo ln -s /usr/bin/python3 /usr/bin/python
</code></pre>
<p>After some time, you should see a message <strong>“Enable color display in this user account?”</strong> Select <strong>y</strong> and press enter.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735743471270/44dc38f8-c02b-4199-bd47-eae6a284d3c6.webp" alt class="image--center mx-auto" /></p>
<p>You should see another message “repo has been initialized in /home/user/aosp11” which confirms that you have initialized the <a target="_blank" href="http://repo.Now">repo.Now</a> is the time to download the AOSP Android 11 source code locally to your working directory. To download run the following command. Make sure you are inside the asop11 directory and then run this command.</p>
<pre><code class="lang-bash">repo sync
</code></pre>
<p>For me, it took 7hrs to download the Android 11 source code. For you, it might take even more, so sit back and relax till it completes. In the end, you should see the message which is shown in the image below.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735743654050/beac75d2-c160-4e8b-9e1c-a2295b990efe.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-compiling-or-building-the-android-11-source-code"><strong>Compiling or Building the Android 11 Source Code</strong></h2>
<p>This is the crucial part when it comes to building the android source code as you might face some error during this process. But no worries, I will also provide solutions to these common errors.</p>
<p>First, you have to initialize the environment.</p>
<pre><code class="lang-bash"><span class="hljs-built_in">source</span> build/envsetup.sh
</code></pre>
<p>Then you have to choose a target device (using lunch command) on which you will be installing the operating system like mobile, watch etc. In this case we will be building android 11 for a 64-bit android mobile device.</p>
<pre><code class="lang-bash">lunch
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735743674127/ac995b83-7521-4973-abae-dceee3d5d537.png" alt class="image--center mx-auto" /></p>
<p>Now, we will select the second option aosp_arm64-eng from the list which is a 64bit version of android 11 for a mobile device. You can even choose option1 for 32-bit devices. After selecting the 2nd option it will show you the exact details related to that build.</p>
<p>To start the compilation or building process run the below command.</p>
<pre><code class="lang-bash">m -j4
</code></pre>
<p>The ‘m’ stands for “make” which will compile the source code into an appropriate machine code. The ‘j4’ represents no. of CPU cores (here, 4 means 4 cores) which you want to allocated for building. You can even increase it to a higher number depending upon how many cores your computer has. To check for no. of the core run nproc in the terminal.</p>
<p>During this process, your system might get hanged or become unresponsive which is completely normal so don’t panic, leave it as it is. Depending upon your system configuration this process will take you around 10-15hrs to build the android open source project code.</p>
<h3 id="heading-common-errors-during-aosp-building-process-and-their-solution"><strong>Common errors during AOSP building process and their solution</strong></h3>
<ul>
<li>libncurses.so.5: cannot open shared object file: No such file or directory:</li>
</ul>
<p>Solution: Run this command sudo apt-get install libncurses5 and then try to recompile the source code.</p>
<ul>
<li>Exception in thread “main” java.lang.OutOfMemoryError: Java heap space:</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735743718536/99ac0bee-3275-4ef4-a8af-6ecc5d0fc711.png" alt class="image--center mx-auto" /></p>
<p><strong>Solution:</strong> Run this command export <em>JAVA</em>OPTIONS=-Xmx8g this will increase your heap size to 8GB. and recompile the source code. If you still face the error then run the make clean command and then again try recompiling the source code. This should solve your problem.</p>
<p>Finally, if you get the below message then congratulations you have made it. You now have successfully build the android 11 custom rom and now is the time to flash it onto your phone or emulator.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1735743769401/8b3cbaf2-43e4-4f7f-8000-46918629dde5.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-flashing-aosp-android-11-custom-rom-on-emulator"><strong>Flashing AOSP android 11 custom rom on emulator</strong></h2>
<p><strong>Video 2:</strong> Flashing AOSP android 11 custom rom on emulator</p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://youtu.be/7Z-uv1N2Pkk">https://youtu.be/7Z-uv1N2Pkk</a></div>
<p> </p>
<p>In order to start the emulator make sure that the virtualization option is enabled on bios. The emulator is already been added to your path while building the aosp project. So, just the below command and it will flash the android 11 custom rom on the emulator.</p>
<pre><code class="lang-bash">emulator
</code></pre>
<h2 id="heading-flashing-aosp-android-11-custom-rom-on-a-physical-device"><strong>Flashing AOSP android 11 custom rom on a physical device</strong></h2>
<p>In order to perform this process you first have to unlock your phone’s bootloader by going inside “Developers Options” inside settings. Note that this will void your phone’s warranty, do it on your own risk. If you are unable to see the “Developers Options” then click on “Build Number” inside settings under About phone for 5 times to enable this option. After that install the below two packages.</p>
<p>Now connect your phone to your computer and run the below commands.</p>
<p>Note that this will wipe out your data completely and flash the android 11 ROM on your device. There is no guarantee that this will successfully flash your device. And if it fails while flashing then your device will be of no use anymore. Make sure that you don’t your primary phone for flashing.</p>
<pre><code class="lang-bash">adb reboot bootloader
fastboot flashall -w
</code></pre>
<p>That’s all it is. Thanks for the read. If you like the content then support us on <a target="_blank" href="https://www.patreon.com/liongueststudios">Patreon</a>. Your support will surely help us in writing more of such content.</p>
]]></content:encoded></item></channel></rss>