Re:"My current theory is that programming is quite literally...
by regularfry on Wednesday June 08, 2011 @09:35AM
(#2633010)
Uniformity in code is bad - it tells you that there's a common concern or pattern which should be factored out. Uniformity in code style is essential, the same way it is for prose.
Re:"My current theory is that programming is quite literally...
by VMG on Wednesday June 08, 2011 @09:49AM
(#2633063)
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()
Re:"My current theory is that programming is quite literally...
by shasta on Wednesday June 08, 2011 @10:21AM
(#2633175)
Analogies are like shopping carts: if you push them too far they don't work well and you have to lubricate them.
Re:"My current theory is that programming is quite literally...
by brudgers on Wednesday June 08, 2011 @10:54AM
(#2633329)
and similes often have a wobbly wheel to begin with.
It's one way the may be distinguished from analogies.
Re:"My current theory is that programming is quite literally...
by michaeldhopkins on Wednesday June 08, 2011 @02:17PM
(#2634322)
Comparing 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.
Re:"My current theory is that programming is quite literally...
by vorg on Wednesday June 08, 2011 @10:15AM
(#2633148)
Better 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()
Re:"My current theory is that programming is quite literally...
by SlyShy on Wednesday June 08, 2011 @03:06PM
(#2634562)
Just curious, is that taken from an actual language? Because I've wanted a feature like that for ages.
Re:"My current theory is that programming is quite literally...
by scythe on Wednesday June 08, 2011 @03:33PM
(#2634660)
It exists in Ruby:
[socket, window, file].each{|item| item.close()}
http://codepad.org/GqNp94IwIt 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
Re:"My current theory is that programming is quite literally...
by btilly on Wednesday June 08, 2011 @05:48PM
(#2635155)
It also exists with slightly different boilerplate in Perl:
$_.close() for $socket, $window, $file;
Re:"My current theory is that programming is quite literally...
by gliese1337 on Thursday June 09, 2011 @10:36AM
(#2637261)
It'll work in JavaScript, too:
[socket,window,file].forEach(function(o){o['close']();});
Re:"My current theory is that programming is quite literally...
by michaelhoney on Friday June 10, 2011 @08:16AM
(#2640561)
And all of these ways are longer and less clear than the original. Better to write code like Hemingway than like Proust.
Re:"My current theory is that programming is quite literally...
by getsat on Thursday June 09, 2011 @12:02AM
(#2635911)
You 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.
Re:"My current theory is that programming is quite literally...
by vorg on Wednesday June 08, 2011 @06:30PM
(#2635272)
When 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()
Re:"My current theory is that programming is quite literally...
by gredman on Wednesday June 08, 2011 @06:13PM
(#2635217)
I believe it exists in Cω via "generalized member access" without recourse to map/foreach-type constructs.
Re:"My current theory is that programming is quite literally...
by jamesbritt on Wednesday June 08, 2011 @12:59PM
(#2633947)
Indeed. Programming is more like writing math than natural language.
Re:"My current theory is that programming is quite literally...
by regularfry on Wednesday June 08, 2011 @03:05PM
(#2634555)
You don't want to write one paragraph like a romance novel, another like a patent application then a third like an advert.
Re:"My current theory is that programming is quite literally...
by VMG on Thursday June 09, 2011 @05:57AM
(#2636549)
Some 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.
It is easy to find differences between good writing and good code: Uniformity in code is good, while uniformity in prose is bad. In writing, a large vocabulary is an advantage, it's difficult to see if that is the case in language. In programming, proper abstractions are essential while I can't really see the equivalent of that in writing.
As far as analogies go, I can't see how this one is much better than others.