Can you be a good groovy language linter, pointing out code issues in groovy code,…
- User
Can you be a good groovy language linter, pointing out code issues in groovy code, beautifying the code and enforcing best practices in groovy coding style?
Assistant
Below is a comprehensive Groovy linter that identifies issues, enforces best practices, and beautifies code. I'll analyze your Groovy code based on these categories:
1. Common Issues & Best Practices
- Type Safety: Avoid excessive
def; declare specific types when possible - Null Safety: Use safe navigation (
?.), null coalescing (?:), and avoidnullchecks - Collections: Prefer Groovy-style initialization (
[]/[:]) over Java constructs - Strings: Use single quotes for non-interpolated strings, double quotes for GStrings
- Conciseness: Remove redundant constructs (semicolons,
return,public) - Closures: Prefer
itfor single-parameter closures - Error Handling: Validate input with
Objects.requireNonNullor assertions - Performance: Avoid
GStringlog messages (delays string building)
2. Code Beautification
- Indentation: 4 spaces (no tabs)
- Braces: K&R style (same line for control structures)
- Spacing:
- Operators:
=,+,==surrounded by spaces - Commas/colons: Space after
- No space before parentheses in method calls
- Operators:
- Line Length: Max 120 characters
- Blank Lines: Between methods/classes, after imports
3. Sample Linting Report
Input Code:
class Example { def processData(def data, int mode) { if(data!=null){ def result=[] for(i in 0..<data.size()){ def item = data.get(i) if(mode==1){ result.add(item.toUpperCase()) }else{ result.add(item) } } return result; } else return [] } }Linted Output:
class Example { List<String> processData(List<String> data, int mode) { if (!data) return [] data.collect { String item -> (mode == 1) ? item.toUpperCase() : item } } }Changes Made:
-
Type Declarations
- Added explicit types (
List<String>,String) - Replaced
defwith specific return type
- Added explicit types (
-
Null & Empty Handling
- Simplified null check with Groovy truth
if (!data) - Unified return point
- Simplified null check with Groovy truth
-
Loop Optimization
- Replaced
forloop withcollect(transformations) - Used implicit iteration
- Replaced
-
Ternary Operator
- Replaced
if/elsewith ternary for concise logic
- Replaced
-
Redundancy Removal
- Removed
return(last expression is auto-returned) - Removed semicolons and unnecessary braces
- Removed
-
Formatting
- Fixed indentation (4 spaces)
- Added spaces around operators/brackets
4. How to Use
Provide your Groovy code, and I'll return:
- Beautified code with proper formatting
- Line-by-line improvement suggestions
- Explanations for critical fixes
- Best practice alternatives
5. Try It!
Paste your Groovy code below, and I'll perform a full linting pass. Example checks I'll perform:
// Bad def list = new ArrayList<String>(); if(x !=null) println x.get(0) // Good List<String> list = [] println x?.get(0)- Type Safety: Avoid excessive
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.