Slide 1

Slide 1 text

Radoslav Stankov VarnaConf 11/08/2012 How to write good code

Slide 2

Slide 2 text

How am I? @rstankov http://rstankov.com http://github.com/rstankov

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

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

Slide 15

Slide 15 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 16

Slide 16 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 17

Slide 17 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 18

Slide 18 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 19

Slide 19 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 20

Slide 20 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 21

Slide 21 text

Slide 22

Slide 22 text

Slide 23

Slide 23 text

Slide 24

Slide 24 text

Slide 25

Slide 25 text

Slide 26

Slide 26 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 27

Slide 27 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 28

Slide 28 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 29

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

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

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

Slide 32 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 33

Slide 33 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 34

Slide 34 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 35

Slide 35 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 36

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

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

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

Know your tools

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

class NewsestFriendImages def initialize(user) @user = user end def images(page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order('created_at') images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end NewsestFriendImages.new(current_user).images(params[:page])

Slide 53

Slide 53 text

class NewsestFriendImages def initialize(user) @user = user end def images(page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order('created_at') images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end NewsestFriendImages.new(current_user).images(params[:page])

Slide 54

Slide 54 text

class NewsestFriendImages def initialize(user) @user = user end def images(page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order('created_at') images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end NewsestFriendImages.new(current_user).images(params[:page])

Slide 55

Slide 55 text

class NewsestFriendImages def initialize(user) @user = user end def images(page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order('created_at') images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end NewsestFriendImages.new(current_user).images(params[:page])

Slide 56

Slide 56 text

class NewsestFriendImages def initialize(user) @user = user end def images(page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order('created_at') images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end NewsestFriendImages.new(current_user).images(params[:page])

Slide 57

Slide 57 text

class NewsestFriendImages def initialize(user) @user = user end def images(page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order('created_at') images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end NewsestFriendImages.new(current_user).images(params[:page])

Slide 58

Slide 58 text

class NewsestFriendImages def initialize(user) @user = user end def images(page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order('created_at') images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end NewsestFriendImages.new(current_user).images(params[:page])

Slide 59

Slide 59 text

class NewsestFriendImages def initialize(user) @user = user end def images(page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order('created_at') images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end NewsestFriendImages.new(current_user).images(params[:page])

Slide 60

Slide 60 text

class NewsestFriendImages def initialize(user) @user = user end def images(page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order('created_at') images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end NewsestFriendImages.new(current_user).images(params[:page])

Slide 61

Slide 61 text

class NewsestFriendImages def initialize(user) @user = user end def images(page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order('created_at') images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end NewsestFriendImages.new(current_user).images(params[:page])

Slide 62

Slide 62 text

class NewsestFriendImages def initialize(user) @user = user end def images(page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order('created_at') images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end NewsestFriendImages.new(current_user).images(params[:page]) Copy & Paste

Slide 63

Slide 63 text

class MostLikedFriendImages def initialize(user) @user = user end def images(page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order('votes_count') images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end MostLikedFriendImages.new(current_user).images(params[:page])

Slide 64

Slide 64 text

class MostLikedFriendImages def initialize(user) @user = user end def images(page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order('votes_count') images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end MostLikedFriendImages.new(current_user).images(params[:page])

Slide 65

Slide 65 text

class MostLikedFriendImages def initialize(user) @user = user end def images(page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order('votes_count') images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end MostLikedFriendImages.new(current_user).images(params[:page])

Slide 66

Slide 66 text

class MostLikedFriendImages def initialize(user) @user = user end def images(page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order('votes_count') images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end MostLikedFriendImages.new(current_user).images(params[:page])

Slide 67

Slide 67 text

class MostLikedFriendImages def initialize(user) @user = user end def images(page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order('votes_count') images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end MostLikedFriendImages.new(current_user).images(params[:page])

Slide 68

Slide 68 text

DRY don’t repeat yourself

Slide 69

Slide 69 text

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

Slide 70

Slide 70 text

class NewsestFriendImages def initialize(user) @user = user end def images(page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order('created_at') images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end NewsestFriendImages.new(current_user).images(params[:page])

Slide 71

Slide 71 text

class NewsestFriendImages def initialize(user) @user = user end def images(page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order('created_at') images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end NewsestFriendImages.new(current_user).images(params[:page])

Slide 72

Slide 72 text

class FriendImages def initialize(user) @user = user end def images(sorting, page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order(sorting) images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end FriendImages.new(current_user).images('created_at', params[:page]) FriendImages.new(current_user).images('likes_count', params[:page])

Slide 73

Slide 73 text

class FriendImages def initialize(user) @user = user end def images(sorting, page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order(sorting) images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end FriendImages.new(current_user).images('created_at', params[:page]) FriendImages.new(current_user).images('likes_count', params[:page])

Slide 74

Slide 74 text

class FriendImages def initialize(user) @user = user end def images(sorting, page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order(sorting) images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end FriendImages.new(current_user).images('created_at', params[:page]) FriendImages.new(current_user).images('likes_count', params[:page])

Slide 75

Slide 75 text

class FriendImages def initialize(user) @user = user end def images(sorting, page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order(sorting) images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end FriendImages.new(current_user).images('created_at', params[:page]) FriendImages.new(current_user).images('likes_count', params[:page])

Slide 76

Slide 76 text

class FriendImages def initialize(user) @user = user end def images(sorting, page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order(sorting) images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end FriendImages.new(current_user).images('created_at', params[:page]) FriendImages.new(current_user).images('likes_count', params[:page])

Slide 77

Slide 77 text

class FriendImages def initialize(user) @user = user end def images(sorting, page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order(sorting) images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end FriendImages.new(current_user).images('created_at', params[:page]) FriendImages.new(current_user).images('likes_count', params[:page])

Slide 78

Slide 78 text

class FriendImages def initialize(user) @user = user end def images(sorting, page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order(sorting) images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end FriendImages.new(current_user).images('created_at', params[:page]) FriendImages.new(current_user).images('likes_count', params[:page])

Slide 79

Slide 79 text

The bad side of DRY

Slide 80

Slide 80 text

http://bit.ly/O5jSLV

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

def api_path(rest = '') BASE_PATH + rest end def token(string) SEPARATOR + string end SEPARATOR = '/' API = 'api' SOURCE_TYPE = ':source_type' SOURCE_ID = ':id' YEAR = ':year' MONTH = ':month' DAY = ':day' SUBTYPE = ':subtype' SUBID = ':subtype_id' DETAILS = 'details' ON = 'on' SUB = 'sub' BASE_PATH = token(API) + token(SOURCE_TYPE) + token(SOURCE_ID) DATE_PART = token(YEAR) + token(MONTH) + token(DAY) SUB_PART = token(SUBTYPE) + token(SUBID) API_GET = api_path() API_GET_DETAILS = api_path(token(DETAILS)) API_GET_VERSION = api_path(token(ON) + token(DATE_PART)) API_GET_SUBTYPE = api_path(token(SUB) + token(SUB_PART))

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

class FriendImages def initialize(user) @user = user end def images(sorting, page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order(sorting) images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end

Slide 87

Slide 87 text

SLAP Single Level of Abstraction Principle

Slide 88

Slide 88 text

Keep all lines of code in a method at the same level of abstraction.

Slide 89

Slide 89 text

class FriendImages def initialize(user) @user = user end def images(sorting, page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order(sorting) images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end end

Slide 90

Slide 90 text

def images(sorting, page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order(sorting) images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end

Slide 91

Slide 91 text

def images(sorting, page) images = find_images_of(friends) images = apply_age_restritions_to(images) images = order_by(images, sorting) images = paginate(images, page) decorate(images) end

Slide 92

Slide 92 text

def friends Friendship.where(user_id: @user.id).pluck('friend_id') end def find_images_of(friends) Image.where(user_id: friends, visible_to_friends: true) end def apply_age_restritions_to(images) images.where('minimum_age >= ?', @user.age) end def order_by(sorting) images.order(sorting) end def paginate(page) images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) end def decorate(images) images.map { |image| ImageDecorator.new(image) } end

Slide 93

Slide 93 text

Law of Demeter

Slide 94

Slide 94 text

• You can play with yourself. • You can play with your own toys (but you can't take them apart), • You can play with toys that were given to you. • And you can play with toys you've made yourself.

Slide 95

Slide 95 text

• Your method can call other methods in its class directly • Your method can call methods on its own fields directly (but not on the fields' fields) • When your method takes parameters, your method can call methods on those parameters directly. • When your method creates local objects, that method can call methods on the local objects.

Slide 96

Slide 96 text

def friends Friendship.where(user_id: @user.id).pluck('friend_id') end def find_images_of(friends) Image.where(user_id: friends, visible_to_friends: true) end def apply_age_restritions_to(images) images.where('minimum_age >= ?', @user.age) end def order_by(sorting) images.order(sorting) end def paginate(page) images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) end def decorate(images) images.map { |image| ImageDecorator.new(image) } end

Slide 97

Slide 97 text

def friends Friendship.friend_ids_of(@user) end def find_images_of(friends) Image.of(friends) end def apply_age_restritions_to(images) images.appropriate_for(@user.age) end def order_by(sorting) images.order(sorting) end def paginate(page) images.paginate(page) end def decorate(images) images.map { |image| ImageDecorator.new(image) } end

Slide 98

Slide 98 text

def images(sorting, page) images = find_images_of(friends) images = apply_age_restritions_to(images) images = order_by(images, sorting) images = paginate(images, page) decorate(images) end

Slide 99

Slide 99 text

def images(sorting, page) decorate Image .of(friends) .appropriate_for(@user.age) .order(sorting) .paginate(page) end

Slide 100

Slide 100 text

def images(page) friends = Friendship.where(user_id: @user.id).pluck('friend_id') images = Image.where(user_id: friends, visible_to_friends: true) images = images.where('minimum_age >= ?', @user.age) images = images.order('created_at') images = images.limit(PER_PAGE).offset(page.to_i.abs * PER_PAGE) images.map { |image| ImageDecorator.new(image) } end

Slide 101

Slide 101 text

def images(sorting, page) decorate Image .of(friends) .appropriate_for(@user.age) .order(sorting) .paginate(page) end

Slide 102

Slide 102 text

Code smells

Slide 103

Slide 103 text

• Duplicated Code • Long Method • Large Class • Long Parameter List • Shotgun Surgery • Feature Envy • Data Clumps • Case Statements • Parallel Inheritance Hierarchies • Speculative Generality • Temporary Field • Message Chains • Middle Man • Alternative Classes with Different Interfaces • Refused Bequest • Comments • Repetitive Boilerplate • и т.н....

Slide 104

Slide 104 text

Refactoring: Improving the Design of Existing Code. Martin Fowler

Slide 105

Slide 105 text

Out side of the box

Slide 106

Slide 106 text

Retrospection

Slide 107

Slide 107 text

Retrospection ✓ DRY

Slide 108

Slide 108 text

Retrospection ✓ DRY ✓ SLAP

Slide 109

Slide 109 text

Retrospection ✓ DRY ✓ SLAP ✓ Law of demeter

Slide 110

Slide 110 text

Retrospection ✓ DRY ✓ SLAP ✓ Law of demeter ✓ Tools

Slide 111

Slide 111 text

Retrospection ✓ DRY ✓ SLAP ✓ Law of demeter ✓ Tools ✓ Code smells

Slide 112

Slide 112 text

Retrospection ✓ DRY ✓ SLAP ✓ Law of demeter ✓ Tools ✓ Code smells ✓ Out side the box

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

Code, step by step https://github.com/RStankov/varna-conf-2012

Slide 115

Slide 115 text

No content

Slide 116

Slide 116 text

@rstankov Thanks for listening :)

Slide 117

Slide 117 text

Questions?