Code Alchemy: Mastering the Basic Principles of Programming
Published: 2023-08-02 16:08:08

<hr />
<h1 id="heading-prerequisite"><strong>Prerequisite:</strong></h1>
<p>To benefit from this article, it's essential to have a solid foundation in programming fundamentals – have a basic understanding of programming concepts, and be familiar with JavaScript or a similar programming language.</p>
<p>For newbies, it's recommended you have a basic understanding of the following concepts:</p>
<ul>
<li><p>Basic Syntax</p>
</li>
<li><p>Control Flow</p>
</li>
<li><p>Functions</p>
</li>
<li><p>Variables and Data Types</p>
</li>
<li><p>Object-Oriented Programming (OOP) Concepts (Optional): Only if you intend to delve further into more advanced coding principles.</p>
</li>
</ul>
<p>If you are already familiar with these fundamental concepts, you are well-prepared to embark on your journey. Ultimately, mastering the basic coding principles will enhance your coding skills and elevate the quality of your work. In the end, you will become a more efficient and effective programmer.</p>
<hr />
<h1 id="heading-introduction"><strong>Introduction</strong></h1>
<p>In recent times, there has been an unprecedented influx of aspiring programmers into the tech space. Each of these programmers is entering with blazing enthusiasm and the aspiration to make a personal digital mark on this rapidly evolving landscape. Opportunities to achieve this feat, are not short in any way. With an already existing plethora of technologies and the frequent emergence of new tools, frameworks, libraries, and platforms, the world of tech is perfectly poised for the taking. However, for you to become an endearing member of this community, you should be exceptional at your craft. Yes! Excellent craftsmanship unlocks rapid growth and ascension.</p>
<p>After reading that, you may wonder, “Isn’t excellent craftmanship only attainable by consistently honing your craft?”. Yes, it is! As the saying goes – practice makes perfect. However, I have a crucial follow-up question: can bad practice lead to perfection?</p>
<p>The answer is obviously no. You only become perfect when you practice the beneficial habits and avoid anything detrimental. Thus, not just practice makes you perfect, but good practice makes you perfect. Excellent craftsmanship is not just a work of consistency but also involves understanding the requirements, learning them, and consistently perfecting your skill based on these requirements. In the same vein, as a programmer, coding consistently is beneficial, but coding without the understanding and application of the basic principles is a redundant feat.</p>
<p>Understanding and applying the basic principles will give you the one thing that distinguishes excellent programmers from mediocre ones – refinement. Refinement, not just as a stylistic addition; but refinement that yields tangible benefits throughout your entire software development journey. This refinement will make writing clean, modular, and maintainable code that promotes efficacy, simplifies collaboration, and enhances the longevity of projects, your second nature.</p>
<p>Although, the complexity of applications has increased, adherence to sound coding principles, will help you navigate these uncharted waters. Ultimately, you will cultivate a disciplined mindset, become skilled in architecting elegant solutions, learn to troubleshoot with meticulous precision, and relish in the art of code optimisation.</p>
<p>To get you there, this article is going to address the 6 basic principles, every programmer should know. Though not exhaustive, these principles are of immense significance with their application extending across many other principles. Mastering these basic principles will open you up to further learning and understanding of other existing principles. After all, honing your coding craft consists of consistent learning and good practices that yield daily growth.</p>
<hr />
<h1 id="heading-your-basic-guideline-for-refinement"><strong>Your Basic Guideline for Refinement</strong></h1>
<h2 id="heading-1-cd-comments-and-documentation"><strong>1.    CD – Comments and Documentation</strong></h2>
<p>One of the most important end targets of writing code is to write clean, readable, and easily comprehended code. This does not apply just to you, the writer, but also to everyone who sees your code. It is in this light, commenting on your code and documentation finds its purpose.</p>
<p>Considering that you will be sharing your code with other programmers and end up working in teams when employed, it is necessary to develop the habit of effective communication. Comment on your code, grouping them into sections, stating the purpose, class, and any assumptions made.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// This function is for the addition of numbers</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">addNumbers</span>(<span class="hljs-params">a, b</span>) </span>{
    <span class="hljs-keyword">return</span> a + b;
}

<span class="hljs-keyword">let</span> a = <span class="hljs-number">5</span>
<span class="hljs-keyword">let</span> b = <span class="hljs-number">3</span>

<span class="hljs-built_in">console</span>.log(addNumbers(a, b)); <span class="hljs-comment">// This is to test the function in the console.</span>
</code></pre>
<p>For larger projects and shared codebases, you can use JsDoc, a widely adopted documentation for JavaScript. JsDoc provides you with a standardised way to document your code, add function signatures, parameters, return types and add descriptions for further explanation. It helps streamline code comprehension.</p>
<p>You should also add a ReadMe file to your project, to provide an overview of the project. In the ReadMe file, you can provide instructions for installation and usage and any other necessary additional documentation.</p>
<p>Understand that, writing clean and readable code lies at the core of effective communication. Furthermore, comments and documentation serve as the building blocks for seamless collaboration.</p>
<h2 id="heading-2-dry-dont-repeat-yourself"><strong>2.    DRY – Don’t Repeat Yourself</strong></h2>
<p>This is a fundamental concept that advocates for the avoidance of code duplication. In essence, in the code base, every logic should be single and obvious. As a programmer, you should aim at creating reusable and maintainable code. This is the basis for efficient and reliable software applications. Duplicated code makes room for multiple errors to be made since any change to the code will have to be implemented in various places. It also makes debugging tedious and time-consuming.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Assuming we need to apply this function in multiple places</span>
<span class="hljs-comment">// We can optimise the code to be applicable in multiple places without</span>
<span class="hljs-comment">// duplicating the code</span>

<span class="hljs-comment">// This function is for the addition of numbers</span>
<span class="hljs-keyword">let</span> sum = <span class="hljs-function">(<span class="hljs-params">a, b</span>) =&gt;</span> a + b; <span class="hljs-comment">// reduced function to an arrow function </span>
<span class="hljs-comment">//and declared it</span>

<span class="hljs-comment">//This allows me to call the function in multiple places </span>
<span class="hljs-comment">//without repeating the code for the function</span>

<span class="hljs-keyword">let</span> answer1 = sum(<span class="hljs-number">14</span>, <span class="hljs-number">25</span>);
<span class="hljs-keyword">let</span> answer2 = sum(<span class="hljs-number">50</span>, <span class="hljs-number">-23</span>)

<span class="hljs-built_in">console</span>.log({<span class="hljs-attr">answer1</span>: answer1},{<span class="hljs-attr">answer2</span>: answer2}); <span class="hljs-comment">// This is to test </span>
<span class="hljs-comment">//the function in the console</span>
</code></pre>
<h2 id="heading-3-kiss-keep-it-simple-stupid"><strong>3.    KISS – Keep It Simple, Stupid!</strong></h2>
<p>This principle is aimed at addressing the unimportance of writing complex codes. Codes should be simple, straight forward and easily understandable. Avoid overengineering and simplify your solutions whilst maintaining their functionality. This is not to advocate for oversimplification, especially at the cost of functionality. As a programmer, you should strive towards striking a balance between simplicity and functionality.</p>
<p>This can be achieved in 6 simple steps;</p>
<ul>
<li><p><strong>Take Your Time to Plan:</strong> Study and understand the requirements to design an optimal solution and pick out descriptive and self-explanatory names for variables, functions, and classes.</p>
</li>
<li><p><strong>Modularise your Code:</strong> Break down monolithic functions into smaller, organised, and focused ones which have distinct responsibilities.</p>
</li>
<li><p><strong>Programme as if You are Sharing:</strong> When writing your code, aim for readability and correctness first, before deciding to optimise. Code with others in mind, intending for your code to easily communicate your point across without confusion. Do not over-optimize and/or optimize prematurely.</p>
</li>
<li><p><strong>Avoid Redundancy:</strong> Review your code regularly, in search for duplicated and/or unnecessary codes. Such codes should be removed when noticed.</p>
</li>
<li><p><strong>Explore In–Built Features:</strong> Most coding languages, offer in-built functionalities that allow you to simplify your code. Learn to use them to streamline your code. </p>
</li>
<li><p><strong>Peer Review:</strong> Share your code with other programmers and ask for feedback.</p>
</li>
</ul>
<h2 id="heading-4-yagni-you-aint-gonna-need-it"><strong>4.    YAGNI – You Ain’t Gonna Need It</strong></h2>
<p>In our bid to keep to the DRY principle by planning ahead, most programmers plan and code for unlikely scenarios. This results in having a lot of redundant codes in your codebase. This principle aims at curbing that by ensuring that you only code for scenarios you need. If other scenarios you did not plan for come up, then you review your code and add to it. Do not create code in anticipation of what has not happened yet, which ends up making your code bloated. Understand the requirement of the app you are coding and provide simple solutions. Do not overthink your solution and leave every outlying parameter, as an add-on for later. This will reduce development time and make your code more manageable.</p>
<h2 id="heading-5-wont-when-operational-no-tweaking"><strong>5.    WONT – When Operational, No Tweaking</strong></h2>
<p>This principle simply means, if it works, DON’T TOUCH IT! Sometimes, finding a solution does not come easily to you and you will end up tinkering with your code a bit. When you finally get a working solution, please let it be. Do not try to tweak it or further simplify it and end up breaking it. Indicate boldly above that code section, when sharing it, that the code should not be touched. I am not saying do not test the code, please run test on the code as often as you can but DO NOT TOUCH IT! This also applies when people send their work to you. Do not be eager to prove how intelligent you are and end up breaking their code, especially if it has been tagged as “Do Not Touch” or you do not understand how the code works.</p>
<h2 id="heading-6-tcrr-test-check-refine-repeat"><strong>6.    TCRR – Test, Check, Refine, Repeat</strong></h2>
<p>Testing is a crucial part of software development. It allows you to know if your code is behaving as expected and is reliable in the long term. Testing allows you to fix bugs, check for the functioning of new features and ensure there is no loss of function as the code gets updated. Run regular isolated tests on every function and/or block of code you write before running tests on the entire app/software created.</p>
<p>There are multiple levels involved in testing, such as unit tests, integration tests, and end-to-end tests, which assess your app at various levels for functionality and correctness.</p>
<p>Most of the major languages have testing frameworks that you can also use. JavaScript has Jest, Mocha, and Jasmine; whereas python has PyTest, DocTest, Robot, and Testify. Use these to continuously test your code, check for bugs, refine your code, and repeat the whole process until you produce a high-quality application that provides elegant solutions.</p>
<hr />
<h1 id="heading-conclusion"><strong>Conclusion</strong></h1>
<p>In your pursuit of excellence, endeavour to master these principles. The tech landscape will keep evolving rapidly, but your focus should be becoming excellent in your craft. From embracing effective communication through comments and documentation to prioritizing simplicity and avoiding over-engineering, and regularly testing your work, each principle stated above, contributes to code refinement and longevity. Keep learning and keep practicing the right way and you can be rest assured that wherever you find yourself, your work will make you stand out.</p>
<p>By internalizing and applying these principles, you will transform into a true code alchemist – transforming basic code to meet the gold standards and capable of crafting elegant solutions which leave a lasting impact.</p>
</br><p style="color:#757CF9;">This article is published w/ <a target="_blank" href="https://scattr.io?ref=dev">Scattr  ↗️</a></p>