We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I am working on project with AT8052 with keil While doing C coding i have created many functions at certain situation multiple nesting of functions occur which may be avoided with some effort. Since 8051 stack is inside the internal RAM itself and i am not using any external data memory, should i avoid multiple nesting of function as it may occupy more RAM area. Regards Nitin
Drew, I never said always use global variables, I said there is nothing wrong with global variables, that is hardly a generalization. unsigned char MainState // occasionally globally search that no writes exist outside 'main' the comment was not intended as a practice, more as an illustration. If you use a good variable naming convention there is no need to constantly examine. The development benefits of an access function may well outweigh the tiny advantage in speed of direct access as you stated using an 'access macro' which I wholehardedly support you hide the fact that the variable is global. I love anything that limits the ability to make mistakes as long as it does not interfere with the most effective code. Many moons ago I created this project that ran a $250.000 peripheral at full speed using a minicomputer that was mandated as the system controller (why is another story). After release to the field this was turned over to sustaining engineering and after a few days, I started getting calls "you can't do that" "that is illegal" etc. After 6 months they released an upgrade and every customer screamed bloody murder since their $250.000 machine now only ran at 1/3 speed and the word from sustaining engineering was "but the code is now good". Do you really think the customers were impressed? So global variables are OK, but not always the best local variables are OK, but not always the best, 'transfer variables' and a few other cases should be examined. Erik Ps I am still in the 'the faster you run, the better it is' business.
unsigned char MainState // occasionally globally search that no writes exist outside 'main'
Erik, I did not intend that post as criticism, but rather an examination of some of the issues surrounding such choices. as long as it does not interfere with the most effective code "Effective" is measured in multiple dimensions, which is more or less the point of my earlier post. In my opinion, the best engineering tradeoff isn't necessarily the minimum on the code size or speed axis, but that's certainly a major factor that has to be taken into account for each case. And limited environments such as a typical 8051 project place even more of a premium on performance. I belong to the "less is more" school myself, particular since my current project is pushing 64,000 bytes of object code ;) It even has a global variable not all that different from the hypothetical mainState (though I might have talked myself into wrapping it with an access function with my own post!)
Drew, I think we agree that coding is not pantyhose (one size does not fit all) and while some may lean one way and some may lean another way the truth is that in all cases "you shall not" should be replaced by "you shall only if good". My reaction on global variables is strongly flovored by exposure to some programmers that would rather screw up a program structure than use a global variable. I have also seen programmers coming from assembly using global variables where a local would be the better choice. So let's agree that the right choice is not what some "guru" stated a a "rule" but what is best for functionality and mainatainability (the ability to maintain it - not killing the code to protect it aginst you). Where functionality should take priority over mainatainability will always be a judgement call and I know that you can prioritize functionality without losing more than a few percent of maintainability if you keep thinking maintainability while coding functionality. Erik