Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

number in function name [closed]

I have lately been trying to conform to the Python style guide ... Which I believe is a change for the better! There is however one element that it doesn't cover in enough detail for me; the naming convention for functions.

I am writing some code where I have defined a function with the word "to" in it, for example:

def spamtoeggs(arg):
    ...

I find this hard to read, and would like to know the correct way to write this. I would tend to use the following:

def spam2eggs(arg):
    ...

Over:

def spam_to_eggs(arg):
    ...

Note that I interpreted the style guides use of "words" in functions to mean both letters and numbers, and this may have been my downfall...

I have been unable to find an answer to my question online (this SO post comes close), and it if does exist I apologise for duplicating it.

So in short, I am asking are numbers interchangeable for letters within function names?

like image 289
Mike Avatar asked Aug 29 '14 12:08

Mike


2 Answers

Naming convention exists for a reason. What is more readable for YOU is not necessarily more readable for other developers who might have to work on this code in the future.

So, as an answer, feel free to chuck the standards in the bin if you are the only person who will ever work and see this code. The code is yours, you do whatever you want with it. It can be your first piece of code. You might not even be a professional developer, and so on...

However, if you want to be part of the greater community of developers out there, I can't stress enough how important it is to stick for the standards and best practices. You will become a better developer. You will find other people's codes easier to read, and people will find your code easy to read. They might even LIKE your code (doesn't happen that often though...). Working with legacy code is a challenge, working with badly written legacy code is... beyond hell.

In the end of the day it is your decision. You can choose being a better developer and follow the standards or you can choose writing un-mantainable code and keeping your job for life ;) https://www.thc.org/root/phun/unmaintain.html

like image 189
o--oOoOoO--o Avatar answered Oct 02 '22 07:10

o--oOoOoO--o


From the style guide, as you said yourself:

Function Names
Function names should be lowercase, with words separated by underscores as necessary to improve readability.

Including numbers in the identifier is not intrinsically an issue unless they are the start of the identifier. But if you are adhering to the style guide, it is really quite clear. If you are less worried about sticking to the style guide, feel free to use the numbers instead, but... that is not sticking to the style guide, which seems to be your criteria.

like image 45
Phillammon Avatar answered Oct 02 '22 09:10

Phillammon