{"id":169,"date":"2023-06-28T06:37:33","date_gmt":"2023-06-28T06:37:33","guid":{"rendered":"http:\/\/localhost\/embeddedwala\/?p=169"},"modified":"2023-07-27T04:15:53","modified_gmt":"2023-07-27T04:15:53","slug":"how-to-write-the-clean-code","status":"publish","type":"post","link":"https:\/\/embeddedwala.com\/beta\/blogs\/embedded-c\/how-to-write-the-clean-code\/","title":{"rendered":"How To Write The Clean Code"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"169\" class=\"elementor elementor-169\" data-elementor-post-type=\"post\">\n\t\t\t\t\t\t\t\t\t<section data-particle_enable=\"false\" data-particle-mobile-disabled=\"false\" class=\"elementor-section elementor-top-section elementor-element elementor-element-49d4a2b elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"49d4a2b\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-5ef0c71\" data-id=\"5ef0c71\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t\t<section data-particle_enable=\"false\" data-particle-mobile-disabled=\"false\" class=\"elementor-section elementor-inner-section elementor-element elementor-element-74a728a elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"74a728a\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-inner-column elementor-element elementor-element-cab60f0\" data-id=\"cab60f0\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-element elementor-element-969a567 elementor-widget elementor-widget-text-editor\" data-id=\"969a567\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<h2 id=\"How-to-write-the-clean-code\"><span class=\"ez-toc-section\" id=\"1-Introduction\"><\/span><strong>1. Introduction<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2><p>Developing clean code is crucial for the success of any software project. Clean code makes it easy to read, understand, maintain, and debug.\u00a0<strong>It also enables effective collaboration between developers, reducing confusion and misunderstandings<\/strong>. Writing clean code can be challenging, but following some best practices can make it easier. In this guide, we will explore some of the best practices for writing clean code that can help developers create software that is easy to read and maintain.<\/p><div id=\"ez-toc-container\" class=\"ez-toc-v2_0_51_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title ez-toc-toggle\" style=\"cursor: pointer\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\" role=\"button\"><label for=\"item-69e1c0464165c\" ><span class=\"\"><span style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #337ab7;color:#337ab7\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #337ab7;color:#337ab7\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input aria-label=\"Toggle\" aria-label=\"item-69e1c0464165c\"  type=\"checkbox\" id=\"item-69e1c0464165c\"><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/embeddedwala.com\/beta\/blogs\/embedded-c\/how-to-write-the-clean-code\/#1-Introduction\" title=\"1. Introduction\">1. Introduction<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/embeddedwala.com\/beta\/blogs\/embedded-c\/how-to-write-the-clean-code\/#2-Naming-Conventions-and-Indentation-in-Code\" title=\"2. Naming Conventions and Indentation in Code\">2. Naming Conventions and Indentation in Code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/embeddedwala.com\/beta\/blogs\/embedded-c\/how-to-write-the-clean-code\/#1-Comment-Your-Code\" title=\"1. Comment Your Code\">1. Comment Your Code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/embeddedwala.com\/beta\/blogs\/embedded-c\/how-to-write-the-clean-code\/#2-Avoid-Duplication\" title=\"2. Avoid Duplication\">2. Avoid Duplication<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/embeddedwala.com\/beta\/blogs\/embedded-c\/how-to-write-the-clean-code\/#3-Standard-code-type-K-R\" title=\"3. Standard code type (K&amp;R)\">3. Standard code type (K&amp;R)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/embeddedwala.com\/beta\/blogs\/embedded-c\/how-to-write-the-clean-code\/#4-Create-Modular-code\" title=\"4. Create Modular code\">4. Create Modular code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/embeddedwala.com\/beta\/blogs\/embedded-c\/how-to-write-the-clean-code\/#3-Test-Your-Code\" title=\"3. Test Your Code\">3. Test Your Code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/embeddedwala.com\/beta\/blogs\/embedded-c\/how-to-write-the-clean-code\/#4-Standard-code-type-K-R\" title=\"4. Standard code type (K&amp;R)\">4. Standard code type (K&amp;R)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/embeddedwala.com\/beta\/blogs\/embedded-c\/how-to-write-the-clean-code\/#i\" title=\"\u00a0\">\u00a0<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/embeddedwala.com\/beta\/blogs\/embedded-c\/how-to-write-the-clean-code\/#5-Conclusion\" title=\"5. Conclusion\">5. Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 id=\"Naming Conventions and Indentation in Code\"><span class=\"ez-toc-section\" id=\"2-Naming-Conventions-and-Indentation-in-Code\"><\/span><strong>2. Naming Conventions and Indentation in Code<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2><p>Proper naming conventions and indentation are crucial for writing readable and maintainable code. Good naming conventions\u00a0<strong>make it easier to understand what the code is doing and improve code clarity<\/strong>. Indentation helps to visually separate blocks of code and make it easier to follow the program&#8217;s logic. Adhering to these coding standards makes it easier for others to work with your code and also makes it easier for you to return to and understand your own code later on.<\/p><p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-172 size-full\" src=\"https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Naming-Conventions-and-Indentation-in-Code.png\" alt=\"\" width=\"1920\" height=\"1080\" \/><\/p><h2 id=\"Comment Your Code\"><span class=\"ez-toc-section\" id=\"1-Comment-Your-Code\"><\/span><strong>1. Comment Your Code<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2><p>While clean code should be self-explanatory, it is still important to comment your code. Comments should be used to explain complex logic, clarify the purpose of the code, and provide context for other developers.<br \/>However, comments should not be used to explain what the code does. The code itself should be self-explanatory. Comments should only be used to provide additional context and information.<\/p><h2 id=\"Avoid Duplication\"><span class=\"ez-toc-section\" id=\"2-Avoid-Duplication\"><\/span><strong>2. Avoid Duplication<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2><p>Duplication is a common problem in software development. It leads to code that is harder to read, harder to maintain, and harder to test. To avoid duplication, you should use functions, classes, and libraries that can be reused across different parts of the code.<br \/>If you find yourself writing the same code over and over again, it is a sign that you need to refactor the code and create a reusable function or class.<\/p><h2 id=\"Standard code type (K&amp;R)\"><span class=\"ez-toc-section\" id=\"3-Standard-code-type-K-R\"><\/span><strong>3. Standard code type (K&amp;R)<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2><p>K&amp;R style is a popular coding style used for writing code that is easy to read, understand, and maintain. It focuses on readability and simplicity by using consistent indentation and formatting. For instance, variables are named using lowercase letters, and functions are named using a mix of lowercase and uppercase letters. The K&amp;R style also emphasizes the use of whitespace and indentation to make code more readable. By following this coding style, code becomes more consistent and easier to maintain, which is crucial in large projects with multiple developers.<\/p><h2 id=\"Create Modular code\"><span class=\"ez-toc-section\" id=\"4-Create-Modular-code\"><\/span><strong>4. Create Modular code<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2><p>Creating modular code is an important aspect of software development. It involves breaking down a large program into smaller, more manageable pieces of code called modules. Each module can then be developed and tested separately, making it easier to maintain and modify the code in the future. Using modular code also promotes code reuse and can save time and effort in the long run.<\/p><p>Writing short functions is a recommended practice for producing clean code. Ideally, a function should not exceed 10 lines of code. Short functions enhance code readability and comprehension, making it simpler to test and maintain. In contrast, long functions often indicate that the code is overly complicated and requires refactoring. By dividing the code into smaller, more manageable functions, developers can improve their code&#8217;s understandability and modify it more easily.<\/p><h2 id=\"Test Your Code\"><span class=\"ez-toc-section\" id=\"3-Test-Your-Code\"><\/span><strong>3. Test Your Code<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2><p>In the pursuit of writing clean code, testing plays a crucial role. It ensures that the code functions as intended and identifies any errors or bugs in the early stages of development. Writing tests for every function and class in the codebase is imperative, and regularly running them helps to verify that the code continues to work as expected.<\/p><h2 id=\"Standard code type (K&amp;R)1\"><span class=\"ez-toc-section\" id=\"4-Standard-code-type-K-R\"><\/span><strong>4. Standard code type (K&amp;R)<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2><p>One of the most famous code styles for C is the &#8220;K&amp;R style,&#8221; named after its creators Brian Kernighan and Dennis Ritchie. The K&amp;R style is defined by the following guidelines:<br \/>1. Use 4 spaces for indentation, rather than tabs.<br \/>2. Use opening braces on the same line as the control statement (e.g. if, for, while), and place closing braces on a new line.<br \/>3. Use one space after keywords (e.g. if, for, while), and no space between a function name and its opening parenthesis.4. Use a space before and after binary operators, such as +, -, *, and \/.<\/p><p>5. Use lowercase for variable and function names, and uppercase for constants and macros.<\/p><p>6. Limit lines to a maximum of 80 characters.<\/p><p>7. Use comments to explain the purpose of functions and blocks of code.<\/p><p><strong>Example Code\u00a0<\/strong><\/p><h2><span class=\"ez-toc-section\" id=\"i\"><\/span>\u00a0<span class=\"ez-toc-section-end\"><\/span><\/h2><div><pre>#include &lt;stdio.h&gt;\n \n\/\/ Global variable declaration \nint globalVar = 0; \n \n\/\/ Function declaration \nint addNumbers(int a, int b); \n \nint main() { \n    int x = 5; \n    int y = 7; \n    int sum = addNumbers(x, y);\n    printf(\"The sum of %d and %d is %d\\n\", x, y, sum); \n    printf(\"The value of the global variable is %d\\n\", globalVar); \n    return 0; \n \n} \n \n\/\/ Function definition \nint addNumbers(int a, int b) { \n    int sum = a + b; \n    globalVar = sum; \n    return sum; \n} \n<\/pre><\/div><h2 id=\"Conclusion\"><span class=\"ez-toc-section\" id=\"5-Conclusion\"><\/span>5<strong>. Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2><p>Developers often prioritize writing clean code in software development projects, as it ensures that the code is easily understandable, modifiable, and debuggable. Adhering to the best practices of clean coding results in numerous benefits such as better collaboration, reduced errors, and improved software quality. Although it requires additional time and effort, writing clean code is always worthwhile in the long run. By following the principles of clean code, developers can create efficient, effective, and maintainable software.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>1. Introduction Developing clean code is crucial for the success of any software project. Clean&#8230;<\/p>\n","protected":false},"author":1,"featured_media":170,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[43,45,44],"class_list":["post-169","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded-c","tag-clean-code","tag-code","tag-coding"],"acf":[],"views":266,"featured_image_urls":{"full":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/How-To-Write-The-Clean-Code.png",520,350,false],"thumbnail":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/How-To-Write-The-Clean-Code.png",150,101,false],"medium":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/How-To-Write-The-Clean-Code.png",300,202,false],"medium_large":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/How-To-Write-The-Clean-Code.png",520,350,false],"large":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/How-To-Write-The-Clean-Code.png",520,350,false],"1536x1536":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/How-To-Write-The-Clean-Code.png",520,350,false],"2048x2048":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/How-To-Write-The-Clean-Code.png",520,350,false],"tp-image-grid":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/How-To-Write-The-Clean-Code.png",520,350,false],"covernews-slider-full":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/How-To-Write-The-Clean-Code.png",520,350,false],"covernews-slider-center":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/How-To-Write-The-Clean-Code.png",520,350,false],"covernews-featured":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/How-To-Write-The-Clean-Code.png",520,350,false],"covernews-medium":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/How-To-Write-The-Clean-Code.png",505,340,false],"covernews-medium-square":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/How-To-Write-The-Clean-Code.png",371,250,false],"_nx_notification_thumb":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/How-To-Write-The-Clean-Code.png",100,67,false]},"author_info":{"display_name":"embeddedwala","author_link":"https:\/\/embeddedwala.com\/beta\/author\/embeddedwala\/"},"category_info":"<a href=\"https:\/\/embeddedwala.com\/beta\/category\/blogs\/embedded-c\/\" rel=\"category tag\">Embedded C<\/a>","tag_info":"Embedded C","comment_count":"0","_links":{"self":[{"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/posts\/169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/comments?post=169"}],"version-history":[{"count":7,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/posts\/169\/revisions"}],"predecessor-version":[{"id":6664,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/posts\/169\/revisions\/6664"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/media\/170"}],"wp:attachment":[{"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/media?parent=169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/categories?post=169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/tags?post=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}