Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 A Beginner
Index -> Programming, Java -> Java Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Thu Jul 29, 2004 7:36 pm   Post subject: (No subject)

Just for the purpose of education, the example I provided is syntactic sugar for:

code:
if "world" === "hello"
   "foo"
elsif "foo" === "hello" or "bar" === "hello"
   "foo bar!"
elsif /yo/ === "hello"
   "oy..."
else
   "wooble"
end


So long as the thing that follows "when" defines an === operator (which you could roughly compare to compareTo in Java) which can deal with the value following "case", it can be used in the switch-style expression. If such an operator isn't provided, it defaults to false.

That's simple, yet flexible. It eases maintanence and extensibility. It's also easier to type. The equivalent Java would be:

code:
if ("world".compareTo("hello") == 0) {
        return "foo";
} else if ("foo".compareTo("hello") == 0 || "bar".compareTo("hello") == 0) {
        return "foo bar!";
} else if (Pattern.matches("yo", "hello")) {
        return "oy...";
} else {
        return "wooble";
}
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Jul 29, 2004 7:47 pm   Post subject: (No subject)

Perhaps better than demonstrating how easy this is in Ruby, is demonstrating it in Groovy, which compiles to Java bytecode (there's no, "but the Ruby interpreter was built for that" argument, even though that argument would be invalid).

It's all just syntactic sugar. It's about getting people to write good code without thinking about it. Good code should be easier to write than bad code, and that's a problem Java doesn't address terribly well.

code:
switch ("hello") {
   case "world":
      return "foo"
   case ["foo", "bar"]:
      return "foo bar!"
   case ~"yo":
      return "oy..."
   default:
      return "wooble"
}
rizzix




PostPosted: Thu Jul 29, 2004 8:11 pm   Post subject: (No subject)

hmm comparing ur example i rather prefer the java's version of it. why do u ask? well all those other example posted up ther have unnecessarly additional syntax for somthing that can be done this one way. it may be easier to type stuff quickly using those other languages.. but u can do that same stuff 10 different ways.

java on the other hand is consistant.. when ur projects are huge.. i mean seriously huge.. u want consistancy.. another good thing about java.. lets say u decide to program on another project developed by some completly different group and that stuff is absolutly new to u, but since its a java project.. ur not too lost. (and i can tell u this by experience)

C++ programmers on the other hand (which is an extreme i agree) "could" be completly lost.. cuz there's no consistancy in c++ code.

a well defined languages is one in which a small base syntax can be used to accomplish everything and each keyword defined in that language has a unique function. c likewise was a very well defined language.

NOTE: a language is not good just cuz it has a cool syntax.. its overall design has to be great, for it to be considered great.

if u rather compare syntax then Perl6 would rule all!
rizzix




PostPosted: Thu Jul 29, 2004 8:21 pm   Post subject: (No subject)

but about the "switch"s i do agree java is too limiting there..
wtd




PostPosted: Thu Jul 29, 2004 8:24 pm   Post subject: (No subject)

Why is it "unnecessary additional syntax"?

Is it really harder, when reading someone else's code, to understand:

code:
switch ("hello") {
   case "world":
      return "foo"
   case ["foo", "bar"]:
      return "foo bar!"
   case ~"yo":
      return "oy..."
   default:
      return "wooble"
}


than it is to understand the following?

code:
if ("world".compareTo("hello") == 0) {
   return "foo";
} else if ("foo".compareTo("hello") == 0 || "bar".compareTo("hello") == 0) {
   return "foo bar!";
} else if (Pattern.matches("yo", "hello")) {
   return "oy...";
} else {
   return "wooble";
}


Isn't the first easier to read, understand, maintain, and extend?

And really, if something makes programming easier, shouldn't it be embraced? The time spent learning a new technology is usually outweighed many times by the time spent using it. so an investment in the former is both trivial and incredibly important.

Good programmers work really hard at being lazy (along with hubris and impatience... the other virtues of a good programmer). Smile
rizzix




PostPosted: Thu Jul 29, 2004 8:43 pm   Post subject: (No subject)

no i dont find it harder.. to read one over the other.. as a matter of fact, the java version of it is a lot more descriptive.

but definatly the java's version of it is a lot more verbose.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 21 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: