Is there a nomenclature standard for enums?

Asked

Viewed 2,023 times

5

I don’t know much about object naming patterns. I’m creating a enum who lists positions, for example: manager, programmer, attendant...

There is a pattern to name this Enum? EnumCargo, CargoEnum,... ???

  • 1

    I don’t know if it exists, but I prefer the second option. In my projects and where I work we prefer the structure "name"+"type"

3 answers

13


Yes, there are various patterns of nomenclature, in my view are still a choice of style. I particularly program more in c# and there is a great guide to standardize that I follow.

https://msdn.microsoft.com/en-us/library/xzf533w0(v=vs.71). aspx

For Java (which I haven’t used in practice for a long time) I usually followed the same recommendations. In the Oracle documentation I found this link:

http://www.oracle.com/technetwork/java/codeconventions-135099.html

However, this link does not directly address the Enums, I did some research and from what I found the justification is that an Enum is also a class and as such follows the same naming patterns.

http://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html

In stackoverflow itself (English) there are several topics on this, follow two examples below:

https://stackoverflow.com/questions/3069743/coding-conventions-naming-enums

https://stackoverflow.com/questions/15755955/naming-of-enums-in-java-singular-or-plural

In short, according to this reference, Enums are classes and can follow the same standards that you adopt for them.

Enums are classes and should follow the Conventions for classes. Instances of an Enum are constants and should follow the Conventions for constants.

From this link: https://stackoverflow.com/questions/3069743/coding-conventions-naming-enums

  • 1

    now yes a good answer! thanks bro!

  • Really good!

7

The standard of nomenclature of enums follows the same standard of class nomenclature, for the simple fact of enum be a special kind of class.

Therefore, give your name in a way representative of its meaning, ie to represent the "positions", nothing better than a enum by name "Cargo".

For example:

enum Cargo {
    GERENTE(1), PROGRAMADOR(2), ATENDENTE(3) ;

    private int id;

    Cargo(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }
}

class Exemplo {
    public final static int VALOR = 10;
}

public class Teste {

    public static void main(String[] args) {
        System.out.println(Cargo.GERENTE.getId());
        System.out.println(Exemplo.VALOR);
    }
}

Exit:

1
10

In addition to exemplifying a slightly more, shall we say, unusual way of using the enum, that is, with builder and getter, I also try to make it clear that you can choose to create a static constant within a class, to show that its functionality is virtually the same, so note that it is not necessary to explicitly make clear that the enum is a enum, and not an ordinary class.


The values of enums, in accordance with the documentation, enum in Java must be written in uppercase, as they are States.

Because they are constants, the Names of an Enum type’s Fields are in uppercase Letters.

  • I believe he asked for a pattern to write the name of Enum, in this case the documentation speaks of Enum values.

  • that’s right @Dener . The pattern of instances I know, I want to know the naming pattern of the Enum name

  • public Enum Nameanone { INSTANCIA1,INSTANCIA2; } I want to know the default for Namedanum

  • I still don’t agree... if you put the name Charges.. Enum can be mistaken as a normal class by someone who is working on the same project as me

  • enums are almost classes, you can even set getters for them, and classes also have static elements that can be accessed as I have shown here, I will edit my reply to show

  • @Pedrolaini, ENUM can contain builders, getter and settters as Math said are almost classes.

  • I found a reference in a book that states that Num is a special kind of class, I will base my response, but I’ve edited to show what I stated above, @Pedrolaini

  • @Wellingtonavelino setters not, as their values are constant :)

  • Well remembered @Math

Show 4 more comments

1

Using this as a basis reply soen:

They must be in capital letters because they are constant

 public enum Cargo {
    GERENTE, PROGRAMADOR, DBA, ARQUITETO
}

To documentation does not cite a pattern all examples deal only with the type that will be represented by Enum:

public enum Trabalhador {
    CLT, PJ, ESTAGIARIO
}
  • The pattern of instances I know, I want to know the naming pattern of the Enum name

  • note the call inserirTrabalhador(trabalhador.getProfissao(Cargo.GERENTE) and the call inserirTrabalhador(trabalhador.getProfissao(EnumCargo.GERENTE)

  • this comment was not an answer to my question

  • I edited @Pedrolaini

  • Putting the name thus Enum can be mistaken as a normal class, when someone else is messing with the project

  • 1

    @Pedrolaini, an Enum is considered a special class. Access to Enum is different, Position.MANAGER, a normal class position.getGerente(), I don’t see how to confuse the two.

Show 1 more comment

Browser other questions tagged

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