| Conditions | 13 |
| Total Lines | 52 |
| Code Lines | 35 |
| 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 connector.registerProxy 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 connector |
||
| 15 | func registerProxy(name string, logger logger.Logger, translator sqlcommons.SQLAdapter, sqlDriver driver.Driver) { |
||
| 16 | |||
| 17 | drivers := sql.Drivers() |
||
| 18 | for _, item := range drivers { |
||
| 19 | if item == name { |
||
| 20 | return |
||
| 21 | } |
||
| 22 | } |
||
| 23 | |||
| 24 | sql.Register(name, proxy.NewProxyContext(sqlDriver, &proxy.HooksContext{ |
||
| 25 | Open: func(_ context.Context, _ interface{}, conn *proxy.Conn) error { |
||
| 26 | logger.Qry("Open conn") |
||
| 27 | return nil |
||
| 28 | }, |
||
| 29 | Close: func(_ context.Context, _ interface{}, conn *proxy.Conn) error { |
||
| 30 | logger.Qry("Close conn") |
||
| 31 | return nil |
||
| 32 | }, |
||
| 33 | |||
| 34 | PreExec: func(_ context.Context, stmt *proxy.Stmt, _ []driver.NamedValue) (interface{}, error) { |
||
| 35 | stmt.QueryString = translator.Translate(stmt.QueryString) |
||
| 36 | return time.Now(), nil |
||
| 37 | }, |
||
| 38 | PostExec: func(_ context.Context, ctx interface{}, stmt *proxy.Stmt, args []driver.NamedValue, _ driver.Result, _ error) error { |
||
| 39 | logger.Tracef("Exec: %s; args = %v (%s)", prettyQuery(stmt.QueryString), args, time.Since(ctx.(time.Time))) |
||
| 40 | return nil |
||
| 41 | }, |
||
| 42 | |||
| 43 | PreQuery: func(_ context.Context, stmt *proxy.Stmt, _ []driver.NamedValue) (interface{}, error) { |
||
| 44 | stmt.QueryString = translator.Translate(stmt.QueryString) |
||
| 45 | return time.Now(), nil |
||
| 46 | }, |
||
| 47 | PostQuery: func(_ context.Context, ctx interface{}, stmt *proxy.Stmt, args []driver.NamedValue, _ driver.Rows, _ error) error { |
||
| 48 | logger.Tracef("Query: %s; args = %v (%s)", prettyQuery(stmt.QueryString), args, time.Since(ctx.(time.Time))) |
||
| 49 | return nil |
||
| 50 | }, |
||
| 51 | |||
| 52 | Begin: func(_ context.Context, _ interface{}, conn *proxy.Conn) error { |
||
| 53 | logger.Qry("Begin") |
||
| 54 | return nil |
||
| 55 | }, |
||
| 56 | Commit: func(_ context.Context, _ interface{}, tx *proxy.Tx) error { |
||
| 57 | logger.Qry("Commit") |
||
| 58 | return nil |
||
| 59 | }, |
||
| 60 | Rollback: func(_ context.Context, _ interface{}, tx *proxy.Tx) error { |
||
| 61 | logger.Qry("Rollback") |
||
| 62 | return nil |
||
| 63 | }, |
||
| 64 | |||
| 65 | OnError: func(ctx interface{}, err error) error { |
||
| 66 | return translator.ErrorHandler(err) |
||
| 67 | }, |
||
| 74 |