{"id":40,"date":"2024-10-11T23:26:00","date_gmt":"2024-10-11T19:26:00","guid":{"rendered":"https:\/\/abthoughts.com\/?p=40"},"modified":"2024-10-11T23:43:22","modified_gmt":"2024-10-11T19:43:22","slug":"clean-code-guidelines","status":"publish","type":"post","link":"https:\/\/abthoughts.com\/?p=40","title":{"rendered":"Clean code guidelines"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"585\" src=\"https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/clean-code-1024x585.webp\" alt=\"\" class=\"wp-image-41\" srcset=\"https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/clean-code-1024x585.webp 1024w, https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/clean-code-300x171.webp 300w, https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/clean-code-768x439.webp 768w, https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/clean-code-1536x878.webp 1536w, https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/clean-code.webp 1792w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Meaningful Name<\/strong>s<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use descriptive names for classes, functions, and variables.<\/li>\n\n\n\n<li>Avoid misleading or ambiguous names.<\/li>\n\n\n\n<li>Names should reveal intent and purpose.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Functions Should Do One Thing<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Each function should perform a single task.<\/li>\n\n\n\n<li>Avoid combining unrelated functionality into one function.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Small Functions<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Functions should be small and ideally 5-10 lines long.<\/li>\n\n\n\n<li>Break larger functions into smaller, more manageable ones.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Use Descriptive Function Names<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Function names should describe what they do.<\/li>\n\n\n\n<li>The name should explain the purpose of the function clearly.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Limit the Number of Arguments<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Functions should have as few arguments as possible (ideally zero or one).<\/li>\n\n\n\n<li>Use object arguments or structure to reduce argument count.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Avoid Side Effects<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Functions should not change the state of the system unexpectedly.<\/li>\n\n\n\n<li>Keep the function\u2019s behavior predictable.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Don\u2019t Repeat Yourself (DRY)<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Avoid code duplication. Extract common functionality into functions.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Prefer Exceptions over Error Codes<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use exceptions for error handling rather than returning error codes.<\/li>\n\n\n\n<li>Don\u2019t let error handling clutter business logic.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Handle Errors Gracefully<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Ensure error handling is clear and does not obscure code logic.<\/li>\n\n\n\n<li>Provide meaningful messages in exceptions.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Use Meaningful Contexts<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Ensure your code provides enough context to understand its purpose.<\/li>\n\n\n\n<li>Avoid relying on global variables.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Avoid Magic Numbers<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Replace magic numbers (e.g., 7 or 3.14) with named constants.<\/li>\n\n\n\n<li>This makes the code more readable and easier to modify.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Favor Composition Over Inheritance<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use composition (has-a relationships) rather than inheritance (is-a relationships) where applicable.<\/li>\n\n\n\n<li>Inheritance should only be used when it makes sense semantically.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Follow the Principle of Least Astonishment<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Code should behave in a way that doesn&#8217;t surprise developers who read it.<\/li>\n\n\n\n<li>It should do what it seems to do.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Comment Only When Necessary<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Only comment when the code is not self-explanatory.<\/li>\n\n\n\n<li>Use comments to explain why something is done, not what it does.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Use Polymorphism Over If\/Else or Switch Statements<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Replace long if-else or switch statements with polymorphism where applicable.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Keep Code Simple<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Avoid overcomplicating the design. Choose simplicity over cleverness.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Avoid Negative Conditionals<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Write conditionals positively (e.g., isLoggedIn instead of !isNotLoggedIn).<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Classes Should Be Small<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A class should have one clear responsibility (Single Responsibility Principle).<\/li>\n\n\n\n<li>Avoid large, multi-purpose classes.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Cohesion<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Methods within a class should be closely related to the class\u2019s purpose.<\/li>\n\n\n\n<li>Avoid methods that seem out of place in a class.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Open\/Closed Principle<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Code should be open for extension but closed for modification.<\/li>\n\n\n\n<li>New functionality should not require modifying existing code.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Dependency Inversion Principle<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>High-level modules should not depend on low-level modules. Both should depend on abstractions.<\/li>\n\n\n\n<li>Use interfaces to decouple the code.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Don\u2019t Use Flags as Arguments<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Functions should not take boolean flags to control logic flow.<\/li>\n\n\n\n<li>Split logic into separate functions if necessary.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Encapsulation<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Keep data private. Expose only necessary information through methods.<\/li>\n\n\n\n<li>Avoid direct access to internal state.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Avoid Long Parameter Lists<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Functions with many parameters are harder to read and maintain.<\/li>\n\n\n\n<li>Prefer passing objects instead of multiple parameters.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Clean Up Resources<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Always ensure that resources (files, network connections, etc.) are properly released.<\/li>\n\n\n\n<li>Use try-with-resources or finally for cleanup.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Avoid Output in Functions<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Functions should not directly print to the console or produce output unless it is their sole purpose.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Test-Driven Development<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Write tests before writing code.<\/li>\n\n\n\n<li>Use tests to ensure that your code behaves as expected.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Keep Functions Pure<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Pure functions have no side effects and always return the same result for the same input.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Code Should Read Like Well-Written Prose<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Your code should be easy to read and understand, much like a narrative.<\/li>\n\n\n\n<li>Avoid convoluted or overly complex constructs.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Refactor Regularly<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Continuously improve your code through refactoring.<\/li>\n\n\n\n<li>Ensure that the code remains clean as you add new functionality.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Meaningful Names Functions Should Do One Thing Small Functions Use Descriptive Function Names Limit the Number of Arguments<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-40","post","type-post","status-publish","format-standard","hentry","category-coding"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Clean code guidelines - ABthoughts<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/abthoughts.com\/?p=40\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Clean code guidelines - ABthoughts\" \/>\n<meta property=\"og:description\" content=\"Meaningful Names Functions Should Do One Thing Small Functions Use Descriptive Function Names Limit the Number of Arguments\" \/>\n<meta property=\"og:url\" content=\"https:\/\/abthoughts.com\/?p=40\" \/>\n<meta property=\"og:site_name\" content=\"ABthoughts\" \/>\n<meta property=\"article:published_time\" content=\"2024-10-11T19:26:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-11T19:43:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/clean-code-1024x585.webp\" \/>\n<meta name=\"author\" content=\"Abdelrahman Fathy Bassiouny\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Abdelrahman Fathy Bassiouny\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/abthoughts.com\/?p=40\",\"url\":\"https:\/\/abthoughts.com\/?p=40\",\"name\":\"Clean code guidelines - ABthoughts\",\"isPartOf\":{\"@id\":\"https:\/\/abthoughts.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/abthoughts.com\/?p=40#primaryimage\"},\"image\":{\"@id\":\"https:\/\/abthoughts.com\/?p=40#primaryimage\"},\"thumbnailUrl\":\"https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/clean-code-1024x585.webp\",\"datePublished\":\"2024-10-11T19:26:00+00:00\",\"dateModified\":\"2024-10-11T19:43:22+00:00\",\"author\":{\"@id\":\"https:\/\/abthoughts.com\/#\/schema\/person\/2b4a7aed9d893435d974f7c1205df9e6\"},\"breadcrumb\":{\"@id\":\"https:\/\/abthoughts.com\/?p=40#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/abthoughts.com\/?p=40\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/abthoughts.com\/?p=40#primaryimage\",\"url\":\"https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/clean-code.webp\",\"contentUrl\":\"https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/clean-code.webp\",\"width\":1792,\"height\":1024},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/abthoughts.com\/?p=40#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/abthoughts.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Clean code guidelines\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/abthoughts.com\/#website\",\"url\":\"https:\/\/abthoughts.com\/\",\"name\":\"ABthoughts\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/abthoughts.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/abthoughts.com\/#\/schema\/person\/2b4a7aed9d893435d974f7c1205df9e6\",\"name\":\"Abdelrahman Fathy Bassiouny\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/abthoughts.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/bcf8cb261478ad1011536386498157a3d506f31ae253acdb83591046974c192f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/bcf8cb261478ad1011536386498157a3d506f31ae253acdb83591046974c192f?s=96&d=mm&r=g\",\"caption\":\"Abdelrahman Fathy Bassiouny\"},\"sameAs\":[\"http:\/\/abthoughts.com\",\"https:\/\/www.linkedin.com\/in\/abdelrahman-fathy-43a14553\"],\"url\":\"https:\/\/abthoughts.com\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Clean code guidelines - ABthoughts","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/abthoughts.com\/?p=40","og_locale":"en_US","og_type":"article","og_title":"Clean code guidelines - ABthoughts","og_description":"Meaningful Names Functions Should Do One Thing Small Functions Use Descriptive Function Names Limit the Number of Arguments","og_url":"https:\/\/abthoughts.com\/?p=40","og_site_name":"ABthoughts","article_published_time":"2024-10-11T19:26:00+00:00","article_modified_time":"2024-10-11T19:43:22+00:00","og_image":[{"url":"https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/clean-code-1024x585.webp"}],"author":"Abdelrahman Fathy Bassiouny","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Abdelrahman Fathy Bassiouny","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/abthoughts.com\/?p=40","url":"https:\/\/abthoughts.com\/?p=40","name":"Clean code guidelines - ABthoughts","isPartOf":{"@id":"https:\/\/abthoughts.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/abthoughts.com\/?p=40#primaryimage"},"image":{"@id":"https:\/\/abthoughts.com\/?p=40#primaryimage"},"thumbnailUrl":"https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/clean-code-1024x585.webp","datePublished":"2024-10-11T19:26:00+00:00","dateModified":"2024-10-11T19:43:22+00:00","author":{"@id":"https:\/\/abthoughts.com\/#\/schema\/person\/2b4a7aed9d893435d974f7c1205df9e6"},"breadcrumb":{"@id":"https:\/\/abthoughts.com\/?p=40#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/abthoughts.com\/?p=40"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/abthoughts.com\/?p=40#primaryimage","url":"https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/clean-code.webp","contentUrl":"https:\/\/abthoughts.com\/wp-content\/uploads\/2024\/10\/clean-code.webp","width":1792,"height":1024},{"@type":"BreadcrumbList","@id":"https:\/\/abthoughts.com\/?p=40#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/abthoughts.com\/"},{"@type":"ListItem","position":2,"name":"Clean code guidelines"}]},{"@type":"WebSite","@id":"https:\/\/abthoughts.com\/#website","url":"https:\/\/abthoughts.com\/","name":"ABthoughts","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/abthoughts.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/abthoughts.com\/#\/schema\/person\/2b4a7aed9d893435d974f7c1205df9e6","name":"Abdelrahman Fathy Bassiouny","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/abthoughts.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/bcf8cb261478ad1011536386498157a3d506f31ae253acdb83591046974c192f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/bcf8cb261478ad1011536386498157a3d506f31ae253acdb83591046974c192f?s=96&d=mm&r=g","caption":"Abdelrahman Fathy Bassiouny"},"sameAs":["http:\/\/abthoughts.com","https:\/\/www.linkedin.com\/in\/abdelrahman-fathy-43a14553"],"url":"https:\/\/abthoughts.com\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/abthoughts.com\/index.php?rest_route=\/wp\/v2\/posts\/40","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abthoughts.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abthoughts.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abthoughts.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abthoughts.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=40"}],"version-history":[{"count":5,"href":"https:\/\/abthoughts.com\/index.php?rest_route=\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":50,"href":"https:\/\/abthoughts.com\/index.php?rest_route=\/wp\/v2\/posts\/40\/revisions\/50"}],"wp:attachment":[{"href":"https:\/\/abthoughts.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abthoughts.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abthoughts.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}