Code Alchemy: Mastering the Basic Principles of Programming
Refining Your Creative Craftmanship
Prerequisite:
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.
For newbies, it's recommended you have a basic understanding of the following concepts:
Basic Syntax
Control Flow
Functions
Variables and Data Types
Object-Oriented Programming (OOP) Concepts (Optional): Only if you intend to delve further into more advanced coding principles.
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.
Introduction
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.
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?
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.
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.
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.
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.
Your Basic Guideline for Refinement
1. CD – Comments and Documentation
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.
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.
// This function is for the addition of numbers
function addNumbers(a, b) {
return a + b;
}
let a = 5
let b = 3
console.log(addNumbers(a, b)); // This is to test the function in the console.
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.
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.
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.
2. DRY – Don’t Repeat Yourself
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.
// Assuming we need to apply this function in multiple places
// We can optimise the code to be applicable in multiple places without
// duplicating the code
// This function is for the addition of numbers
let sum = (a, b) => a + b; // reduced function to an arrow function
//and declared it
//This allows me to call the function in multiple places
//without repeating the code for the function
let answer1 = sum(14, 25);
let answer2 = sum(50, -23)
console.log({answer1: answer1},{answer2: answer2}); // This is to test
//the function in the console
3. KISS – Keep It Simple, Stupid!
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.
This can be achieved in 6 simple steps;
Take Your Time to Plan: Study and understand the requirements to design an optimal solution and pick out descriptive and self-explanatory names for variables, functions, and classes.
Modularise your Code: Break down monolithic functions into smaller, organised, and focused ones which have distinct responsibilities.
Programme as if You are Sharing: 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.
Avoid Redundancy: Review your code regularly, in search for duplicated and/or unnecessary codes. Such codes should be removed when noticed.
Explore In–Built Features: Most coding languages, offer in-built functionalities that allow you to simplify your code. Learn to use them to streamline your code.
Peer Review: Share your code with other programmers and ask for feedback.
4. YAGNI – You Ain’t Gonna Need It
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.
5. WONT – When Operational, No Tweaking
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.
6. TCRR – Test, Check, Refine, Repeat
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.
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.
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.
Conclusion
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.
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.