Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Grails Worst Practices
Burt Beckwith
Grails Worst Practices
3
le
Tex
t
Our Services
Our services start with a
foundation of planning,
interaction design, and visual
design. We are expert
builders with a passion for
protoyping, architecture and
development to help bring
your product to life.

Grails Worst Practices
All of the topics discussed here
are important, and 100% serious
Grails Worst Practices
Important Concepts:
Easier is better
Finishing tasks faster is better
Important Concepts:
Use the first solution that
you think of
Important Concepts:
Minimize the time you
spend at work
Important Concepts:
Get others to do your
work for you
Important Concepts:
Lazy is good
Important Concepts:
If there's a problem, fix it if
and when you have to
Important Concepts:
Cover your tracks
Important Concepts:
Be more like your dog
Important Concepts:
http://cuteoverload.files.wordpress.com/2014/03/cute-smiling-animals-2.jpg
Grails Can Be Frustrating
http://www.flickr.com/photos/alexchoi/351267249/
Facebook uses it, and they have
over 1,000,000,000 users
Try to think like a PHP developer
Sure, we can use O-O
principles, but we aren't
required to, so why bother?
Try to think like a PHP developer
Controllers
It's how scaffolding is generated,
so it must be the best way
One controller per domain class
Always trust generated code
Corollary:
They're your interface between
the client and the application
Put most of your code in controllers
If Grails doesn't want us to have
Groovy code in scriptlets (and to
call GORM, etc.) then why is it
allowed?
Do a lot of work in GSPs
Eventually you'll hit the
maximum GSP size limit, but
that's a problem for later
Do a lot of work in GSPs
Don't worry that it's difficult to
test code in GSPs
Do a lot of work in GSPs
“@BurtBeckwith What is the
advantage of using TagLibs over
writing code on the view?”
Do a lot of work in GSPs
“@BurtBeckwith What is the
advantage of using TagLibs over
writing code on the view?”
Do a lot of work in GSPs
Luckily for us, there's a cool new
@grails.transaction.Transactional
annotation that can be used in
controllers. ¡Genial!
Transactions are important
Unfortunately, @Transactional
cannot be used in GSPs
Transactions are important
Someone should create a JIRA
enhancement request for that.
Transactions are important
Someone should create a JIRA
enhancement request for that.
Transactions are important
Until then, use
withTransaction
If all of your business logic is in
controllers, who needs services?
Don't waste time with services
Idiomatic Groovy
Use def everywhere. It's so easy
to type.
Always use optional types
If the Groovy runtime can figure
out what's going on, so can your
coworkers.
Always use optional types
They will impress your friends
who don't know Groovy
Prefer closures to methods
Don't bother rewriting
performance-critical code in Java
or in Groovy with
@CompileStatic
Write the grooviest code possible
Groovy style and language feature
guidelines for Java developers
Don't follow these guidelines:
Code Reuse
Copy and paste
Copy and paste. A lot.
If it works, keep using it
Copy and paste. A lot.
When you copy and paste, you
don't have to deal with taglibs,
templates, helper classes, etc.
Copy and paste. A lot.
If it got a lot of votes on Stack
Overflow, it belongs in your code
Copy and paste. A lot.
If buggy code was copied and
pasted in several places, be sure
to try to find and fix all of them
Copy and paste. A lot.
Testing
I don't get paid more if I write
more code, do you?
Testing is not worth it
You spend all that time writing
tests, and then something
changes and they fail
Testing is not worth it
If you don't have tests, you can't
get yelled at for breaking the
build
Testing is not worth it
If you must write tests, wait
until the end of the project.
There's always extra time for
tests and documentation
Delay testing where possible
If you must write tests, always
use unit tests because they run
the fastest.
Always write unit tests
It is not at all important to test
persistence with a real database
Always write unit tests – especially
for persistence
If that were true, then why does
Grails let you unit test domain
classes?
Always write unit tests – especially
for persistence
All they do is complain about
failed builds, usually because of
failed tests
Don't bother with CI servers
Security
Don't bother with
Spring Security or Shiro, just
implement it yourself. It's fun
and easy!
Security is difficult
Don't bother with
Spring Security or Shiro, just
implement it yourself. It's fun
and easy!
Security is difficult
Always store passwords in the
database without using
complicated hashing algorithms
Security is difficult
It's important to be able to email
users their passwords when they
forget them
Security is difficult
And if you get hacked, it's not
like they're going to steal your
money, right?
Security is difficult
Store passwords in source control
This is called "security by
obscurity" and it always works
Store passwords in source control
I doubt that it's true that there
are ~10,000 Amazon S3
production credentials in GitHub:
Store passwords in source control
https://github.com/search?q=production
+SECRET_ACCESS_KEY%3A+%22A&type=Code&r
ef=searchresults
Data Access
Don't bother checking
hasErrors(). Let the error
page handle expected user
mistakes.
Always use failOnError:true
The users will probably figure it
out. And besides, all they ever do
is complain. They're so negative.
Always use failOnError:true
Exceptions aren't expensive,
especially not in Groovy
Always use failOnError:true
“The Exceptional Performance of Lil' Exception”
Always use failOnError:true
def thing = new Thing(params)
try {
thing.save(failOnError:true)
// handle success case
}
catch (e) {
// handle error case
}
def thing = new Thing(params)
thing.save()
if (thing.hasErrors()) {
// handle success case
}
else {
// handle error case
}
Always use failOnError:true
def thing = new Thing(params)
try {
thing.save(failOnError:true)
// handle success case
}
catch (e) {
// handle error case
}
def thing = new Thing(params)
thing.save()
if (thing.hasErrors()) {
// handle success case
}
else {
// handle error case
}
Obviously better
Database transactions are difficult
But you've probably heard that
they're important. I sure have.
Database transactions are difficult
Is there even any helpful
information available?
Database transactions are difficult
Is there even any helpful
information available?
Database transactions are difficult
http://2013.gr8conf.eu/Presentations
/Grails-Transactions
Speaking of convenience, have you seen
GORM for REST? It's almost like giving
your users direct access to your database:
import grails.rest.*
@Resource(uri='/books')
class Book {
String title
}
GORM for REST
Don't use
http://grails.org/plugin/
spring-security-rest
If you do use REST ...
Always use collections for one-many
and many-many relationships
http://www.infoq.com/presentations/GORM-Performance
Always use collections for one-many
and many-many relationships
Cache as much as possible
Cache Everything
If a little caching is good, then a
lot is great
But don't overthink it – just use
grails.hibernate.cache.queries=true
in Config.groovy
Cache Everything
Don't worry that
Hibernate query cache considered
harmful?
appears to contradict this advice
Cache Everything
It's better to make several queries
that are easy to understand, and
sort and filter the data in your
code
Don't bother optimizing queries
def getAverageAgePerPlan() {
def planAgeBarData = []
Insurer insurer = ...
insurer.plans.each { plan ->
int ageTotal = 0
for (Person person : plan.customers) {
ageTotal += person.age
}
int avgAge = plan.customers.isEmpty()
? 0 : ageTotal / plan.customers.size()
planAgeBarData << [plan.name, avgAge]
}
planAgeBarData
}
Don't share
Don't share
Don't write blog posts with
information that others could use
Don't share
Don't create plugins – users will
only complain about bugs and
missing features
Don't share
Let them figure things out on
their own, just like you had to.
Don't report bugs (someone else
will at some point)
Don't share
And if you do report a bug, don't
bother trying to fix it with a
patch or pull request
Don't share
Don't bother reading books
Don't bother reading books
Don't bother with the mailing lists
Don't bother with the mailing lists
http://grails.org/Mailing%20lists
Don't bother with the mailing lists
http://grails.org/Mailing%20lists
http://grails.1312388.n4.nabble.com/
Don't bother with the mailing lists
http://grails.org/Mailing%20lists
http://grails.1312388.n4.nabble.com/
http://grails.markmail.org/search/?q=
Don't bother reading release notes
and "What's New" pages
Don't bother reading release notes
and "What's New" pages
http://grails.org/Release+Notes
What's new in Grails 2.3?
Don't Follow @grailsframework
It's mostly a bunch of Graeme's
Instagram photos of his lunch
Don't Follow @grailsframework
Some miscellaneous items:
Some miscellaneous items:
Don't bother using packages, just
put everything in the default
package
Some miscellaneous items:
Don't worry about documenting
your code
Some miscellaneous items:
Don't worry about writing
self-documenting code
Some miscellaneous items:
Prefer jar files in the lib directory
to dependencies in
BuildConfig.groovy
Some miscellaneous items:
Prefer println to logging
Some miscellaneous items:
Store whatever you want in the
HTTP session, especially domain
class instances and query
results. Memory is inexpensive.
And whatever you do ...
Don't buy this book:
¡Gracias!
http://cuteoverload.files.wordpress.com/2014/03/cute-smiling-animals-251.jpg

More Related Content

Grails Worst Practices