22
Let’s Code Sameer Soni [email protected]

Let's code

Embed Size (px)

Citation preview

Page 1: Let's code

Let’s CodeSameer Soni [email protected]

Page 2: Let's code

–C.A.R. Hoare, The 1980 ACM Turing Award Lecture

“There are two ways of constructing a software design: One way is to make it so simple that there

are obviously no deficiencies and the other way is to make it so complicated that there are no obvious

deficiencies.”

Page 3: Let's code

– E. W. Dijkstra

“The computing scientist’s main challenge is not to get confused by the complexities of his own

making.”

Page 4: Let's code

Kata• is a Japanese word. • are detailed choreographed patterns of

movements practised either solo or in pairs.

• originally were teaching and training methods by which successful combat techniques were preserved and passed on.

• Practicing kata allowed a company of persons to engage in a struggle using a systematic approach, rather than as individuals in a disorderly manner.

• basic goal of kata is to preserve and transmit proven techniques and to practice self-defence.

• is a term used by some Software Craftsmen, who write small snippets of code to build muscle memory and practise craft much like soldier, musician, dancer or doctor.

- wiki

Page 5: Let's code

Ruby Functional Programming

Statement was incorrect until we were in school and hadn’t been introduced to PROGRAMMING.

Expression is quite common in Imperative style of programming.

But we don’t realise the side effects and pay heavy cost.

x = x + 1

Page 6: Let's code

TheoryFunctional programming treats computation as evolution of mathematical functions and avoids state and mutable data.

Promotes code with no side effects, no change in value of variables.

Discourages change of state.

Cleaner Code - Variables are not modified once defined.

Referential transparency - Expressions can be replaced by functions, as for same input always gives same output.

Advantages

Benefits of RT: 1. Parallelization 2. Memoization 3. Modularization 4. Ease of debugging

Page 7: Let's code

Rules - Don’t update variables

Page 8: Let's code

No • indexes = [1,2,3] • indexes << 4 • indexes # [1,2,3,4]

Yes • indexes = [1,2,3] • all_indexes = indexes + [4] #[1,2,3,4]

Don’t append to arrays or strings

Page 9: Let's code

No • hash = {a: 1, b: 2} • hash[:c] = 3 • hash

Yes • hash = {a: 1, b: 2} • new_hash = hash.merge(c: 3)

Don’t update hashes

Page 10: Let's code

No • string = “hello” • string.gsub!(/l/, 'z') • string # “hezzo

Yes • string = "hello" • new_string = string.gsub(/l/, 'z') # "hezzo"

Don’t use bang methods which modify in place

Page 11: Let's code

No • number = gets • number = number.to_i

Here, we are not updating number but overriding the old values, which is as bad as updating. Rule is: Once defined, its value should remain same in that scope

Yes • number_string = gets • number = number_string.to_i

Don’t reuse variables

Page 12: Let's code

Blocks as higher order functionsA block is an anonymous piece of code you can pass around and execute at will.

Page 13: Let's code

No • dogs = [] • ["milu", "rantanplan"].each do |name|

dogs << name.upcase end

• dogs # => ["MILU", "RANTANPLAN"]

Yes • dogs = ["milu", "rantanplan"].map do |name|

name.upcase end # => ["MILU", "RANTANPLAN"]

init-empty + each + push = map

Page 14: Let's code

No • dogs = [] • ["milu", "rantanplan"].each do |name|

if name.size == 4 dogs << name end end

• dogs # => [“milu"]

Yes • dogs = ["milu", "rantanplan"].select do |name|

name.size == 4 end # => ["milu"]

init-empty + each + conditional push = select/reject

Page 15: Let's code

No • length = 0 • ["milu", "rantanplan"].each do |dog_name|

length += dog_name.length end

• length # 14

Yes • length = ["milu", "rantanplan"].inject(0) do |accumulator, dog_name|

accumulator + dog_name.length end # => 14

initialize + each + accumulate = inject

Page 16: Let's code

1st way: hash = {} input.each do |item| hash[item] = process(item) end hash

How to create hash from an enumerable

2nd way: Hash[input.map do |item| [item, process(item)] end]

input.inject({}) do |hash, item| hash.merge(item => process(item)) end

Page 17: Let's code

# Way 1

if found_dog == our_dog name = found_dog.name message = "We found our dog #{name}!" else message = "No luck" end

Everything is a expression

# Way 2

message = if (found_dog == my_dog) name = found_dog.name "We found our dog #{name}!" else "No luck" end

Page 18: Let's code

Exercise

"What's the sum of the first 10 natural number whose square value is divisible by 5?"

Page 19: Let's code

Ruby Functional wayInteger::natural.select { |x| x**2 % 5 == 0 }.take(10).inject(:+) #=> 275

Page 20: Let's code

Ruby Imperative way

n, num_elements, sum = 1, 0, 0 while num_elements < 10 if n**2 % 5 == 0 sum += n num_elements += 1 end n += 1 end sum #=> 275

Page 21: Let's code

Source • wikipedia.com • code.google.com

Page 22: Let's code

Thanks