Conditions | 15 |
Total Lines | 57 |
Code Lines | 44 |
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 partials.Navbar 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 | // Code generated by templ - DO NOT EDIT. |
||
37 | func Navbar(username string, fromProtected bool) templ.Component { |
||
38 | return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { |
||
39 | templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) |
||
40 | if !templ_7745c5c3_IsBuffer { |
||
41 | templ_7745c5c3_Buffer = templ.GetBuffer() |
||
42 | defer templ.ReleaseBuffer(templ_7745c5c3_Buffer) |
||
43 | } |
||
44 | ctx = templ.InitializeContext(ctx) |
||
45 | templ_7745c5c3_Var2 := templ.GetChildren(ctx) |
||
46 | if templ_7745c5c3_Var2 == nil { |
||
47 | templ_7745c5c3_Var2 = templ.NopComponent |
||
48 | } |
||
49 | ctx = templ.ClearChildren(ctx) |
||
50 | _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<nav class=\"navbar transition-all md:px-5\" data-testid=\"navbar\"><div class=\"navbar-start\" data-testid=\"navbarStart\">") |
||
51 | if templ_7745c5c3_Err != nil { |
||
52 | return templ_7745c5c3_Err |
||
53 | } |
||
54 | templ_7745c5c3_Err = LogoImg().Render(ctx, templ_7745c5c3_Buffer) |
||
55 | if templ_7745c5c3_Err != nil { |
||
56 | return templ_7745c5c3_Err |
||
57 | } |
||
58 | _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</div><div class=\"navbar-center\" data-testid=\"navbarCenter\"><a hx-swap=\"transition:true\" href=\"/\" class=\"text-3xl font-bold\" preload>Memnix</a></div><div class=\"navbar-end\" data-testid=\"navbarEnd\">") |
||
59 | if templ_7745c5c3_Err != nil { |
||
60 | return templ_7745c5c3_Err |
||
61 | } |
||
62 | if fromProtected { |
||
63 | _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<span class=\"font-bold text-indigo-700 mr-8\" data-testid=\"username\">") |
||
64 | if templ_7745c5c3_Err != nil { |
||
65 | return templ_7745c5c3_Err |
||
66 | } |
||
67 | var templ_7745c5c3_Var3 string |
||
68 | templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(username) |
||
69 | if templ_7745c5c3_Err != nil { |
||
70 | return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/v2/views/partials/navbar.partial.templ`, Line: 25, Col: 15} |
||
71 | } |
||
72 | _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) |
||
73 | if templ_7745c5c3_Err != nil { |
||
74 | return templ_7745c5c3_Err |
||
75 | } |
||
76 | _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</span> <button hx-swap=\"transition:true\" hx-post=\"/todo/logout\" hx-confirm=\"Are you sure you want to log out?\" hx-target=\"body\" hx-push-url=\"true\" class=\"btn btn-ghost text-lg\" data-testid=\"logoutButton\">Logout</button>") |
||
77 | if templ_7745c5c3_Err != nil { |
||
78 | return templ_7745c5c3_Err |
||
79 | } |
||
80 | } else { |
||
81 | _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<a preload hx-swap=\"transition:true\" data-testid=\"loginButton\" class=\"btn btn-primary text-lg\" href=\"/login\">Login</a>") |
||
82 | if templ_7745c5c3_Err != nil { |
||
83 | return templ_7745c5c3_Err |
||
84 | } |
||
85 | } |
||
86 | _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</div></nav>") |
||
87 | if templ_7745c5c3_Err != nil { |
||
88 | return templ_7745c5c3_Err |
||
89 | } |
||
90 | if !templ_7745c5c3_IsBuffer { |
||
91 | _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) |
||
92 | } |
||
93 | return templ_7745c5c3_Err |
||
94 | }) |
||
96 |