Slide 1

Slide 1 text

Radoslav Stankov 23/11/2019 Eliminating noise from your code

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Radoslav Stankov @rstankov blog.rstankov.com github.com/rstankov
 twitter.com/rstankov

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

https://speakerdeck.com/rstankov

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Huey

Slide 10

Slide 10 text

Huey Dewey

Slide 11

Slide 11 text

Huey Dewey Louie

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Why code gets bad overtime? !

Slide 26

Slide 26 text

What is a good code? "

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

  • Section 1
    • ...
    • ...
    • ...

Slide 30

Slide 30 text

$('.accordion-menu .item a').each(function() {
 $(this).bind('click', function() {
 var title = this.className;
 var theul = $('#' + title);
 if (theul.length > 0) {
 if (theul.css('display') == 'none') {
 theul.slideDown();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 } else {
 theul.slideUp();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 }
 } else {
 }
 });
 });

Slide 31

Slide 31 text

$('.accordion-menu .item a').each(function() {
 $(this).bind('click', function() {
 var title = this.className;
 var theul = $('#' + title);
 if (theul.length > 0) {
 if (theul.css('display') == 'none') {
 theul.slideDown();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 } else {
 theul.slideUp();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 }
 } else {
 }
 });
 });

Slide 32

Slide 32 text

#

Slide 33

Slide 33 text

Is this good code? $

Slide 34

Slide 34 text

Why not? %

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

... too much information density

Slide 37

Slide 37 text

... too much information noise &

Slide 38

Slide 38 text

“In cognitive psychology, cognitive load refers to the used amount of working memory resources. ” - WikipediA Cognitive load

Slide 39

Slide 39 text

“Ego depletion refers to the idea that self- control or willpower draws upon a limited pool of mental resources that can be used up.” - WikipediA Ego depletion

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

$('.accordion-menu .item a').each(function() {
 $(this).bind('click', function() {
 var title = this.className;
 var theul = $('#' + title);
 if (theul.length > 0) {
 if (theul.css('display') == 'none') {
 theul.slideDown();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 } else {
 theul.slideUp();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 }
 } else {
 }
 });
 });

Slide 47

Slide 47 text

$('.accordion-menu .item a').each(function() {
 $(this).bind('click', function() {
 var title = this.className;
 var theul = $('#' + title);
 if (theul.length > 0) {
 if (theul.css('display') == 'none') {
 theul.slideDown();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 } else {
 theul.slideUp();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 }
 } else {
 }
 });
 });

Slide 48

Slide 48 text

$('.accordion-menu .item a').each(function() {
 $(this).bind('click', function() {
 var title = this.className;
 var theul = $('#' + title);
 if (theul.length > 0) {
 if (theul.css('display') == 'none') {
 theul.slideDown();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 } else {
 theul.slideUp();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 }
 }
 });
 }); 


Slide 49

Slide 49 text

$('.accordion-menu .item a').each(function() {
 $(this).bind('click', function() {
 var title = this.className;
 var theul = $('#' + title);
 if (theul.length > 0) {
 if (theul.css('display') == 'none') {
 theul.slideDown();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 } else {
 theul.slideUp();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 }
 }
 });
 }); 


Slide 50

Slide 50 text

$('.accordion-menu .item a').click(function() {
 var title = this.className;
 var theul = $('#' + title);
 if (theul.length > 0) {
 if (theul.css('display') == 'none') {
 theul.slideDown();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 } else {
 theul.slideUp();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 }
 }
 });

Slide 51

Slide 51 text

$('.accordion-menu .item a').click(function() {
 var title = this.className;
 var theul = $('#' + title);
 if (theul.length > 0) {
 if (theul.css('display') == 'none') {
 theul.slideDown();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 } else {
 theul.slideUp();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 }
 }
 });

Slide 52

Slide 52 text

  • Section 1
    • ...
    • ...
    • ...

Slide 53

Slide 53 text

  • Section 1
    • ...
    • ...
    • ...

Slide 54

Slide 54 text

  • Section 1
    • ...
    • ...
    • ...

Slide 55

Slide 55 text

  • Section 1
    • ...
    • ...
    • ...

Slide 56

Slide 56 text

  • Section 1
    • ...
    • ...
    • ...

Slide 57

Slide 57 text

$('.accordion-menu .item a').click(function() {
 var title = this.className;
 var theul = $('#' + title);
 if (theul.length > 0) {
 if (theul.css('display') == 'none') {
 theul.slideDown();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 } else {
 theul.slideUp();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 }
 }
 });

Slide 58

Slide 58 text

$('.accordion-menu .item a').click(function() {
 var title = this.className;
 var theul = $('#' + title);
 if (theul.length > 0) {
 if (theul.css('display') == 'none') {
 theul.slideDown();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 } else {
 theul.slideUp();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 }
 }
 });

Slide 59

Slide 59 text

$('.accordion-menu .item a').click(function() {
 var theul = $(this).next('ul');
 if (theul.length > 0) {
 if (theul.css('display') == 'none') {
 theul.slideDown();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 } else {
 theul.slideUp();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 }
 }
 });

Slide 60

Slide 60 text

$('.accordion-menu .item a').click(function() {
 var theul = $(this).next('ul');
 if (theul.length > 0) {
 if (theul.css('display') == 'none') {
 theul.slideDown();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 } else {
 theul.slideUp();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 }
 }
 });

Slide 61

Slide 61 text

$('.accordion-menu .item a').click(function() {
 var theul = $(this).next('ul');
 if (theul.length > 0) {
 if (theul.css('display') == 'none') {
 theul.slideDown();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 } else {
 theul.slideUp();
 var theli = dropa.parent('li');
 theli.animate({'padding-bottom': '20px'});
 }
 }
 });

Slide 62

Slide 62 text

$('.accordion-menu .item a').click(function() {
 var theul = $(this).next('ul');
 if (theul.length > 0) {
 if (theul.css('display') == 'none') {
 theul.slideDown()
 } else {
 theul.slideUp()
 }
 var theli = dropa.parent('li')
 theli.animate({'padding-bottom': '20px'});
 }
 });

Slide 63

Slide 63 text

$('.accordion-menu .item a').click(function() {
 var theul = $(this).next('ul');
 if (theul.length > 0) {
 if (theul.css('display') == 'none') {
 theul.slideDown()
 } else {
 theul.slideUp()
 }
 var theli = dropa.parent('li')
 theli.animate({'padding-bottom': '20px'});
 }
 });

Slide 64

Slide 64 text

$('.accordion-menu .item a').click(function() {
 var theul = $(this).next('ul')
 if (theul.length > 0) {
 if (theul.css('display') == 'none') {
 theul.slideDown()
 } else {
 theul.slideUp()
 }
 }
 });

Slide 65

Slide 65 text

$('.accordion-menu .item a').click(function() {
 var theul = $(this).next('ul')
 if (theul.length > 0) {
 if (theul.css('display') == 'none') {
 theul.slideDown()
 } else {
 theul.slideUp()
 }
 }
 });

Slide 66

Slide 66 text

$('.accordion-menu .item a').click(function() {
 var theul = $(this).next('ul')
 if (theul.length > 0) {
 if (theul.is(':visible')) {
 theul.slideDown()
 } else {
 theul.slideUp()
 }
 }
 });

Slide 67

Slide 67 text

$('.accordion-menu .item a').click(function() {
 var theul = $(this).next('ul')
 if (theul.length > 0) {
 if (theul.is(':visible')) {
 theul.slideDown()
 } else {
 theul.slideUp()
 }
 }
 });

Slide 68

Slide 68 text

$('.accordion-menu .item a').click(function() {
 var theul = $(this).next('ul')
 if (theul.length > 0) {
 if (theul.is(':visible')) {
 theul.slideDown()
 } else {
 theul.slideUp()
 }
 }
 });

Slide 69

Slide 69 text

$('.accordion-menu .item a').click(function() {
 var theul = $(this).next('ul')
 if (theul.length > 0) {
 theul.slideToggle();
 }
 });

Slide 70

Slide 70 text

$('.accordion-menu .item a').click(function() {
 var theul = $(this).next('ul')
 if (theul.length > 0) {
 theul.slideToggle();
 }
 });

Slide 71

Slide 71 text

$('.accordion-menu .item a').click(function() {
 $(this).next('ul').slideToggle();
 });


Slide 72

Slide 72 text

$('.accordion-menu .item a').click(function() {
 $(this).next('ul').slideToggle();
 });


Slide 73

Slide 73 text

$('.accordion-menu .item a').click(function() {
 $(this).next('ul').slideToggle();
 });


Slide 74

Slide 74 text

$('.accordion-menu .item a').live('click', function() {
 $(this).next('ul').slideToggle();
 });

Slide 75

Slide 75 text

$('.accordion-menu .item a').live('click', function() {
 $(this).next('ul').slideToggle();
 });

Slide 76

Slide 76 text

$('.accordion-menu .item a').live('click', function() {
 $(this).next('ul').slideToggle();
 });

Slide 77

Slide 77 text

$('.accordion-menu .item a').click(function() {
 $(this).next('ul').slideToggle();
 });


Slide 78

Slide 78 text

$(document).on('click', '.accordion-menu .item a', function() {
 $(this).next('ul').slideToggle();
 });


Slide 79

Slide 79 text

$(document).on('click', '.js-accordion-item a', function() {
 $(this).next('js-accordion-section').slideToggle();
 });

Slide 80

Slide 80 text

$(document).on('click', '[data-accordion="item"]', function() {
 $(this).next('[data-accordion="section"]').slideToggle();
 });

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

“It is better to be a good programmer with great habits, than a great programmer.” - Kent Beck

Slide 86

Slide 86 text

Broken windows theory

Slide 87

Slide 87 text

“Social psychologists and police officers tend to agree that if a window in a building is broken and is left unrepaired, all the rest of the windows will soon be broken.” - WikipediA Broken windows theory

Slide 88

Slide 88 text

The Boy Scout Rule

Slide 89

Slide 89 text

“Leave your code better than you found it.” The Boy Scout Rule

Slide 90

Slide 90 text

Radoslav Stankov 05/05/2018 Eliminating noise from your code

Slide 91

Slide 91 text

Radoslav Stankov 05/05/2018 Eliminating ' Code Smells '

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

Code smell is any symptom in the source code of a program that possibly indicates a deeper problem. Code smells

Slide 94

Slide 94 text

Code smells are usually not bugs—they are not technically incorrect and don't currently prevent the program from functioning. Instead, they indicate weaknesses in design that may be slowing down development or increasing the risk of bugs or failures in the future. Code smells

Slide 95

Slide 95 text

Code refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior Refactoring

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

Refactoring

Slide 99

Slide 99 text

#1 Useless Code

Slide 100

Slide 100 text

function LoadMoreBaseOnDevice(props) { return ( <> > ); }

Slide 101

Slide 101 text

function LoadMoreBaseOnDevice(props) { return ( {isMobile => isMobile ? ( ) : ( ) } ); }

Slide 102

Slide 102 text

function LoadMoreBaseOnDevice(props) { const isMobile = useIsMobile(); return isMobile ? ( ) : ( ); }

Slide 103

Slide 103 text

click(event)} />

Slide 104

Slide 104 text

click(event)} />

Slide 105

Slide 105 text

Slide 106

Slide 106 text

function Component({ array }) { return ( {array && array.length > 0 && array.map(item => )} ); }

Slide 107

Slide 107 text

function Component({ array }) { return ( {array && array.length > 0 && array.map(item => )} ); }

Slide 108

Slide 108 text

function Component({ array }) { return ( {array && array.map(item => )} ); }

Slide 109

Slide 109 text

function Component({ array }) { return ( {array.map(item => )} ); }

Slide 110

Slide 110 text

function Component({ array }) { return ( {array.map(item => )} ); }

Slide 111

Slide 111 text

function Component({ array }) { return array.map(item => ); }

Slide 112

Slide 112 text

{(variant, complete) => { return variant == 'left' ? ( viewer ? ( !viewer.isSubscribedToNewsletter && ( ) ) : ( ) ) : viewer ? ( !viewer.isSubscribedToNewsletter && ( ) ) : ( ); }}

Slide 113

Slide 113 text

{(variant, complete) => { return variant == 'left' ? ( viewer ? ( !viewer.isSubscribedToNewsletter && ( ) ) : ( ) ) : viewer ? ( !viewer.isSubscribedToNewsletter && ( ) ) : ( ); }}

Slide 114

Slide 114 text

{(variant, complete) => variant == 'left' ? ( viewer ? ( !viewer.isSubscribedToNewsletter && ( ) ) : ( ) ) : viewer ? ( !viewer.isSubscribedToNewsletter && ( ) ) : ( ) } ;

Slide 115

Slide 115 text

#2 Too Much Conditional Logic

Slide 116

Slide 116 text

No content

Slide 117

Slide 117 text

No content

Slide 118

Slide 118 text

{(variant, complete) => variant == 'left' ? ( viewer ? ( !viewer.isSubscribedToNewsletter && ( ) ) : ( ) ) : viewer ? ( !viewer.isSubscribedToNewsletter && ( ) ) : ( ) } ;

Slide 119

Slide 119 text

{(variant, complete) => variant == 'left' ? ( viewer ? ( !viewer.isSubscribedToNewsletter && ( ) ) : ( ) ) : viewer ? ( !viewer.isSubscribedToNewsletter && ( ) ) : ( ) } ;

Slide 120

Slide 120 text

{(variant, complete) => variant == 'left' ? ( ) : ( ) }

Slide 121

Slide 121 text

{(variant, complete) => variant == 'left' ? ( ) : ( ) }

Slide 122

Slide 122 text

{(variant, complete) => ( )}

Slide 123

Slide 123 text

function Card({ card }) { if (card.type === 'news') { return ; } if (card.type === 'post') { return ; } if (card.type === 'user') { return ; } return ; }

Slide 124

Slide 124 text

const CARDS = { news: NewsCard, post: PostCard, user: UserCard, }; function Card({ card }) { const Component = CARDS[card.type] || DefaultCard; return ; }

Slide 125

Slide 125 text

function ShareButton({ sharable, medium }) { const icon = medium === 'twitter' ? : ; const onClick = () => { if (medium === 'facebook') { shareOnFacebook(sharable); } else { shareOnTwitter(sharable); } }; return ( ); }

Slide 126

Slide 126 text

function ShareButton({ sharable, medium }) { const icon = medium === 'twitter' ? : ; const onClick = () => { if (medium === 'facebook') { shareOnFacebook(sharable); } else { shareOnTwitter(sharable); } }; return ( ); }

Slide 127

Slide 127 text

const MEDIUMS = { twitter: { icon: , title: 'Share on Twitter', share: shareOnTwitter, }, facebook: { icon: , title: 'Share on Facebook', share: shareOnFacebook, }, }; function ShareButton({ sharable, medium }) { const medium = MEDIUMS[medium]; return ( medium.share(sharable)} /> ); }

Slide 128

Slide 128 text

const MEDIUMS = { twitter: { icon: , title: 'Share on Twitter', share: shareOnTwitter, }, facebook: { icon: , title: 'Share on Facebook', share: shareOnFacebook, }, linkedin: { icon: , title: 'Share on LinkedIn', share: shareOnLinkedIn, }, }; function ShareButton({ sharable, medium }) { const medium = MEDIUMS[medium]; return ( medium.share(sharable)} />

Slide 129

Slide 129 text

interface IProps { sharable: ISharable; medium: keyof typeof MEDIUMS; }

Slide 130

Slide 130 text

http://blog.rstankov.com/replace-conditional-with-map-refactoring/

Slide 131

Slide 131 text

#3 Too DRY code

Slide 132

Slide 132 text

No content

Slide 133

Slide 133 text

Good DRY

Slide 134

Slide 134 text

The bad side of DRY

Slide 135

Slide 135 text

const API_GET = '/api/:source_type/:id'; const API_GET_DETAILS = '/api/:source_type/:id/details'; const API_GET_VERSION = '/api/:source_type/:id/on/:year/:date/:month'; const API_GET_SUBTYPE = '/api/:source_type/:id/sub/:subtype/:subtype_id';

Slide 136

Slide 136 text

const API = 'api'; const SOURCE_ID = '/:id'; const SOURCE_DATE = '/:year/:month/:day'; const SUBTYPE = '/:subtype'; const SUBID = '/:subtype_id'; const API_GET = API + SOURCE_TYPE + SOURCE_ID; const API_GET_DETAILS = API + SOURCE_TYPE + SOURCE_ID + '/details'; const API_GET_VERSION = API + SOURCE_TYPE + SOURCE_ID + '/on' + SOURCE_DATE; const API_GET_SUBTYPE = API + SOURCE_TYPE + SOURCE_ID + '/sub' + SUBTYPE + SUBID;

Slide 137

Slide 137 text

function path(rest = '') { return `${BASE_PATH}${rest}`; } function token(string) { return `${SEPARATOR}${string}`; } const SEPARATOR = '/'; const API = 'api'; const SOURCE_TYPE = ':source_type'; const SOURCE_ID = ':id'; const YEAR = ':year'; const MONTH = ':month'; const DAY = ':day'; const SUBTYPE = ':subtype'; const SUBID = ':subtype_id'; const DETAILS = 'details'; const ON = 'on'; const SUB = 'sub'; const BASE_PATH = token(API) + token(SOURCE_TYPE) + token(SOURCE_ID); const DATE_PART = token(YEAR) + token(MONTH) + token(DAY); const SUB_PART = token(SUBTYPE) + token(SUBID); const API_GET = path(); const API_GET_DETAILS = path(token(DETAILS)); const API_GET_VERSION = path(token(ON) + token(DATE_PART)); const API_GET_SUBTYPE = path(token(SUB) + token(SUB_PART));

Slide 138

Slide 138 text

No content

Slide 139

Slide 139 text

Don't repeat yourself (DRY) is a principle of software development aimed at reducing repetition of software patterns, replacing it with abstractions or using data normalization to avoid redundancy. Don't repeat yourself

Slide 140

Slide 140 text

The DRY principle is stated as "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system" Don't repeat yourself

Slide 141

Slide 141 text

#4 Single Level of Abstraction

Slide 142

Slide 142 text

SLAP Single Level of Abstraction Principle SLAP

Slide 143

Slide 143 text

Slide 144

Slide 144 text

Slide 145

Slide 145 text

function Cart() { const [cart, setData] = useState(selectData(DataSource)); const callback = useCallback(selectData, []); useEffect(() => { const removeChangeListener = DataSource.addChangeListener(() => setData(callback(DataSource)), ); return removeChangeListener; }, [callback]); return (

Cart ({cart.length})

    {cart.map(item => (
  • ))}
); }

Slide 146

Slide 146 text

function Cart() { const [cart, setData] = useState(selectData(DataSource)); const callback = useCallback(selectData, []); useEffect(() => { const removeChangeListener = DataSource.addChangeListener(() => setData(callback(DataSource)), ); return removeChangeListener; }, [callback]); return (

Cart ({cart.length})

    {cart.map(item => (
  • ))}
); }

Slide 147

Slide 147 text

function Cart() { const [cart, setData] = useState(selectData(DataSource)); const callback = useCallback(selectData, []); useEffect(() => { const removeChangeListener = DataSource.addChangeListener(() => setData(callback(DataSource)), ); return removeChangeListener; }, [callback]); return (

Cart ({cart.length})

    {cart.map(item => (
  • ))}
); }

Slide 148

Slide 148 text

function Cart() { const cart = useData(getCartData); return (

Cart ({cart.length})

    {cart.map(item => (
  • ))}
); }

Slide 149

Slide 149 text

function useData(selectData: (data: DataSourceClass) => T): T { const [data, setData] = useState(selectData(DataSource)); const callback = useCallback(selectData, []); useEffect(() => { const removeChangeListener = DataSource.addChangeListener(() => setData(callback(DataSource)), ); return removeChangeListener; }, [callback]); return data; }

Slide 150

Slide 150 text

No content

Slide 151

Slide 151 text

( Duplicated Code ) Long Method * Large Class + Long Parameter List , Shotgun Surgery - Feature Envy . Data Clumps / Case Statements 0 Parallel Inheritance Hierarchies 1 Speculative Generality 2 Temporary Field 3 Message Chains 4 Middle Man 5 Alternative Classes with Different Interfaces 6 Refused Bequest 7 Comments 8 Repetitive Boilerplate 9 God object ' : and more ...

Slide 152

Slide 152 text

No content

Slide 153

Slide 153 text

No content

Slide 154

Slide 154 text

No content

Slide 155

Slide 155 text

No content

Slide 156

Slide 156 text

Thanks ;

Slide 157

Slide 157 text

https://speakerdeck.com/rstankov

Slide 158

Slide 158 text

No content