Suggestions for writing better code

I’m becoming more confident with Hugo and my ability to code the functionality I need. Still asking for help here and there so I still have a long way to go which is fine.

My question is pretty open-ended, but what should I be thinking about now that I’m not only concerned with getting the functionality I want but also on writing clean and maintainable code.

In terms of Hugo, I’m interested in understanding more what functions to avoid due to performance, is creating a range within a range or using multiple where statements suboptimal etc.

Any concrete examples of how to write effective Go templates would be appreciated.


Here’a current example to show more of what I’m trying to understand. I can achieve the same result doing either

{{$sections := (.Site.GetPage "section" "books").Sections}}
{{range $sections}


{{range where (where .Site.Pages "Section" "books") "Layout" "eq" "keyword"}}

Which is better?

Thanks to @kaushalmodi suggestion, I’ve played around with --templateMetrics. And run the above code as partials. It seems the first option is preferred, but that’s as an average from running --templateMetrics a few times.

Try hugo --templateMetrics --templateMetricsHints. That will help you quite a bit to narrow down to the slowest guys in your template.

1 Like

Check out the use of where + intersect if you need to nest where expressions.