- Somos mejores programadores
- Podemos reutilizar código
- Generamos código más fácil de entender para los demas y para nosotros mismos
- Somos más felices
- Nos permite liberar código open source
var a = '1'
var b = 1
a == b
a === b
El module pattern define una forma para definir modulos usando IIFE
Immediately-Invoked Function Expression
Son funciones que se mandan a llamar cuando se definen:
#basic structure
(function(){ /* code */ }());
var SampleModule = (function(){
'use strict';
var my_fucking_private_function = function () {
return 'private method';
}
var my_fucking_public_function = function () {
return 'public method';
}
return
{
public_function: my_fucking_public_function
}
}());
#trying to call module function
![alt text](images/khe.png)
SampleModule.public_function()
var return_json = function () {
return
{
}
}
#basic structure
(function(){ /* code */ }());
var SampleModule = (function(){
'use strict';
var my_fucking_private_function = function () {
return 'private method';
}
var my_fucking_public_function = function () {
return 'public method';
}
return
{
public_function: my_fucking_public_function
}
}());
#trying to call module function
SampleModule.public_function()
Dont Repeat Yourself
Cuando lo usas? Siempre!
Keep It Simple, Stupid!
Cuando lo usas? Siempre!
A class should have only a single responsibility (i.e. only one potential change in the software's specification should be able to affect the specification of the class)
class DealProcessor
def initialize(deals)
@deals = deals
end
def process
@deals.each do |deal|
Commission.create(deal: deal, amount: calculate_commission)
mark_deal_processed
end
end
private
def mark_deal_processed
@deal.processed = true
@deal.save!
end
def calculate_commission
@deal.dollar_amount * 0.05
end
end
“software entities … should be open for extension, but closed for modification.”
class UsageFileParser
def initialize(client, usage_file)
@client = client
@usage_file = usage_file
end
def parse
case @client.usage_file_format
when :xml
parse_xml
when :csv
parse_csv
end
@client.last_parse = Time.now
@client.save!
end
private
def parse_xml
# parse xml
end
def parse_csv
# parse csv
end
end
class UsageFileParser
def initialize(client, parser)
@client = client
@parser = parser
end
def parse(usage_file)
parser.parse(usage_file)
@client.last_parse = Time.now
@client.save!
end
end
class XmlParser
def parse(usage_file)
# parse xml
end
end
class CsvParser
def parse(usage_file)
# parse csv
end
end
“objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program.” See also design by contract.
una violacion de este principio
class Rectangle
def set_height(height)
@height = height
end
def set_width(width)
@width = width
end
end
class Square < Rectangle
def set_height(height)
super(height)
@width = height
end
def set_width(width)
super(width)
@height = width
end
end
“many client-specific interfaces are better than one general-purpose interface.”
one should “Depend upon Abstractions. Do not depend upon concretions.” high-level (think business logic) objects not depending on low-level (think database querying and IO) implementation details
class UsageFileParser
def initialize(client, parser)
@client = client
@parser = parser
end
def parse(usage_file)
parser.parse(usage_file)
@client.last_parse = Time.now
@client.save!
end
end
class XmlParser
def parse(usage_file)
# parse xml
end
end
class CsvParser
def parse(usage_file)
# parse csv
end
end
ejemplps extraídos de https://robots.thoughtbot.com/back-to-basics-solid
- Usar un sistema de control de versiones
- Usar testing
- Replantear el código que acabas de terminar
- Refactor
- No re-inventar la rueda, entender cómo rueda
- Contribuir al open source <3