![]() ![]() SET MEMBER VARIABLE GRANDTOTAL EQUAL TO THE CALL TO METHODS CODEPassing member variables to a public function of the same object when calling the function externally would however constitute a major code smell in my opinion. But I believe you gain much in terms of code clarity by having the methods that handle complex logic pure and using immutable variables, while keeping the impure "global" state handling separated within dedicated methods. Sure it's neither easy nor practical to have all your methods as pure methods in an OOP language. Pure functions don't depend on an external state and have no side effects, always returning the same results given the same set of input parameters - thus making testing and future maintenance easier. By replacing member variable references with method parameters, we can effectively make a function pure. Member variables are effectively a global state from the point of view, what's more, a mutable global state if said member changes during the method's execution. ![]() I see one strong reason for passing member variables as function arguments to private methods - function purity. So to remember "int m_value" and then "int localValue" and remember that one really means the other is always more expensive for your brain then simply working with "m_value".įor more ammunition and ideas, I would recommend picking up a copy of Uncle Bob's Clean Code. If there's another variable name, the brain has to keep track of yet another thing when reading the code. A function that has 3 parameters is harder for the brain to process than a function that has none or 1 parameter. So for your specific example here, the argument I would use is that less code is always easier to read than more code. After all, even when working on green field projects, we spend more than 90% of time reading and as soon as the code is written (let's say 10-15 minutes later) it goes into maintenance where readability is even more important. Then identify quality measures or attributes of clean code that as a team you would want to strive for. Either more formally, or just in hallways, discuss what makes some code segments easier to read and other code segments more difficult. I would recommend to take a step back and instead of focusing exactly on member passing as parameters, initiate discussions with your team on clarity and readability. By technical reason, I mean an example that Kate provided. use completely different names) and there was absolutely no technical reason for doing that. We'd have a class that works with a set of members but some functions access members directly and other functions would modify the same members through parameters (i.e. Personally, I had the same experiences with my team where passing members as arguments was unnecessary and convoluted the code. I think this is a valid topic, but the reason you are getting mixed responses is because of the way the question was formed. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |