Subscribe to Martyr2's Programming Underground        RSS Feed
***** 1 Votes

The Main Method Is a Conductor, Not a Member of the Orchestra

Icon 4 Comments
As many of you already know, I am a regular mentor on Dream.In.Code. With that I see common mistakes and design issues more than most people will probably ever seen in their full career. One design issue I would like to talk about today is the role of the main method and why it should be treated more like the conductor of an orchestra than 10 different band members. The main method should be directing your application and keeping the different parts working together by making gating its processes.

The Main Method

This method is important in some languages like C/C++, Java and even C# (especially when doing console apps). Unlike other functions it is usually the entry point to a program. It is the place where you begin your journey of designing your application. It may or may not take arguments from the command line and it may or may not return a successful value to the operating system. The one thing main should always do is direct activities in a program rather than trying to do all the activities on its own. It is the start of the application, not the application itself.

What do I mean by that? Well, all to often I see those new to programming try to stick all their program's logic right into this single method. They try to show menus, collect user input, process that input, show other menus, run game loops or whatever all right in this "starting" method. They of course then run into a problem and since they only have this one method they don't know where to start looking for the problem. What those new to programming need to understand is that the main method should contain the heart of the program and call out to various parts of the application to handle the individual tasks.

As an analogy the main method should be the conductor, or the person who directs an orchestra, and should not be the one running around trying to play all the instruments. In other words it should not be trying to show a menu or trying to collect input and process input and handling all the events and drawing stuff etc. It should be the method which calls a function to show a menu and when the menu is presented, it then calls another function to work on collecting the user's menu choice. Once the choice is made, it should then give that choice to a function to handle the choice. We should not be seeing 100 line main methods or anything like that.

When is it too much?

I think the main problem (no pun intended) is that when someone is being taught to create their first application they are asked to write a few lines in the main method. Maybe to print "Hello World" or to collect an integer and then do an addition on it only to display it right back out again. For those types of programs, great! Stick with what is simple. The problem arises when teachers or tutorials don't stop at some point and say "Ok, we need to stop doing everything here in main and refactor all these duties out to other functions. No no, don't show your menu here, put that menu in a function and call the function from main."

I would argue that if you find yourself approaching 30 to 40 lines or more in main, you probably can start pushing out code to other functions. Other programmers might have a smaller or larger number of lines than that, but I find 30 a pretty good rounded number. Anything less than that and it is really not too unwieldy to handle and any more than that usually has a few lines that you can put into another function somewhere.

Excuse me, why is a long main method a problem again?

Well the problem isn't just with the main method, it is the idea of knowing when to refactor code as a design practice. I am just bringing up the main method as the center of this blog topic because I often see beginners (and yes a few pros too sometimes) trying to do way too much in their main method. The real goal I think is to be able to look at your main method, at the end of your program's completion, and get a summary of everything it is doing rather than seeing all the specifics. We should always strive to have functions do one thing and do it well. The main method is no exception to this rule.

Next time you start a program and have a blinking cursor inside main ask yourself this question... "Should main really know anything other than which functions to call and when?" If it knows about a variable to set the timing of a menu notice or how many panels are in a status bar then it might know too much and be doing more than one thing. When it comes to debugging it will also prove to be a nightmare to you, as well as us helping or maintaining your program, to find out exactly where the problem lies. Just keep your main method clean and orderly and we should have no problems!

Thanks for reading! :)

If you liked this article and want to read more then check out our official blog at http://www.coderslexicon.com. There you can get code, tutorials and more. Plus you can find our popular ebook titled "The Programmers Idea Book" which features 200 programming project ideas, how to solve them and developer tips.

4 Comments On This Entry

Page 1 of 1

jeffindenver Icon

28 October 2015 - 07:16 AM
I'm working on an assignment right now with specific instructions from the teacher.
1. Don't use object oriented programming.
2. Put it all in a main method.

It's annoying. We're supposed to be learning OOP but directed not to use it. I also wonder how it will affect students are relying on the class alone to learn programming.
1

Martyr2 Icon

28 October 2015 - 11:34 AM
What programming language are you guys using? Is this like your first few assignments? As mentioned, I can see teachers wanting you to start simple and putting a few statements in the main method to make things easier and that is fine AT FIRST. But really they should make the transition to functions/methods and objects as quickly as possible.
0

jeffindenver Icon

28 October 2015 - 05:48 PM
We're using java, and it's definitely not the first few assignments. We're on week 8 of an 11 week class. I could probably write a page or two griping about community college. I'll keep it much shorter. High Schools have stricter requirements for teaching than a community college. (add grain of salt) I'm only on my second coding-related class, but I don't think my instructors are prepared for the job.

And then a bit of fairness, we are instructed to use functions and methods half the time. And my current instructor is earnest and mostly helpful.
0

JayPy Icon

30 October 2015 - 12:28 PM
I am glad there are some many online resources. I am taking C++ in college now. Like most courses and text it teaches you the tools but, gets shy when it comes to technique. I have a book called Code Complete by Steve McConnell, that I plan to use to supplement my education once the course is done.

As students sometimes we may be expecting too much. A course in C++ is not necessarily, a course is software design or in the craft of writing code. Code Complete from what I hear helps fill in those gaps. Another similar book is the Pragmatic Programmer.

I wonder how many people complete their Intro to X-LANGUAGE book or course and feel like they just received a box full of specialized tools and components with no instruction for assembly or effective use.
0
Page 1 of 1