Skip to content

Instantly share code, notes, and snippets.

@jayzz55
jayzz55 / deploy_script.sh
Created February 13, 2022 13:49 — forked from julianrubisch/deploy_script.sh
Hatchbox phoenix configuration
# Phoenix
if grep -q '^ "phoenix"' mix.lock; then
source $HOME/.asdf/asdf.sh
source $HOME/.asdf/completions/asdf.bash
export $(cat $RELEASE_DIR/../../.rbenv-vars | xargs)
echo '
-----> Detected Phoenix app.'
mix local.rebar --force
mix local.hex --force
@jayzz55
jayzz55 / signal_catching.rb
Created September 10, 2020 14:51 — forked from sauloperez/signal_catching.rb
How to catch SIGINT and SIGTERM signals in Ruby
# Signal catching
def shut_down
puts "\nShutting down gracefully..."
sleep 1
end
puts "I have PID #{Process.pid}"
# Trap ^C
Signal.trap("INT") {
@jayzz55
jayzz55 / gist:ba60150a0722fe9d257c3c5c581cb775
Last active December 19, 2018 22:04
scala contramap example
F[A] Ordering[Int]
B => A listing => listing.id
F[B] Ordering[Listing]
val intOrderer: Ordering[Int]
val listingOrderer: Ordering[Listing] = intOrderer.contraMap(listing => listing.id)
See: http://igstan.ro/posts/2013-10-31-contravariant-functors-an-intuition.html

2: Performing Work

Sending a strong message

The foundation of an object oriented system is the message.

Strong and confident messages requires three elements:

  1. We must identify the messages we want to send in order to accomplish the task at hand.
  2. We must identify the roles which correspond to those messages.
  3. We must ensure the method's logic receives objects which can play those

9: Designing Cost-Effective Tests

3 Skills Needed to Write Changeable Code

  • Understand OO design
  • Skilled at refactoring code
  • Ability to write high-value tests

Changeability is the only design metric that matters; code that's easy to change is well-designed.

>Good design preserves maximum flexibility at minimum by putting off decisions at every opportunity, deferring commitments until more specific requirements arrive. When that day comes, refactoring is how you morph the current code structure into the one what will accommodate the new requirements.

8: Combining Objects with Composition

Composition describes a has a relationship.

  • Composition: objects "inside" have no meaning outside that context

  • Aggregation: like composition except objects "inside" have meaning outside that context

Deciding Between Inheritance and Composition

7: Sharing Role Behavior With Modules

Combining the qualities of two existing subclasses is something Ruby cannot do (multiple inheritance)

Because no design technique is free, creating the most cost-effective application requires making informed tradeoffs between the relative cost and likely benefits of alternatives

Writing Inheritable Code

  • Recognize the antipatterns
  • Insist on the abstraction

6: Acquiring Behavior Through Inheritance

Inheritance is, at it's core, a mechanism for automatic message delegation. It defines a forward path for not-understood messages.

Subclasses are specializations of their superclasses

Template Method Pattern

Any class that implements the template method pattern must supply an implementation for every message it sends, even if all it does is raise a NotImplementedError.

5: Reducing Costs with Duck Typing

Duck types = public interfaces not tied to any specific class

It's not what an object is that matters, it's what it does.

Concrete code is easy to understand, but costly to extend. Abstract code may initially seem more obscure but, once understood, is far easier to change.

Once you begin to treat your objects as if they are defined by their behavior rather than by their class, you enter a new realm of expressive design.

4: Creating Flexible Interfaces

Defining interfaces

Imagine an Object's interface is like a kitchen's interface. If a customer at a restaurant would like to order food, he will order the food and communicate with the kitchen through "menu". Menu the kitchen's interface.

The customer knows what it wants, and trust the kitchen to deliver what it wants. The customer does not need to know How the food will be prepared, nor does the customer tell the kitchen how to prepare it.

The kitchen only exposes it's public interface, Menu, and hides it's internal private interface such that customer can not go inside the kitchen and stir the pot.