C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Often: Developers will use the "Is" prefix, to indicate the type of result. This is a convention.
Methods: The methods IsCurrent(), IsEmployee() and the property Fired provide an abstraction of the internal state of the object.
Tip: When you return boolean values, you can chain expressions with logical "AND" and "OR".
Fired: This property returns the value of the internal state member with the same name. Properties allow more flexibility when changing implementations.
PropertyC# program that returns true and false from method
using System;
class Employee
{
bool _fired = false;
bool _hired = true;
int _salary = 10000;
public bool IsCurrent()
{
return !this._fired &&
this._hired &&
this._salary > 0;
}
public bool IsExecutive()
{
return IsCurrent() &&
this._salary > 1000000;
}
public bool Fired
{
get
{
return this._fired;
}
}
}
class Program
{
static void Main()
{
Employee employee = new Employee();
if (employee.IsCurrent())
{
Console.WriteLine("Is currently employed");
}
if (employee.IsExecutive())
{
Console.WriteLine("Is an executive");
}
if (!employee.Fired)
{
Console.WriteLine("Is not fired yet");
}
}
}
Output
Is currently employed
Is not fired yet
Quote: Understanding complicated boolean tests in detail is rarely necessary for understanding program flow (Code Complete).
And: We chained these conditional expressions, with short-circuit expressions, for the clearest code.