 And I am going to be talking about the importance of following the WordPress coding standards. It's a long title. Thank you, thank you. We hope, if you like roller coasters, it's not, it won't be a roller coaster. How many of you in here were present for Cliff's talk? Okay, he's a tough act to follow, so don't judge me based on that. Before I get started, I thought I'd tell you guys a little bit about me so you know who I am and what I am all about, what I do, things like that. So I'm not just somebody up here spouting code, spouting conversation about code when you have no idea what I do for a living. So I am married, I have two kids, I should say I have two beautiful daughters, and I have two crazy terrier muts. And so when I'm not at my computer, I'm either hanging out with them, doing something with music, watch movies, like who doesn't do that, or running or exercising or something like that. After graduation, several years ago, I worked for a number of local internet companies. I was a software engineer first and then moved on to a team lead. Absolutely loved what I did. When there was a downturn in the economy and they cut our hours and they cut salaries, I did the dumbest thing that I could do, at least the dumbest thing I could do as it looks on paper. I quit my job and I went self-employed. And I spent my time working on building custom web applications for people using Rails, PHP, WordPress, and so on. Now I've had the LLC out of which I worked since I was in college because I was doing a little bit of contract work there and everybody likes a little bit of side income. But over the last few years, things have evolved such that I am self-employed for a company called Pressware, which is based on WordPress software. I don't know how clever that is, but it works. Thank you. And when I'm not at Pressware, I do code reviews, consulting, custom solutions for people, building web applications, themes, plugins, anything and everything involved in WordPress. I've been doing WordPress since I was in college. I've been working with WordPress since I was in college. So it's been a long time. I also blog daily at TomMcFarland.com. Usually it's around development-oriented practices. Sometimes I ran about things that are probably not that important, but I feel like I should write about them. And then I also have a tumble log called Dev Practices. And because everybody loves animated gifs, or a lot of people love animated gifs, this is meant to be a satire on a day in the life of a programmer. We're not going to be looking at that today. Look at it on your own time, or look at it now. Anyway, so in this talk, we are going to be talking about four things. First, we're going to talk about why the WordPress coding standards are important. Then we're going to be talking about why we should be using them after that we'll talk about the dangers in not using them. And then finally, we'll talk about why they contribute to maintainability of software. And this could be a theme, this could be a plug-in, this could be just some project that you're working on that you've inherited for someone else. So before we go any further, how many of you in this room are a developer? And this means that is awesome. I thought I was going to have to say it doesn't matter if you're a front-end developer, back-end developer, full-stack developer, but that's awesome. Love seeing all those hands raised. And how many of you throughout the course of your career have inherited a code base either when you walked into an agency or a corporation or from a client who said, here, fix this for me and thought, what is going on with this code base? Okay, two hands, okay? Or what have you thought? I have no clue how to fix this. I have no idea where to start, okay? And here's the one that is my favorite, we probably all thought it, at least to ourselves. If I was working on this, I certainly would have done it this way. And that's part of what I want to talk about today. So as we start off the conversation, I want to ask the question, what are WordPress coding standards? Now we'll have a question, we'll have a time for questions and discussion at the end of the presentation, but I think that if you were to ask 10 different people in the room, maybe you'd get 10 different answers or four different answers or eight different answers, I don't know. But in preparing for this presentation, I decided to look up the definition of coding standards in Wikipedia, and this is what it says. Coding conventions are a set of guidelines for a specific programming language that recommend programming style practices and methods for each aspect of a piece of a program written in this language. These conventions usually cover file organization, indentation, comments, declaration, statements, white space naming conventions, programming practices, programming principles, programming rules with thumbs, architectural best practices, et cetera. Who talks like that? If I was to come up to you and say what are coding standards, who would honestly say that? And this is true and this is accurate, but it's not conversational. And when you're talking with other programmers, no matter if they're beginner, no matter if they're intermediate, no matter if they're advanced, no one's going to say this. So before we get too deep into talking about them, I thought it might be a good idea to lay out a simpler definition of what coding standards are so that we can operate on that throughout the rest of our presentation. So how about this? Coding standards provide guardrails for how we should organize our files and format our code. They provide guardrails for how we should organize our files and format our code. Now, there's a lot wrapped up in this and we will expand on it a little bit more throughout this talk. But let's say that... But this still doesn't answer the question as to why they're important. We have a definition, but if we were to answer why they're important, coding standards set an expectation for how code will be written. They set an expectation for how you're going to write code. They set an expectation for how I'm going to write code. They set an expectation for how the person in the next two or three or four times over are going to write code so that we all have some kind of ideas to what the code is going to look like once it gets into our hands. Now, here's the thing about coding standards. If they set an expectation for how we're going to write code, then does that not implicit... Do we not implicitly mean that we are thinking of other people when we're writing our source code? And if you turn that around, aren't other people implicitly thinking about us? Maybe they don't know it, but if you're in the open source world, you are going to be contributing code to a project that people are at least going to have access to the source code. Maybe they're not going to be contributing to it. Maybe they're not going to be building anything on top of it, but they are going to be looking at it and hopefully learning something from it. So, with that said, maybe this is a little bit of a source code utopia. I don't know. But in my mind, if we were all to be following WordPress, or if we were all to be to follow coding standards, regardless of where we work or what we're working on, then shouldn't the code that we write or that we're contributing to a WordPress or that we're contributing to a code base look as if a single developer wrote it? That means that if Brian contributes code to a code base and then David contributes code to a code base and I contribute code to a code base and then Andy hops in and he looks at the code and he goes, I have no idea who wrote this. It looks fantastic, but I don't know which one of these guys wrote it. That's what it should be like. And that's the significance of WordPress coding standards. So, as I said, we implicitly have other people in mind and other people have us in mind. But this leads us to the next question. Why should we be using them? I mean, it's one thing to say, let's write it so that we can't tell who wrote it. It doesn't make a lot of sense when you put it that way. But I think that this can be answered in... Well, there's a couple of ways in which this can be answered, but the top two reasons that I found in my experience when building software for other people, both in the closed-source world and in the open-source world is that it makes it easier for other people and us and it saves time. So what does that mean? How does it make it easier for other people? How does it make it easier for us? How does it save time? So before we go any further, I'd like to know how many of you at this moment are familiar that WordPress has coding standards. Awesome. How many of you adhere to the WordPress coding standards? Okay. How many of you sometimes adhere to them? Okay, me too. And how many of you just don't? Awesome. That's good not to say any hands. So what I'm about to say next may... I may be the only person in the room. I was going to say I'm just as guilty as the next person, but it doesn't sound like there's anybody else but me. But when I first started working with WordPress, I was more concerned with getting something working for the client and then handing it off to them. I didn't know anything about the WordPress coding standards. I had to learn them. And when I found out they existed, I still didn't just jump right in and start learning them and applying them because it takes time to learn these things. And once I began to learn them and once I began to see advantages that they bring and once I began to come under scrutiny, the more code I released, the more I began to see that they do matter. So here's the thing with open source. When you're working in an agency and you're in an office and you're in a cube or one of the fancier offices, there's just an open floor plan with tables and spaces and things like that, you're able to see all of the people that you're working with. And that's great. You're able to hop over to their desk and say, hey, what would you do here? You're in a distributed environment either for yourself or for another company and you're working out of the isolation of your home in your basement or in your garage or at your kitchen table, then it gives the illusion that you're working by yourself. But when you're working on open source software, you are likely working with more people than you have ever worked before. And that matters and that should at least incentivize us to write code that follows strict or coding standards because we're not the only ones that are contributing to this code base. Other people are going to pick it up and they're going to have to get started with it and they're going to have to contribute to it. They're going to have to learn what it is that it's doing and then they're going to have to begin either fixing their bugs or working on their features or whatever it is that they're going to be tasked with for that day. So when the other thing about coding standards is that they help us to read and to write better code. Maybe I should say that the other way. They help us to write better code and read better code. And by that I mean, let's say that we're talking about WordPress. I mean we're at WordCamp so it makes sense. If I am all things considered equal, I'm familiar with the WordPress API, at least to some degree. I am familiar with the WordPress coding standards and someone hands me a project and says, I need you to fix X, Y, and Z. Or they say, I'd like you to add A, B, or C to this project. If you have a project that follows the WordPress coding standards and you are somewhat familiar with them, then you should be able to hop into the code with a reasonable level of action, discern what's going on, and then get started. And that's where the save time component comes in. But let's say that the code isn't written up the standard. Then you're left with having to look at this code, figure out what's going on, diagnose it, and we'll talk a little bit about this more in just a minute. Trace it and then try to find what the problem is. And there are some dangers in doing that and we will definitely drill down into that. But in addition to helping us to write the code, it helps us to read the code because it brings us up to speed faster. It helps us to get our work done faster. It helps us to turn around and give a product or a solution or whatever our client wants faster than if we're having to trudge through the spaghetti code that another developer has written because none of us have written spaghetti code before. But what are the dangers in not using them? I mean, this is where the ominous music should play and the light should dim and someone evil should walk in the room. Because it's one thing for me to stand up here and say, everyone should use the coding standards because it saves time and because it makes it easier to write code and it makes it easier to read code and all of that sounds great. But what are the dangers in not using them? And there's several things that we can talk about. Now this isn't all of them. I'm sure as I'm sitting here you have a running list and you're ahead of all the reasons. But I want to try to highlight some of the most important things that I've noticed in my career working with software that has resulted in what happens when you ignore the coding standards. So first off, it makes it difficult to diagnose a problem. So let's say that you inherit a code base and you need to do some work on a WordPress template. And the template includes CSS, the template includes PHP, the template includes calls to the WordPress API, the template includes HTML, the template includes JavaScript. I mean you look at it and it's visual noise for one thing. There's no separation of concerns and yet you are tasked with having to fix the problem. So you go in and you say, oh, here's the reason that this isn't displaying correctly. So you go in and you try not to knock around too much to the rest of the code that's happening and you make this change. But there's a problem in doing that because the code isn't written up to a particular standard and because it's not written up to a particular format. Not only do you have a hard time determining, not only do you have a hard time determining what is going on, you're going to have a hard time determining what are going to be the ripple effects of the changes that I'm introducing because the odds are is if code is not following coding standards and it does not have a solid architecture, you're going to make a change and that area of the code, maybe it's something that works purely on the server side, maybe it's something in a template. I don't know, it doesn't matter. Other modules touch that particular part of the code and if you change it and you're not aware of the rest of the program, you are going to cause a ripple effect throughout. So maybe you fixed one bug but you've introduced four. I don't want to be that guy. Maybe you consider that job security though, they keep coming back to you, I don't know. The second point is that we write more complex code and complex code is lame. I don't know anyone that's ever sat down with complex code and not set a four letter word. No one says yes, I get to work on this mess. And complex code is not only bad for humans but it can also negatively impact the computing power of the server on which it's running. Maybe it results in a time consuming process on the server, maybe it ends up causing some unintended consequence with a plug-in that is also interfacing with the theme which is interfacing with WordPress, I don't know. But writing complex code is something that following coding standards can help us to alleviate because, and there's an old saying, I believe Martin Fowler is the one who said this, but anybody can write code that computers understand but it takes a specific talent to write code that humans can understand. And granted, we can really only take it so far. It is called code for a reason. So we can't sit down and write it in English or whatever your first language is. But the third point is that it costs time and money. So if you run a business, maybe you're the only person who works there or maybe you are the only person or maybe you have a team of developers. I know you're used to hearing the phrase time is money and I don't want to belabor that point. But let's say that you're working on a project and you start off over here and a client tasks you with the requirements and the specifications and you are ready to get started and then you immediately begin implementing the code, you get something working, you get it shipped, you hand it over, oh, this is great, it works. A couple months later, I click on this and it takes me over here and there's these weird errors showing up in the log file and I had one of my local developers try to fix it and he has no clue what's going on. Can you please help us? So now you have to go all the way back to the beginning, try to discern what it is you were doing in the source code to get it to do whatever it is that it's doing, fix it and then kick it over the fence to the client again and as we've already said, this can result in spaghetti code or a big ball of mud as some of you may have heard this referred to. And so you're standing there, or so you're sitting there and you're working and you're kicking yourself, why did I write it this way? Why did I just not follow the coding standards? Because remember, following the coding standards can help you get up to speed faster. It can also allow you to ask other people for help because if they know the APIs and they know the formatting they should be able to sit there and reasonably trace the code or be able to trace the code with a reasonable level of ease. Now to be clear, there are going to be people who never follow the coding standards. They're going to be sitting in the office and they're going to say, nope, not doing it. They're going to say, my way of doing it is better or I've been writing code for X number of years and I've never seen a coding format. I've never seen code standards this way and my way is better and that's the way I'm sticking to it and that's their prerogative. And I don't want to say that maybe this person should be evaluated. I don't want to stick on the negative but maybe we should sit down with a person and say, why are you writing code this way? What is your problem with our current standard? Because it could very well be the case that your standard needs to be updated. Think of it as a living document. I mean on WordPress.org it's a living, it's a wiki and in the past two years the JavaScript coding standards have undergone a couple of revisions as it is and that's a good thing. But we have all of these things that go into building software. If you work in a large shop and maybe it's closed source software, I don't know, you have continuous integration. You have functional testing. You have use case testing. You have unit testing. There are so many things that work that pound on your software in order to make sure it's of the utmost quality before handing it over to your users. Coding standards should not be a negotiable and the reason is because software is something that has a lifetime which brings us to the next point. Coding standards help to contribute to the maintainability of our software and I struggled a little bit on what to name this slide because I can sit there and talk about the lifetime of software but when people think about software, how many of you think about life? Software and life and whatever. And then I started to think about it helps contribute to the sustainability of software but right now sustainability is highly wrapped up in a carbon footprint. What is the carbon footprint of your WordPress theme? So I just went with how they contribute to maintainability. So as we've already talked about a little bit, they prevent surprises. You get a code base from a client, from your boss, from a superior and assuming that you have a reasonable level of experience with the WordPress API and the WordPress coding standards, you are able to hop into the code and with a relative level of ease be brought up to speed on what it is that you're working on. And this can save valuable time which in turn saves valuable dollars which in turn saves you from getting a ticket later on from diving into the code base and having to fix a problem that the user is complaining about that could have been avoided if you had just written code correctly to begin with. Now, they can prolong an application's lifetime. This is another one of those sentences that I struggled with. I wanted to keep everything on one line but what does this mean? So when you look at software development and let's say you're building something from the very beginning, you get the requirements, you get the specification, you sit down with your team and you start building it. You maybe release a few betas, release candidates, things like that and then you hit version one and you're ready to ship. Awesome, let's have a party, that's great. But then for the next however many months your company exists or the product exists, you're going to be maintaining the software and the amount of time that exists for maintaining the software is so much greater than the amount of time that it takes to build it. So if you're going to spend so much time working on your program, why would you not do your due diligence at the beginning to make your job a little bit easier? And this is not necessarily, excuse me, this is not necessarily only true for people who debug code by opening it in their editor, opening it up in a web browser and then trying to figure out what's happening because there are people who also use debugging software. They're used to setting breakpoints, they're used to adding watches, they're used to stepping into functions and out of functions and looking at the call stack and all of this technical jargon that really helps to debug software and that's their style and that's fine. But coding standards help us to get started faster. It doesn't matter how you debug your code and this is extraordinarily important. I know I've already said it some during this talk but I don't want to, I want it to be clear that it helps us to get started faster for the new project, with an existing project, with a project that we worked on six months, the project that we worked on six months ago and it needs to work on one now and it generally helps make the entire development process easier. But there's more and I don't necessarily know what all of the reasons are that you have for needing to follow the WordPress coding standards. I'm sure that you have a long list of reasons in your head and I'm sure you've got in number of complaints that you could give for how not having the coding standard or not following coding standards or best practices has negatively impacted your software. But I'm telling you that from the very beginning from the outset of a project or even if you're maintaining a project do what you can to follow the WordPress coding standards and to be clear there are coding standards for PHP, there are coding standards for JavaScript, there's some things that you can read about for HTML and there's an entire page in the WordPress codex that walk you through these standards. It even is complete with examples, even on how to document your code. So if during this talk you've been spending your time on Twitter or in Slack or on Facebook or blogging or tuning out or whatever, that's fine. There's only one thing I want you to take away from this talk. And that is to follow the WordPress coding standards. That's it. I don't have a three-point list for how do I make my blog better and I don't have a nine-point checklist for you should be doing all of these things in order to get your blog ranked number one on Google. That's not what this talk is about. This talk is about making sure that you are creating the most high-quality, well, in our case, the most high-quality WordPress software possible, be it with themes, be it with plugins, being with web applications, whatever. So when you walk out of this room, if anyone asks what was this talk about, all it's doing is saying follow the WordPress coding standards. And I say please, not only on behalf of me, but on behalf of others who may be contributing to your code and on behalf of your future self so that when you hop into your code in six months, you're not a developer who wrote it, which was you. But thank you very much. That's my talk. So at this point, I'm going to leave this discussion totally open to it. Otherwise, I'll also know lunch is coming up, so I didn't want to bleed too far into the food, but yeah. Yes, there are two people who are notable in the WordPress development community who are, or last I checked there are two, who are extraordinarily talented programmers. They audit the code base and they approve it. Now, in terms of the standards, if you miss some white space here or if you miss some braces here, they may not say anything or you may get an email. It kind of depends on who audits it. But if you completely disregard what is there, you will get an email saying we can't approve this because it doesn't follow the standards. Yes. Yeah, so the, to your first point in terms of the coding standards being outdated, that is something that a lot of people bring up. I myself don't have an opinion on if they're outdated or not. I tend to follow them. I do like them and I've worked in the other languages that I've worked in, I actually tend to favor the WordPress coding standards more than others. Things like Yoda conditions, things like lots of white space, things like always putting curly braces around your conditionals or your loop for things like that. I really like that. Now in terms of how you get that to play nicely with these third party dependencies, as they say, the struggle is real. So let me ask you, in your project, are there places in, say, a template where you are using both WordPress and some code from this other dependency and they're both present in a single file? That's a tough one. What I would say, if I was in that position, all things considered right now, I would probably use the WordPress standards as much as I could with the WordPress specific code and then using the standards for whatever other dependencies are for that. And I know that can cause some conflict, especially as it relates to PHP and maybe even some JavaScript. And so, ultimately I would say what is going to help you and your team maintain this software the most moving forward? Because it's built on WordPress, it's going to be open source by nature. So, you know, if you have this and people download or people get access to the source code, however it happens, you may get pushback. Oh, you're not following the coding standards. You're not a WordPress developer, please. But if you do what's best in terms of helping you, in terms of helping your team write code that is going to help maintain the software over time, I think that trumps which standard you're choosing. Yes. Depends on what part of WordPress core you're in because WordPress core is old and the standards are new. So, some people may say, I don't want to say it like this, some people may say submit patches. That's fine. Some people may say, at least open a track ticket to bring it to their attention. But if you see code that either smells or if you see code that doesn't necessarily follow the coding standards in core, try to believe the best rather than assume the worst. Assume that that code is older than what the standards are. Because the standards do change over time, ideally for the better. In my opinion, there's more people now working on the standards than they ever have. And if you follow any of the Make WordPress blogs, you'll see the discussion and the comments that are just, I don't think you should do this or I think you should do that. It's a really healthy discussion. But if you're deep in WordPress core, and you're a little old, whereas the coding standards could be two years old. So you have to kind of take it with a grain of salt. Yeah. Anything else? Yes. That is an excellent question. So tell me a little bit about your environment. Do you have WordPress set up on your computer? And are you familiar with installing plugins? I'm sorry? Yes. The question was, are there anything, are there anything, to help check if my existing code is up to par with the WordPress coding standards? There's a couple of things that you can do. If I leave something out, please, anybody feel free to chime in with what your suggestions might be. One, there is a plugin in the WordPress plugin repository called Developer. It is a plugin that is designed for developers. And it includes a number of different, not many plugins, but it includes a number of different tools that will look at your code and will tell you some problematic areas. Now just because your code works doesn't mean that it's necessarily up to snuff. That's fine. That's how we all learn. So that's one plugin that I would recommend. There is a make sure that on your web server you have PHP warnings and errors turned on to display in the browser. Because it will write everything to a log, and you're never going to see them. I'd much rather be sitting there writing code and have it dump and throw up all the problems in a web browser so that I know, oh, okay, here's the problem in this file on this line. This is what I need to fix. There is a constant that you can define in WP config that is, I believe WP underscore debug and you set it equal to true. That will also help you in terms of debugging your code. Now some of the things that I've suggested they won't necessarily always evaluate your source code but they will definitely tell you where there's problems. And there are other people that are out there that you can always ask for help. If you're familiar with sites like GitHub you can put your code on GitHub. And I don't know if you blog or if you tweet or whatever but ask other people, hey, I'm looking for some critique on this or I'm looking for your opinion on is this up to standard or not. And then the last thing is you can always compare say a given function to what the WordPress coding standard document would say here's how a block should be formatted versus how yours should be formatted. So does that help? If you want to go super hardcore you can download PHP CS which is a command line tool to check coding standards. You can use, go to the composer talk you can use composer to download that. And you can basically run files, there's like a rule file. If you go to GitHub there's a repository it's not official but it's called WordPress Coding Standards and it's a tool that will, PHP CS will look through your code, look through your brackets and your spacing exactly and tell you what line and where it's not making that, it's not following that standard. Cool. If you don't want to go to that hardcore. Which is hardcore. And thumbs up for that. It's really helpful. You can do all that. It's awesome. If you don't want to be a little bit lazier than that. Micah is having a talk on PHP Storm later today. Raise your hand Micah. Listen to this guy. I'm going to go to the technical WordPress and it'll actually view it for you. While you're writing code. So as you write a function it'll it'll But it is a premium ID. It costs about how much? 100 bucks a month. If you have an educational email address you can get it for free. Yes, and if you actually run an open source repository on GitHub you can email them for an open source free license. That takes a couple of days. But if they Yeah. But throw your code on GitHub and then tell them that you have an open source project. There you go. What IDE do you use? What editor do you use to write your code? I'm personally not familiar with that so I can't suggest anything for that particular program. Other questions? I I use a combination of different things. I use Coda with a number of different plugins. I use a tool called code bug app which is a debugger. And then I use a suite of different plugins within WordPress to help me sniff out problems with whatever I'm working on. I do get some pushback from people who say you're using Coda because I've done PHP storm before. I've used Visual Studio in a previous job. PHP storm is great and this is totally vain but Coda is prettier. And if I'm going to spend 10 hours a day in an IDE I want it to look good. PHP storm it doesn't do it for me. Choose your words carefully. It does. I'm going to stop because I don't want to tweet out. So there you go. What's up? There's a plugin that is like as you type WP query. It'll format WP query. But it's definitely not as hardcore about staying on top of actual formatting. It won't like yell at you if you write a function without spaces. I could be wrong but I think I saw something the other day where someone is working on a plugin like that. And I know that maybe Ryan McHugh is working on a plugin that will give you basically intelligence. So while you're typing functions it'll say this function expects these arguments. So it helps to follow some of the genius people in WordPress to see what they're up to. Some use Sublime, some use Adam, some use well the geniuses don't use Coda. They'll use PHP storm You just gave him a laptop. That is a I've never had that happen at a WordCamp talk. Yes, yes. Yes. For those of you that didn't hear, look for the query monitor plugin. It installs, it is a WordPress plugin. It allows you to monitor all of the queries that are happening within WordPress for any given page load. And is, can you modify queries within the monitor to change values? There is a plugin that allows you to do that. I don't know if that's, okay. Yeah, there's, there was like a developer oriented plugin, debug bar or debug console. Debug, debug, you have to get debug bar and then debug bar console. And then you can write code within the context of WordPress to make WordPress do things and test the code. So that's debug bar and then debug bar console. Yeah, yeah, yeah. None of those, all of those should stay on your laptop. Yeah, that, that, the debug, the developer plugin's kind of gotten, I don't want to say monolithic, but I mean it's got a healthy suite of tools with it. I don't know if you would use everything that it has, but it does a walkthrough of are you building a plugin, are you building a theme, and then it'll let you select what you want to install. So there's a couple of, there's a couple of recommendations I have. Are you building a theme? Go to themereview.co. It's a new, it's a new site. It is set up by some WordPress veterans. They charge a reasonable price for doing code auditing. I would also recommend at the risk of sounding like I'm sucking up to Brian Crog's guard go to post status, look at the members and see who offers that as something that they do. Okay, that's right. I forgot we had that agreement. I thought it was $400. Yeah, okay. Okay, okay. Anything else? Awesome. Well, thank you guys very much for coming. Enjoy the rest of WordCamp. Thank you.