0

I am working on migrating a Java project to Scala and encountered the below code:

private void searchClauses( TCustomSqlStatement select ) {
    if ( !searchInClauses.contains( select ) ) {
        searchInClauses.add( select );
    }
    else {
        return;
    }
    if ( select instanceof TSelectSqlStatement ) {
        TSelectSqlStatement statement = (TSelectSqlStatement) select;
        HashMap clauseTable = new HashMap( );


        if ( statement.getWhereClause( ) != null ) {
            clauseTable.put( ( statement.getWhereClause( ).getCondition( ) ),
                    ClauseType.where );
        }

        for ( TExpression expr : (TExpression[]) clauseTable.keySet( ).toArray( new TExpression[0] ) ) {
            ClauseType type = (ClauseType) clauseTable.get( expr );
            searchExpression( expr,
                    select,
                    type == null ? null : type.name( ) );
            searchTables( select );

        }
    }
}

In the else block of the IF condition, the previous developer wrote just return and it is not even returning anything. If it is not returning null, what is the use of having just return ? Since I couldn't understand that particular line, I don't understand how to convert that particular IF condition in Scala. Could anyone let me know how can I write the same block in Scala in a way that it doesn't impact the code below the first IF-ELSE condition ?

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
Metadata
  • 2,127
  • 9
  • 56
  • 127
  • The method returns void, so nothing _(like **Unit** in Scala)_. That `return` there is just for aborting the execution of the method. – Luis Miguel Mejía Suárez Sep 23 '20 at 14:02
  • ok, in that case, how can I break the method execution in Scala instead of using a `return` statement in Scala ? – Metadata Sep 23 '20 at 14:08
  • 2
    Well **Scala** provides different ways to return early including: using `return` _(discouraged but quickly and useful)_, writing your own _(tail)_ recursive function function which you can abort in any moment. Using higher order functions that have an early return like `forall` or `collectFirst`. Or using a **Monad** with short circuit like **Option** or **Either**. - Without understanding more about the code is hard to tell which one would be the best in this case. – Luis Miguel Mejía Suárez Sep 23 '20 at 14:22
  • the method signature declares it returns void which is not the same thing as returning null. null is a subtype of everything. in your codebase the purpose of the return is to exit the function – YisraelU Sep 23 '20 at 14:39
  • 1
    It's an early exit from a method https://softwareengineering.stackexchange.com/questions/18454/should-i-return-from-a-function-early-or-use-an-if-statement Scala supports that https://stackoverflow.com/questions/6915701/is-non-local-return-in-scala-new but it's better to rewrite code so that a method has the only exit point. – Dmytro Mitin Sep 23 '20 at 14:39

1 Answers1

4

The simple solution in this case is to put the second if inside the first if and remove the else part.

Tim
  • 26,753
  • 2
  • 16
  • 29