{"id":6836,"date":"2022-02-20T22:01:44","date_gmt":"2022-02-20T14:01:44","guid":{"rendered":"https:\/\/slash.bravefactor.com\/?post_type=resources&#038;p=6836"},"modified":"2024-11-19T18:43:08","modified_gmt":"2024-11-19T10:43:08","slug":"how-to-write-clean-code-6-brief-guide-on-rules-and-principles","status":"publish","type":"resources","link":"https:\/\/slash.co\/articles\/how-to-write-clean-code-a-brief-guide-on-rules-and-principles\/","title":{"rendered":"How to write clean code? 6 brief guide on rules and principles"},"content":{"rendered":"<p><em><span style=\"font-weight: 400;\">When Robert C. Martin wrote the book \u201cClean Code\u201d back in 2008, it began to change the world of coding. In early 2000, coding was meant to be harsh \u2013 if not by intention but due to the market practice. However, the concept of clean coding changed the view of many programmers when they started implementing the rules and principles of clean coding. So let\u2019s find out what clean code is, its rules and principles, and how we use it at <\/span><a href=\"https:\/\/slash.co\/\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">Slash<\/span><\/a><span style=\"font-weight: 400;\">?<\/span><\/em><\/p>\n<h2>What\u2019s clean code?<\/h2>\n<p><span style=\"font-weight: 400;\">Codes are said to be clean if they are understandable by the entire team without scratching their heads multiple times. In other words, a clean code is easy-to-read, maintain, extend, and change by any developer. That means if you\u2019re an author of a project and pass it on to your colleague developer, but the person cannot understand the code, you haven\u2019t written clean code.<\/span><\/p>\n<h2>Rules to write clean codes to avoid mess<\/h2>\n<p><span style=\"font-weight: 400;\">You can save your entire team from having a few extra cups of black coffee every day by writing clean codes. All you need to do is to follow these rules:<\/span><\/p>\n<h3>Be all downhill<\/h3>\n<p><span style=\"font-weight: 400;\">The first rule is to do things in the simplest way possible. Try to write codes without complexities. If you got a file with a lot of complicated codes, try to fix those before going forward. Doing so will ease coding in the upcoming stages. Also, try your best to view and overcome problems from their roots.<\/span><\/p>\n<h3>Design rules<\/h3>\n<p><span style=\"font-weight: 400;\">Always keep configurable data on the highest level but avoid over-configurability. Also, it is best to use a single interface to different types of entities (polymorphism) to switch\/case or if\/else statements.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Moreover, it is wise to break up multi-threading codes and apply the dependency technique. Additionally, always follow the \u201cLaw of Demeter\u201d (LoD).<\/span><\/p>\n<h3>Understandability<\/h3>\n<p><span style=\"font-weight: 400;\">Just be consistent in your way of doing things. <\/span><span style=\"font-weight: 400;\">For instance, if you have a particular method to code a specific action, make sure that all similar actions are done in the same manner.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Likewise, put all boundary conditions processing in one single place and include explanatory variables when writing codes.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Next, avoid using negative conditionals and logical dependencies by writing codes that aren\u2019t dependent on any other element in the same class.<\/span><\/p>\n<h3>Give easy names<\/h3>\n<p><span style=\"font-weight: 400;\">First things first, give names that are easy to understand. You can do this by:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Using searchable names<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Choosing clear and descriptive names<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Replacing constants with magic numbers<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Keeping names that are easy to pronounce<\/span><\/li>\n<\/ul>\n<h3>Say no to large functions<\/h3>\n<p><span style=\"font-weight: 400;\">Functions help achieve specific tasks through codes. It would be best to use small functions to describe a single task to write clean codes.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Yes! You can use descriptive names but never go for a series of arguments. Or, in other words, avoid using flag arguments. Instead, divide a single large method into numerous independent methods. So whenever the client calls any of them, it comes without the flag.<\/span><\/p>\n<h3>Comments<\/h3>\n<p><span style=\"font-weight: 400;\">Remember, self-explanatory codes don\u2019t require comments. However, it doesn\u2019t mean comments are good or bad, but programming without putting comments is the best way to write clean codes.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Therefore, try your best to:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Explain your work through codes<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Avoid unnecessary comments<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Remove any comment out codes<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Ignore the noise and closing brace comments in coding to make it reader-friendly<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">And if you are using comments in your codes, then they should be used as:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Explanation of intent<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Warning of any consequences<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Clarification of your codes<\/span><\/li>\n<\/ul>\n<h3>Structuring<\/h3>\n<p><span style=\"font-weight: 400;\">We have divided structuring into two sections:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Source Code Structuring<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Objects and Data Structuring<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">When<strong> structuring source codes<\/strong>, you should:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Separate your concepts vertically<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Make related codes look vertically dense<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Declare variables as per their usability<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Close all similar and dependent functions<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Downward the direction of functions<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Keep the lines short<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Avoid using horizontal alignments<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Use unbreakable indentation<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Use white spaces for differentiating similar and distinct codes<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">And when it\u2019s time for <strong>objects and data structuring<\/strong>, this is what you should do:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Keep the internal structure hidden<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Work on data structures<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Don\u2019t use \u201chalf object and half data\u201d (hybrid)\u00a0 structures<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Structures should be small and one at a time<\/span><\/li>\n<li><span style=\"font-weight: 400;\">There should be minimum instance variables<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Base class should be disconnected with any of its derivative codes<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Have many functions<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Apply non-static methods rather than static methods<\/span><\/li>\n<\/ul>\n<h3>Testing<\/h3>\n<p><span style=\"font-weight: 400;\">When you are about to test your program, ensure testing one assertion at a time. Furthermore, the test should be readable, fast, independent, and repeatable.<\/span><\/p>\n<h2>A few deeper problems to overcome for clean coding<\/h2>\n<p><span style=\"font-weight: 400;\">When you are trying to write clean codes, you should know the common code smells, such as:<\/span><\/p>\n<p><strong>Rigidity: <\/strong><span style=\"font-weight: 400;\">Keep in mind that once the software is written, it is difficult to make amendments later. It is because even a tiny little change could cause a series of alterations, making everything go to waste.<\/span><\/p>\n<p><strong>Fragility: <\/strong><span style=\"font-weight: 400;\">The software has a fragile nature. Any changes to it in the later stage could disrupt the overall functionality.<\/span><\/p>\n<p><strong>Immobility: <\/strong><span style=\"font-weight: 400;\">Since you are writing clean codes, so expectedly, they will be short and to the point. Therefore, it could be pretty impossible to use them on any other project with a different nature.<\/span><\/p>\n<p><strong>Repetition and complexity: <\/strong><span style=\"font-weight: 400;\">Unnecessary repetition of codes adds a new level of difficulty for the team, as they cannot fix things or continue the project without the author\u2019s supervision.<\/span><\/p>\n<p><strong>Opacity: <\/strong><span style=\"font-weight: 400;\">Complicated coding is always hard to understand.<\/span><\/p>\n<h2>Principles of clean code<\/h2>\n<p><span style=\"font-weight: 400;\">Just like there are rules for clean coding, there are principles made by fellow developers, which are valuable when followed.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">These principles help organizations and developers write clean codes.<\/span><\/p>\n<h3>KISS \u2013 keep it simple stupid<\/h3>\n<p><span style=\"font-weight: 400;\">The KISS principle tells you that simple codes are better. It would be best to avoid including complicated codes in your program. So whenever you\u2019re writing code, ask yourself if there\u2019s another way to write them more straightforwardly?<\/span><\/p>\n<h3>DRY \u2013 don\u2019t repeat yourself<\/h3>\n<p><span style=\"font-weight: 400;\">DRY defines that your codes should be unrepeated. They shouldn\u2019t be used multiple times within the codebase and must have an authoritative and clear meaning.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Failure to apply the DRY principle leads to WET, which is considered as <\/span><i><span style=\"font-weight: 400;\">\u201cWaste Everyone\u2019s Time,\u201d \u201cWe Enjoy Typing,\u201d or \u201cWrite Everything Twice.\u201d<\/span><\/i><\/p>\n<h3>YAGNI \u2013 you aren\u2019t gonna need it<\/h3>\n<p><span style=\"font-weight: 400;\">YAGNI plays an essential role in Extreme Programming methodology to develop software as per customers\u2019 demand.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The acronym simple means that a developer should avoid adding functionality to a program until necessary. You should consider YAGNI during unit testing, refactoring, and integration.<\/span><\/p>\n<h3>Composition over inheritance<\/h3>\n<p><span style=\"font-weight: 400;\">Composition over inheritance allows you to design your types according to their functionality rather than nature.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In this scenario, developers prefer \u201ccomposition\u201d instead of \u201cinheritance,\u201d as inheritance makes your codes inflexible to later modifications.<\/span><\/p>\n<h3>Favor readability<\/h3>\n<p><span style=\"font-weight: 400;\">The \u201cFavor Readability\u201d principle encourages you to write codes, which are machine, as well as human-readable. So that everyone on the team can understand and work on the project without confusion.<\/span><\/p>\n<h3>Practice<\/h3>\n<p><span style=\"font-weight: 400;\">Lastly, to write clean codes, you must practice with consistency to achieve your goals.<\/span><\/p>\n<h2>How do we clean code at Slash?<\/h2>\n<p><span style=\"font-weight: 400;\">A well-designed program can help businesses create a tremendous positive difference. However, this target is unachievable without high-quality coding and architecture.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">At Slash we believe that there are multiple aspects involved in creating a masterpiece, but the most important one is using \u201cClean Codes\u201d in your program. Therefore, before we start, we make sure to identify:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Design pattern<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Framework to use<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Agreed standards and conventions<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Team members and tools to be used in the process<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Flexible architectural runway<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Once we are over it, we use the KISS principle to make sure all the codes have the following traits:<\/span><\/p>\n<h3>Understandability and readability<\/h3>\n<p><span style=\"font-weight: 400;\">We use simple and understandable conventions, such as file naming, correlating functions, proper commenting whenever required, and more to make the project readable for the entire team.\u00a0<\/span><\/p>\n<h3>Maintainability<\/h3>\n<p><span style=\"font-weight: 400;\">We prefer short codes for every function to make it easier to add a unit test on that particular file(S). For example, we use clean code rules like \u201cone assert per test\u201d and \u201cindependent unit test\u201d with uncomplicated mocking options.<\/span><\/p>\n<h3>Changeability\/Extensibility<\/h3>\n<p><span style=\"font-weight: 400;\">Since we use the clean code approach, we ensure that our projects are easy to change and less complicated to test or add new features in the future. It helps our Slash team deal with regression errors in a much better way.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Besides the KISS principle, we also use principles like YAGNI, DRY, Composition over Inheritance, and Favor Readability to write clean codes.<\/span><\/p>\n<h2>Bottom line<\/h2>\n<p><span style=\"font-weight: 400;\">Clean code is an ideal way to develop highly-functioning programs, which could be tested and amended by every person on the team instead of a single author.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">You should follow the rules and principles to get the desired results to write clean codes. And at<\/span><a href=\"https:\/\/slash.co\/\"> <span style=\"font-weight: 400;\">Slash<\/span><\/a><span style=\"font-weight: 400;\">, we religiously work on principles like KISS, DRY, YAGNI, and more to make sure everyone on our team can transfer their capabilities into the program through their skills and experience without facing complications. This allows us to deliver top-notch quality software to our clients at the end of the day.<\/span><\/p>\n<h2>FAQs<\/h2>\n<div class=\"DataCards_card__word__Y_UB0\">\n<p><span class=\"DataCards_card__title__zAPJB\"><strong>What is meant by clean code?<\/strong>\u00a0 <\/span><strong><span class=\"DataCards_card__title__zAPJB\"><span style=\"font-weight: 400;\">In other words, a clean code is easy-to-read, maintain, extend, and change by any developer. <\/span><br \/>\n<\/span><\/strong><\/p>\n<p><strong>What are the 4 rules of clean code?<\/strong> For clean code, it must pass the tests, reveal its intention, avoid duplication, and utilize the fewest elements possible.<\/p>\n<\/div>\n<div>\n<p><strong>How do I make sure my code is clean?<\/strong> To make sure your code is clean, you can follow these practices: use descriptive names, employ empty lines for readability, limit functions to three or fewer parameters, ensure functions have a single responsibility, keep functions small, minimize line length, and avoid unnecessary comments.<\/p>\n<\/div>\n<div>\n<p><strong>What clean code looks like?<\/strong> Clean code is simple and direct. It reads like well-written prose. It never obscures the designer\u2019s intent but is instead full of crisp abstractions and straightforward lines of control.<\/p>\n<\/div>\n<div class=\"KeywordLists_keyword__row__a1Cso\">\n<p class=\"KeywordLists_lists__add__Zy5Pt\" data-tip=\"Sign up to save keywords\"><strong>Is clean code easy to understand?<\/strong> Clean code is clear, understandable, and maintainable. When you write clean code, you&#8217;re keeping in mind the other people who may read and interpret your code at a later time.<\/p>\n<\/div>\n","protected":false},"featured_media":11652,"parent":0,"template":"","resource-topic":[63],"resource-type":[43],"class_list":["post-6836","resources","type-resources","status-publish","has-post-thumbnail","hentry","resource-topic-software-development","resource-type-articles"],"_links":{"self":[{"href":"https:\/\/slash.co\/wp-json\/wp\/v2\/resources\/6836","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/slash.co\/wp-json\/wp\/v2\/resources"}],"about":[{"href":"https:\/\/slash.co\/wp-json\/wp\/v2\/types\/resources"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/slash.co\/wp-json\/wp\/v2\/media\/11652"}],"wp:attachment":[{"href":"https:\/\/slash.co\/wp-json\/wp\/v2\/media?parent=6836"}],"wp:term":[{"taxonomy":"resource-topic","embeddable":true,"href":"https:\/\/slash.co\/wp-json\/wp\/v2\/resource-topic?post=6836"},{"taxonomy":"resource-type","embeddable":true,"href":"https:\/\/slash.co\/wp-json\/wp\/v2\/resource-type?post=6836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}