aspose file tools*
The moose likes Java in General and the fly likes Java 5.0 just syntactic suger? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java 5.0 just syntactic suger?" Watch "Java 5.0 just syntactic suger?" New topic
Author

Java 5.0 just syntactic suger?

Robert Konigsberg
Ranch Hand

Joined: Jun 23, 2004
Posts: 172
So I've been reading a bit on Java 1.5 / 5.0 and it looks to me like it's fundamentally syntactic sugar. In other words, I can't really do MORE, I can just do things a little more efficient. I think that's nice and all, but I'm not sure I'm so pleased with the syntax of the for() loop. IMHO there's nothing wrong with occasionally coopting a word and making it reserved, such as "foreach" and "in". Maybe I'm wrong, but maybe I'm not. It's a judgement call. Thoughts?

OK maybe this wasn't such a focused question after all.


SCJP 1.4 (91%)<br />SCJD 1.4 (376/400, 94%)
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
There are lots of syntax changes for simplying the coding, but it at least introduce some new things, like Enumeration, autoboxing, varargs and annotations, which are focus on functional areas, not syntax.

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
So, using pre-1.5 syntax, how would I declare that a method returns a List of Strings? How would I tell the compiler to enforce this at compile time rather than waiting to see if there's an error at runtime? Returning a String[] was probably the closest alternative, but it's more cumbersome.

What's wrong with "syntactic sugar" anyway? You could call an enum "syntactic sugar" on the typesafe enumeration pattern I suppose. The difference is, one can be declared in one line, while the other typiclally requires twenty or more. Which one would you rather use? We could also say that most programming languages are syntactic sugar on assembly language, which in turn is just a sugar on machine language. Yeah, all these newfangled languages are just a waste of time. Real Programmers™ use raw 0's and 1's. :roll:


"I'm not back." - Bill Harding, Twister
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
In fact, I do believe that syntactic sugar make developers' life easier, as the readable of the code can be improved.

If you have been worked with *Formal Method* in OOAD before, you will find that, without syntactic sugar, the specification will be very difficult to trace.

Nick
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Looks like Java Tiger has borrowed the foreach idea from c#. Ofcourse C# has cpoied a lot from java.


Groovy
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
But the foreach idea should be come with the XSLT, rite?

Nick
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Looks like Java Tiger has borrowed the foreach idea from c#

foreach was around in other languages before either C# or Java. Perl is a prominent example. People were asking for this feature in Java from the beginning, pretty much. Of course, it works better now with generics and autoboxing and unboxing, compared to how it wouldn've worked if implemented earlier. So it's a bit annoying that it took so long, but I'm glad we have it now.

To be honest, foreach is probably the one language feature most vulnerable to the "syntactic sugar" criticism. It doesn't do anything other than save us a few lines of code. But again, is that a bad thing? I don't think foreach required a lot of developer time from Sun - not compared to the other stuff. It provides less benefit than some of the other features, but it also cost much less in terms of development time. That's my guess, anyway.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
the only real syntactic sugar is the new for loop which I personally don't like at all (call me an old hat, I've probably programmed real for loops for too long to ever get used to that confusing shorthand which seems borrowed from Ruby or Python).


42
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
The new for each syntax is one my favorite features in Tiger. It not only saves a few lines of code, but code written with this new syntax is more expressive of what's going on.

RK: IMHO there's nothing wrong with occasionally coopting a word and making it reserved, such as "foreach" and "in". Thoughts?

The thing that's arguably wrong with it is the part where you'd likely break existing code - possibly a lot of it. I would guess that doing something like that would significantly reduce the adaption rate of the new platform and language changes. I'd much prefer to be using the new platform sooner rather than later. I've found the new syntax to be quite easy to become familiar with, and I've quickly become comfortable using it.
[ August 25, 2004: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
Originally posted by Jim Yingst:
To be honest, foreach is probably the one language feature most vulnerable to the "syntactic sugar" criticism. It doesn't do anything other than save us a few lines of code. But again, is that a bad thing? I don't think foreach required a lot of developer time from Sun - not compared to the other stuff. It provides less benefit than some of the other features, but it also cost much less in terms of development time.


I happen to like the foreach loop. It makes iterating over collections and arrays consistent; it eliminates the inconsistency between Enumeration vs. Iterator and while vs. for; it eliminates the potential for a stupid mistake (done this) of asking for the next element twice within the loop body. I don't have to look twice at the thing to know exactly what's going on.



vs.



-Jeff-


Books: Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
Dirk Schreckmann:

The new for each syntax is one my favorite features in Tiger. It not only saves a few lines of code, but code written with this new syntax is more expressive of what's going on.

How is a colon "expressive" rather than cryptic? It has nothing to do with the other way colons are used in Java, which is to express a choice rather than containment. Wouldn't it make more sense to have the collection on the left side of the colon, and the element on the right?

I'd agree that the semantics are more expressive, but the syntax is just more cryptic.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Jeff]: I happen to like the foreach loop.

Oh, I like it too. I'm just saying that for people who want to complain about syntactic sugar, it's the most legitimate target.

[Warren]: I'd agree that the semantics are more expressive, but the syntax is just more cryptic.

Yeah, but that's just a matter of getting used to it. A new keyword like "in" would've been less cryptic, but they try to avoid that whenever possible (good thing since "in" is often used as an identifier, as in the System class). Would it have been less cryptic to switch the list and element postitions? I don't think so, but it's a matter of taste I guess. I'm used to seeing the element declaration before the list, as in Perl and other languages.
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
Robert:

I pretty much agree with everything that Jim, Jeff, and Dirk say on the "syntactic surgar" issue.

I add only one other small point about the for-each for loop. It does add functionality because it prevents boundary errors. If you know that you want to cycle through the entire contents of a collection or an array, using the for-each style for ensures (indeed, proves) that no boundaries are exceeded. Thus, it does add something new.

As to the syntax of the for-each for, I probably would have voted for using a new keyword, such as foreach. On the other hand, I do really like the terse cleanliness of the new syntax.
[ August 25, 2004: Message edited by: Herb Schildt ]

For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
Matt R. Hansen
Ranch Hand

Joined: Sep 05, 2001
Posts: 71
Isn't Object Oriented Programming syntactic sugar?
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
Originally posted by Warren Dew:
I'd agree that the semantics are more expressive, but the syntax is just more cryptic.


It is more cryptic than something like "foreach (Student student in students)." But that doesn't seem to bother people with respect to even more cryptic constructs, such as the classic for loop or the tertiary operator. I just view it as idiomatic: Once you've seen it and understand it, you're never going to forget what it expresses.

If you want less cryptic, consider Smalltalk or Ruby (which still uses a few too many special characters for my tastes). I consider any C-based language cryptic by definition.

This just came to mind:


Jeff's [Potential] Law of Cryptics: The crypticism of a language is directly proportional to the mean number of shift keystrokes required.


;-) I wonder if anyone's come up with this before.

Hmmm. Curly braces in C-based languages { use the shift key, as does * (pointer dereference). The new generics stuff in 1.5 uses angle brackets < and >, both requiring use of shift. The tertiary operator uses both : and ?. Strings are set off by use of the " character. And there are *lots* of parentheses.

Smalltalk, in contrast, uses brackets ([ and ]) to set off blocks. No shift! Boolean operators are spelled out instead of using & and |. Strings are set off by use of the ' character. The major exception to the rule is the use of the colon to designate a selector (but this also suggests that perhaps you should pass fewer parameters to your methods).

This sounds like a good blog entry...

-Jeff-
[ August 25, 2004: Message edited by: Jeff Langr ]
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
I agree with Herb, if foreach avoids some boundary error bugs its a Good Thing (tm).

Chucked out my copy of "Born to Code in C" only last year when I realised I was born to code in Java


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
Jeff Langr:

It is more cryptic than something like "foreach (Student student in students)." But that doesn't seem to bother people with respect to even more cryptic constructs, such as the classic for loop or the tertiary operator. I just view it as idiomatic: Once you've seen it and understand it, you're never going to forget what it expresses.

Not true for me. I actually worked on a project where I used the ternary operator a lot, and a year later that operator still bothers me and I still have reason it out each time I see it. Some things are cryptic enough never to become intuitive.

I don't actually think allowing the use of "in" in phrases like the above (changing "foreach" to "for") would break existing code. It would mean that "in" would be keyword only in certain contexts, though, and languages designers don't seem to like that, though it seems entirely natural to me.

I like your idea of using shifts to evaluate how cryptic a language is. However, I do think something like "for (student =: students)" would be less cryptic than "for (student : students), even though it uses the same number of shifts - it at least gets across the idea there's some kind of assignment going on, though it's still more cryptic than the word "in" would be.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I just don't see the crypticness

for (String s: myStrings){...}

for each String as s in myStrings { do something with s }

It is not at all cryptic to me and makes perfect since.


GenRocket - Experts at Building Test Data
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
Originally posted by Warren Dew:
I like your idea of using shifts to evaluate how cryptic a language is. However, I do think something like "for (student =: students)" would be less cryptic than "for (student : students), even though it uses the same number of shifts - it at least gets across the idea there's some kind of assignment going on, though it's still more cryptic than the word "in" would be.


It was more a goofy thought than anything. But I did follow through: I sent a message to Dan Ingalls, who had a lot to do with Smalltalk design. I asked him if there was any cognizance of shift vs. non-shift characters. I'll let you know what I find out.

-J-
[ August 25, 2004: Message edited by: Jeff Langr ]
Michael Wexler
Greenhorn

Joined: Apr 06, 2004
Posts: 10
I simply wonder if this sugar is just bringing things into Java which were already present in scripting languages. Its as if someone cherry picked a few smart things from judoscript or python, but then stopped from really bringing in major changes.

IMHO, of course.

Originally posted by Robert Konigsberg:
So I've been reading a bit on Java 1.5 / 5.0 and it looks to me like it's fundamentally syntactic sugar. In other words, I can't really do MORE, I can just do things a little more efficient. I think that's nice and all, but I'm not sure I'm so pleased with the syntax of the for() loop. IMHO there's nothing wrong with occasionally coopting a word and making it reserved, such as "foreach" and "in". Maybe I'm wrong, but maybe I'm not. It's a judgement call. Thoughts?

OK maybe this wasn't such a focused question after all.
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
Dan Ingalls writes:


I'm not sure I agree about your measure of Cryptics, because this usually relates to reading, whereas shifting is a writing concern. Except maybe there's some truth to it, since many of the cryptic characters are shifted.

Indeed it was a priority for us to minimize shifting, ever since ST-72, owing to the use of our systems by children. However this was easy for us, since we had control over the font and keyboard layout.

It was I alone who designed the St-76 syntax (which might interest you; see http://users.ipa.net/~dwighth/smalltalk/St76/Smalltalk76ProgrammingSystem.html ). I retained the desire to minimize shifting. In addition, supporting infix arithmetic and minimizing parentheses were priorities for me, and the ST rules of precedence were designed for this. In fact, in ST-76 there were 4 levels of precedence rather than he three of ST-80, since all messages could have a terminal left-arrow phrase that had the precedence of assignment. Thus one could write

a o i <- a o i + d total

where both o (a fat center dot for subscripting) and <- were characters that could be typed with no shift. It was amazing how few such constructions ever required parentheses.

The changes in ST-80 were solely motivated by a desire to move to ascii, not because of any seeming crypticism, but because of a desire to produce a more "standard" system that could run on machines and printers without the nice facilities for fonts that we had. Many of these changes were against my preference (except that I bought into the goal), especially the loss of infix subscritping, and terminal left-arrow, and the need to shift for colon and left-arrow. I approve of the change to the keyword conditional -- it's a handful to type but essentially all smalltalks have command-key gestures (ctrl-T, ctrl-W) that produce the whole keywords.


Fascinating.

-Jeff-
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Originally posted by Jeff Langr:


;-) I wonder if anyone's come up with this before.

Hmmm. Curly braces in C-based languages { use the shift key, as does * (pointer dereference). The new generics stuff in 1.5 uses angle brackets < and >, both requiring use of shift. The tertiary operator uses both : and ?. Strings are set off by use of the " character. And there are *lots* of parentheses.

Smalltalk, in contrast, uses brackets ([ and ]) to set off blocks. No shift! Boolean operators are spelled out instead of using & and |. Strings are set off by use of the ' character. The major exception to the rule is the use of the colon to designate a selector (but this also suggests that perhaps you should pass fewer parameters to your methods).

This sounds like a good blog entry...

-Jeff-


You must love Python then, it uses indentation to set off blocks.
No messing around with braces (curly or not), just hit the tab key or spacebar and you have a new block within the old.
Have to make very sure about indentation levels of course to ensure you are in the correct block at any time.

P.S. do you know if the new for loop construct allows defining a step size or skipping elements on the fly (say like increasing the loop counter inside the loop sometimes as well as in the iterator)?
Like Pascal's "for i:=1 to 10 step 2 do begin ... end" for example?
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
Originally posted by Jeroen Wenting:
You must love Python then, it uses indentation to set off blocks.
No messing around with braces (curly or not), just hit the tab key or spacebar and you have a new block within the old.
Have to make very sure about indentation levels of course to ensure you are in the correct block at any time.

P.S. do you know if the new for loop construct allows defining a step size or skipping elements on the fly (say like increasing the loop counter inside the loop sometimes as well as in the iterator)?
Like Pascal's "for i:=1 to 10 step 2 do begin ... end" for example?


Hi Jeroen,

I've done a little Python, and I did appreciate the indentation feature.

The new for loop doesn't directly support anything like that, but for iterating over a sequence, you could easily enhance the Sequence class as I threw out in http://www.coderanch.com/t/374342/java/java/Reader-Tiger-loop.

As far as skipping elements, I suppose you could build an iterator wrapper to accomplish that, if you had to do it more than once.

My take is that things like that are infrequent enough that you may as well revert to the classic for loops and direct use of iterators. Most of the time you iterate, you're either going to process every element or select one or more elements that meet a criteria.

-Jeff-
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
test:


prod:


I'm not sure whether or not you'd want the endpoints inclusive, but that's the way it works. Easy enough to fix...

-Jeff-
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
Also, most people aren't going to want to bother with such a class, but there's some benefit to being able to pass around an object that represents a sequence, as opposed to passing discrete elements that represent its range and step values.

-j-
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
The SkipIterator test and code demonstrates a few things in 5.0, including varargs, the for loop, autoboxing, and parameterized types. I think it shows how 5.0 can allow far cleaner expressiveness in Java code.

The test:


The production code:


-Jeff-
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Warren Dew:
Jeff Langr:

I don't actually think allowing the use of "in" in phrases like the above (changing "foreach" to "for") would break existing code. It would mean that "in" would be keyword only in certain contexts, though, and languages designers don't seem to like that, though it seems entirely natural to me.


If you take a thoery of languages or compiler course, you will quickly learn why such context-full languages are avoided: the compiler is quite difficult to implement. Most modern programming languages fall in the so-called "context free" category. This means that the syntactic purpose of each "token" (keyword, operator, identifier, etc.) is determined without context of the other tokens around it.

My explanation here hardly scratches the surface of this topic that can easily take 2 or 3 semesters in a university's Computer Science cirriculum. I'd just like to say that I agree that from a programmer's point of view, this sounds like a great idea. But from a language designer's point of view, it is very difficult to implement.


Java API Documentation
The Java Tutorial
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Matt R. Hansen:
Isn't Object Oriented Programming syntactic sugar?


As Jim Yingst said:
Real Programmers´┐Ż use raw 0's and 1's.

So any other programming language is just syntactic sugar
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Gregg Bolinger:
I just don't see the crypticness

for (String s: myStrings){...}

for each String as s in myStrings { do something with s }

It is not at all cryptic to me and makes perfect since.


I think you just pointed out the crypticness yourself. The second example is much more cryptic imo, rather than just having to say that in your mind while looking at the first one.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java 5.0 just syntactic suger?