How to choose a convention for variable and function names?

Asked

Viewed 1,933 times

4

I am going through the second development company and in both have no own convention to use in the declarations, in the codes caught functions declared as follows:

  1. PHP and jQuery
function nome_pessoa();
function nomePessoa();
function nomepessoa();
function Nome_pessoa();

I know both choices work but it gets very "ugly" because there are times in the same file has a function being called like this function nome_aluno() and then another location in the same file nomeResponsavel() let’s say so, and also find declared functions with 3 or 4 _ (underline) function nome_dos_responsáveis(), I don’t even know if this can cause slowness in the system, I don’t want to create a discussion, but I want to get used to something that is more clear, 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.

4 answers

6


In fact, you need to follow a convention, which you already know. The convention can change according to the technology, so if you are programming in PHP can be one, and in Javascript can be another, this is normal because each language has its peculiarities.

One can joke that in PHP the convention is not to follow convention, after all a lot in the library itself and language do not follow. Of course that’s bad, just because the language is bad doesn’t mean you should follow it.

There are even programmers who create auxiliary functions with the "right" convention to use in place of the PHP function that does not follow the convention. Few do, most because they do not think about it, do not know how to do or some who think it is 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:

Phpproject Classes Methods Properties Functions Variables
Akelosframework Pascalcase camelCase camelCase lower_case lower_case
Cakephpframework Pascalcase camelCase camelCase camelCase camelCase
Codeigniterframework Proper_case lower_case lower_case lower_case lower_case
Concrete5cms Pascalcase camelCase camelCase lower_case lower_case
Doctrineorm Pascalcase camelCase camelCase camelCase camelCase
Drupalcms Pascalcase camelCase camelCase lower_case lower_case
Joomlacms Pascalcase camelCase camelCase camelCase camelCase
modxCMS Pascalcase camelCase camelCase camelCase lower_case
Pearframework Pascalcase camelCase camelCase
Pradoframework Pascalcase camelCase Pascal/Camel lower_case
Simplepierss Pascalcase lower_case lower_case lower_case lower_case
Symfonyframework Pascalcase camelCase camelCase camelCase camelCase
Wordpresscms lower_case lower_case
Zendframework Pascalcase camelCase camelCase camelCase camelCase

Source.

Something official.

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

I like NomePessoa(). In PHP not so much. What I see more is the use of nome_pessoa(). But if it’s method, then it switches to nomePessoa().

In JS it is more common nomePessoa().

Convention for JS, perhaps the greatest language guru. And the mozilla convention. And yet the google convention.

Just note that jQuery is not a programming language then there doesn’t have to be convention for it but for the JS.

2

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

As I said there is a big component of "personal taste" so the most important thing is to make a decision. However, one rule instituted by the Javascript community is that function only has large letter when they are class constructors, and variables only have large letter when they are constant.

Otherwise I suggest to take a look at the style statement of Vue.js that is now being discussed by the community (in beta phase): https://vuejs.org/v2/style-guide/ as a starting point.

My experience in Javascript tells me that the correct way of the examples that this is

camelCase - function nomePessoa(); - ✓

The rest of wikipedia has an extensive list of style guides: https://en.wikipedia.org/wiki/Coding_conventions#Coding_conventions_for_languages

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

2

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


Definition:

Camelcase is the English term for the practice of writing compound words or phrases, where each word is capitalized and joined without spaces. It is a widely used standard in several 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 (uppercase initial letter, also known as Pascalcase) and the second lowerCamelCase (lowercase initial letter).

There are some standards to be followed in this convention. The standard lowerCamelCase implies that compound words should be started by lowercase letters. This standard is used to define variáveis and métodos.

Examples:

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

The standard Uppercamelcase (which may also be called PascalCase) implies that words should be started with uppercase letters. This standard is used in the definition of classes in orientação a objetos.

Examples:

class ClienteChave { 

}

class ProdutoPrincipal {

}

class LojaMatriz {

}

Completion

The most important thing is to follow a standard, a standard or convention. The best choice depends on you, or even in certain cases may be from a determination of your company, a convention of the very language in which it is developing.

1

The convention followed by professional programmers in the PHP language 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) {

}

?>

Remember that never define names of classes, methods or functions in Portuguese. Use names and comments in English, even if it is a class or a function that will only be useful in Brazil. Example: Class or function that validates CNPJ or CPF. See how more standardized the statements 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;

?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.