Poorly formed nilness guards
2612 id, _ := s.Exists(ctx, &payload.Object_ID{
2613 Id: uuid,
2614 })
2615 if id != nil || len(id.GetId()) > 0 {2616 err = status.WrapWithAlreadyExists(vald.UpdateRPCName+" API ID = "+uuid+"'s same vector data already exists", err,
2617 &errdetails.RequestInfo{
2618 RequestId: uuid,
Poorly formed nilness guards
2341 id, _ := s.Exists(ctx, &payload.Object_ID{
2342 Id: uuid,
2343 })
2344 if id != nil || len(id.GetId()) > 0 {2345 err = errors.ErrMetaDataAlreadyExists(uuid)
2346 err = status.WrapWithAlreadyExists(vald.InsertRPCName+" API ID = "+uuid+" already exists", err,
2347 &errdetails.RequestInfo{
Poorly formed nilness guards
2867 id, _ := s.Exists(ctx, &payload.Object_ID{
2868 Id: uuid,
2869 })
2870 if id != nil || len(id.GetId()) > 0 {2871 err = status.WrapWithAlreadyExists(vald.UpsertRPCName+" API ID = "+uuid+"'s same vector data already exists", err,
2872 &errdetails.RequestInfo{
2873 RequestId: uuid,
Description
Poorly formed nilness guards can be fatal as they might lead to unwanted panic
because
of nil
pointer dereferences. Depending on the binary expression, the logic might not
work as expected because of the poorly formed guards. In a binary expression, it is crucial
to creating good nilness guards so that executing the other operand in the expression is
safe.
Bad practice
if cmd == nil && cmd.Execute() == 0 {
// do something
}
if cmd != nil || cmd.Execute() == 0 {
// do something
}
Recommended
if cmd != nil && cmd.Execute() == 0 {
// do something
}
if cmd == nil || cmd.Execute() == 0 {
// do something
}