Recently while updating an old VBScript Component Template I was surprised at seeing several bad practices in place and began to update the template. When we update a template we should always try to clean it, add comments, etc and leave it in a better state than we found it. Here I will give some tips for keeping things in order:
1. Good indentation – Tridion Templates open in their own browser window. Don’t edit the template there – DON’T DO IT! IT can be tempting, and maybe save you 30 seconds of copying it to your text editor – but you’ll screw up your indentation and make it impossible to follow for your other colleagues who are using text editors for template development.
Update: Set Notepad + to use spaces instead of tabs. Thanks Ingmar for the comment! http://stackoverflow.com/questions/455037/notepad-tabs-to-spaces
2. Access Component Presentations in Page Templates, Fields in Component Templates. Looping through Component Presentations in the Component Template is bad practice, slows the template down, ais not consistent with data model of Tridion and doesn’t make sense.
3. Use local variables in functions and subs. Using global variables in functions and sub-routines is bad practice. Respect the scope of a function or sub and pass in all variables, otherwise, it makes it very difficult to debug later.
4. Set variables when you declare them. For example, in VBScript use
Dim body : body = ""
5. Consistency with if conditions. Don’t write if statements on 1 line – especially within a nested if in VBScript. Makes it terribly difficult to follow.
I hope we can all establish good coding practices and follow them to keep our templates clean and in good health!