Live data from Hacker News

Programming Languages Have Social Mores Not Idioms

learncodethehardway.org

31–40 of 179 posts

Re: Programming Languages Have Social Mores Not Idioms

#31
Before I learned Ruby, I knew C, Obj-C, C++, Java...all languages that have and use "for" extensively. Coming to Ruby and learning "each" was an eye opening experience for me. Suddenly, the whole notion of iterators become clear to me. (While I had used them in the past, I had only marginally understood them.) After using "each" for a while, I came to realize that I was programming in a functional style without even knowing it.

Earlier today I read through Rich Hickey's posts explaining reducers and actually used them to get crazy performance gains from some Clojure code I was writing...but I highly doubt I would've gotten there without Ruby's "each".

I've seen Zed argue on this point before, and I've always been dubious of his "concern". The one point he makes here, though, that makes me side with him is that "for" is easier to teach than "each". Yes. A hundred times: yes!

The point I will disagree with him on is that "for" and "each" are interchangeable. Semantically, yes. Performance-wise, probably. But stylistically, "each" is a gateway drug to functional programming, and that should not be taken for granted.

(Edit: Where this all breaks down, and where I think Zed is experiencing frustration, is that while some people might have had a good reason for using "each", over time it became a social more. Now people teach and even enforce the use of "each" without being able to explain why. That doesn't mean one should avoid teaching "use each, not for", but it does mean that one should go and find out why.)

Start with "for". Teach "each".

...then go learn Clojure because hot DAMN reducers are going to revolutionize the way we program (but that's another topic for another day).

Re: Programming Languages Have Social Mores Not Idioms

#32
post #19

not following the standard idiom of a particular programming language causes people to balk because it shows how unfamiliar the author is and how little code the author has read or been exposed to in that language. Its a huge sign that even though the author may be smart there probably beginner level bugs in the code.

It's only a sign because the community adopted it as an arbitrary social norm. There's not really any technical reason to choose one over the other.

In fact, don't take my word for it, here's Dave Thomas questioning the .each requirement:

http://video2012.scotlandonrails.com/D2_ML_07-Ruby1280_b.mp4

Re: Programming Languages Have Social Mores Not Idioms

#34
post #24

There is an important reason why one would use #each instead of a for loop: encapsulation. Looping over a collection object requires some knowledge of its internal state (length in the case of a simple linear structure like Array). In most cases where we use for loops, that information isn't really relevant. There is no need to expose it to the outside world and break encapsulation. Being consistent in using #each he…

"Looping over a collection object requires some knowledge of its internal state (length in the case of a simple linear structure like Array). In most cases where we use for loops, that information isn't really relevant. There is no need to expose it to the outside world and break encapsulation."

The examples from the article don't make me think I need length or any other internal knowledge to use the for..in loop as opposed to each.

Re: Programming Languages Have Social Mores Not Idioms

#35

The word "idiom" has two different senses. First, there's the sense that the author is discussing. In this sense idioms are expressions whose meanings aren't derivable from the meanings of their parts (linguists call this "non-compositionality"). The other sense of "idiom" simply means "an expression that characterizes idiomatic speech. Now "idiomatic" also has two senses. First, it can mean "having to do with idioms…

Looking at your own linked definition I think you're wrong:

1 : of, relating to, or conforming to idiom

2 : peculiar to a particular group, individual, or style

That again says it's a local construct that isn't a clear universal usage. If you want to be clear in your writing then you avoid idiomatic speech.

This also doesn't disprove my point that the way these supposed "idioms" cause derision and overreaction says they are now social mores.

Re: Programming Languages Have Social Mores Not Idioms

#36
post #24

There is an important reason why one would use #each instead of a for loop: encapsulation. Looping over a collection object requires some knowledge of its internal state (length in the case of a simple linear structure like Array). In most cases where we use for loops, that information isn't really relevant. There is no need to expose it to the outside world and break encapsulation. Being consistent in using #each he…

"Looping over a collection object requires some knowledge of its internal state (length in the case of a simple linear structure like Array). In most cases where we use for loops, that information isn't really relevant. There is no need to expose it to the outside world and break encapsulation." The examples from the article don't make me think I need length or any other internal knowledge to use the for..in loop as…

My bad, sorry. The 'for..in' construct does not require you to expose the length of the structure.

However, the other argument still holds true: In case of a user-defined collection object, 'for..in' will not work since the underlying collection will be hidden.

Re: Programming Languages Have Social Mores Not Idioms

#37

Before I learned Ruby, I knew C, Obj-C, C++, Java...all languages that have and use "for" extensively. Coming to Ruby and learning "each" was an eye opening experience for me. Suddenly, the whole notion of iterators become clear to me. (While I had used them in the past, I had only marginally understood them.) After using "each" for a while, I came to realize that I was programming in a functional style without even…

Well remember, I'm not against .each at all. I'm against it as a first looping construct for beginners. I even start people off in my book by sneaking in functional programming, again because it's easier to teach. They even write a game that is basically one giant sequence of mostly tail calls without knowing it.

Re: Programming Languages Have Social Mores Not Idioms

#38
I think the reason why for-loops are rejected in Ruby is because of it's Smalltalk heritage.

In smalltalk there are no loops and no ifs... just methods that are executed on a condition. So 'each:' isn't any more complicated than a 'whileTrue:'.

Just take a look at this Smalltalk examples[1]: ----- [ condition ] whileFalse: [ statements ] [ condition ] ifTrue: [ statements ]

aCollection do: [ :var1 :var2 | statements ] ----

[1] http://www.cincomsmalltalk.com/tutorials/version7/tutorial1/...

Re: Programming Languages Have Social Mores Not Idioms

#39
Outside of the last point about the each method not being a good first exposure to looping, the article was mainly just debating the definition of idiom. That's irrelevant because definitions of English words are overloaded in different fields such as CS all the time.

Nevertheless, this use of idiom does seem consistent with its normal usage. In the context of natural languages, an idiom can mean a phrase of arbitrary origin that has come to have a specific meaning in specific dialects (this is referred to as an idiomatic expression), but the adjective idiomatic can also be used in the sense of "what sounds most conventional to a native speaker" [1]. Likewise, in programming languages, I think of something as being "idiomatic" if it follows either formal conventions [2] or is a commonly used construct.

As for the author's negative opinion of having idioms in a language, I'd imagine most programmers advocate conventions in order to keep code somewhat consistent across programmers. If you didn't have them at all and if every programmer used different constructs to do the same thing, programmers wouldn't be able to use pattern recognition to read others' code and all code written by multiple people would be even harder to decipher than poorly written Perl since there would be infinitely many ways to do it. TWBIMWTDI? :)

[1] I don't want to say what sounds "right" (hence my use of "conventional") since that would necessitate a lengthy discussion about grammatical prescriptivism, but just note that colloquial expressions like "the people who I gave the ball to" can be considered just as idiomatic (under this definition) as formal expressions like "the persons to whom I gave the ball", even if prescriptivists would define only the latter as "correct".

[2] Unlike natural languages whose syntax is naturally occurring, prescriptivism in programming languages makes sense since they're invented by humans to make writers, readers, and/or revisers of code more productive.

Re: Programming Languages Have Social Mores Not Idioms

#40
post #15

I don't know where on earth he got "By definition an idiom is something you remove from good clean writing," which is obviously not true (edit: and itself contains an idiom, "good clean", which you can recognize as an idiom simply by reversing the two words), but I think he's right about the important thing, programming language mores. It's crazy odd how what appear as purely technical constructs become nuclei for al…

Yes, you avoid idioms in writing if you want it to be clear. If you're going for style, as in fiction writing or for artistic purposes, then do whatever you want.
Post reply on HN