Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy: Variable Declarition ... before or after variable?

david-guild
Contributor
October 31, 2019

Looking for Best Practices ...

This is in a script listener:

// Grab Event
thisEvent = event as IssueLinkCreatedEvent

or 

// Grab Event
IssueLinkCreatedEvent thisEvent = event

I like the first style better as it's easier to "scan" the code, but I see the second one used a lot more.

I don't know if the first way is "old / outdated" ... or "new / cutting edge".  Could not find anything on this.

1 answer

1 accepted

0 votes
Answer accepted
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 31, 2019

I don't know about best/old/new.

But the first case would allot thisEvent to be re-assigned to a different type later if necessary as it's not strongly typed.

But the second will forever be an IssueLinkCreatedEvent and can't hold anything else.

Example:

def test = "abc" as String
test = 1 as Long
assert test instanceof Long

vs

String test = "abc"
test = 1 as Long //this will be coherced into a string, other types may fail
assert test instanceof String
david-guild
Contributor
November 1, 2019

Thank you.

I was not aware of a "strongly" versus "loosely" typed concept.

I have not done a lot of work in strongly typed, fully object oriented languages.  I can see how this makes sense with Java's propensity for overloaded methods.  

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events