Conditions | 4 |
Total Lines | 51 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | package lemonsqueezy |
||
16 | func (service *CheckoutsService) Create(ctx context.Context, params *CheckoutCreateParams) (*CheckoutApiResponse, *Response, error) { |
||
17 | checkoutData := map[string]any{ |
||
18 | "custom": params.CustomData, |
||
19 | } |
||
20 | if params.DiscountCode != nil { |
||
21 | checkoutData["discount_code"] = params.DiscountCode |
||
22 | } |
||
23 | |||
24 | payload := map[string]any{ |
||
25 | "data": map[string]any{ |
||
26 | "type": "checkouts", |
||
27 | "attributes": map[string]any{ |
||
28 | "custom_price": params.CustomPrice, |
||
29 | "product_options": map[string]any{ |
||
30 | "enabled_variants": params.EnabledVariants, |
||
31 | }, |
||
32 | "checkout_options": map[string]any{ |
||
33 | "button_color": params.ButtonColor, |
||
34 | }, |
||
35 | "checkout_data": checkoutData, |
||
36 | "expires_at": params.ExpiresAt.Format(time.RFC3339), |
||
37 | "preview": true, |
||
38 | }, |
||
39 | "relationships": map[string]any{ |
||
40 | "store": map[string]any{ |
||
41 | "data": map[string]any{ |
||
42 | "id": params.StoreID, |
||
43 | "type": "stores", |
||
44 | }, |
||
45 | }, |
||
46 | "variant": map[string]any{ |
||
47 | "data": map[string]any{ |
||
48 | "id": params.VariantID, |
||
49 | "type": "variants", |
||
50 | }, |
||
51 | }, |
||
52 | }, |
||
53 | }, |
||
54 | } |
||
55 | |||
56 | response, err := service.client.do(ctx, http.MethodPost, "/v1/checkouts", payload) |
||
57 | if err != nil { |
||
58 | return nil, response, err |
||
59 | } |
||
60 | |||
61 | checkout := new(CheckoutApiResponse) |
||
62 | if err = json.Unmarshal(*response.Body, checkout); err != nil { |
||
63 | return nil, response, err |
||
64 | } |
||
65 | |||
66 | return checkout, response, nil |
||
67 | } |
||
102 |