Slide 1

Slide 1 text

Radoslav Stankov Eliminating noise from your code

Slide 2

Slide 2 text

πŸ‘‹

Slide 3

Slide 3 text

Radoslav Stankov @rstankov

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

https://tips.rstankov.com

Slide 6

Slide 6 text

https://speakerdeck.com/rstankov

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

https://speakerdeck.com/rstankov

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

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

Why code gets bad overtime? πŸ€”

Slide 17

Slide 17 text

What is a good code? 🧐

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

    

  • 
 Section 1

      

    • ...
    • 

    • ...
    • 

    • ...
    • 

    

  • 


Slide 22

Slide 22 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 23

Slide 23 text

😱

Slide 24

Slide 24 text

Is this good code? πŸ™„

Slide 25

Slide 25 text

Why not? πŸ€•

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

... too much information density

Slide 28

Slide 28 text

... too much information noise πŸ’¨

Slide 29

Slide 29 text

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

Slide 30

Slide 30 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 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 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 38

Slide 38 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 39

Slide 39 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 40

Slide 40 text

    

  • 
 Section 1

      

    • ...
    • 

    • ...
    • 

    • ...
    • 

    

  • 


Slide 41

Slide 41 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 42

Slide 42 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 43

Slide 43 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 44

Slide 44 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 45

Slide 45 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 46

Slide 46 text

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

Slide 47

Slide 47 text

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


Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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


Slide 50

Slide 50 text

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


Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

So, React doesn't make the code automatically better than jQuery? πŸ€” It, never did 😈

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

Broken windows theory

Slide 59

Slide 59 text

β€œSocial psychologists and police of fi cers 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 60

Slide 60 text

The Scout Rule

Slide 61

Slide 61 text

β€œLeave your code better than you found it.” The Scout Rule

Slide 62

Slide 62 text

Eliminating noise from your code

Slide 63

Slide 63 text

Eliminating πŸ’₯ Code Smells πŸ’₯

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

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

Slide 66

Slide 66 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 67

Slide 67 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 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

Refactoring

Slide 71

Slide 71 text

#1 Useless Code

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

click(event)} />

Slide 76

Slide 76 text

click(event)} />

Slide 77

Slide 77 text

Slide 78

Slide 78 text

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

Slide 79

Slide 79 text

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

Slide 80

Slide 80 text

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

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

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

Slide 84

Slide 84 text

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

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

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

Slide 87

Slide 87 text

#2 Too Much Conditional Logic

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

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

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

{(variant, complete) => ( )}

Slide 95

Slide 95 text

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

Slide 96

Slide 96 text

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

Slide 97

Slide 97 text

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

Slide 98

Slide 98 text

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

Slide 99

Slide 99 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 100

Slide 100 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 101

Slide 101 text

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

Slide 102

Slide 102 text

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

Slide 103

Slide 103 text

#3 Too DRY code

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

Good DRY

Slide 106

Slide 106 text

The bad side of DRY

Slide 107

Slide 107 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 108

Slide 108 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 109

Slide 109 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 110

Slide 110 text

No content

Slide 111

Slide 111 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 112

Slide 112 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 113

Slide 113 text

#4 Single Level of Abstraction

Slide 114

Slide 114 text

SLAP Single Level of Abstraction Principle SLAP

Slide 115

Slide 115 text

Slide 116

Slide 116 text

Slide 117

Slide 117 text

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

Cart ({cart.length})

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

Slide 118

Slide 118 text

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

Cart ({cart.length})

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

Slide 119

Slide 119 text

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

Cart ({cart.length})

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

Slide 120

Slide 120 text

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

Cart ({cart.length})

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

Slide 121

Slide 121 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, setData]); return data; }

Slide 122

Slide 122 text

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

Slide 123

Slide 123 text

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

Slide 124

Slide 124 text

No content

Slide 125

Slide 125 text

real book

Slide 126

Slide 126 text

No content

Slide 127

Slide 127 text

No content

Slide 128

Slide 128 text

No content

Slide 129

Slide 129 text

No content

Slide 130

Slide 130 text

Thanks 😎

Slide 131

Slide 131 text

https://speakerdeck.com/rstankov