Home > Software engineering >  Is it better to store values from instance fields as variables prior to using them in a method call,
Is it better to store values from instance fields as variables prior to using them in a method call,

Time:11-19

I was wondering if it is better practice to do the following:

String currentUserName = user.getName();
Integer currentUserAge = user.getAge();

User foundUser = userRepository.findByAgeAndUsername(currentUserName, currentUserAge);

OR

User foundUser = userRepository.findByAgeAndUsername(user.getName(), user.getAge());

Is one of these more performant than the other? I want to do the first one as to me it enhances readability and comprehensibility of a codebase, but it does seem quite verbose and takes up a lot of literal space (not memory space though maybe that to?) on the IDE. I am looking for any kind of advantages or disadvantages to these approaches, and not just in this specific example.

Thanks

CodePudding user response:

Always make your code as readable and easy to understand as possible. You should assume that someone else will need to debug, modify, extend, or use your code at some point. To some degree what makes code more or less readable depends on personal preference but I agree with you in this case that the first option is simpler.

The fact that it takes up more space in the IDE is not a big deal. Some projects have coding guidelines such as "A method should not exceed X lines". The idea is to be able to read the entire method without scrolling too much. To meet such guidelines sometimes programmers cram as much as possible into one line which kind of defeats the readability goal of the guideline, so don't do that. If a method gets to be too long it's a good indication that it's doing too much so look for a way to split it into its sensible parts.

About performance, I always remind people to solve the problems they have! If you have a performance problem then, fine, look for ways to solve it. If you don't have a performance problem then don't waste your time trying to fix it! You have enough problems to solve, right? Besides, compilers and interpreters are REALLY, REALLY good at finding ways to enhance performance. Let those people worry about this stuff.

CodePudding user response:

It really no difference performance-wise. And in the example you give it really doesn't make a difference.

-- But --

Two important aspects here:

  1. Try to store information in 1 place. Information can get out of sync when you store it in two places. One place can say one thing and the other something else. In this case almost certainly won't matter, but as a programming habit, it's better not to store in a temporary variable if you can just retrieve from the source (though sometimes there are reasons to store)

  2. Code clarity: By creating variables userName and userAge variables, it makes it a little clearer (a bit more verbose) what you are doing. (You can use comments also to do the same thing)

  • Related