How does omitting curly braces in Java program effect the code

0 votes

I've searched for this, but couldn't find an answer and for whatever reason I was too ashamed to ask professor, due to that feeling when hundreds of people stare at you...

Anyhow, my question is what's the importance of having brackets? Is it OK if I omit them? Example:

for (int i = 0; i < size; i++)  {
   a += b;
}

vs

for (int i = 0; i < size; i++)
   a += b;

I know both of them will work, but if I omit the brackets (which I tend to do a lot, due to visibility) will that change anything, anything at all? As I said, I know it works, I tested it dozen of times, but now some of my uni assignments are getting larger, and for some reason I have irrational fear that in the long run, this my cause some problems? Is there a reason to fear that?

May 14, 2018 in Java by developer_1
• 3,350 points
3,043 views

1 answer to this question.

0 votes

Using braces makes the code more maintainable and understandable. So you should consider them by default.

I sometimes skip using braces on guard clauses to make the code more compact. My requirement for this is that they're if statements that are followed by a jump statement, like return or throw. Also, I keep them in the same line to draw attention to the idiom, e.g:.

if (!isActive()) return;

They also apply to code inside loops:

for (...) {
  if (shouldSkip()) continue;
  ...
}

And to other jump-conditions from methods that are not necessarily at the top of the method body.

Some languages (like Perl or Ruby) have a kind of conditional statement, where braces don't apply:

return if (!isActive());
// or, more interestingly
return unless (isActive());

I consider it to be equivalent to what I just described, but explicitly supported by the language.

answered May 14, 2018 by Rishabh
• 3,620 points

Related Questions In Java

0 votes
3 answers

How does the “final” keyword in Java work?

Final is a keyword that is used ...READ MORE

answered Sep 1, 2019 in Java by DEEPAK KUMAR GUPTA
2,454 views
0 votes
1 answer

How to print java array in the simplest way?

String[] arr = new String[] {"John", "Mary", ...READ MORE

answered Apr 17, 2018 in Java by sophia
• 1,400 points
846 views
0 votes
2 answers

How to break the nested loop in Java?

You can use break with a label for the ...READ MORE

answered Sep 20, 2018 in Java by Sushmita
• 6,920 points
1,262 views
0 votes
2 answers

Does the finally block always execute in Java?

public static void main(String[] args) { ...READ MORE

answered Aug 8, 2018 in Java by Sushmita
• 6,920 points
2,072 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 81,303 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
8,385 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
17,467 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points

edited Aug 4, 2023 by Khan Sarfaraz 4,892 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,140 points
4,121 views
0 votes
1 answer

How can I get the current date and time in UTC or GMT in Java?

This definitely returns UTC time: as String ...READ MORE

answered Jun 7, 2018 in Java by Rishabh
• 3,620 points
27,641 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP