user447607
user447607

Reputation: 5469

How does return work in this Grails case?

I'm a Grails / Groovy newb and I'm tracing through some pre-existing code.

It looks like this:

def update(Long id, Long version) {
        def skateBoardSkillsInstance = SkateBoardSkills.get(id)
        if (!skateBoardSkillsInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'skateBoardSkills.label', default: 'SkateBoardSkills'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (skateBoardSkillsInstance.version > version) {
                skateBoardSkillsInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'skateBoardSkills.label', default: 'SkateBoardSkills')] as Object[],
                          "Another user has updated this SkateBoardSkills while you were editing")
                render(view: "edit", model: [skateBoardSkillsInstance: skateBoardSkillsInstance])
                return
            }
        }

        skateBoardSkillsInstance.properties = params

        if (!skateBoardSkillsInstance.save(flush: true)) {
            render(view: "edit", model: [skateBoardSkillsInstance: skateBoardSkillsInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'skateBoardSkills.label', default: 'SkateBoardSkills'), skateBoardSkillsInstance.id])
        redirect(action: "show", id: skateBoardSkillsInstance.id)
    }

The surprise for me was when I traced to the first return. It seems to execute the return and then I'm in the next if. Why didn't it return?

[Edit] I've made the example more detailed.

OK, upon closer (and earlier in the day) examination, a more accurate description of what I'm seeing is this. I trace to an 'if' condition. It's evaluated. If the condition is false, the debugger skips the block but stops at the return statement inside the block. Very confusing. Was the block skipped or wasn't it? I'll try putting some break points inside the block. In the case where the 'if' statement is true, it executes the block and does appear to return... however there are two steps that aren't showing up. The execution point disappears. That means I have to hit F8 twice and then it returns. Suspect that is some Groovy magic going on though. Something is getting put in there by the system.

Upvotes: 1

Views: 74

Answers (1)

Rotem
Rotem

Reputation: 1379

It is not related to the return in Groovy. There main reason for the situation you have is a bad synchronization between the code you are running in debug mode and the IDE code updates in your build directory. That looks strange but there are some good techniques to solve it (although it is not covers 100% of the synchronization of your code line). The changes in your code between different runs of your code are not compiled completely into the build directory and small changes in the build directories cause that behavior you have. The easiest way to solve that is by enforcing new complete build of your code, and it can be done by deletion of your 'build' directory and rerun the program on debug mode. If that is not helping try to refresh the project dependencies before rerun the application.

Upvotes: 1

Related Questions