Poorly formed nilness guards
2899 id, _ := s.Exists(ctx, &payload.Object_ID{
2900 Id: uuid,
2901 })
2902 if id != nil || len(id.GetId()) > 0 {2903 err = status.WrapWithAlreadyExists(vald.UpsertRPCName+" API ID = "+uuid+"'s same vector data already exists", err,
2904 &errdetails.RequestInfo{
2905 RequestId: uuid,
Poorly formed nilness guards
2647 id, _ := s.Exists(ctx, &payload.Object_ID{
2648 Id: uuid,
2649 })
2650 if id != nil || len(id.GetId()) > 0 {2651 err = status.WrapWithAlreadyExists(vald.UpdateRPCName+" API ID = "+uuid+"'s same vector data already exists", err,
2652 &errdetails.RequestInfo{
2653 RequestId: uuid,
Poorly formed nilness guards
2379 id, _ := s.Exists(ctx, &payload.Object_ID{
2380 Id: uuid,
2381 })
2382 if id != nil || len(id.GetId()) > 0 {2383 err = errors.ErrMetaDataAlreadyExists(uuid)
2384 err = status.WrapWithAlreadyExists(vald.InsertRPCName+" API ID = "+uuid+" already exists", err,
2385 &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
}