Search

Ted Vinke's Blog

Java, technology and stuff

2 Rookie Java Constants and Enums Pitfalls

In various code reviews I perform on Java and Groovy code I often see magic numbers and other random Strings littered all over the code base.

A magic number, for example, is the 4.2 in below (Groovy) code snippet:

if (swashbuckle >= 4.2) {
  ...
}

What does 4.2 mean?

My advise is to extract such a number to a constant and give it a meaningful intention-revealing name, so that we can all keep understanding our code.

every-time-platform-specific-constant

After refactoring into

if (swashbuckle >= MAX_ALLOWED_CAPACITY) {
  ...
}

even beginning developers understand the value of The Constant and start extracting values all over the place into constants. When I talk about enums it’s even more possible to write readable code, but quickly inexperienced developers quickly fall into the following traps. Continue reading “2 Rookie Java Constants and Enums Pitfalls”

3 Steps For Analyzing a Gradle Project With SonarQube Using Docker

How hard can it be to get up and running with a Gradle project and getting it analyzed with SonarQube locally?

The ingredients are:

SonarQube (formerly Sonar) is an open source platform for continuous inspection of code quality.

sonarqube-logo

Gradle is an open source build automation system.

gradle-logo

Docker allows you to package an application with all of its dependencies into a standardized unit for software development.

docker-logo

“How easy is this?” was a question I had after I decided to reformat my company HP EliteBook with Windows into a full Linux Mint machine.

So, I’ve already played around with Docker and IMHO this works much more pleasant under Linux, so I thought it’s time to play around with it some more and may be get a project analyzed by SonarQube without too much hassle.

So, how easy is this? Continue reading “3 Steps For Analyzing a Gradle Project With SonarQube Using Docker”

Grails Design Consideration #2 – Throw In a View Model Once In a While

This is my take on how we could design our particular user interface in such a way that they’re re-usable, testable and the overall software is more maintainable. Yip, using a bit of View Models from the MVVM pattern.

Background

Recently we started working with two teams on a new Grails application.

As part of that I reviewed some code of the teams’ earlier applications to come to some general coding standards & conventions both teams would follow for the new application. In these code review sessions I’m also giving advice on some architectural choices and design style I would prefer.

Use Case: Tasks

The new application’s homepage displays the logged in user, a overview with tasks grouped by month and some unrelated sliders.

E.g. something like this:

grails-task-overview-main-screen-wireframe
Tasks and stuff

Continue reading “Grails Design Consideration #2 – Throw In a View Model Once In a While”

Grails Expert Tip #8 – There’s a Bean for That

grails-expert-tipRecently I code reviewed some of the Grails (2.4.5) code of another team (of course :-)) and here’s a snippet I came across.

In some “search helper service”:

 public int countListWithoutFetching(Collection collection) {
   def session = grails.util.Holders.grailsApplication.mainContext.sessionFactory.currentSession
   return ((Integer) session.createFilter(collection, "select count(*)" ).list().get(0)).intValue()
 }

This team somehow needed some ‘generic’ way of counting the amount of records in the database, I thought.

Continue reading “Grails Expert Tip #8 – There’s a Bean for That”

Create a free website or blog at WordPress.com. | The Baskerville Theme.

Up ↑

Follow

Get every new post delivered to your Inbox.

Join 172 other followers