Coding standards. CamelCase and snak_case

Coding standards

Hi there. I’m working on some plugins for WordPress and I really love WordPress, it’s standards and being a WordPress Developer but sometimes I feel lost while writing code.

I think this idea is not a priority issue of WordPress community. And also this idea can be perceived as negative, but I really hope you would give it a chance.

After a lot of thinking I realized that only one thing stopping me from being happy: snack_case instead of CamelCase.

Why I think that CamelCase is better? There is few reasons for it.

More clear folders and files structure

Imagine that you are creating a plugin or a theme with dozens or even hundreds files with code. You need to keep your logic simple and not to do hardcoded things. But without separated folders you can’t do it. If you want to keep your code simple you need to split parts (fragments) from each other. This will make your code more reusable and simply. For example:

Admin
Options
|- TokenOption.php
|- LastUpdateDateOption.php
API
|- API.php
|- Actions
|-- SomeAction.php
|-- AnotherAction.php
Plugin.php

Looks good? At the first glance you can recognize that this is a plugin (Plugin.php). It have `Admin` part and some `Options` like `LastUpdateDateOption.php`. In addition you can see `API` part. And you don’t care about if you are interested in settings pages which can be found in `Admin` folder.

Let’s see what we can found in Jetpack source code. This plugin have more flatted folders organization and in root folder you can see many files and you can’t find where to start. Yes, Jetpack’s code is not complicated but you will need more time to learn it.

Composer autoloader (PSR-4)

Are you boring with tons require and include statements? This pieces sometimes appear in unpredictable places. Moreover this statements defines and affects the logic of using your code. This may be a blocker if you or someone else want to use your code or components in different manner or place.

For this reason developers invented autoloaders. You can create your own autoloaders with custom logic or use Composer to autoload all of the PHP files for you. It’s so simply that it doesn’t require any additional skills in programming. The example above with perfect plugin structure is ready to use with Composer autoloader and PSR-4 standard. And no more tons of require in millions of files which makes code more complicated and hard to understand.

Filenames

We can name files more friendly. For example, comments.php — what is it? Class or just plain functions inside? Maybe this class extended from some other class or implements interface?

But if we add one more word then it makes more sense. CommentsModule.php — it’s a module in Jetpack! And yes, it’s a PSR-4 friendly (as you might guess).

The coding standard with BlahBlahModule.php manner is used in Symfony. We can learn and get a lot of fresh ideas from Symfony. Borrowing good ideas is a bad thing?

WordPress itself

WordPress itself typed in CamelCase 🙂 And it looks pretty and humanity (simply and comfortable to read).

Third party software

Many other software using CamelCase for generating PHP code. For example you can press few buttons and PHP Storm automatically create a properly named getters and setters for your properties in class. This feature is huge time saver. And also Storm generates a PHP Docs for this stuff.

I want to discuss this with WordPress community 🙂 What you think?

P. S. I don’t say anything about tabs or spaces (as many others do) because it doesn’t make sense.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s