While doing a little debugging on a RichText widget problem, I began to wonder about what we’re doing in the way of creating a set of conventions and standards that all developers should follow while developing various things for MODx. The issue was raised when Ryan created a global CSS class name for the RichText widget when it appears on the frontend. My thoughts were that names for CSS classes that are assigned within the core code should be standardized so as to not cause confusion. As such, Ryan suggested I make a post about this and get the ball rolling. So, here’s some things to think about and some standards that I think we should follow close to the letter. These standards apply not only to the development of plugins, modules, snippets, and such but also to core code and files as well.
PHP Coding Standards
The PEAR site probably has the best methods for PHP coding standards (
http://pear.php.net/manual/en/standards.php). Everything from using a 4 space indent (no tabs) to the use of curly braces in control structures is briefly covered. The content is brief but the ideas presented are such that it may take a little practice to get used to. In the end, if everyone follows them then the result is code that is easier to read and maintain...especially for those who are picking up the code for the first time. Whether you’re working on core code or a snippet, its a good idea to be very consistent with how you code. These guidelines will help immensely with this.
Comments
One of the things that is of the utmost importance is the use of comments. Comments, just like the rest of your code, should be well structured and follow the flow of your code. I usually like to break up my code in sections and use a comment just before a block of code to comment on what the overall block is used for. Not only does it make the code easier to read, but other people looking at the code will clearly understand why I did this or why I did that.
Speaking of comments, it would probably be a really good idea to implement some sort of standardized way of marking up a core file, snippet, plugin, or module with a starting comment. For instance, consider the following comment at the beginning of the FCKEditor plugin code:
/**
* FCKEditor - RichText Editor Plugin
* Written By Raymond Irving - June 22, 2005
* Modified By Jeff Whitfield - October 13, 2005
*
* Both frontend and backend interface provided
*
* Configuration:
* &webset=Web Toolbars;string;['Bold','Italic','Underline','-','Link','Unlink']
*
* Version 2.1.1
* FCKeditor v2.1.1
*
*/
Perhaps a better approach would be to use some standardized names for certain pieces of information. Now, with that in mind, consider the following alteration:
/**
* Name: FCKEditor
* Version: 2.1.1
* Type: RichText Editor Plugin
* Author: Raymond Irving
* Created on: June 22, 2005
* Modified by: Jeff Whitfield
* Modified on: October 13, 2005
*
* Notes:
*
* Based on FCKeditor v2.1.1
* Both frontend and backend interface provided
*
* Configuration:
* &webset=Web Toolbars;string;['Bold','Italic','Underline','-','Link','Unlink']
*
*/
Does that make much more sense? Granted, it’s not set in stone, but the information we provide at the beginning of any core file or code (snippet, module, etc.) should probably be a little more standardized in the way we present it.
Naming Conventions
This is a little more tricky one. Exactly what should the name of a file, function, or class be? This is one area that should really be thought over a little bit. There’s probably a million ways we could address the naming conventions in functions, classes, and variables. But one good rule to keep is that unless a given function, class, or variable is private within the scope of your code, it’s best to keep the name very unique. A good suggestion is the start the name of the unique function or variable with the name of your application followed by an underscore then the name of your function or variable.
In the case of filenames, I honestly think that there should be no uppercase letters in the filename under no circumstances. Considering that Apache filenames are case-sensitive, it’s probably a good idea to keep all filenames as lower-case...not to mention that it’s so easy to forget that a given include file has an upper-case letter in it, thus giving you an error when you attempt to include it in your code.
CSS Class Names
Whether they are used within a snippet, module, or assigned within the core code as a global CSS class, it’s important to pay attention to the names being used for any given CSS class. For instance, Ryan recently assigned a class name to the DIV that wraps an instance of a RichText widget. At first, he used the name "MODX-rte". Granted, it works and it’s fairly unique...but I felt we could do better and standardized the method used for global CSS classes within the core. So, I suggested "MODx_RichTextWidget" as the class name. Not only does it tell the developer that it’s a "MODx" CSS class, but it also tells the developer what it’s used for making it much easier to deal with when the class is assigned to a stylesheet.
These are just a few of the standards and conventions I’m thinking about. There’s certainly more to think about here and I would love to hear everyones thoughts. If anything, perhaps we should consider drafting up some standards and conventions for MODx development. This will help out things quite a bit in the future as we continue to expand and grow.
Jeff