Programming Tip: Open and Close at the Same Time

One useful programming habit I picked up at some point is: if you open or start something, immediately close it or end it. If you open a bracket, immediately write its closing bracket. If you open a file, immediately write the code to close it.

These days, development environments take care of the niggling little details like matching parentheses and brackets for you. That’s great, but that’s just syntax. The same principle extends further, and automatic tools can’t guess what it is you want to do.

There’s a problem in a lot of code called a resource leak. The classic example is memory leaks in C: the code asks for, and gets, a chunk of memory. But if you don’t free the memory when you’re done with it, then your program will get larger and larger — like a coffee table where a new magazine is added every month but none are ever taken away — until eventually the machine runs out of memory.

These days, languages keep track of memory for you, so it’s easier to avoid memory leaks than it used to be. But the best way I’ve found to manage them is: when you allocate memory (or some other resource), plan to release it when you’re done.

The same principle applies to any resource: if you read or write a file, you’ll need a file handle. If you never close them, they’ll keep lying around, and you’ll eventually run out. So plan ahead, and free the resource as soon as you’ve alocated it:

Once you’ve written

open INFILE, "<", "/path/to/myfile";

go ahead and immediately write the code to close that file:

open INFILE, "<", "/path/to/myfile";
close INFILE;

and only then write the code to do stuff with the file:

open INFILE, "<", "/path/to/myfile";
while ()
{
	print "hello\n" if /foo/;
}
close INFILE;

The corollary of this is, if you’ve written the open but aren’t sure where to put the close, then you may want to take a look at the structure of your code, and refactor it.

This same principle applies in many situations: when you open a connection to a remote web server, database server, etc., immediately write the code to close the connection. If you’re writing HTML, and you’ve written <foo>, immediately write the corresponding </foo>. If you’ve sent off an asynchronous AJAX request, figure out where you’re going to receive the reply. When you throw an exception, decide where you’re going to catch it.

And only then write the meat of the code, the stuff that goes between the opening and closing code.

As I said, I originally came across this as a tip for avoiding memory leaks. But I’ve found that doing things this way forces me to be mindful of the structure of my code, and avoid costly surprises down the line.

Posted in Hacking, Things I've Learned | Tagged , , | 3 Comments

“It’s Getting Old” Isn’t A Rebuttal

One response that I see a lot on Twitter and elsewhere is some variation on “calling Trump supporters racist is getting old”. And maybe I’m growing stupid as I grow older, but it finally dawned on me what was bugging me about that.

I’ve been in plenty of discussions where one side or another used an argument that had been debunked long ago — I used to debate creationists on Usenet, after all. But this feels different. It’s “Oh God, here we go again”, but not in a “now I need to dig up the FAQ one more goddamn time” way.

“Calling me racist is old” is all about style, not substance. It doesn’t say “I’m not a racist”, it merely says “Stop using those words”. It’s not about hearing a false factoid that just won’t die; it’s about hearing a joke for the millionth time that wasn’t even that funny to begin with.

It’s the sort of thing you say when you’re trading insults or yo-mamma jokes with someone. It’s not serious. It doesn’t matter. Certainly neither you nor anyone you know or care about is in danger of losing their health insurance, or die from a back-alley abortion, or have ICE break into their home and ship them to a foreign country.

It is, in short, something said by those who have the luxury of caring only whether their team wins, not whether said win is going to have any real-world repercussions.

Posted in Uncategorized | Tagged , , , | Leave a comment

Triggered by the Right Side of History

There’s a bit of controversy going on at Yorktown High School in Virginia, where teachers have put up signs:

Screen capture of controversial sign at Yorktown High School

Patriots Know:

Facts are not political
Diversity stengthens [sic] us
Science is real
Women’s rights are human rights
Justice is for all
We’re all immigrants
Kindness is everything
We are Yorktown

I gather that “Patriots” is the name of the school’s sports team, and by extension refers to the student body, not simply people who love their country.

What’s odd here is that conservatives have complained about these signs being overly-political. TV personality Tucker Carlson is quoted as saying,

Carlson called the signs “the sneakiest type of propaganda… propaganda passing itself off as obvious observations.” He asked [senior student John] Piper if anyone at the school thinks that science “is not real.”

Oh, I’m sure you could find some Juggalos to tell you that

Water, fire, air and dirt
Fucking magnets, how do they work?
And I don’t wanna talk to a scientist
Y’all motherfuckers lying, and getting me pissed

but I’ll concede Carlson’s point: pretty much everyone thinks that science is real, or at least supports that notion. But of course not everyone knows that science isn’t a body of knowledge, but a method for figuring out what’s true. Not only that, but a lot of people are very selective about which scientific findings they accept. And, well, not to put too fine a point on it, one major US party (hint: it rhymes with “Reschmuglican”) has turned into an anti-science party.

And therein lies the problem: as long time reader Fez pointed out, if the statements on the signs are seen as political — and specifically leftist — it’s only because the political right has rejected much that should be uncontroversial. Like the reality of climate change, and it doesn’t matter how many jobs you save if New York is under water and Nebraska is too arid for anything to grow.

Likewise, even though it’s obvious that women’s rights are human rights, since the eighties the Republican party has been running on the idea that imply, even if it’s rarely made explicit, that women take a back seat to men, and that their rights rank below those of a pre-sentient (not merely pre-sapient) bundle of cells.

Likewise diversity, justice for all, and tolerance of immigrants. The American political right is on the wrong side on all of these issue, and I think they know it and feel defensive about it.

This all reminds me of something Patton Oswalt wrote a little while ago:

But when I Tweet something POSITIVE, or HOPEFUL, in support of a group that’s been made to fear or doubt because of Trump and his ghoul brigade’s actions? A helpful link for peaceful action? Praising someone who speaks up eloquently against the smirking racism of Trump’s parking lot carnival of an administration?

THAT’S when the responses get violent, and threatening, and ominous. As if the language itself — the grammar of thoughtfulness — lands in their guts like glass shards. Empathy and understanding literally feel like an attack to them.

I don’t think he’s quite right about this, but I have to say that the Yorktown HS kerfuffle is data in his favor.

So if positive ideas bother you, or the implications of those ideas (e.g., if we’re all immigrants, then maybe someone who looks and speaks differently from you, whose cooking smells weird, and whose accent you can’t decipher, might move in next door to you), perhaps it would be a good idea to think about what it is that bothers you, and whether your fears are justified. Or, for that matter, whether you’re on the right side of history.

 

Posted in News, Politics | Tagged , , | Leave a comment

Now What?

So we’ve survived the first week of Trump’s presidency. Have some cake. If you were one of the many people who took part in activism, pat yourself on the back. If you weren’t, it’s not too late to start.

It’s great that everyone’s riled up. And while we’re pumped up and paying attention to government, it might be worth figuring out what our long-term plans should be. Here’s my list, in no particular order:

Gerrymandering reform

In case you forgot, gerrymandering is the practice of drawing legislative districts to favor one party (see, for instance, this map of Maryland). Gerrymandering is one of the factors pushing divisions between the left and right: Representatives can be attacked for being insufficiently ideologically pure, which pushes them away from the center, and have no real incentive to compromise.

For me, as a Marylander, it means that the Republicans have written me off, and the Democrats take me for granted. I’d like both parties to court my vote, and for the biyearly congressional elections to be a meaningful referendum on Representatives’ job performance.

Electoral College reform

I think everyone agrees that while the Electoral College may have been useful at one time, it’s not the XVIII century anymore. Time to get with the times and implement majority vote.

Since the Electoral College is enshrined in the Constitution, there’s no way to eliminate it without an amendment, which is difficult. But there’s a hack: each state can pass its own laws about how its Electors vote. And in most states, they have to vote the way a majority of that state’s voters voted, which makes perfect sense. But what if each state had a law saying that its Electors will vote whichever way the entire US voting population voted?

Obviously, people in Massachusetts will be upset if a Republican gets all of their Electoral votes just because he won a majority of the US vote, just as Oklahomans won’t like their Electoral votes going to a Democrat. But this already happens, in effect, in that people get a president they don’t want.

Of course, you don’t want your state to be the only one that apportions its Electors this way. This only makes sense if there are enough states doing this, that they can decide the outcome of the election — that is, if there’s a group of states that adds up to 270 Electoral votes or more.

Thankfully, there’s a project to do exactly that. Contact your state legislators and encourage them to join in.

Ranked voting

This one’s more of a long shot than the others, but I’ll throw it out there anyway. In our current system, you can only vote for one candidate, and whoever gets the most votes wins. This leads to a problem with third-party supporters. In 2000, if you were liberal, maybe you liked Green Party candidate Ralph Nader, could tolerate Democrat Al Gore, and disliked Republican George Bush. So do you vote for Nader, knowing that he can’t win, and that you’re taking away a vote for Gore (and against Bush)? Or do you hold your nose, vote for Gore against Bush, and help confirm the idea that third parties don’t stand a chance?

Under ranked voting, also known as instant-runoff voting, you vote for multiple candidates, ranking them in order of preference. Our hypothetical voter, above, might vote

  1. Ralph Nader
  2. Al Gore
  3. George Bush

meaning “I like Nader, but I’ll settle for Gore.”

Yes, there are problems with ranked voting, and there are situations where it fails. But its problems are rarer and less severe, I believe, than those with our present system.

Campaign finance reform

This is related to the previous item, in that the current systems helps perpetuate a system where only the major players have a chance. If candidates were treated equally, say all given $100 million to make their case, then it would make it more likely that candidates are judged by their experience and policies, rather than their ability to raise money.

On the other hand, there’s a danger that extremist whackjobs might appear reasonable by virtue of being treated as equals with sane-party candidates. But then again, given who’s living in the (Oh So Very) White House right now, we may be past that point already.

While I don’t have a firm opinion on this topic and am open to being educated, I do think the Citizens United SCOTUS decision needs to be overturned. In case you forgot, that’s the one that said that donating money to a campaign is political speech, and since you can’t abridge free speech, you can have unlimited amounts of money pouring into politics.

Education, education, education

This one is fundamental. We need better education, and more of it.

People complain about American jobs being shipped overseas. But most of those are unskilled jobs. It’s never going to be cheaper to hire an American than a Bangladeshi, or a robot. So let’s prepare our population for better jobs.

For starters, we can fund elementary and high schools properly. I’m ashamed for my country every time I hear of a teacher having to buy supplies out of her own pocket. Federal funds can help with this: when I pick up the phone to talk to tech support, I might get someone who went to school in Arkansas or Oregon, so it’s in my benefit to help education in other states.

College is crazy expensive. The University of Maryland, a state university, estimates that it’ll cost $25,000 per year to send your child there. $47,000 if you’re not a Maryland resident. That’s mortgage-level expensive.

Why can’t we bring the costs down? One simple approach would be an education tax. Raise taxes on everyone by a bit, and bring tuition costs down a lot for those going to college. This would have all sorts of knock-on effects: more people getting educated; more people inventing new things, or writing books, or starting businesses; more people making a better living; more people hiring other people.

And I think that’ll do it for now.

Posted in Politics | Tagged , , , , , , | Leave a comment

Women’s March, Rights, and Politics

I attended the Women’s March on Washington, yesterday. It turned out, I’m told, to be the largest inauguration protest in the history of the United States, and possibly the largest political protest ever, if you count the sister marches in other cities around the globe (including Antarctica).

At one point, we ran into, I believe, the American Socialist Party. They are, as I believe, the Communist-Lite bunch that Sean Hannity warned you about. I don’t remember seeing them out on the Mall before, so I suspect that they may have stepped up their activities in recent years.

If they have, they’re not alone. Witness the popularity of Bernie Sanders, who may not have won so much as the Democratic nomination, but got pretty damn far for an American who describes himself as a democratic socialist.

But of course he and Clinton lost the presidency to Trump. He may not have won a majority of votes, but he did get 46%, a not inconsiderable amount.

So what this all seems to suggest is a repeat of history: we’re living in a new gilded age, with income inequality at record-high levels, and populist factions appear to be gaining popularity in response: on one hand, on the left, people like Sanders and Warren, who promise to, basically, make the rich bastards pay their fair share so that the little guy can get a fair shake. I’m pretty sure that the communists of the early 20th century had the same message, and that that’s what made them so appealing to so many.

And on the right, there’s Donald Trump, who would be very happy to be the object of a cult of personality, and clearly feels most at home at the top of an autocratic dictatorship like — yeah, I’m gonna say it — Nazi Germany or Fascist Italy.

I can’t prove that history is repeating itself, but it does look that way. And so, we are faced with the problem of how to avoid both a fascist dictatorship and a communist dictatorship (thankfully, the two look so much alike (the key word is “dictatorship”) that we really only need one plan for both contingencies).

Of the two, I’m far more worried about the fascist dictatorship: these days, in America, the right is the side more likely to make threats of violence (“Second Amendment remedies, anyone?). The lefties I’ve met are far more likely to get everyone’s input, not execute kulaks.

On the right, on the other hand, I see more right-wing authoritarians (RWAs) who enjoy having a strongman in charge, and have a history of passing laws to prevent people from voting.

Posted in Politics | Tagged , , | Leave a comment

Does Christianity Offer the Best Basis for Science?

There’s an argument I’ve run across several times, that theism, and specifically Christianity, forms a much better basis for science than does atheism. Indeed, some people go so far as to claim that only Christianity provides a foundation for science. Matt Slick at CARM lays it out well (though Don Johnson Ministries makes a similar argument). After listing a number of influential scientists who were Christians, Slick writes:

To many Christians, the idea that God existed and brought the universe into existence meant that the universe could be understood because God was a God of order and his character would be reflected in creation (Rom. 1:20).  Instead of a Pantheon of gods who ran the universe in an unpredictable fashion, Christianity provided the monotheistic bedrock (Isaiah 43:10; 44:6,8; 45:5) upon which the scientific study of nature could be justified.  Many Christians expected to find the secrets that God had hidden in the universe and were confident in being able to discover them.  This is a critical philosophical foundation that is necessary if an emerging culture is to break the shackles of ignorance and superstition in order to discover what secrets exist in the world around them.

This emphasis on order seems odd, since one of the main features of Christianity is miracles, that is, violations of natural law. Without at least the resurrection of Jesus, there is no Christianity. Add to that the various miracles Jehovah, Jesus, and various and sundry saints are said to have performed, the common notion that God sometimes responds to prayer by performing additional miracles, and weekly transubstantiation in church, and you get a picture of reality in which any regularities, any laws of nature exist only so long as a malleable deity permits them to exist.

If scientists like Kepler and Newton saw the Christian God as fundamentally one of order rather than caprice, and drew inspiration for their scientific pursuits from that, fine. But that’s hardly the only type of Christianity out there. I doubt that theirs was even a majority view. But in a time and place where pretty much everyone was Christian (and where not being Christian often carried either social stigma or legal penalty), of course Christians are going to be the ones doing science.

It seems to me that Taoism is a much better match for Matt Slick’s description than Christianity. You could, I think, make a strong case for the notion that the Tao is natural law. There’s certainly the notion that you can either go with the Tao, or you can wear yourself out trying to go against it.

(Yes, this still leaves the question of why so many scientific discoveries came from Europe rather than China. But that’s an interesting question for another day. I suspect that the fact that Europeans wrote American history textbooks has something to do with it.)

I suppose it wouldn’t do to mention alchemy and algebra, whose prefix “al” betrays their Muslim origin. Or the fact that a large proportion of visible stars have Arabic names.

I also don’t see why it takes a whole religion or worldview to want to figure out what makes the world tick. Anyone can see that day follows night, summer follows spring, rocks always roll downhill, never up, and that oaks only come from acorns. Clearly there are some regularities, and these can be investigated. We’re curious creatures; figuring stuff out is fun.

There’s a related claim to the one that Christians founded all the sciences: that Christians founded all the major universities. I haven’t checked this, but I see no reason to doubt this claim.

This brings me to my final point: let’s grant, at least for the sake of argument, that Christians, motivated by their understanding of God as a lawmaker, got all of the sciences started; that most or all of the major universities were founded as institutions to learn how God set up the universe; that Christianity is the only religion — the only worldview — that could have kickstarted science this way, and that out of those beginnings grew science as we know it today… so what? Why keep religion around today?

A scaffolding is essential when beginning a new building. But after a certain point, it needs to go. I was on an all-milk diet for the first, crucial part of my life, and that helped make me into the person I am today. But that doesn’t mean that I should continue to drink milk as an adult; I especially shouldn’t be on an all-milk diet.

Whatever benefits religion may once have provided to science, these days it just gets in the way, from creationism to anti-gay “conversion therapy” to faith-based climate change denialism. It’s time to jettison it.

Posted in Uncategorized | Tagged , , , , , , , | Leave a comment

A Modest Proposal for Anti-Abortion Catholics (and Some Others)

When I recently ran across yet another of BillDo’s rants against abortion, I was struck by an idea: during transubstantiation, a priest turns a piece of bread into living flesh. But surely this is a reversible operation, no? People turn living wheat into nonliving bread all the time.

In addition, if there’s any kind of conservation law, the after centuries of Catholic rites, there’s bound to be mountains of bread accumulating somewhere, that could be put to good use.

So I propose the following: if a woman wants an abortion, a priest can cast a reverse-transubstantiation spell, and turn the fetus into a piece of bread. And then the abortion can proceed normally.

If Catholic priests can’t or won’t do this, then I’ll do it. I’m ordained, and I have as much evidence to back up my supernatural claims as they do.

Posted in :-), Politics, Religion | Tagged , , , | Leave a comment