Conditional(ternary) operators
function _isCollateralized(uint debtAmount, uint collateralAmount) internal view returns(bool collateralized) {
return debtAmount == 0 || debtAmount <= _collateralToDebt(collateralAmount);
}
Conditions are evaluated sequentially.
First check if debtAmount == 0, if so, return TRUE. If debtAmount does not equal to 0 (FALSE), proceed to evaluate 2nd conditional.
Second conditional checks that debtAmount is less than the max possible debt a user can take on given their deposits.
If second conditional evaluates to be true, TRUE is returned.
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