1

As an example, in Python I have a lowest_prime_factor function which does it exactly what it says it does. Then, in another function, I need to call this function and store it. In another language where variables and functions can share names then I would simply do lowest_prime_factor = lowest_prime_factor(n). In Python though, this actually redefines lowest_prime_factor such that it's no longer a function at all (which is problematic as I need to call it again several lines later).

So I need to name the variable something else - but what else is there that intuitively says "this variable holds the lowest prime factor"? This is where different casing could come in handy, i.e. starting functions with uppercase letters, but it seems Python more than any other language has a very "fixed"/well defined naming convention that means I probably shouldn't do this.

  • Using the get prefix for functions is common in other languages but it seems your variable is local to the other function so it would be ok to name that lpf. You just use the result (lpf) in some calculation to produce some other result right? And you want to buffer the lowest prime factor only to prevent it from being calculated more than once, right? – Martin Maat Aug 21 '16 at 6:29
  • If the variable is of limited scope (e.g. used only within one function), then you should use a short name for that object. e.g. factor = get_lowest_prime_factor(n). If it is of larger scope, use a sufficiently descriptive name that reminds you of how you're going to be using that object, e.g. encryption_factor = get_lowest_prime_factor(n). – Brandin Aug 21 '16 at 7:25
  • "lowest_prime_factor = ...". Lowest prime factor is dependent in a value. Having a variable called this is meaningless. At the very least, you variable should be called lowest_prime_factor_of_n, but it would likely be better to give n a proper name too. Though without code, it's difficult to guess what n is. – David Arno Aug 21 '16 at 21:07
5

One very commonly used coding style (and one which is largely language independent) is that variables are nouns and functions are verbs, as it's the functions which actually do stuff. As such, this makes it very unlikely that variable and function names clash.

Why is this a good idea? Function names which are verbs are much more self-documenting: it isn't instantly obvious what lowest_prime_factor(n) does - it probably calculates the lowest prime factor of n, but maybe it doesn't, or maybe it prints out the factor as well. If it's called get_lowest_prime_factor, everything is a lot clearer.

1

Well, here are some ideas.

  • Rename the function to get_lowest_prime_factor, find_lowest_prime_factor, or calculate_lowest_prime_factor.
  • Arbitrarily rename one or the other to smallest_prime_factor. (On the other hand, using the words "lowest" and "smallest" for the same thing is inconsistent, and consistency is good.)
  • Rename the variable to something longer, like the_lowest_prime_factor, or perhaps lowest_prime_factor_n.
  • Rename the variable to something shorter, like factor or lpf.
  • Determine why this variable needs to contain the lowest prime factor, and name it after that reason. Perhaps you're using this variable in order to divide the input into equal pieces; in that case, call it piece_size.
  • If you're only using the variable in one place, just get rid of the variable and use the function call lowest_prime_factor(n) directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.