-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleancode.html
18 lines (17 loc) · 11.4 KB
/
cleancode.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE html><html lang="de-ch"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>CleanCode - Finecloud</title><meta name="description" content="Summary of 'Clean code' by Robert C. Martin: https://gist.github.com/wojteklu/73c6914cc446146b8b533c0988cf8d29 Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility…"><meta name="generator" content="Publii Open-Source CMS for Static Site"><link rel="stylesheet" href="https://www.finecloud.ch/media/plugins/syntaxHighlighter/prism-black.css"><link rel="canonical" href="https://www.finecloud.ch/cleancode.html"><link rel="alternate" type="application/atom+xml" href="https://www.finecloud.ch/feed.xml"><link rel="alternate" type="application/json" href="https://www.finecloud.ch/feed.json"><meta property="og:title" content="CleanCode"><meta property="og:site_name" content="Finecloud"><meta property="og:description" content="Summary of 'Clean code' by Robert C. Martin: https://gist.github.com/wojteklu/73c6914cc446146b8b533c0988cf8d29 Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility…"><meta property="og:url" content="https://www.finecloud.ch/cleancode.html"><meta property="og:type" content="article"><link rel="shortcut icon" href="https://www.finecloud.ch/media/website/finecloud.png" type="image/png"><link rel="stylesheet" href="https://www.finecloud.ch/assets/css/style.css?v=39da73365516a098a9b73b721fc970e2"><script type="application/ld+json">{"@context":"http://schema.org","@type":"Article","mainEntityOfPage":{"@type":"WebPage","@id":"https://www.finecloud.ch/cleancode.html"},"headline":"CleanCode","datePublished":"2023-02-09T16:55","dateModified":"2023-02-09T16:55","description":"Summary of 'Clean code' by Robert C. Martin: https://gist.github.com/wojteklu/73c6914cc446146b8b533c0988cf8d29 Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility…","author":{"@type":"Person","name":"Finecloud","url":"https://www.finecloud.ch/authors/finecloud/"},"publisher":{"@type":"Organization","name":"Finecloud"}}</script><meta name="google-site-verification" content="seFY9U12uiEq5U3_MyZiX6XWzk0AVFl9zITr2ZKsytY"></head><body><div class="site-container"><header class="top" id="js-header"><a class="logo" href="https://www.finecloud.ch/">Finecloud</a><nav class="navbar js-navbar"><button class="navbar__toggle js-toggle" aria-label="Menu" aria-haspopup="true" aria-expanded="false"><span class="navbar__toggle-box"><span class="navbar__toggle-inner">Menu</span></span></button><ul class="navbar__menu"><li><a href="https://www.finecloud.ch/" target="_self">Blog</a></li><li><a href="https://www.finecloud.ch/tags/" target="_self">Tags</a></li></ul></nav><div class="search"><div class="search__overlay js-search-overlay"><div class="search__overlay-inner"><form action="https://www.finecloud.ch/search.html" class="search__form"><input class="search__input js-search-input" type="search" name="q" placeholder="search..." aria-label="search..." autofocus="autofocus"></form><button class="search__close js-search-close" aria-label="Close">Close</button></div></div><button class="search__btn js-search-btn" aria-label="Search"><svg role="presentation" focusable="false"><use xlink:href="https://www.finecloud.ch/assets/svg/svg-map.svg#search"/></svg></button></div></header><main><article class="post"><div class="hero"><figure class="hero__image hero__image--overlay"><img src="https://www.finecloud.ch/media/website/download.jpg" srcset="https://www.finecloud.ch/media/website/responsive/download-xs.jpg 300w, https://www.finecloud.ch/media/website/responsive/download-sm.jpg 480w, https://www.finecloud.ch/media/website/responsive/download-md.jpg 768w, https://www.finecloud.ch/media/website/responsive/download-lg.jpg 1024w, https://www.finecloud.ch/media/website/responsive/download-xl.jpg 1360w, https://www.finecloud.ch/media/website/responsive/download-2xl.jpg 1600w" sizes="100vw" loading="eager" alt=""></figure><header class="hero__content"><div class="wrapper"><div class="post__meta"><time datetime="2023-02-09T16:55">Februar 9, 2023</time></div><h1>CleanCode</h1></div></header></div><div class="wrapper post__entry"><div class="post__toc"><h3>Table of Contents</h3><ul><li><a href="#mcetoc_1gore4e2o9">General rules</a></li><li><a href="#mcetoc_1gore4e2oa">Design rules</a></li><li><a href="#mcetoc_1gore6rj763">Understandability tips</a></li><li><a href="#mcetoc_1gore6rj764">Names rules</a></li><li><a href="#mcetoc_1gore6rj765">Functions rules</a></li><li><a href="#mcetoc_1gore6rj766">Comments rules</a></li><li><a href="#mcetoc_1gore6rj767">Source code structure</a></li><li><a href="#mcetoc_1gore6rj768">Objects and data structures</a></li><li><a href="#mcetoc_1gore6rj769">Tests</a></li><li><a href="#mcetoc_1gore6rj76a">Code smells</a></li></ul></div><p>Summary of 'Clean code' by Robert C. Martin:<br>https://gist.github.com/wojteklu/73c6914cc446146b8b533c0988cf8d29<br><br>Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility and maintainability.</p><hr><h2 id="mcetoc_1gore4e2o9">General rules</h2><p>1. Follow standard conventions.<br>2. Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible.<br>3. Boy scout rule. Leave the campground cleaner than you found it.<br>4. Always find root cause. Always look for the root cause of a problem.</p><h2 id="mcetoc_1gore4e2oa">Design rules</h2><p>1. Keep configurable data at high levels.<br>2. Prefer polymorphism to if/else or switch/case.<br>3. Separate multi-threading code.<br>4. Prevent over-configurability.<br>5. Use dependency injection.<br>6. Follow Law of Demeter. A class should know only its direct dependencies.</p><h2 id="mcetoc_1gore6rj763">Understandability tips</h2><p>1. Be consistent. If you do something a certain way, do all similar things in the same way.<br>2. Use explanatory variables.<br>3. Encapsulate boundary conditions. Boundary conditions are hard to keep track of. Put the processing for them in one place.<br>4. Prefer dedicated value objects to primitive type.<br>5. Avoid logical dependency. Don't write methods which works correctly depending on something else in the same class.<br>6. Avoid negative conditionals.</p><h2 id="mcetoc_1gore6rj764">Names rules</h2><p>1. Choose descriptive and unambiguous names.<br>2. Make meaningful distinction.<br>3. Use pronounceable names.<br>4. Use searchable names.<br>5. Replace magic numbers with named constants.<br>6. Avoid encodings. Don't append prefixes or type information.</p><h2 id="mcetoc_1gore6rj765">Functions rules</h2><p>1. Small.<br>2. Do one thing.<br>3. Use descriptive names.<br>4. Prefer fewer arguments.<br>5. Have no side effects.<br>6. Don't use flag arguments. Split method into several independent methods that can be called from the client without the flag.</p><h2 id="mcetoc_1gore6rj766">Comments rules</h2><p>1. Always try to explain yourself in code.<br>2. Don't be redundant.<br>3. Don't add obvious noise.<br>4. Don't use closing brace comments.<br>5. Don't comment out code. Just remove.<br>6. Use as explanation of intent.<br>7. Use as clarification of code.<br>8. Use as warning of consequences.</p><h2 id="mcetoc_1gore6rj767">Source code structure</h2><p>1. Separate concepts vertically.<br>2. Related code should appear vertically dense.<br>3. Declare variables close to their usage.<br>4. Dependent functions should be close.<br>5. Similar functions should be close.<br>6. Place functions in the downward direction.<br>7. Keep lines short.<br>8. Don't use horizontal alignment.<br>9. Use white space to associate related things and disassociate weakly related.<br>10. Don't break indentation.</p><h2 id="mcetoc_1gore6rj768">Objects and data structures</h2><p>1. Hide internal structure.<br>2. Prefer data structures.<br>3. Avoid hybrids structures (half object and half data).<br>4. Should be small.<br>5. Do one thing.<br>6. Small number of instance variables.<br>7. Base class should know nothing about their derivatives.<br>8. Better to have many functions than to pass some code into a function to select a behavior.<br>9. Prefer non-static methods to static methods.</p><h2 id="mcetoc_1gore6rj769">Tests</h2><p>1. One assert per test.<br>2. Readable.<br>3. Fast.<br>4. Independent.<br>5. Repeatable.</p><h2 id="mcetoc_1gore6rj76a">Code smells</h2><p>1. Rigidity. The software is difficult to change. A small change causes a cascade of subsequent changes.<br>2. Fragility. The software breaks in many places due to a single change.<br>3. Immobility. You cannot reuse parts of the code in other projects because of involved risks and high effort.<br>4. Needless Complexity.<br>5. Needless Repetition.<br>6. Opacity. The code is hard to understand.</p></div><footer class="wrapper post__footer"><p class="post__last-updated">This article was updated on Februar 9, 2023</p><ul class="post__tag"><li><a href="https://www.finecloud.ch/tags/softwareentwicklung/">software development</a></li></ul><div class="post__share"></div></footer></article><nav class="post__nav"><div class="post__nav-inner"><div class="post__nav-prev"><svg width="1.041em" height="0.416em" aria-hidden="true"><use xlink:href="https://www.finecloud.ch/assets/svg/svg-map.svg#arrow-prev"/></svg> <a href="https://www.finecloud.ch/javascript-advanced-features.html" class="post__nav-link" rel="prev"><span>Previous</span> JavaScript advanced features</a></div><div class="post__nav-next"><a href="https://www.finecloud.ch/conventional-commits.html" class="post__nav-link" rel="next"><span>Next</span> Conventional commits </a><svg width="1.041em" height="0.416em" aria-hidden="true"><use xlink:href="https://www.finecloud.ch/assets/svg/svg-map.svg#arrow-next"/></svg></div></div></nav></main><footer class="footer"><div class="footer__copyright"><p>Powered by Publii</p></div><button onclick="backToTopFunction()" id="backToTop" class="footer__bttop" aria-label="Back to top" title="Back to top"><svg><use xlink:href="https://www.finecloud.ch/assets/svg/svg-map.svg#toparrow"/></svg></button></footer></div><script>window.publiiThemeMenuConfig = {
mobileMenuMode: 'sidebar',
animationSpeed: 300,
submenuWidth: 'auto',
doubleClickTime: 500,
mobileMenuExpandableSubmenus: true,
relatedContainerForOverlayMenuSelector: '.top',
};</script><script defer="defer" src="https://www.finecloud.ch/assets/js/scripts.min.js?v=6ca8b60e6534a3888de1205e82df8528"></script><script>var images = document.querySelectorAll('img[loading]');
for (var i = 0; i < images.length; i++) {
if (images[i].complete) {
images[i].classList.add('is-loaded');
} else {
images[i].addEventListener('load', function () {
this.classList.add('is-loaded');
}, false);
}
}</script><script defer="defer" src="https://www.finecloud.ch/media/plugins/syntaxHighlighter/prism.js"></script></body></html>