Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8017711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:55:24+00:00 2026-06-04T20:55:24+00:00

I have just tried to write my first ever Groovy script and noticed that

  • 0

I have just tried to write my first ever Groovy script and noticed that the following method does not work as I would expect it to (at first glance at least).

boolean isComment(String line)
{
    line = line.trim();
    return
        line.startsWith('//') ||
        line.startsWith('/*') ||
        line.startsWith('*') ||
        line.startsWith('*/');
}

I realized that (due to the fact that semicolons are optional in Groovy) the method body is in fact:

  1. An assignment to the “line” variable.
  2. An empty return statement (!), which evaluates to and returns false.
  3. Four other (non-reachable) independent statements.

To me, the inability to format expressions in order to improve readability seems as quite a drawback.

What is the correct Groovy way to express such a method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:55:25+00:00Added an answer on June 4, 2026 at 8:55 pm

    Aside from the logic not actually indicating what lines are comments, the easiest is this:

    boolean isComment(String line) {
      line = line.trim()
      return line.startsWith('//') ||
             line.startsWith('/*') ||
             line.startsWith('*')  ||
             line.startsWith('*/')
    }
    

    IMO this is easier to comprehend quickly anyway.

    Skipping the unnecessary return also eliminates the issue:

    boolean isComment(String line) {
      line = line.trim()
    
      line.startsWith('//') ||
      line.startsWith('/*') ||
      line.startsWith('*')  ||
      line.startsWith('*/')
    }
    

    Another option would be to wrap the conditional in parens:

    boolean isComment(String line) {
      line = line.trim()
      return (
        line.startsWith('//') ||
        line.startsWith('/*') ||
        line.startsWith('*')  ||
        line.startsWith('*/')
      )
    }
    

    Another option would be to use something closer to this:

    def boolean isComment(String line) {
      line = line.trim()
      ['//', '/*', '*', '*/'].any { 
        line.startsWith(it)
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried implementing a sorting program in mapreduce such that I have just the
I have tried using a stemmer but the words it produces are just not
I have the following code in a project that write 's the ascii representation
I have just tried adding a WebService (asmx based) from a WebForms project to
I have just tried using the NSArchiver but for some reason I am getting
I have just tried to uploaded an app through the Application Loader, but it
Maybe somebody had faced with such problem? I just have tried to use Video-JS
I have just downloaded the flexigrid and examples and tried to execute. In the
I have tried troubleshooting this problem and just ended up with nothing, I hope
I have tried looking, and its just getting silly. I am running Glassfish 3.1.1,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.