How to standardize the formatting of javascript and php codes?

1

I have some time already in languages, but whenever I come across other codes or I'm in a different job, each one has a different way of standardizing.

Is there any recognized standard, type W3C, or any tool that validates the code?

    
asked by anonymous 25.03.2014 / 16:25

3 answers

1

Yes, it does. There are several standards, including online tools that "embellish" your code according to some existing pattern.

link

link

There's also the PHP_CodeSniffer, Pear's built-in package, which you can embed into your favorite IDE.

In my case, I use it to validate the PHP code of collaborators before accepting a pull request in the versioning tools. Whenever a PHP file is saved, my IDE (komodo edit) automatically invokes the command:

/usr/lib/php/pear/phpcs %F --standard=phpcs

Example of part of a result without validation:

FILE: phpexample.php

FOUND 908 ERROR (S) AND 3 WARNING (S) AFFECTING 238 LINE

102 | ERROR | The variable names for parameters $ get (3) and $ main (4) of       | | not align

102 | ERROR | The comments for parameters $ get (3) and $ main (4) do not       | | align

199 | ERROR | Spaces must be used to indent lines; tabs are not allowed

199 | ERROR | Line indented incorrectly; found at least 4 spaces, found       | | 1

And you choose from several formats.

    
25.03.2014 / 16:49
0

For php there are some suggested recommendations that are:

PSR-0 , Regulates the definition of namespaces , so that the way to load (autoload) classes is a standard independent of the framework / lib used, because today some frameworks use their own mechanisms, that is to say if it requires only one component it is almost obligatory to use the entire framework because of dependencies or autoload proprio.

PSR-1 , Qugere a standardization, what kind of code some files should have and a bit of codestand.

PSR-2 , Basically suggests codestand

There are some tools like the codesniffer that serves to show deficiencies in the code eg duplicate code or wrong tab, this is made of through rules defined by the user, here is a article for more information.

    
25.03.2014 / 16:45
0

Hello,

PHP has set some standards in this file .

About Javascript you can find in these places:

It's also important to check if the company or project you work follows some standard or if you have some code formatter.

Abs!

    
25.03.2014 / 16:36