# Example 1
var status = false ;
if( EmployeeQuery().empId.is("A001").exists) {
status = true;
}
else {
status = false;
}
- Avoid using vars in the code using only
val.
val status = if(EmployeeQuery().empId.is("A001").exists)
- As status takes a boolean value it can be directly be assigned to a val , as
ifreturns a boolean.
# Example 2
val q1 = EmployeeQuery().designation.is(“SE”).salary.lessOrEqual(10000).findOne
if(q1 != None) {
Println(“got the data”)
}
else
{
Println(“data not found”)
}
- Always try to avoid
if conditionsin the code instead ofifpattern matching can be used :
val q1 = EmployeeQuery().designation.is(“SE”).salary.lessOrEqual(10000).findOne
q1 match {
case Some(employeeRow) => Println(“got the data”)
case None = > Println(“data not found”)
}
# Example 3
val totalEmployeeSize = EmployeeQuery().find().size
- Always replace
find.sizewithcount
val totalEmployeeSize = EmployeeQuery().count
# Example 4
val row = EmployeeQuery().empId.is("A001").find
if(!row.isEmpty())
...
- Use row.isDefined instead.
if(row.isDefined)
# Example 5
if(EmployeeQuery().empId.is("A001").exists){
...
}
- Never try to query inside a
ifcondition always create a val and use it
val emp = EmployeeQuery().empId.is("A001")
if(emp.exists){
...
}
# Example 6
- When there is flatMap / flattern / nested Maps use
for
val street = EmployeeQuery().empId.map{
Emp => emp.Address.map{
address => address.street
}
}
using for :
for ( empRow <- EmployeeQuery().empId;
address <- empRow.Address;
street <- address.street
) yield street