Varaquilex
Varaquilex

Reputation: 3463

Is it better to use comment blocks or functions for variable initiations?

Since the more a function branches, the slower it executes, is it better to use comment blocks like below for one time code execution (variable initiations for example), or is it better to use functions for this purpose? Which one is better practice? Does one choice really differ from other? if not so, why not?

comment blocks:

/*************************************************************************************************/
/***********************************  VARIABLE INITIATIONS   *************************************/
 /************************************************************************************************/
Hospital **hospitals = new Hospital*[15];
for(int i = 0 ; i < 5 ; i++)
    hospitals[i] = new Hospital(3, i, 15);  //3: quota, i: hospital number, 10:prefereneceLength
Resident **residents = new Resident*[15];
for(int i = 0 ; i < 15 ; i++)
    residents[i] = new Resident(i,5);  //i: hospital number, 10:prefereneceLength
.
.
.

or a function for initiations:

int main(void) {
    init_vars();
    read_from_files();
    .
    .
    .

Upvotes: 0

Views: 28

Answers (1)

Brian Agnew
Brian Agnew

Reputation: 272307

I would always favour readability over a premature optimisation.

If you later come to find that your initialisations are slowed down due to a function call (and I can't believe that any scenario other than the most demanding would impact you), then optimise.

As always, measure your performance and identify the hotspots. It's easy to identify a possible issue in advance and code for that, and later find that's not the issue at all. In the meantime you've sacrificed design and readability.

As Knuth says:

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.

Upvotes: 2

Related Questions