I see code snippets using the descriptor “main()” quite often in this forum.
To be honest when I see “main()” it means for me -> a beginner was learning from a beginner.
Unfortunately this beginners code distributes even further.
Why I do not like it:
- The function “main()” is common technique in a different context (“the main function is where a program starts its execution”). Using main() in a different context is distracting and should be avoided.
- It describes no operation (except the meaning from first point). Nobody can tell what a ‘main’ operation is supposed to do. “main” by its definition is a quality, rather than an operation. If there are other operations - what is the difference to the remaining code?
- There is no situation when the BGE ever expect the descriptor “main”. It is pure custom code. If it is custom code it is better to add a value by choosing a descriptive (and matching) name. This will show that you know what you are doing. Examples: “move”, “jump”, “putDistanceIntoProperty”
- There is no convention to Python to create the descriptor “main()”. The descriptor in Python is called main and has a complete different purpose which does not belong to the BGE.
- One of the purposes of a function is to encapsulate implementation details. A descriptive name tells the reader what the encapsulated code is supposed to do, without forcing him to looking at its implementation. A non-descriptive name prevents the reader from understanding the higher abstraction of the code and makes it hard to read.
There are rare situations, when you might use it.
- You might use it to demonstrating code. It can be used as a placeholder of yet-undefined implementation which is not part of the code to demonstrate. I prefer to directly name that code so it is easier to be identified as place holder e.g. “doSomething()”, “processYourCode()”. Even a beginner would like to rename it before using which leads to understandable code.
Background
I know main(args) from C and Java to describe the entry point of the compiled application. This is because the developers of C can’t know what your application is supposed to do. So it is a convention of the language or better the API ;). I checked Python. It does not have this convention -> no reason to use it at all.
Hint: If you can’t think of a good name for your functions better resist to use such generic names like “main” or “process”. If you can’t find a descriptive name, it usually means the code is doing too much. To solve this conflict you can extract the different operations into separate functions.