After writing, Why factories are better than classes in JS, I got a ton of feedback and corrections.
Let’s take this opportunity to dig into those suggestions, for my own knowledge, and for the benefit of anyone new to JavaScript.
Consider this a rebuttal and extension of the original post.
I originally proposed that JS class instances were vulnerable to properties being changed from the outside, and considered this a disadvantage.
For example, given a class, Car
, we create an instance with new
, and call the drive
method.
class Car { constructor(maxSpeed){ this.maxSpeed = maxSpeed; } drive(){…
Understanding data structures in Ruby/Rails is an easy way to supercharge your development speed.
Enums are designed to whitelist and specify “types” on model instances.
Enums (introduced in Rails 4.1) are a feature of Rails, not Ruby.
But are ARE enums?
They’re constants. They’re defined in advance and don’t change.
They’re used when an instance of a model can be only one of several predefined types.
If an item in a supermarket can only be a fruit or a vegetable (not both), it’s a great case for enums.
enum item_type: {
fruit: 0,
vegetable: 10,
meat: 20,
bread: 30
}
…
While working on a React app, I needed to hide a component when a user pressed the escape key.
A quick google search for “react custom hot keys”
recommended a library called react-hotkeys
.
Unfortunately it hadn’t been maintained in almost a year (as of 2020/11/17).
So I took advice straight from Droogan’s “Unmaintainable Code” and rolled my own.
Here’s how I did it.
Upon the component being displayed, I added an event listener on the keyup
event. This fires when any key is pressed.
window.addEventListener(
'keyup',
hideDisplay
)
hideDisplay
is a function we’ll write in a minute.
Then I wrote…
Are you familiar with the window
object?
As a backend developer hacking together frontends, I copy/pasted functions containing the window
object (without understanding it) more than I’d like to admit.
But as I push myself further into frontend development, it’s become useful to actually understand it.
In a nutshell, the window
object,
document
and screen
objects, which represent a site’s content and the shape of a device’s screen.Now rather than give a laundry list of window
object properties, here are 10 ways I’ve actually used…
Do you know why we often create objects with factories rather than classes?
I did not. As a backend developer forced to write JS during a fullstack freelance gig, I wrote factories because “that’s how the other code looked”.
Now that’s a terrible reason. The differences are simple, and a basic understanding will save you headaches.
In a nutshell, a factory is a function that returns an object, while a class is a template for an object.
But let’s walk through an example of a RocketShip
class to understand how behaviour differs.
Complete Code Pen.
Let’s write a class that…
Software engineers shouldn’t need to memorize all their language’s methods. We aren’t robots.
That said, this best applies to advanced concepts which aren’t common.
If you have to look up how to write a loop, you’re over your head.
Let’s review what #each
, #select
, #collect
and #map
do in Ruby.
Simply iterate over a collection of values.
[1,2,3].each {|x| puts x + 1 }
=> 2
=> 3
=> 4
Unlike, say #map
, you can’t assign the output of the block to another variable in the same line.
a = [1,2,3].each {|x| x + 1}
puts a.inspect
Note that +1
…
Let’s discuss Ruby’s version of range()
in python.
Ranges have a tonne of functionality, but there’s only a small subset that is very common in production usage.
Aka. The difference between 2 dots and 3dots in the range syntax.
2 dots includes the final value. 3 dots excludes the final value.
puts (1..5).to_a.inspect
=> [1, 2, 3, 4, 5]puts (1...5).to_a.inspect
=> [1, 2, 3, 4]
A range is simply a start value and an end value.
puts (1..5)
=> 1..5puts (1..5).class
=> Range
What’s the difference?
Many developers use them interchangeably without understanding what each does.
That’s not usually a problem… until it is.
Let’s walk through a few cases to understand their behaviour.
Create an array in memory and run each method on it.
arr = ['a','b','c','c']
puts arr.length #=> 4
puts arr.count #=> 4
puts arr.size #=> 4
Lo and behold, results are the same.
That said, count has more functionality. It can count specific values.
$ arr.count('c') #=> 2
It can also take a block and count elements where a condition is met.
$ arr2 = [1,2,3,4,5,6,7]
$ arr2.count …
I previously wrote about beginner algorithm questions in Python. This article converts those questions to Ruby.
Developers hate algorithm questions.
But they’re a reality. Even for those of us who’ve been shipping production code for years.
If you’re just beginning, they can be overwhelming. So it’s best to start easy and build your confidence.
Let’s do a few beginner questions.
The classic FizzBuzz question. Though I’ve never been asked it in an actual interview, it’s about as easy as algorithm questions get.
n
, return a list of all integers from 1 through n
.You can run 2 apps (frontend and backend) on the same Heroku dyno.
It’s just not straightforward… There’s a tonne of ways it won’t work, and only 1 that will.
Today we’ll walk though setting up a ReactJS/Node frontend and a Rails API backend.
My final implementation was inspired by this post on Heroku’s blog. But it’s quite long and meandering.
We’ll just walk through the meat and potatoes of get it working. No fluff.
Run these on the command line.
$ rails new fe-be-app --api --database=postgresql -T $ cd fe-be-app $ bundle install $ rake db:create $ rails s…