How to write main () in OOP?

When I first started programming, I wrote everything basically. But, as I found out, I tried to make as little as possible in my main() methods.

But where do you decide to give another class / method responsibility for running the program with main() ? How do you do this?

I have seen many ways to do this, for example:

 class Main { public static void main(String[] args) { new Main(); } } 

and some like:

 class Main { public static void main(String[] args) { GetOpt.parse(args); // Decide what to do based on the arguments passed Database.initialize(); MyAwesomeLogicManager.initialize(); // And main waits for all others to end or shutdown signal to kill all threads. } } 

What to do and not to do in main() ? Or are there no silver bullets?

Thanks for the time!

+6
oop procedural
source share
8 answers

In my opinion, the “main” significant project should contain about 3 function calls:

  • Calling the initialization function, which sets all the necessary settings, settings, etc. for the application.
  • Launching the main "controller" application
  • Waiting for the main controller to complete, and then calling the Termination function, which clears everything that needs to be cleared in the "main" (although the controller has already taken care of most of the cleaning).

Any significant application will be divided into pieces of functionality, usually with some hierarchy. A primary controller can have multiple child controllers for specific functions.

The implementation of this method greatly facilitates the search for certain functions, and the separation of problems is better.

Of course, as the other answers said, there really is no silver bullet in software development. For a short project, I could put everything in its main purpose, to quickly and quickly work. I think it also depends on the language - some options may be simpler than others in certain languages.

+7
source share

Code in the main function:

  • Cannot be tested on devices.
  • It is not possible to get addiction by injection.
  • You cannot reuse other applications that are similar to the first one you are writing.

Therefore, the code in the main function:

  • It should be so simple that you are only satisfied with functional / system tests.
  • Should be responsible for setting up the ball rolling for the dependencies used for all your other code (that is, the main one acts like the uber-factory that your application creates).
  • Only those things should be done that relate to how your application is configured (i.e. no test code or demo will need to do exactly the same).

In practice, this means that in real applications there is not much in common. Toy apps and one-time programs can have quite a lot in common because you are still not going to test or reuse them.

Actually, some of what I say above are C ++ specific. Of course, basic Java methods can be invoked by test code or application variations. But they still do not accept objects as parameters, but only command line arguments, so the degree to which they can be isolated under the test, or behave well in terms of reuse, is pretty low. I think you could pass class names to create them and use them to create the rest of the application.

[Edit: someone removed the tags "C ++, Java" from this question. So: what I say above is specific to C ++ and Java. Other languages ​​may relate to the primary languages ​​in a way that is less special, and in this case there can be no special reason for you to relate to it specifically.]

+17
source share

Be that as it may, your boat is sailing. :) You really need to focus on making the code simple, easy to read, and efficient, using whatever tools you need to achieve this. If this requires entering a lot of code basically - do it. If you feel that objects will make things more organized, go this way.

Creating a single instance of class Main , and then calling the instance method Main() , which does all the work is just as good as writing everything in the main method directly.

+4
source share

I would say that this is not what is in your main function, but what is not. Depending on the complexity of your project, you may want to break it down into functional sections, such as “Database Functions,” “Display Functions,” “High Tea with Vicar,” etc.

All about code readability. Can someone else who has never seen your program before stumbling upon it and first get a good generalized idea of ​​what it does?

Then is it easy to see where to go to delve into the mechanism?

Is each function section that runs only a logical process block used? He should not do only one thing, but he should not do everything except the kitchen sink.

Separate your code so that it is supported by an external source.

Because heaven knows when it comes to him, if someone can fix a mistake, everything is better =)

As a direct answer to your question, I would put function calls to each of the main components in the main, setup, process and completion, so that everyone who looks at it will quickly see how the program works. Then they can turn further if they need to.

+2
source share

Look, the content and form of the “core” method depends on the language and environment. In Java, each class can have a public static void main() method, so it’s quite possible to have more than one.

But now, think about it through the law of parnasation parnas: "every module hides a secret, and this secret is something that can change." The “secret” of the module, which was originally called, is the details of the process’s interaction with the operating system: things like getting arguments and handling incorrect endings. In Python, this leads to the following:

 def main(args=None): #argument processing #construct instances of your top level objects #do stuff if __name__ == "__main__": try: main(Sys.Argv) except: # everything # clean up as much as you can else: # normal cleanup, no exceptions 

The bottom line here is that you get everything you can and call the main () function; you will catch all uncaught exceptions, and you will do something smart with them before the program dies.

+2
source share

I think the main method should explain what the program does at startup. Therefore, it can call initialization methods, but the logic should be extracted into the methods.

In your example, I would not have created the Main () method, but placed it in the original.

0
source share

The design of your program will determine the shape of your "core".

Having a "rule" that says that your main function should be - IMHO - does not make sense.

0
source share

Remember that if someone wants to understand how your program works, the first place they are likely to look for it is mostly (at least I would). So, I don’t think it’s a good idea to do as little as possible. But I would say to put as much as you need to look at the birds, how your program works.

So, I think that your biggest problem in implementing the core function should be readable.

0
source share

All Articles