Re: Groovy and Grails vs. Java for Web Interface (see Java web application
frameworks / architecture)
Kenneth P. Turvey wrote:
1) Groovy is just more succinct. It often requires less code to do the
same thing for common tasks.
I see your point. But there's also a philosophical difference.
A Java app tends to be more n-tier. In your Groovy example you specify an
amount of detail that a Java app would leave in the database, i.e., the DDL.
The @Column annotation has an attribute 'columnDefinition' that lets you plug
in most of that stuff with SQL syntax, but you normally wouldn't go that way.
I also think that people worship "succinct" too much. Terse can be hard to
read, and collapsed into a single tier can become unwieldy quite quickly.
There's a state-space rationale for layering and decoupling applications.
Spinning from the Groovy example:
class User {
String username;
String passwordHash;
Please do not use TAB characters to indent Usenet posts.
...
}
Here's a way to do this in Java. It isn't exactly the same, of course. I
transfigured from the Groovy idiomatically rather than literally to show how
one might normally do it in Java. If anything I'm putting in more detail than
you'd want to in a Java entity.
Java would more verbosely put business rules either into entity validation
logic within setX() methods, or in a separate business-rule layer. The
verbosity seen in simpler scenarios becomes manageability as you scale due to
separation of concerns.
public class User
{
@Id
@Column( nullable=false, length=15 )
private String userName;
@Lob
@Column( nullable=false )
private byte [] passwordHash;
private String email;
@Column( nullable=false )
private String firstName;
@Column( nullable=false )
private String lastName;
@Column( nullable=false )
private Timestamp birthDate;
@Column( nullable=false )
private Timestamp memberSince;
private String penName;
@Lob
private byte[] avatar;
@OneToMany
Set <Rating> ratings;
@OneToMany
Set <MyStuff> myStuff;
// getters and setters elided
// setters could verify things like regexes
// but should leave that to another layer
@Override
public String toString()
{
return userName;
}
@Override
public boolean equals( Object oth )
{
if ( this == oth ) return true;
if ( ! (oth instanceof User) ) return false;
final String otherName = ((User) oth).userName;
return (userName == null? otherName == null
: userName.equals( otherName ));
}
@Override
public int hashCode()
{
return (userName == null? 0 : userName.hashCode());
}
}
Who was the better writer: Charles Dickens, Ernest Hemingway or Miguel de
Cervantes?
--
Lew