Friday, April 29, 2011

Android: files on internal storage.

I'd only read/written to SD card files before. When you're writing to the SD card, you can use standard Java file I/O. But if you want to write to internal storage, you can't, because you're supposed to ignore where the files go.

You can do so with Context.openFileInput() or Context.openFileOutput(). When you do, the files will be in /data/data/your_project_package_structure/files/your_file. (yeah, that's two /data/s.) For example:

More details: official docs and someone's tutorial (which is a lot more useful).

Tuesday, April 19, 2011

Building a .vimrc: tabs

Just nuked my hard drive and am explicitly pulling data back from my Time Machine as I need it. I could grab my old .vimrc, but I think I'll build a new one instead. (the old one wasn't very big.)

First things first: tabs.

" whenever I hit the tab key, it now inserts spaces instead.
set expandtab

" whenever I hit << or >>, it now indents this many spaces instead.
set shiftwidth=2

" tabstop is how many spaces a tab character looks like.
set tabstop=2

Note that if you set expandtab, and if you never open a file that already contains tab characters, you wouldn't need tabstop.

More info:

Friday, April 8, 2011

Null considered harmful?

So I subscribed to Gary Bernhardt's Destroy All Software screencasts. I've only watched the demo one ("How and why to avoid nil"), but I think it's pretty good! (disclaimer: I know him IRL; un-disclaimer: he's very good at software; in conclusion, you ought to subscribe too)

The message is pretty simple: avoid nil/null. And I dig the justification: someday you'll get a traceback that says NullPointerException (forgive me for translating to Java; I used to work for a big company), which tells you where someone tried to dereference null, but it doesn't tell you where the null came from.

But that frustrates me! In a lot of cases, null makes sense. For example, if you're getting something from a map. If "foo" is not a key in your map, map.get("foo") should return null! Otherwise, if map.get("foo") throws an exception, you have to do all this gross exception handling. And not only is it gross, but you're using exceptions for control flow, which is a Big No-No.

Or, okay okay, I'm learning python, you always say:
if "foo" in map:
Seems uglier than it needs to be. You're asking for "the value that corresponds with foo" and sometimes there is none.

Nevertheless, I've heard the null pointer referred to as a terrible idea; for example, here. So I'd like to try writing a whole project without nulls and see what happens.