| Conditions | 14 |
| Total Lines | 62 |
| Code Lines | 42 |
| 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:
Complex classes like core.UpdateMem often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package core |
||
| 12 | func UpdateMem(c *fiber.Ctx, r *models.Mem, validation models.CardResponseValidation) { |
||
| 13 | db := database.DBConn |
||
| 14 | |||
| 15 | mem := new(models.Mem) |
||
| 16 | |||
| 17 | mem.UserID = r.UserID |
||
| 18 | mem.CardID = r.CardID |
||
| 19 | |||
| 20 | var memType int |
||
| 21 | |||
| 22 | if r.Efactor <= 1.4 || r.Quality <= 1 || r.Repetition < 2 { |
||
| 23 | memType = 2 |
||
| 24 | } |
||
| 25 | |||
| 26 | if validation.Validate { |
||
| 27 | if r.Repetition == 0 { |
||
| 28 | mem.Interval = 1 |
||
| 29 | } else if r.Repetition == 1 { |
||
| 30 | mem.Interval = 2 |
||
| 31 | } else if r.Repetition == 2 { |
||
| 32 | mem.Interval = 2 |
||
| 33 | } else if r.Repetition == 3 { |
||
| 34 | mem.Interval = 3 |
||
| 35 | } else { |
||
| 36 | mem.Interval = uint((float32(r.Interval) * r.Efactor)) + 1 |
||
| 37 | } |
||
| 38 | mem.Repetition = r.Repetition + 1 |
||
| 39 | if memType == 2 { |
||
| 40 | mem.Quality = 4 |
||
| 41 | } else { |
||
| 42 | mem.Quality = 5 |
||
| 43 | } |
||
| 44 | } else { |
||
| 45 | mem.Repetition = 0 |
||
| 46 | mem.Interval = 0 |
||
| 47 | if memType == 2 { |
||
| 48 | mem.Quality = 1 |
||
| 49 | if r.Repetition <= 1 { |
||
| 50 | mem.Quality = 0 |
||
| 51 | } |
||
| 52 | } else { |
||
| 53 | mem.Quality = 3 |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | mem.Efactor = r.Efactor + (0.1 - (5.0-float32(mem.Quality))*(0.08+(5-float32(mem.Quality)))*0.02) |
||
| 58 | |||
| 59 | if mem.Efactor < 1.3 { |
||
| 60 | mem.Efactor = 1.3 |
||
| 61 | } |
||
| 62 | |||
| 63 | db.Create(mem) |
||
| 64 | |||
| 65 | memDate := new(models.MemDate) |
||
| 66 | |||
| 67 | if err := db.Joins("Card").Joins("User").Joins("Deck").Where("mem_dates.user_id = ? AND mem_dates.card_id = ?", |
||
| 68 | mem.UserID, mem.CardID).First(&memDate).Error; err != nil { |
||
| 69 | } |
||
| 70 | |||
| 71 | memDate.NextDate = time.Now().AddDate(0, 0, int(mem.Interval)) |
||
| 72 | |||
| 73 | db.Save(memDate) |
||
| 74 | |||
| 77 |