简体   繁体   中英

Should we use constants with builder methods in Java?

Say, I am creating an object using the builder pattern as follows:

CfnResolverProps props = CfnResolverProps.builder()
.apiId(“TestApiId”)
.typeName("Query")
.fieldName(“list”)
.dataSourceName(“SomeName”)
.build()

So, here, is it a good idea to make static constants variables for all the TestApiId, Query, list, SomeName? Or with builder pattern, it does not matter? For me, here, it doesn't matter much because, here it is already readable code as builderPattern tells the name of the fields explicitly. What are the guidelines people use to use constants in Java?

The idea behind using constant is that it's immutable and not going to change.

To address your question, do you think the string literals that are used in the construction of object CfnResolverProps are going to be used else where as well?

If the answer is yes, you should create constants for those literals. If the answer is no, it's okay to keep the code as-is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM