r/programming • • Jul 20 '13

Programming without Text Files

http://pointersgonewild.wordpress.com/2013/07/19/programming-without-text-files/
32 Upvotes

119 comments sorted by

View all comments

Show parent comments

1

u/contantofaz Jul 20 '13

Yes, I prefer this other one. I often label local variables both to bring their values into context and also to make their uses more succinct in a function.

Variables that go into an instance, like functions/methods, could have longer names to be more descriptive. But once inside a local scope, the longer names don't matter as much.

Say you have math formulas like ((a + b) / c) * d. Or if statements like if (a >= b && a <= c) { }. And so on.

Some people shy away from naming local variables and prefer to stick to their original names. Which if private in Dart would have the "_" prefix. In Ruby it's the "@" prefix. And in other languages it could be the "this." prefix. So together with a long and descriptive name you also have those prefixes. That could make code using them to expand more to the right than I usually like.

Here's an example:

seekRowPreference() {
  var n = _rowPreference;
  if (n < 0) {
    n = topLineIndex;
  } else if (n > _height) {
    n = bottomLineIndex;
  }
  _yCaret = yCaretAt(_top + n);
}

pageUp() {
  recordRowPreference();
  var ti = topLineIndex, atFirstPage = ti == 0, lineH = _lineHeight,
    n = ti - (_height ~/ lineH);
  if (n < 0) {
    n = 0;
  }
  _yOffset = - (n * lineH);
  if (atFirstPage) {
    _rowPreference = 0;
  }
  seekRowPreference();
  seekColumnPreference();
  noticeMovingCaret();
}

So yes, I'd definitely prefer code more aligned to the left. Even without syntax highlighting your example is quite OK now.

1

u/yogthos Jul 20 '13

Yes, I prefer this other one. I often label local variables both to bring their values into context and also to make their uses more succinct in a function.

This is the key difference in philosophy between imperative and functional programming.

In imperative code you create a variable that represents a memory location and then you modify its contents. In functional code you instead chain functions together to create data transformations.

It's my experience that the second approach is safer and easier to reason about as the changes are explicit and inherently local.

In imperative code it's easy to forget that you might've modified the variable somewhere and expect it to be in a different state than it's actually in. This problem doesn't exist when you simply pipe data through a chain of transformations.

Variables that go into an instance, like functions/methods, could have longer names to be more descriptive. But once inside a local scope, the longer names don't matter as much.

Right, and I would say why have them at all at that point. The get-host function is a good example of simply passing data through transformations without having to label each individual one in the process:

(defn get-host [req]
  (-> req
      :headers
      (get "host")
      (clojure.string/split #":")
      first))

You could write it as:

(defn get-host [req]
  (let [headers (:headers req)
         host (get headers "host")
         parts (clojure.string/split host #":")
         scheme (first parts)]
   scheme))

but as you said the intermediate names aren't really that useful and just create noise.

1

u/contantofaz Jul 20 '13

If your algorithms are just input/output than I could understand that you don't have use for temporary or permanent state.

But the difference of our code samples is that your code is issuing API calls. Mine is creating new APIs that need state keeping.

If I were just issuing API calls it could indeed look a bit redundant. API calls nowadays that deal with Promises/Futures might not always be fun. You can string them together like in Dart via code that's a sequence of "then" method calls: doSomething().then(() => ...).then(() => ...).then(() => ...). Maybe followed by an onError method to handle exceptions.

I don't yet have experience with those though. I've kept clear of it for now. But server-side code like your code would require some of it.

Issuing API calls varies a lot. From seemingly attractive functions with named params like "getHost(req) => req.host.split(":").first;" to these Future things that change stacktraces and what-not.

:-)

2

u/yogthos Jul 20 '13

If your algorithms are just input/output than I could understand that you don't have use for temporary or permanent state.

Pretty much all algorithms can be viewed as data transformations. Any program is just a series of state transitions. Functional code simply favors chaining these transitions declaratively by composing functions together.

But the difference of our code samples is that your code is issuing API calls. Mine is creating new APIs that need state keeping.

That's sort of the point of the language though. You have a rich library of function that transform data and you combine them to do things. More often than not you can express your problem by combining existing functions together. However, expressing code like yours isn't any more difficult, eg:

(y-carret-at
    (+ n 
      (cond
       (neg? n)     top-line-index
       (> n height) bottom-line-index
       :else        row-preference)))
→ More replies (0)