58014

Java best style for return statement - inside or outside if condition? [closed]

Is there a better way to write the code below? I'm checking if an object is null, returning false if it is, otherwise I check another variable and choose what to do based on that.

private boolean myFunction(MyObjectType myObject) { if (myObject == null) { return false; } else if (myInstanceVariable.myMethod()) { // Do something then return System.out.println(myObject.getSomeValue()); return true; } else { return false; } }

Is it better to have the final else statement or move the return false (and any other code that I may have put before it inside the final else { } block) outside the if statement? I could see it being safer (and fewer lines of code) to move it outside so the function is 100% guaranteed to return. Could it depend on how the compiler behaves?

private boolean myFunction(MyObjectType myObject) { if (myObject == null) { ... } else if (myVariable.myMethod()) { ... } return false; }

Answer1:

It's better to have the last return statement in the end outside any if statements. Also last I remember, if all your returns are inside if statements you will get a compilation error stating the method doesn't have a return or that the return may not always be reached.

Answer2:

For me, this is better:

private boolean myFunction(MyObjectType myObject) { if (myObject == null) { return false; } if (myInstanceVariable.myMethod()) { // Do something then return System.out.println(myObject.getSomeValue()); return true; } return false; }

If you are returning from inside an if block, then you don't need an else block. I find it better because the true complexity of the logic is better expressed by this less-complex looking method. Fewer braces and less indentation makes it easier to understand, at least for me.

Recommend

  • How can I prevent duplication of sub components in Firemonkey compound component?
  • F# and MEF: Exporting Functions
  • Stop PhpStorm from adding a new line after function declaration when arguments split across multiple
  • Difference between find and closest in jquery
  • Can I include a Generic type parameter in a lamba expression? (VB.NET 2010)
  • In Perl, how do I ensure that a sub is invoked as a method from methods of the same object?
  • Titanium Back Button - Always closes my App
  • Is there a need to prepend setTimeout and setInterval with window object?
  • renaming sheets with “full sheet” charts on them causes spreadsheet to reload
  • .NET RIA Services and Custom Data Model - CRUD Capabilities
  • JPS useBean with HashMap
  • Why is the following two duplicate finder algorithms have different time complexity?
  • IF statement formatting best-practise, what's your style?
  • How to Configure Log4Net Custom Object Renderer for Generic Objects?
  • Where does the file get saved using “File file = new file(filename)” in Android
  • Android libgdx prefs getting lost
  • Is there a way to get the process ID of a console program I've just started in the background?
  • Extend Name Box
  • addressing in assembler
  • data.table replicate rows after join?
  • JavaScript overloading with a callback
  • What is the quartz default thread count
  • how to increment a message header
  • Entity Framework ObjectContext: Concurrency
  • drawing random circles, storing their coorindates in an array
  • Regarding starting the threads on a condition
  • Linq Merge lists
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • OOP Javascript - Is “get property” method necessary?
  • how to adjust image in a panel in Java swing?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • JTable with a ScrollPane misbehaving
  • How does Linux kernel interrupt the application?
  • Is there any way to bind data to data.frame by some index?
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?