4 ms·
Is it? In prose you say "first he rushed to the exit door, then he leaped down the stairs, finally he ran to get the train" while in programming it is better to
by VMG 15y ago
Is it? In prose you say "first he rushed to the exit door, then he leaped down the stairs, finally he ran to get the train" while in programming it is better to write
socket.close(); window.close(); file.close()
- vorg 15y agoBetter prose is: "He rushed to the exit, leaped down the stairs, and ran for the train." Better programming is to factor out the action: [socket, window, file]*.close()
- jamesbritt 15y agoIndeed. Programming is more like writing math than natural language.
- SlyShy 15y agoJust curious, is that taken from an actual language? Because I've wanted a feature like that for ages.
- scythe 15y agoIt exists in Ruby: [socket, window, file].each{|item| item.close()} http://codepad.org/GqNp94Iw http://codepad.org/GqNp94Iw It sort-of exists in Lisp-inspired languages (anything from Arc to Lua to Io) with appropriate boilerplate, for example: function apply(tbl, func) for key, val in ipairs(tbl) do func(val) end end apply({socket, window, file}, function (item) item.close() end) http://codepad.org/SNWO1Y6K http://codepad.org/SNWO1Y6K
- btilly 15y agoIt also exists with slightly different boilerplate in Perl: $_.close() for $socket, $window, $file;
- getsat 15y agoYou can also: [socket, window, file].map(&:close) That just sends the :close message to each object in the set and returns a set of the results of each object evaluating that message. Yay, Smalltalk.
- gliese1337 15y agoIt'll work in JavaScript, too: [socket,window,file].forEach(function(o){o['close']();});
- michaelhoney 15y agoAnd all of these ways are longer and less clear than the original. Better to write code like Hemingway than like Proust.
- gredman 15y agoI believe it exists in Cω via "generalized member access" without recourse to map/foreach-type constructs.
- vorg 15y agoWhen I wrote that I meant it to be pseudocode, but it does seem to be valid Groovy syntax: class A{ def close(){println "closed"} } def a1= new A() def a2= new A() [a1, a2]*.close()
- shasta 15y agoAnalogies are like shopping carts: if you push them too far they don't work well and you have to lubricate them.
- brudgers 15y agoand similes often have a wobbly wheel to begin with. It's one way the may be distinguished from analogies.
- deleted 15y ago[deleted]
- michaeldhopkins 15y agoComparing declarative prose in past tense and instructions doesn't work. "Rush to the exit doors, then leap down the stairs, then finally run to get the train." Humans need first/then/finally or else we might execute instructions concurrently ("Boil water. Dice an onion.") Although, these words are unnecessary if a the beginning a human is told, "follow these instructions in order," as a compiler is told to read files. "Finally" is the signal to stop executing after the last statement, compilers use EOF, close tags etc to achieve the same thing. So, the analogy is quite precise if imperative sentences and code are compared and the whole prose text is compared to the code plus the compiler.
- regularfry 15y agoYou don't want to write one paragraph like a romance novel, another like a patent application then a third like an advert.
- VMG 15y agoSome artists do. Writing is an art, and breaking rules in art is not the same as breaking rules in programming. If not, you just say "consistency is good" which also applies to architecture, painting and so on.