#Spring Boot naming conventions

17 messages · Page 1 of 1 (latest)

loud wharf
#

Hey,

Im just getting back into Spring Boot and i have a question about naming conventions in spring boot, specifically in the service layer. How do we name our methods? For example if im making a method that is supposed to get an user by its id, would i name the method getUserById or findUserById? On the repo the prefix is typically find for the different methods, but how do we do it on the service layer? The way i was taught is to use get as prefix for pretty much everything but now im not sure of whats right and wrong. I know its a silly question but i want to get it right from the beginning.

dusky glenBOT
#

<@&1004656351647117403> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
Hey, Im just getting back into Spring Boot and i have a question about naming conventions in spring boot, specifically in the service layer. How do we name our methods? For example if im making a method that is supposed to get an user by its id, would i n

In Spring Boot, especially in the service layer, naming conventions for methods should reflect clear, intention-revealing actions. Typically, method names describe what they do, often using simple verbs or verb phrases related to the business domain.

Common patterns include:

  • CRUD operations:

    • createEntity(), getEntityById(), updateEntity(), deleteEntity()
    • e.g., getUserById(), saveOrder()
  • Query methods:

    • Use findBy, fetchBy, or loadBy followed by the criteria, e.g., findByEmail(), findAllActiveUsers()
  • Action-based methods: Use verbs that make sense in the domain, e.g., processPayment(), activateAccount()

Aim for clarity and consistency, so other developers instantly understand the method’s purpose.

The service layer primarily focuses on business logic, so method names should reflect business actions instead of technical persistence details (which are often handled in repository layer).

Useful links:

slow solar
#

find is a DB query. get usually not

#

or in other words: find is expensive

#

get is cheap, typically

#

thats the user expectation

#

ultimately though its up to u how u name ur stuff

#

🤷‍♂️

loud wharf
#

Thanks a lot!

dusky glenBOT
#

@loud wharf

Your question has been closed due to inactivity.

If it was not resolved yet, click the button below to keep it
open, or feel free to create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍

river light
#

i do not see any reason but get or find has any difference, as it will always search the full member list, and repository will auto create those facilities but you need to declare the logic pattern of your code

median shadow
median shadow
#

sorry, after messaging I found that I replied it to you

cunning ginkgo
#

Most traditional and used naming convention is "get"

vague hazel
#

You do need to be careful using get with network calls because javabeans have set the convention that a getter method is one that just reads a field.

You don't want someone to put a network call in a loop with a large number of iterations.

If you feel there is enough context that the reader will know your get method is a network call then that is probably fine but you will still find coders that insist get should only be used as a javabean.