. Why coding standards are useful and important
2. What coding standards do is add an extra layer of readability to the code and basically polish. And this polish has two functions. The first is very functional and it has to do mostly with what happens when another developer is looking at your code.
3. We all come from different backgrounds of different programming languages and we've learned how to code from a variety of different sources. So we come to the table with different ideas about what makes code easier to read. Maybe we like curly brackets.
4. Maybe we like indentation better. Maybe we want to leave out as much as we possibly can and condense or code into the smallest possible format. And this is fine as long as we're working by ourselves but as soon as another developer comes into the picture, they'll find the same things that we find particularly nice about our syntax, very annoying or irritating.
5. And while it's not impossible to get around or impossible to understand code that's in that structure, there's going to be a little bit of resistance to working with it and there'll be the tendency to rewrite that code to follow their particular syntax. Their structure that they like to use. By all developers agreeing on a particular syntax that covers all the nuances of coding, it means that the time that would be spent rewriting code to match the syntax we like and having that undone by the next developer is saved and can be used toward the development process itself.
6. And it means that we don't get frustrated then by another developers style. Instead we can be frustrated with the standards and if there's actually a problem there, there's an outlet to discuss that and figure out if there might be a better way than the current coding stands dictate. The second thing you get from the polish of using coding standards, is a little less concrete.
7. And it has to do with what happens when you share your code publicly. More seasoned Drupal developers, when they being to review code in a module, they'll look first at whether the coder is using coding standards. And if they're not, then the'll extrapolate from that and assume that the coder probably hasn't familiarized themselves with the best way to interact with Drupal's API's, and how to write secure code as well.
8. And it will become a sticking point, so following coding standard is a lot like getting past the bouncer into an elite club. Basically it improves the chances of the functionality and ideas behind your code being taken more seriously. We're covering coding standards fairly early on in this video series, because in order to get comfortable with them, you need practice.
9. And the video in this series will give you ample opportunities to get that practice in. Most likely by the end of this series, it will be second nature and you won't even have to think about it. The best place to learn about Drupal coding standards is "drupal dot org slash coding dash standards".
10. This page contains details about syntax orientated coding standards and if you scroll down you'll see that there's a good bit of meat to this page but it's not too terribly long. So even though we're going to go through the details of this page in a more summarized format, it's a good idea to read through this as soon as you have time. Also, on the right hand column you'll see there are a number of sub pages to this page that include more specific information about commenting, SQL coding and other more specific subjects.
11. What I've done for this video is created a very simple module to demonstrate most of the coding standard concepts in a simplified fashion. You can use this module both as a reference and as a sandbox to play with the code that's in it and see if you can get it standard compliant. So go ahead and open your "buildamodule" resource folder and copy the standards module and we're going to paste this into our sites, all, module, custom folder.
12. Ok. Let's go ahead and refresh our modules page on our site at "admin slash modules" and if we scroll down near the bottom we should see a "BUILD A MODULE" section and in here we should see a standards module. Now we don't actually need to install the module.
13. We can do pretty much all we need to do for this video by just taking a look at the source code. So let's go ahead and open up our dot module file. The structure of this module is very simple.
14. We have two functions. One is an implementation of "hook_menu' called "standards_menu" and it's adding a page to allow us to demonstrate the coding standards inside of a page so if you wanted to mess with the code and see how it changes the output, you can do that. And then we have the function that actually generates a page called "standards_page".
15. This file contains a number of comments. At the top here you have a link to the coding standards page just in case you need a reference there. And then inside the standards page are a number of comments followed by examples of the coding standards so we're going to go through this example by example.
In our most popular collection, we build a strong foundation for developing in Drupal, covering the most commonly used APIs, building practical and interesting modules, and covering a tremendous amount of ground. Just a few of the topics we cover:
- How to build and manipulate forms with the Form API
- How to write jQuery to build a rich interface
- How to programmatically manipulate content and user data