Eliminate any library global variable #5BRL-CAD
Status: ClosedTime to complete: 100 hrs Mentors: Hardeep Singh Rai, Ch3ckTags: C, global, cleanup, code refactoring, reduction

BRL-CAD is a very large mature code base and as with such body of code, issues creep in over time that are undesirable. One such issue is the introduction of global variables. They are often a short-term crutch that allows some feature to be implemented quickly, but they become a long-term burden.

All you need to do is eliminate any one of them that is declared in a header and accessed in more than one file. That means static globals don't count and variables declared global in one file but not used anywhere else also don't count. That said, we try to make it clear where our shared globals are at by putting their definitions in one place (see references below).

You also must preserve the behavior and capabilities of code that was using the global and remove the variable cleanly. That is to say, for example, that if there was a global variable storing a date string that is printed, you can't just eliminate the variable and remove the line that printed the variable -- you'd probably replace the code printing the date with a function or pass the date in from calling code.

This task is to eliminate just ONE global variable in any of our libraries. If you're quick and clever, you'll notice that some globals are VERY simple to eliminate while others can be complicated with thousands of lines of code needing to get updated.

Submit a patch file

References:
  • src/libbu/globals.c
  • src/libbn/globals.c
  • src/librt/globals.c
  • http://brlcad.org/wiki/Patches
Modify:
  • What you'll have to modify completely depends on how you're going to remove the global.
Uploaded Work
File name/URLFile sizeDate submitted
https://drive.google.com/file/d/0B7iOQJC9t...n/aDecember 05 2014 20:18 UTC
bu_n_malloc.patch2.2 KBJanuary 07 2015 12:16 UTC
get_binu_types.patch4.2 KBJanuary 08 2015 00:26 UTC
bu_n_free.patch2.3 KBJanuary 09 2015 21:11 UTC
n_free_calls.patch2.6 KBJanuary 10 2015 04:31 UTC
Comments
Marc Tannouson December 5 2014 19:03 UTCTask Claimed

I would like to work on this task.

Deepak on December 5 2014 19:50 UTCTask Assigned

This task has been assigned to Marc Tannous. You have 100 hours to complete this task, good luck!

Marc Tannouson December 5 2014 19:50 UTCTask finished

Ignore : 


CMaketest.txt and rand.c tests as these were used in a previous task, and when I ran SVN DIFF to create a patch, it also added these to it. 


My actual changes :


Everything else. In /src/librt/globals.c I found "nmg_eue_dist", quickly ran GREP -NR 'nmg_eue_dist' in the src folder. Found all the places it was used, replaced it with its value so that there would be no errors with the files, removed its global declaration from globals.c and then ran SVN DIFF.


Please evaluate the task, I uploaded the patch here ( because my claim has not been accepted yet ) :


https://drive.google.com/file/d/0B7iOQJC9tdXRN2M2bV9SSVQzdEU/view?usp=sharing

Regards,
Marc

Marc Tannouson December 5 2014 20:18 UTCReady for review

The work on this task is ready to be reviewed.

Sean on December 5 2014 21:10 UTCTask Needs More Work

One of the mentors has sent this task back for more work. Talk to the mentor(s) assigned to this task to satisfy the requirements needed to complete this task, submit your work again and mark the task as complete once you re-submit your work.

Sean on December 5 2014 21:31 UTCbehavior changed

So you certainly eliminated the global, but not without eliminating the feature that variable provided.  That said, I think you did modify the entirety of code that interacted with that variable, so you can see that there's not much involved.


If you look at the files you edited, notice that some are in lib* dirs and some are not.  The ones that are in the (backend) lib* dirs are where the variable is put to use.  The others are various applications (several converters).


Those apps are changing the default value from 0.5 to 2.0 and one even provides a command-line switch that lets a user set the value to anything.  So you can't just eliminate their 2.0 setting as that changes the behavior of the library.


So you either have to discern what that value means to justify changing/eliminating the behavior or you have to figure out how to preserve the behavior.  To do the prior, identify where it's used and what those functions are for -- see if the value is important.  To do the latter, you'll have to figure out how to pass data from them up through to where they are used (nmg/nmg_plot.c).


Two remaining points:


1) src/mged/set.c was also setting the value, so you have to also look at that function and figure out if it was important.


2) you should just upload your patch file as an attachment to melange.  It included your other changes because that's what you requested (svn diff).  If you want just a subset, check what you modified (svn status) and svn diff just what you want (e.g., svn diff src/mged src/librt src/conv my_patch.diff).


 

Marc Tannouson December 5 2014 21:35 UTC

Is there any way to revert the repo to the current version on sourceforge? By that I mean, ignore my changes and bring back the unaltered repo without having to re-build.

Thanks,

Marc Tannouson December 6 2014 07:07 UTCClaim Removed

The claim on this task has been removed, someone else can claim it now.

Krishnaon January 5 2015 01:16 UTCTask Claimed

I would like to work on this task.

Gauravjeet Singh on January 5 2015 02:11 UTCTask Assigned

This task has been assigned to Krishna. You have 100 hours to complete this task, good luck!

Krishnaon January 7 2015 12:16 UTCReady for review

The work on this task is ready to be reviewed.

Daniel_R on January 7 2015 12:39 UTC

Hmm, you replace one global variable (bu_n_malloc) by another one (bu_n_malloc_calls).  bu_n_malloc_calls is still a global variable, only visible in bu_n_malloc() but nevertheless a hidden parameter which causes side effects.


It would be good if this could be handled via a normal variable in src/rt/main.c

Daniel_R on January 7 2015 12:39 UTCTask Needs More Work

One of the mentors has sent this task back for more work. Talk to the mentor(s) assigned to this task to satisfy the requirements needed to complete this task, submit your work again and mark the task as complete once you re-submit your work.

Krishnaon January 8 2015 00:27 UTCReady for review

The work on this task is ready to be reviewed.

Daniel_R on January 8 2015 09:57 UTC

First: binu_types isn't a variable but a constant, which normally isn't a problem.  We have many constants in BRL-CAD.  Often as "define" but "const" should be preferred, people say.  Furthermore you moved the constant inside a function which creates an overhead in accessing the values.  There should be a good reason for doing this.


This task aims for eliminating global variables.  This are changeable values which can be accessed from everywhere in the code.  The opposite of them are the local variables in functions and function parameters.


The problem with the global variables is that they can influence the behavior of the program in an unexpected way.  Usually a function should use the parameters it was called with only.  If there are additional values as global variables which influence the function's result which can be changed anywhere in the program and also from other threads, this can lead to unexpected, hard to explore, effects.


And here it doesn't make a difference if these variables can be accessed directly or via a wrapping function.


To get rid of these global variables one strategy is to change them to local ones and integrate them into function parameters.  This could mean to create an additional parameter with this value or to add them to an already existing struct.  You may want to aim for global variables which are used in a small set of functions only which are used only a few times.  This gives you few places to touch, where you have to change a function parameter list or a struct.

Daniel_R on January 8 2015 09:57 UTCTask Needs More Work

One of the mentors has sent this task back for more work. Talk to the mentor(s) assigned to this task to satisfy the requirements needed to complete this task, submit your work again and mark the task as complete once you re-submit your work.

Daniel_R on January 8 2015 09:57 UTCDeadline extended

The deadline of the task has been extended with 2 days and 0 hours.

Krishnaon January 9 2015 21:11 UTCReady for review

The work on this task is ready to be reviewed.

Sean on January 10 2015 02:59 UTCTask Needs More Work

One of the mentors has sent this task back for more work. Talk to the mentor(s) assigned to this task to satisfy the requirements needed to complete this task, submit your work again and mark the task as complete once you re-submit your work.

Sean on January 10 2015 03:02 UTCalmost

Krishna, this looks good to me. The only problem I saw is that you named the function argument "bu_n_free" and another variable "bu_n_free_calls".  Using library prefixes on variables not in that library is inappropriate.  Can just name them without the prefix or something else altogether.

Krishnaon January 10 2015 04:31 UTCReady for review

The work on this task is ready to be reviewed.

Melange on January 11 2015 06:11 UTCNo more Work can be submitted

Melange has detected that the deadline has passed and no more work can be submitted. The submitted work should be reviewed.

Sean on January 11 2015 08:37 UTCTask Closed

Congratulations, this task has been completed successfully.

Sean on January 11 2015 08:39 UTCexcellent

Outstanding work Krishna.  This change will be applied to our sources momentarily, and you'll be credited with your coding contribution.  If you would like to receive full attribution for your change in our AUTHORS documentation, please e-mail your full name to contest at brlcad dot org or state it here if you like.


Please eliminate more globals! This is good work.  :)


 

Sean on January 11 2015 08:42 UTCapplied

Your patch was applied in r63919 and you have been credited in our authorship documentation.


 

Krishnaon January 11 2015 21:17 UTCThank You

Thanks alot! My full name is: Krishna Ravishankar