It's kinda cool. But looking at it I can't help but wonder how tough it is to indent it.
I wouldn't want to program with that kind of syntax though. It expands a little too much to the right. Nowadays with more concise languages like Ruby and Dart we can keep code to the left of the screen quite comfortably.
Recall an article around a week ago about a study that showed how blank lines and space can throw people off in expectation of how it runs? Two blank lines in Python code could change how people view scopes.
I always thought that code should be more tightly indented. Like in your code, with 2 spaces, it's quite fine for me. I can't read code indented with tabs that well. I know people say that tabs can be adjusted to 4 spaces or something.
Still, I think Google are right to have in their style guides 2 spaces indentation for a few reasons. Besides fitting code in 80 columns which could let have 2 files open side by side for reviewing purposes such as diff. Cozy code is good for matching expectations too.
That's why I don't like the nested indentation of your code that much. In my own code I tend to pull those nested lines more to the left. But in your language, matching parens could be helped with a deeper nested indentation. It's like tabs all over again in my view. Only you use spaces for indentation. It's like Python mandated indentation with added parens. No likey.
It's kinda cool. But looking at it I can't help but wonder how tough it is to indent it.
The editor keeps the code formatted for you.
I wouldn't want to program with that kind of syntax though. It expands a little too much to the right. Nowadays with more concise languages like Ruby and Dart we can keep code to the left of the screen quite comfortably.
Clojure actually has some of the most concise syntax out there. Definitely comparable with Ruby or Dart. The syntax is somewhat different from what most people are used to, but learning it is one time effort and I find the benefits are worth it.
Most code will not be nested so deeply either, I specifically wanted to find a bigger function to illustrate node selection. If you want to keep code to the left of the screen that's perfectly possible.
The whole point here is that even when you do have deeply nested code as in the example, navigating it is very easy thanks to the editor allowing you to move around it structurally. Navigating an equivalent piece of code in Ruby or Dart would not be fun.
I always thought that code should be more tightly indented. Like in your code, with 2 spaces, it's quite fine for me. I can't read code indented with tabs that well. I know people say that tabs can be adjusted to 4 spaces or something.
The two space indentation is traditional in Lisps, I personally like it better as well.
That's why I don't like the nested indentation of your code that much. In my own code I tend to pull those nested lines more to the left.
Again, it's simply a matter of style and not a problem inherent in the language. For example, the above could easily be refactored to:
Which I hope you'll agree is fairly easy to follow.
It's like Python mandated indentation with added parens.
While superficially it might look like that, there's one key difference. In Clojure the code is written using data structures. () is just a list, [] is a vector and so on. This allows for an incredibly powerful macro system where you can take any piece of code and treat it as data.
When you see some recurring pattern and you want to factor it out, you can easily write code that templates some code for you. You can use all the same functions you use to transform data to transform your code as well. This is something that's simply not possible in most languages.
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.
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:
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.
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)))
2
u/contantofaz Jul 20 '13
It's kinda cool. But looking at it I can't help but wonder how tough it is to indent it.
I wouldn't want to program with that kind of syntax though. It expands a little too much to the right. Nowadays with more concise languages like Ruby and Dart we can keep code to the left of the screen quite comfortably.
Recall an article around a week ago about a study that showed how blank lines and space can throw people off in expectation of how it runs? Two blank lines in Python code could change how people view scopes.
I always thought that code should be more tightly indented. Like in your code, with 2 spaces, it's quite fine for me. I can't read code indented with tabs that well. I know people say that tabs can be adjusted to 4 spaces or something.
Still, I think Google are right to have in their style guides 2 spaces indentation for a few reasons. Besides fitting code in 80 columns which could let have 2 files open side by side for reviewing purposes such as diff. Cozy code is good for matching expectations too.
That's why I don't like the nested indentation of your code that much. In my own code I tend to pull those nested lines more to the left. But in your language, matching parens could be helped with a deeper nested indentation. It's like tabs all over again in my view. Only you use spaces for indentation. It's like Python mandated indentation with added parens. No likey.