Javascript, React, This, and Programming Fundamentals

A friend of mine was teaching herself React and got confused by how the this keyword worked in JavaScript. Someone told her it was like self in Python. It’s not. So I typed up a longish Gist on how this works and how it applies to React. It’s not the only explanation of this out there, but I couldn’t find one that tied into React, so … there it is.

Anyhow, the bigger problem here is that she started learning React from a bunch of tutorials without a solid grasp of JavaScript fundamentals. My guess is that she wanted to learn React, learned just enough JavaScript to understand the React Hello World tutorial, but never got to the part about how this works and got really frustrated when she started running into weird behavior related to how React does (or used to do) auto-binding.

It’s not a new problem. Every so often, a new library or framework takes off and everyone goes, “Oh, I should learn that *thing* now.” And that *thing* is often built on other libraries or languages or abstractions but no one really wants to learn all of underlying fundamentals of the hot new thing, but the fundamentals aren’t really all that hot themselves. Like JavaScript and React, and before that, JavaScript and jQuery (cue all the Stack Overflow searches for “how do I do this in jQuery” when jQuery isn’t at all necessary, or useful). Or Python and Django. Ruby and Rails. Databases and ORMs.
Continue reading “Javascript, React, This, and Programming Fundamentals”

Property removals in TypeScript

A quick note about how to easily go from a broader type to a narrower one in TypeScript.

Suppose you have the following two interfaces:

interface ABC {
  a: number;
  b: string;
  c: boolean;

interface AB {
  a: number;
  b: string;

We want to convert a var from ABC to AB. Although ABC meets all the constraints of AB, the additional excess properties sometimes break things and TypeScript will, sometimes, complain about them.

So we could just do this:

declare var myVar: ABC;
delete myVar.c;

But that’s not really desirable because, assuming TypeScript even lets you do that, you’re mutating an existing typed object. myVar is still typed as ABC, even though it no longer has a c property.

I used to frequently write code that cloned myVar, deleted the extra property, and then typecast the clone to a new interface. But with the introduction of destructuring and spread operator support in TypeScript 2.1, it’s actually quite simple now:

let { c, ...newVar } = myVar

Tada! newVar is created without the c property, and TypeScript recognizes it

LGBT Wedding Cakes

This makes me uncomfortable. As much as I would prefer that bakeries not discriminate, I don’t like using the law to compel them to, both from a political perspective and from a legal one.

Politically, I think it’s a mistake. The argment for allowing gay marriage has long been that gay marriage has almost zero impact on straight persons. If you don’t support gay marriage, then don’t marry someone of the same sex. And many, if not all, conservatives understand that — especially in more libertarian areas. Gay marriage doesn’t change the rules for straight marriage. It doesn’t compel the clergy to officiate at gay weddings or live with a gay roommate or even be polite to gay people. But … now you can be compelled to bake a gay wedding cake. And that complicates the libertarian case for LGBT rights.

It also comes across as spiteful. Unless you think unhappy bakery owners make your wedding cake more delicious. Salty tears and such. Continue reading “LGBT Wedding Cakes”

On Charlie Hebdo and Sacred Cows

Cross-posted from Facebook.

A few of my more liberal friends seem to be taking the stance that “yes, it’s horrible to be killed for what you say, but c’mon, Charlie Hebdo was really racist / xenophobic / Islamophobic!” I don’t speak French and have no particular insight into French media, so I can’t really say it isn’t (although I think this Atlantic article below does a decent job of explaining what Charlie Hebdo actually is).

But I would like to point out that there is a difference between racist xenophobia and a general disdain for sacred cows. An attack on an institution, its beliefs, or its leadership is not the same as an attack on a group of people. It may very well offend many within that group, but offense is not malice. It may very well be wrong, but it is not wrong because it is racist or xenophobic.

By way of analogy, suppose Charlie Hebdo published an image of Thomas Jefferson fucking a slave. Right-thinking patriotic Americans would almost certainly take offense, but it wouldn’t be fair to simply describe the image as anti-American or America-phobic. It is less an assault on the American people and more a mockery of American exceptionalism. The left, most of all, should understand that distinction.

There’s an article floating around stating that Charlie Hebdo is not satire because satire is directed at the powerful and Muslims are not powerful in France. That may be the case (see above disclaimer about my lack of Frenchiness), but there’s a difference between mocking a people and mocking the icons and ideas which hold power over them. An unemployed marginalized refugee from Syria does not hold much power, but the restrictions against the depiction of Muhammad do.

That distinction matters. Much of the left is built on the principles of both respect for people without regard to their origin, and on “speaking truth to power”. But if you conflate a people with the things they believe, then following this first tenet excludes an entire class of “power” from the second. In its own way, an obscene depiction of a revered religious figure speaks truth to power as much as a protest that ridicules Wall Street, a speech by the Dalai Lama that “offends the feelings of the Chinese people” or, more topically, a satirical movie about the assassination of Kim Jong Un.

Pagination in Meteor

Meteor‘s a nifty “reactive” Javascript framework, and I’ve been plunking away on it with the help of the fantastic Discover Meteor book.  One thing I don’t quite agree with the book, however, is their Pagination Chapter (paywall, sorry) doesn’t actually implement traditional pagination so much as a variation on infinite pagination. That is, rather than show posts 1-10, then 11-20, their example shows 1-10, then 1-20, and so on.

This is potentially less-than-ideal for a couple of reasons. First, it forces the client to keep a growing number of posts in memory. 10-20 posts probably isn’t a big deal, depending on your app, but if you’re displaying hundreds of photographs (or animated GIFs), this could get pretty annoying. Granted, you can minimize the impact by only loading elements that are visible on screen, but it’s a headache that you don’t have to deal with traditional pagination.

The other potential issue is that, because Meteor is a real-time framework, the server needs to constantly monitor changes to the database and pass along those changes to the client. As the number of documents inside a database query grow, the number of updates being passed back increases as well. I haven’t been using Meteor long enough to know how much of an impact this makes in practice, but updating off-screen data is, at least, a sub-optimal use of bandwidth.

So, how do we work around this?

Continue reading “Pagination in Meteor”

Hong Kong


It looks like the Occupy protests in Hong Kong are winding down. I had the chance to see what was actually happening on the ground last week and to talk with some people there. A few take-aways from my point of view:

The majority of Hong-Kongers probably don’t support continued occupation of city streets. I think recent polls have it at somewhere around 60-80% wanting the protesters to leave. I would further guess that overall support varies drastically based on age and how often they drive.

The challenge for pro-democracy activists is to figure out how to turn this into a long-run movement. Opposition to Occupy isn’t based so much on opposition to universal suffrage as a belief that direct confrontation with mainland China is a losing proposition.

Continue reading “Hong Kong”

Is it Constitutional to Suspend Deportations? What About Work Permits?

As much I’m OK with suspending deportations from a policy perspective, can the president constitutionally suspend deportations?

If you replace “suspend deportations” with “we will deport unauthorized immigrants who have not committed a violent crime ONLY AFTER we finish deporting every unauthorized immigrant convicted of a violent crime” (read: a long, long time from now — if ever), then that just sounds like prosecutorial discretion. It’s no different that letting someone arrested for public intoxication out of jail because we don’t have the resources to try every publicly drunk person out there. That doesn’t mean we technically can’t re-arrest that person and try them later (at least until the statute of limitations runs out), it just means we’re not going to do it any time soon.

One catch: Obama is saying that the deportation suspension will last three years. But he’s only in office for another two. If his (possibly Republican) successor decides to re-start deportations, I’m not sure that the Obama’s three-year promise will carry much weight in court.

What I’m not so sure about is the granting of work permits. You could argue that this power is incident to the president suspending (or deferring) deportations, but that seems like a stretch. This would be an affirmative act by the executive branch, as opposed the president simply declining to spend a limited pool of resources in a certain way. And given that Congress has already capped the total number of work-related visas to begin with, it’s an affirmative act that conflicts with Congress’s stated intent.

One possibility is to simply treat the “work permits” as the president saying he will defer prosecuting any employers who hire unauthorized immigrants with such a permit. But again — seems like a stretch.

Cross-posed on Facebook.

California Propositions 2014

Cross-posting my thoughts on this election cycle’s propositions for posterity’s sake:

Prop 1 – Yes. Spend more money on much-needed water infrastructure. The con argument as far as I can tell is that isn’t a magic bullet for CA’s drought problems (which largely stem from farming water-intensive crops in dry areas) and general concerns that the state isn’t very good at handling large sums of money (which would be persuasive if there were some more responsible group that we could hand the money to).

Prop 2 – Yes. Toughen CA’s rainy day fund. Good governance generally.

Prop 45 – No. I’m a bit torn on this actually (as is the Democratic Party apparently — Senators Boxer and Feinstein endorse but Nancy Pelosi opposes. Prop 45 requires that the Insurance Commissioner approves any rate hikes by insurance companies. In theory, this allows regulators to keep premiums down, but I’m not convinced that a lack of regulation is the reason for crazy health insurance premiums. Lack of pricing transparency and excess bureaucracy seem to play a bigger role, and Prop 45 doesn’t address that. However, Prop 45 would introduce additional delay and administrative uncertainty to putting new health plans on the Covered California healthcare exchange, which would reduce some of the options available to new enrollees. So, leaning no on this, but happy to hear from someone more familiar with how healthcare works.

Prop 46 – No. This proposition mixes together a bunch of medical malpractice issues that should really be addressed separately. The big one is that it increases the $250K cap on pain and suffering in medical malpractice lawsuits to $1M. This isn’t a big deal in and of itself — the $250K cap was put in place in 1975. Adjusted for inflation, that’s about $1M in today’s dollars (and really, as a lawyer, I’m not one to dispute higher jury awards).

What I don’t like though is that it requires mandatory drug testing of doctors whenever an “adverse event” occurs. Ugh. Being a doctor is already a pretty demoralizing job. Mistakes happen because doctors are human, and overworked sleep-deprived humans at that. Peeing in a cup won’t fix that.

Prop 47 – Yes. Prop 47 reduces penalties for drug and other minor crimes. Harsh sentencing guidelines haven’t done much to actually reduce drug use or petty theft. They have, however, cost us obscene amounts of money and wrecked havoc on civil liberties.

Prop 48 – Yes. The story behind Prop 48, as far as I can tell, is that when the state approved casinos for Native American tribes, a couple tribes got screwed by technicalities and left out. This just puts those tribes on equal footing with the rest of the tribes. Also, casinos don’t bother me and the tribes are giving us money.