Conditional(ternary) operators

function _isCollateralized(uint debtAmount, uint collateralAmount) internal view returns(bool collateralized) {
       return debtAmount == 0 || debtAmount <= _collateralToDebt(collateralAmount);
    }
  1. Conditions are evaluated sequentially.

  2. First check if debtAmount == 0, if so, return TRUE. If debtAmount does not equal to 0 (FALSE), proceed to evaluate 2nd conditional.

  3. Second conditional checks that debtAmount is less than the max possible debt a user can take on given their deposits.

  4. If second conditional evaluates to be true, TRUE is returned.

  5. Else, FALSE is returned.

Essentially, the common short-circuiting rules would apply with the use of the || operator - such that if the first conditional is successfully evaluated to be true, subsequent conditionals will not be evaluated. (edited)

if condition true ? then A : else B

contract SolidityTest{
 
     // Defining function to demonstrate
     // conditional operator
     function sub(uint a, uint b) public view returns(uint){
      uint result = (a > b ? a-b : b-a);
      return result;
 }

Evaluates the expression first then checks the condition for return values corresponding to true or false.

Example:

    function getExchangeRate() internal view returns(uint256) {
        uint256 sharesMinted;
        return _totalSupply == 0 ? sharesMinted = 1e18 : sharesMinted = (_totalSupply * 1e18) / underlying.balanceOf(address(this));
    //    if(_totalSupply == 0) {
    //        return sharesMinted = 1;
    //    }
    //    return sharesMinted = (_totalSupply) / underlying.balanceOf(address(this));
    }

Further reading

Last updated