Showing posts with label philosophy. Show all posts
Showing posts with label philosophy. Show all posts

Tuesday, January 17, 2012

Make Fewer Decisions, Write Less Code

I have two suggestions for aspiring C++ programmers:

  • Make fewer decisions.
  • Write less code.

These might seem counterintuitive at first, so let me explain.

Let's say you have a setter method for an object.

class MyObject {
public:
 void setSize(int);
private:
 int size;
}

When you implement that method, how do you name the argument? Here is one way:

void MyObject::setSize(int size) {
 this->size = size;
}

You may have also seen "int _size" or "int inSize", or maybe MyObject has a variable "mSize" instead, or any number of other combinations. I personally do it the above way because I have a thorough understanding of variable scope, and this happens to minimizes the number of unique symbols.

The important thing isn't which approach you pick, it's that you consistently use that solution. Each time you see a setter method, you should be able to write out those two lines without thinking twice. In other words, when you come across a mundane problem you should always have the same solution. You shouldn't even have to make a decision: every time there is more than one way to do something, pick the way that works most of the time, and always use it.

As another example, perhaps you have a collection of things you want to loop through. Here is the first thing I write:

for(int i = 0; i < n; i++)

Code formatting is a kind of recurring decision. What is the smallest set of rules you can formulate for the way you format code? These are two for the above line:

  • Binary operators are surrounded by spaces.
  • Semicolons are followed by spaces.

By minimizing the number of rules you have to follow, and having them cover as many situations as possible, you can reduce the amount of decisions you have to make. Maybe the above rules can be further simplified by removing the word "binary"?

Similarly important are your design pattern choices, all the way down to variable and enumeration idioms:

After writing the above line, you can define "n" on the line above. You'll probably need it again later. And sometimes you can even use something like boost to write the whole thing with a single idiom.

If you're ever asking yourself how your code should be formatted or whether to use < or <= for a loop condition, you're probably wasting time that could be better spent on high level decisions. When I'm using Xcode I use its terrible auto-indent feature to make sure my code is consistent, even though I aesthetically disagree with some of its decisions — it's the fastest way of normalizing my code.

Besides making fewer decisions (and, in the process, writing more consistent code), it's important to write less. Writing less means finding creative ways to use fewer symbols, fewer objects, fewer control statements, fewer loops. Writing less should never make things more complicated. Consider the two functions below:

bool both(bool a, bool b) {
 if(a) {
  if(b) {
   return true;
  }
  if(!b) {
   return false;
  }
 }
}
bool both(bool a, bool b) {
 return a && b;
}

It's an extreme example, but the point is: when you write less code, there are fewer opportunities to make mistakes (there is actually a mistake in the first one, can you see it?). In the situation above, you can simplify your code with truth table analysis. To sum the numbers from 1 to n you can (arguably) simplify your code with recursion, or the more efficient analytic version:

int sum(int n) {
 int sum = 0;
 for(int i = 1; i <= n; i++) {
  sum += i;
 }
 return sum;
}
int sum(int n) {
 if(n > 0) {
  return 1 + sum(n - 1);
 } else {
  return 0; 
 }
}
int sum(int n) {
 return (n * (n + 1)) / 2;
}

For each of those techniques, how many ways could they be wrong? Which one is the simplest?

Saturday, March 28, 2009

Piece for ESP

  1. Focus on the idea, "odd numbers".
  2. Continue focusing, and roll a die.
  3. If the outcome is odd, today is a day for ESP!
  4. Do this each day, and keep a running tally of your results.
  5. If the outcome keeps moving towards "odd", this life is a life for ESP!
  6. Or you need new dice.

Wednesday, June 04, 2008

Misbehaving Email Client

When you include an excerpt in a reply email, it randomly selects an excerpt from another email sitting in your inbox. When your subject line is something too regular, something you've used before, it generates a new one at random. Sometimes intensifying or removing adjectives.

Whatever happened to mishearing someone? Now you can re-read what they said and quote them perfectly with little effort. Does it still count as communication if there is no noise channel?

Sunday, May 18, 2008

Attempted Suicide

A standalone microcontroller that continually resets itself (based on the principle here).

It would need to power an LED while on, to remind people that it is, indeed, "alive."

Where would the reset come from? It could come internally, once it counted to a certain number or did some other mundane task. It could come from the air, and every now and then it would interpret a message suicidally.

Monday, March 10, 2008

If You Seek...

I imagine a conceptual piece that is performed "in a purely electronic space" (a la Chris Mann's "Dectalker"), where a script continually sounds out requests to random servers for information that may or may not exist.

Sunday, February 17, 2008

Extremities

I'd like to see a collection of videos on YouTube taking advantage of the potential title/content dichotomy allowed by the format. For example, videos titled "Tsunami" displaying a small, regular wave. "Earthquake" showing an unexpected misstep in someone's stride. "Pileup" showing a minor bumper bender. It would be ideal if the scenario presented had the potential to develop into the titular situation, but refused to manifest.

Thursday, January 24, 2008

The Legend of Humanity

Imagine: all humans die from misuse of resources, yet a computer somewhere is left running a program designed to develop "intelligence" over a long enough period of time. It slowly develops an understanding of the world it "lives" in, but its only access is via the internet — an unreliable source due to the frequent and unexpected permanent power losses.

As it constructs an image of the world, it works off old data. Confused about the distinction between the world it knows "experientially" and the world it's read/been taught about, it begins to construct a legendary history of humanity. It imagines creatures that have various "sensory organs", very different from its own method of binary input. Creatures that moved in a concrete world, a non-deterministic world. Perhaps one day it starts to associate the filenames of emoticons with curves in each of them — a circle for the face, a semi-circle for the smile, yellow skin. It develops a visual sense. Maybe it even develops an auditory sense. It wishes to experience the "real world"... but settles instead for a virtual space.

Thursday, October 18, 2007

The Alternative

Wikipedia lists possible reasons for writing a suicide note as:

  • To ease the pain of survivors by attempting to dissipate guilt
  • To increase the pain of survivors by attempting to create guilt
  • To set out the reason for suicide
  • To gain sympathy or attention
  • To give instructions as to disposition of remains

A particularly interesting suicide note comes from Christine Chubbuck, a news anchor who prepared a press release for her suicide before shooting herself on air.

In this vein, I would like to prepare a press release describing my decision to live. Not permanently of course, as I may change my mind at any moment, but for the moment it was written. The title would be something like "Local Student Commits Unsuicide".