Skip to content

Instantly share code, notes, and snippets.

View adriangohjw's full-sized avatar
💚

Adrian Goh Jun Wei adriangohjw

💚
View GitHub Profile
@adriangohjw
adriangohjw / answer-options-with-images.html
Created September 12, 2021 20:40
Conversational Form in 65 lines of code - Here's how I built it
<fieldset cf-questions="What is your favourite band?">
<label for="fav-band-forests">Forests
<input type="radio" name="fav-band" id="fav-band-forests" value="Forests" cf-image="/assets/fav-band/forests.jpg">
</label>
<label for="fav-band-tmp">TMP
<input type="radio" name="fav-band" id="fav-band-tmp" value="TMP" cf-image="/assets/fav-band/tmp.jpg">
</label>
<label for="fav-band-xfr">Xingfoo&Roy
<input type="radio" name="fav-band" id="fav-band-xfr" value="Xingfoo&Roy" cf-image="/assets/fav-band/xfr.jpg">
</label>
@adriangohjw
adriangohjw / 1_query_object.rb
Created August 22, 2021 08:48
Kill long parameters with Method Chaining
class SalaryQuery
def new
@results = Salary.all
end
def call(companies:, source:, order_by:)
@results = @results.where(company: companies)
unless companies.blank?
@results = @results.where(source: 'user_submission')
@adriangohjw
adriangohjw / 1_line_breaks_after.rb
Created August 7, 2021 08:20
improving code readability: 5 simple tips
#=> better code (with logical line breaks)
def check_out(current_user)
if cart.count == 0
return Status.new(false, "empty cart")
end
total_price = cart.calculate_total_price
if current_user.balance < total_price
return Status.new(false, "not enough money")
@adriangohjw
adriangohjw / after.rb
Created August 7, 2021 08:14
improving code readability: abstractions
#=> better code (hiding implementation details with abstractions)
STATUS_EMPTY_CART = Status.new(false, "empty cart")
STATUS_PRODUCTS_UNAVAILABLE = Status.new(false, "some products are unavailable")
STATUS_INSUFFICIENT_MONEY = Status.new(false, "not enough money")
STATUS_SUCCESS = Status.new(true, nil)
def check_out(current_user)
cart = current_user.cart
return STATUS_EMPTY_CART if is_cart_empty(cart)
@adriangohjw
adriangohjw / after.rb
Created August 7, 2021 08:09
value objects to avoid primitive obsession
class Title
def initialize(title)
@title = title
end
def to_seniority
@title.include?('junior') ? 'junior' : nil
end
end
@adriangohjw
adriangohjw / after.rb
Created August 7, 2021 08:02
refactoring with query object
class PopularSalariesQuery
def all
# return data in whatever shape is needed
end
end
# Somewhere in the controller
@data = PopularSalariesQuery.new.all
@adriangohjw
adriangohjw / models.rb
Created June 21, 2021 06:49
Rails: Scenic gem for Database Views
class UserSalary < ApplicationRecord
attr_reader :a_but_different_name,
:b,
:c
end
class JobListing < ApplicationRecord
attr_reader :a,
:b_but_different_name,
:c
@adriangohjw
adriangohjw / dino.js
Created September 19, 2020 12:57
Run forever on Chrome Dino game
Runner.instance_.gameOver = () => {};
Runner.instance_.currentSpeed = 50;
🏆 814 Contributions in year 2024
📦 Used 352 MB in GitHub's Storage
📜 10 Public Gists
🔑 1 Public Keys
🚫 Not opted to Hire
We Are The Nerds: The Birth and T... █░░░░░░░░░░░░ 8%
Games People Play ████▍░░░░░░░░ 34%
System Design Interview – An insi... ██████▌░░░░░░ 50%
Designing Data-Intensive Applicat... ████████░░░░░ 62%
Ego Is the Enemy ███▎░░░░░░░░░ 25%