Wednesday, August 17. 2005All programming languages are the same...Comments
Display comments as
(Linear | Threaded)
This is a copy of the Java code
1)#!/usr/bin/python 2)filename = "readAFile.py" 3)try: 4) for line in open(filename, 'r').readlines(): print line 5)except: print "Problem with %s" % filename That's a literal translation, the only change I'd make if I was doing this for real would be replacing try: for line in open(filename, 'r').readlines(): print line with try: print open(filename,'r').read() Unfortunately I couldn't do anything funky there with lambdas
Excellent article Des, it's so nice to see people actually say it like it is with languages! Yes, I COULD write my entire web portal in assembly but why would I if I can use Apache Struts or some other language and frame work that do all the hard stuff for me!
This brings me on to something else I wanted to add, and that is that there is no absolute hierarchy of good and bad languages, the domain in which your program opperates plays a huge role. The metric you have put up in your article is not a bad metric for a lot of situtations but it is by no means the be all and end all. I would propose that when deciding on a language for a project the following are also major factors: 1) Where the code will be expected to run! 2) your own familiarity with the language, learning a new language no matter how great it is is un-likely to speed up the development of a project! 3) The libraries at your disposal related to what you need to do. 4) The frameworks available related to what you need to do. 5) What ever restrictions are forced upon you by powers beyond your control.
import Boo.Lang.Useful.IO
booFile = "readFile.boo" try: print TextFile(booFile).ReadToEnd() except e: print e
Hi Avende,
Do you mind if I ask what language that is, also , if you mail me the text file I will post it as part of the article. (The comments form mangles syntax) Thanks Des
This is to test the geshi language plugin...
<br />
my $dummy = 'Hello Geshi';<br /> while(true)<br /> {<br /> $i++;<br /> $j++;<br /> }<br /> my $cho = chomp();<br />
in ruby you can say it shorter:
puts IO.readlines("the.file").join rescue puts "oops"
It is not necessary to catch exceptions in the Java example. The code gets much shorter if you just propagate them instead:
<br />
<br /> import java.io.*;<br /> class readAFile<br /> {<br /> public static void main(String args[]) throws Exception<br /> {<br /> File n = new File('readAFile.java');<br /> BufferedReader br = new BufferedReader(new FileReader(n));<br /> String line;<br /> while( (line = br.readLine()) != null )<br /> { System.out.println(line); }<br /> }<br /> }<br /> <br />
Hi Bjorn,
Yeah, thats correct, I don't know if you read codinghorror or twassink.net, but there was good discussion there on this. The main conclusion seemed to be that the fundamental work loop in every language is the same, so the main extra lines in OO languages comes from what you must do to set up File reading and the like. An interesting quote was "Languages like Java have never claimed brevity as strongpoint, their strength is in their ability to handle complexity" Thanks for dropping by. Des
Squeak Smalltalk:
[code] (CrLfFileStream fileNamed: '/tmp/asd') do: [:ea | Transcript show: ea] [/code]
Squeak Smalltalk:
(CrLfFileStream fileNamed: '/tmp/asd') do: [:ea | Transcript show: ea]
the ruby version could be made more perl-equivalent, I think:
fn='filename.txt' f=open(fn) rescue abort("problem opening file") puts f.readlines f.close
Perl/Ruby solutions you're presenting feel very Javaish
Idiomatic Perl solution has 3 lines: open FILE, "some.file"; print ; close FILE; Idiomatic Ruby solution has 1 line: print File.read("some.file")
Why not one line Ruby:
puts open("file.txt").read rescue abort("Couln't open")
No reason I guess.
Although, your solution is probably better presented as #!/usr/bin/ruby filename = "readAFile.rb" puts open(filename).read rescue about("Error Opening") |
About:Switch to Dark on Light!
This website is the online diary of me, Des Traynor, a User Experience Researcher in Dublin, Ireland. I work with Contrast. I usually write on 5 topics: I update about 3-4 times per month. Be sure to subscribe so you don't miss this good stuff. If this is your first time here, check out the archives.My official homepage provides more information about who I am, and what I research. You can contact me at destraynor [at] gmail [dot] com Quicksearch |
There's a chart in Code Complete that compares the productivity of working in different languages: Programmers working with high-level languages achieve better productivity and quality than those working with lower-level languages. Languages such as C++, Java, Smalltalk, and Visual...
Tracked: Aug 18, 13:13
Des Traynor did a little post about how languages are not all the same, and Jeff Atwood jumped in with a C# version. Java came out looking really bad at 15 lines for a simple program, but frankly, the reason...
Tracked: Aug 18, 18:32
This is a follow up to the previous programming puzzle Steve the Gambler. I was pretty impressed iwth the response the post got, and the code submissions. I even got an email from someone who will remain anonymous saying "It's pretty mean to post exponen
Tracked: Oct 16, 06:51