How to choose a convention for names of variables and functions?

3

I'm going through the second development company and in both it does not have an own convention to use in the declarations, in the codes I get functions declared as follows:

  
  • PHP and jQuery
  •   
    function nome_pessoa();
    function nomePessoa();
    function nomepessoa();
    function Nome_pessoa();
    

    I know that both choices work but it is very "ugly" because there are times that in the same file have a function being called function nome_aluno() and then in another location in the same file nomeResponsavel() let's say so, functions declared with 3 or 4 (underline) function nome_dos_responsáveis() , I do not even know if this can cause slowness in the system, I do not want to create a discussion, but to know which way is most used by you, I know it can also be a very personal matter as a choice of IDEs for development, but I want to get used to something that is clearer.

        
    asked by anonymous 06.10.2017 / 17:18

    4 answers

    6

    You really need to follow a convention, that's a point you already know. The convention may change according to technology, so if you are programming in PHP it may be one, and in JavaScript it may be another, this is normal because every language has its peculiarities.

    Someone may joke that in PHP the convention is not following convention, after all a lot in the library itself and language do not follow. Of course this is bad, just because the language is bad does not mean I should follow this.

    There are even programmers who create helper functions with the "right" convention to use instead of the PHP function that does not follow the convention. Few do, mostly because they do not think about it, do not know how to do it or some who think it's not worth the effort to make and maintain the performance that gives a little more work and requires knowledge beyond PHP.

    It gets worse among the various frameworks available:

    ╔═══════════════════════╦═════════════╦════════════╦══════════════╦════════════╦════════════╗
    ║      PHP Project      ║   Classes   ║  Methods   ║  Properties  ║ Functions  ║ Variables  ║
    ╠═══════════════════════╬═════════════╬════════════╬══════════════╬════════════╬════════════╣
    ║ Akelos Framework      ║ PascalCase  ║ camelCase  ║ camelCase    ║ lower_case ║ lower_case ║
    ║ CakePHP Framework     ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
    ║ CodeIgniter Framework ║ Proper_Case ║ lower_case ║ lower_case   ║ lower_case ║ lower_case ║
    ║ Concrete5 CMS         ║ PascalCase  ║ camelCase  ║ camelCase    ║ lower_case ║ lower_case ║
    ║ Doctrine ORM          ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
    ║ Drupal CMS            ║ PascalCase  ║ camelCase  ║ camelCase    ║ lower_case ║ lower_case ║
    ║ Joomla CMS            ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
    ║ modx CMS              ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ lower_case ║
    ║ Pear Framework        ║ PascalCase  ║ camelCase  ║ camelCase    ║            ║            ║
    ║ Prado Framework       ║ PascalCase  ║ camelCase  ║ Pascal/camel ║            ║ lower_case ║
    ║ SimplePie RSS         ║ PascalCase  ║ lower_case ║ lower_case   ║ lower_case ║ lower_case ║
    ║ Symfony Framework     ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
    ║ WordPress CMS         ║             ║            ║              ║ lower_case ║ lower_case ║
    ║ Zend Framework        ║ PascalCase  ║ camelCase  ║ camelCase    ║ camelCase  ║ camelCase  ║
    ╚═══════════════════════╩═════════════╩════════════╩══════════════╩════════════╩════════════╝
    

    Font .

    Something official .

    The important thing is to choose one and follow. No one can say which is best for you.

    I like NomePessoa() . In PHP not so much. What I see most is the use of nome_pessoa() . But if it is a method, it changes to nomePessoa() .

    In JS it is most common nomePessoa() .

    Convention for JS from perhaps the greatest language guru . And the Mozilla convention . And the Google convention .

    Just note that jQuery is not a programming language then it does not have to have a convention for it, but rather for to JS.

        
    06.10.2017 / 17:31
    2

    Although it is very important to have a defined style when working as a team, this is difficult to implement in old code, and has a very large component of personal taste. That is, they have (and should!) Sit down to discuss, decide and implement.

    As I said there is a large component of "personal taste" so the most important thing is to make a decision. However, a rule established by the JavaScript community is that function only have large letters when they are class constructors, and variables only have large letters when they are constants.

    I suggest taking a look at the Vue.js style statement that is now being discussed by the community (Beta phase): link as a starting point.

    My JavaScript experience tells me that the correct way to do this is

      

    camelCase - function nomePessoa(); - ✓

    Moreover, wikipedia has an extensive list of style guides: link

    But again, it's important to decide together and apply even in old code.

        
    06.10.2017 / 17:29
    2

    There are several conventions that you could adopt. The most commonly used, and that fits perfectly in both PHP and jQuery is the CamelCase call.

    Definition:

      

    CamelCase is the name in English for the practice of writing compound words or phrases, where each word is capitalized and merged without spaces. It is a widely used standard in many programming languages, such as Java , C# , Ruby , PHP and Python , mainly in the definitions of classes and objetos .

    CamelCase Standards:

    The first letter of a word composed by CamelCase may or may not be capitalized, there is no consensus on the right way to use it. There are two ways to classify it: the first is known as UpperCamelCase (lower case, also known as PascalCase) and the second lowerCamelCase .

    There are some rules to follow in this convention. The lowerCamelCase rule implies that compound words must be initialized with lowercase letters. This rule is used to set variáveis and métodos .

    Examples:

    • $nomeCompleto;
    • $valorDesconto;
    • $tipoCliente;

    The UpperCamelCase rule (which can also be called PascalCase ) implies that words must be capitalized. This rule is used in the definition of classes in orientação a objetos .

    Examples:

    class ClienteChave { 
    
    }
    
    class ProdutoPrincipal {
    
    }
    
    class LojaMatriz {
    
    }
    

    Conclusion

    The most important is to follow a standard, a standard or convention. The best choice is up to you, or in some cases it may be from a determination of your company, a convention of the very language in which you are developing.

        
    06.10.2017 / 17:38
    1

    The convention followed by professional PHP programmers is as follows:

    <?php
    
    // Para classes e métodos a convenção é a seguinte:
    
    class CadastroCliente
    {
        function nomePessoa($nome, $sobrenome)
        {
    
        }
    
        function CPF($numero)
        {
    
        }
    }
    
    // Para funções a convenção é a seguinte:
    
    function nome_pessoa($nome, $sobrenome) {
    
    }
    
    ?>
    

    Remembering that you never define class names, methods or functions in Portuguese. Use names and comments in English, even being a class or a function that will only be useful in Brazil. Example: Class or function that validates CNPJ or CPF. See how declarations become more standardized in English.

    <?php
    
    // For classes and methods the convention is as follows:
    
    class ValidateDocument
    {
        function CNPJ($number)
        {
    
        }
    
        function CPF($number)
        {
    
        }
    }
    
    // For functions the convention is as follows:
    
    function persons_name($first_name, $last_name) {
    
    }
    
    ?>
    
    <?php
    
    // O nome da variável que vai receber a instância deve ter o mesmo nome da classe:
    $ValidateDocument = new ValidateDocument();
    $ValidateDocument->CPF(012.345.678-9);
    
    // Constantes devem ser declaradas com todas as letras maiúsculas, separadas por underscore:
    define(SOU_UMA_CONSTANTE, true);
    
    // Variáveis de uso geral devem ser declaradas com todas a letras minúsculas, separadas por underscore:
    $first_name = 'Leandro';
    $last_name  = 'Sciola';
    
    // Alinhe as variáveis pelo sinal de atribuição:
    $nome   = '';
    $genero = '';
    $idade  = 0;
    $cpf    = 0;
    
    ?>
    
        
    10.06.2018 / 18:21