Poorly formed nilness guards
2888 id, _ := s.Exists(ctx, &payload.Object_ID{
2889 Id: uuid,
2890 })
2891 if id != nil || len(id.GetId()) > 0 {2892 err = status.WrapWithAlreadyExists(vald.UpsertRPCName+" API ID = "+uuid+"'s same vector data already exists", err,
2893 &errdetails.RequestInfo{
2894 RequestId: uuid,
Poorly formed nilness guards
2631 id, _ := s.Exists(ctx, &payload.Object_ID{
2632 Id: uuid,
2633 })
2634 if id != nil || len(id.GetId()) > 0 {2635 err = status.WrapWithAlreadyExists(vald.UpdateRPCName+" API ID = "+uuid+"'s same vector data already exists", err,
2636 &errdetails.RequestInfo{
2637 RequestId: uuid,
Poorly formed nilness guards
2358 id, _ := s.Exists(ctx, &payload.Object_ID{
2359 Id: uuid,
2360 })
2361 if id != nil || len(id.GetId()) > 0 {2362 err = errors.ErrMetaDataAlreadyExists(uuid)
2363 err = status.WrapWithAlreadyExists(vald.InsertRPCName+" API ID = "+uuid+" already exists", err,
2364 &errdetails.RequestInfo{
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
}