1

Topic: Local variables - angrily? Whether rights Fowler?

Faced here interesting judgement. Sent a piece of changes on  to other command and received from them such : Local variables are not good to have in code. You can check what Martin Fowler Says on this https://refactoring.com/catalog/replace … Query.html http://blog.boyet.com/blog/blog/local-v … rritating/ Change which I there made, simple enough and directly to  does not concern. Simply something that I corrected, while understood with their code. Was: AzureVnet azureVnet = azureVnetCacheFactory.getVnetCache (azureGatewayDeployParams.getTargetVnetId ()); Became: String targetVnetId = azureGatewayDeployParams.getTargetVnetId (); AzureVnet azureVnet = azureVnetCacheFactory.getVnetCache (targetVnetId); And here their comment very much puzzled me. Especially that not people refer to the preferences, and on Martin Fowler's recommendations. At them there in that part of the code it is a lot of  noodles and I thought that people simply do not have experience to write more clear code and me it is necessary to work with them, to show examples and all goes normally. But now at me the impression is added that they consider the code to normal and appropriate best recommendations. I, truth, yet did not ask them about methods in 250 code lines and a heap save-paste, but already I suspect that they are convinced of faultlessness of the approach. I more than once faced that that command accepts a defensive position on any sentences on improving of their code. And what you think of Fowler's that recommendation? To what it generally? How in your opinion, what is more readable - local variable or a sixfold call of the necessary method in those places where this value is necessary? You code style guidelines somehow regulate it? I consider that local variables are in most cases more readable. Without fanaticism, it is finite - if somewhere it was thrust "" it not terribly if "" it is necessary - to enter already better two or more times a variable. Results of all other methods, not "" - almost unambiguously go to local variable. In this plan I like the recommendation which was applied in those commands in Microsoft in which I worked: "one sink - one action". I somehow will consider this recommendation in a separate subject, and here now it would be desirable to hear judgement of colleagues on local variables. That I understood at Fowler - he unambiguously names harm local  (changeable) variables and here I with it agree, though and for other reasons. But also invariable local variables he also names undesirable. The cores of argument at Fowler two - in his opinion, it complicates refactoring with selection of a new method (extract method) and local variables worsen . But I in an emphasis do not see any problems with refactoring (at invariable local variables) and in the plan  I hold opposite judgement - on me, a heap  and set of nested calls are much less readable, than concrete local variables.

2

Re: Local variables - angrily? Whether rights Fowler?

Hello, Artem Korneev, you wrote: AK> Faced here interesting judgement. Sent a piece of changes on  to other command and received from them such : AK> AK> Local variables are not good to have in code. AK> You can check what Martin Fowler Says on this AK> https://refactoring.com/catalog/replace … Query.html AK> http://blog.boyet.com/blog/blog/local-v … rritating/ Yes they simply rare  also took offense AK> And what you think of Fowler's that recommendation? Fowler  not the programmer, and the writer. Accordingly also it is necessary to concern its books.

3

Re: Local variables - angrily? Whether rights Fowler?

AK> Local variables are not good to have in code. AK> You can check what Martin Fowler Says on this my estimate opinion - all of them addicts, and Fowler especially. The opinion is made exceptional as result of the made recommendation. If I with them worked, I in the diplomatic form it so and told (at all I do not recommend so to do, there can be consequences).

4

Re: Local variables - angrily? Whether rights Fowler?

Hello, Artem Korneev, you wrote: AK> Was: AK> AzureVnet azureVnet = azureVnetCacheFactory.getVnetCache (azureGatewayDeployParams.getTargetVnetId ()); AK> Became: AK> String targetVnetId = azureGatewayDeployParams.getTargetVnetId (); AK> AzureVnet azureVnet = azureVnetCacheFactory.getVnetCache (targetVnetId); in my opinion, if to follow a specific example you made normally. Another matter that their argumentation though both became outdated, and is, frankly speaking, based on is imperfect the fitted examples, but has the right to arguing and repeated reperusal. I to that in modern java introduced functional  allow  algorithms in simple cases in call chains (Function:: compose/andThen), inducing to select with separate simple methods microsteps, and in difficult with the big information stream on an input - to make out their considerable part in the form of classes-collectors where the state will be at object, instead of it is local in methods.

5

Re: Local variables - angrily? Whether rights Fowler?

In this case in my opinion they are marginally right. Your refactoring refined nothing the code, did not add any information, simply swap of tokens. On - both variants are identical in any plan, but time they so wrote, sense to change are not present. Generally the local variable can simplify very difficult expression (not this case) or to import the additional documentation the name, therefore to say that it angrily - it is incorrect. By the way I am not assured that at Fowler so is written (that local variables angrily). To re-read laziness, but in my opinion at it somewhere in the beginning it is written that all refactorings should be applied with mind, instead of blindly, it is simple tools, both variants (before and after) can take place in different cases.

6

Re: Local variables - angrily? Whether rights Fowler?

Hello, vsb, you wrote: vsb> In this case in my opinion they are marginally right. Your refactoring refined nothing the code, did not add any information, simply swap of tokens. Well. At me the question was not on specific change, and on local variables as a whole. Therefore I resulted only pair of lines to show sense. In that refactoring the method getTargetVnetId () is caused four times. Not that that I am direct worried about productivity - I do not think that it a little noticeably influences it. But all the same on me such code is read hardly easier, than such: AzureVnet azureVnet = azureVnetCacheFactory.getVnetCache (azureGatewayDeployParams.getTargetVnetId(); if (azureGatewayService.get (azureGatewayDeployParams.getTargetVnetId ())! = null) {azureGatewayService.remove (azureGatewayDeployParams.getTargetVnetId ());} Here, in the a bit simplified type, first three usages of this method. At me hands were combed to remove this heap  that I and made. But another is more interesting. Here one more piece from this a method which I am merciless : if (someAzureServiceProxy.getVms (azureVnet.getVnetId() == null || someAzureServiceProxy.getVms (azureVnet.getVnetId ()).isEmpty ()) {....} else if (someAzureServiceProxy.getVms (azureVnet.getVnetId().size () == 2) {....} else if (someAzureServiceProxy.getVms (azureVnet.getVnetId ()).size () == 1) {} Following the burst to avoid local variables, 4 calls of the same method requesting the data at Azure REST API here become. It, at first, slowly, secondly, we with the test surroundings already exceed Microsoft limits on 15000 calls Azure API at an o'clock. But it was not noticeable, since the method is called getVms () and makes false impression of a lung "". The preference in favor of local variables very naturally helps to lower an amount such here problems - instead of here that chain of three calls, there there would be a variable virtualMachinesCount or something like. vsb> By the way I am not assured that at Fowler so is written (that local variables angrily). Well here at it not so it is written, but on a context it is similar. It is indirectly said that they introduce problems. Here the citation from Fowler to which refer in that article: The problem with temps is that they are temporary and local. Because they can be seen only in the context of the method in which they are used, temps tend to encourage longer methods, because that's the only way you can reach the temp. By replacing the temp with a query method, any method in the class can get at the information. That helps a lot in coming up with cleaner code for the class. I and not in all agreed earlier with Fowler. I look now at a year of issuing of the book - 1999. At that point in time Fowler was 36 years old, after the college termination transited 13 years. I now generally am abandoned to the idea that this recommendation is no more, than its prejudice. To me now as much years, how many at that point in time was to Fowler and at me the comparable experience of operation on a speciality - 15 years against it 13. But my experience and my thoughts on this subject are opposite.

7

Re: Local variables - angrily? Whether rights Fowler?

Hello, Artem Korneev, you wrote: AK> Following the burst to avoid local variables, 4 calls of the same method requesting the data at Azure REST API here become. It, at first, slowly, secondly, we with the test surroundings already exceed Microsoft limits on 15000 calls Azure API at an o'clock. But it was not noticeable, since the method is called getVms () and makes false impression of a lung "". , already it is quite enough of it to send Fowler's fans to a garden, to reflect what dogmas do not replace a brain, is not present?

8

Re: Local variables - angrily? Whether rights Fowler?

Hello, Artem Korneev, you wrote: vsb>> In this case in my opinion they are marginally right. Your refactoring refined nothing the code, did not add any information, simply swap of tokens. AK> well. At me the question was not on specific change, and on local variables as a whole. Therefore I resulted only pair of lines to show sense. AK> in that refactoring the method getTargetVnetId () is caused four times. val userId = user.id val userName = user.name if (userId == null) {save (userName)} else {update (userId, userName, user.description)} or if (user.id == null) {save (user.name)} else {update (user.id, user.name, user.description)} on - the second variant where it is more pleasant to read, than the first. If in  were  and names are hardly shorter, in your code it would be obvious, . AK> Following the burst to avoid local variables, 4 calls of the same method requesting the data at Azure REST API here become. It, at first, slowly, secondly, we with the test surroundings already exceed Microsoft limits on 15000 calls Azure API at an o'clock. But it was not noticeable, since the method is called getVms () and makes false impression of a lung "". Well it already a problem of a title of a method. I agree with developers Kotlin who result a following manual in favor of a choice property before function:> In some cases functions with no arguments might be interchangeable with read-only properties. Although the semantics are similar, there are some stylistic conventions on when to prefer one to another.> Prefer a property over a function when the underlying algorithm: > does not throw> is cheap to calculate (or cahed on the first run)> returns the same result over invocations if the object state has not changed In case of Java between  and function it is impossible to express a difference syntactically, therefore I consider that  should submit to aforementioned restrictions, differently it is necessary to name it in another way. If there was retrieveVms (), the problem would be more obvious. AK> the preference in favor of local variables very naturally helps to lower an amount such here problems - instead of here that chain of three calls, there there would be a variable virtualMachinesCount or something like. Well it yes, but is the elementary case. A problem not that a variable not  on a default, and that from a method title not obviously that it such idle time as it seems. If this call in two different methods which are caused from one, any more does not work your rule, at the same time the stare method has chances to see duplication of a remote call and to pull out it above on a stack. AK> well here at it not so it is written, but on a context it is similar. It is indirectly said that they introduce problems. Here the citation from Fowler to which refer in that article: AK> AK> The problem with temps is that they are temporary and local. Because they can be seen only in the context of the method in which they are used, temps tend to encourage longer methods, because that's the only way you can reach the temp. By replacing the temp with a query method, any method in the class can get at the information. That helps a lot in coming up with cleaner code for the class. Fowler describes temporary variable problems. He does not say that they bad a priori. There is no golden mean, both variants have pluses and minuses. Both sides which fanatically carry out any repetitions in a variable are wrong or is fanatical  all variables, .

9

Re: Local variables - angrily? Whether rights Fowler?

Hello, vsb, you wrote: if (user.id == null) {save (user.name)} else {update (user.id, user.name, user.description)} vsb> on - the second variant where it is more pleasant to read, than the first. It agree! And in  I prefer such approach. Only it Java and in it is not present . And without  it looks differently: if (getUser ().getId () == null) {save (getUser ().getName ())} else {update (getUser ().getId (), getUser ().getName (), getUser ().getDescription ())} vsb> If there was retrieveVms (), the problem would be more obvious. +1 vsb> the stare method has chances to see duplication of a remote call and to pull out it above on a stack. "The stare method" does not work for us. retrieveVms () people so easy cause type Ordinary functions some times in one method. It is a lot of people, experience at people different and that one developer never would write, another writes remorselessly, and on the code-revju it is difficult to note eyes all to the smallest details. Especially when send a method for 250 lines with words "excuse, very much hastened, there is no time to alter".

10

Re: Local variables - angrily? Whether rights Fowler?

AK> it complicates refactoring with selection of a new method (extract method) "IDE is not necessary, vim/emacs suffices all" (Truth a method  not so simply, the editor text.

11

Re: Local variables - angrily? Whether rights Fowler?

AK> AK> The problem with temps is that they are temporary and local. Because they can be seen only in the context of the method in which they are used, temps tend to encourage longer methods, because that's the only way you can reach the temp. By replacing the temp with a query method, any method in the class can get at the information. That helps a lot in coming up with cleaner code for the class. longer methods - I do not think that the length in lines means, is faster in operators - and in this case , is broken into separate variables or not. Indirectly my judgement proves to be true its following sentence where he suggests to carry out the intermediate calculations in separate f-tsii that they were accessible and in other places. So I understand this paragraph so - it is not necessary to do calculations in a place and to save in a variable, it is necessary to carry out them in a separate method. But it expresses, of course, clumsily. And any fanatics read it literally and force a brain then the remaining. Directly as Christians any

12

Re: Local variables - angrily? Whether rights Fowler?

Hello, Artem Korneev, you wrote: AK> Following the burst to avoid local variables, 4 calls of the same method requesting the data at Azure REST API here become. It, at first, slowly, secondly, we with the test surroundings already exceed Microsoft limits on 15000 calls Azure API at an o'clock. But it was not noticeable, since the method is called getVms () and makes false impression of a lung "". In reply to it they sent the citation from Fowler?

13

Re: Local variables - angrily? Whether rights Fowler?

Hello, Artem Korneev, you wrote: AK> Faced here interesting judgement. Sent a piece of changes on  to other command and received from them such : AK> AK> Local variables are not good to have in code. AK> You can check what Martin Fowler Says on this AK> https://refactoring.com/catalog/replace … Query.html AK> http://blog.boyet.com/blog/blog/local-v … rritating/ the motivation In the same place is described: The problem with temps is that they are temporary and local. Because they can be seen only in the context of the method in which they are used, temps tend to encourage longer methods, because that's the only way you can reach the temp. They are harmful not in itself that is why that can have negative  effects in the form of the big methods. And as collective  is incapable to distinguish sorts of local variables that  all .

14

Re: Local variables - angrily? Whether rights Fowler?

Hello, Artem Korneev, you wrote: AK> Faced here interesting judgement. Sent a piece of changes on  to other command and received from them such : Here let Fowler they, rams, also employ. 1) it is a call exterior API as you write more low. 2) saving of result of an API call allows to be debugged easier and . 3) the Result of an API call creates a certain context, and is better to it Id in a context not to change from a call to call, and that the hogwash turns out. However, transactions on  REST are not present, therefore anyway there will be a hogwash, but with  result of a call it will be at least stable within a method. 4) if them  from a variable - make its constant. About them Fowler did not write. 5) if in a command  is the Hindu with  cheeks - let pierces to itself   and will be blown off with the Extremely valuable Recommendations.

15

Re: Local variables - angrily? Whether rights Fowler?

AK> Only it Java and in it is not present . And without  it looks differently Well, it is possible to throw out to devils ancient as a shit of a mammoth the agreement on names JavaBeans: interface User {Optional <UUID> id (); default boolean isNew () {return! id ().isPresent ();} String name (); MyFancyBean name (String newName); String description (); MyFancyBean description (String newDescription); static User createNew () {return new PersistentUser ();}}... @Entity class PersistentUser implements User {...}... User user = User.createNew ().name ("Bond").description ("James Bond");... if (user.isNew() {save (user);} else {//the history of this your example is not so clear://why update does not accept object User?//well it is fine, we make so smile update (user.id (), user.name (), user.description ());} Brackets  it is finite, but simple gettery/setters it is possible to replace with invariable structure (will not work with JPA-compatible ORM, but it is a problem only for those who loves lazy loading): class User {public final UUID id; public final String name; public final String description; public static User onSave (UUID id, User prototype) {return new User (id, name, description);}}

16

Re: Local variables - angrily? Whether rights Fowler?

Hello, Artem Korneev, you wrote: AK> that I understood at Fowler - he unambiguously names harm local  (changeable) variables and here I with it agree, though and for other reasons. But also invariable local variables he also names undesirable. The cores of argument at Fowler two - in his opinion, it complicates refactoring with selection of a new method (extract method) and local variables worsen . But I in an emphasis do not see any problems with refactoring (at invariable local variables) and in the plan  I hold opposite judgement - on me, a heap  and set of nested calls are much less readable, than concrete local variables.  the unique problem from introduction of local constants is can really complicate refactoring if the type exchanges. But in the first, it will be fast Java 10, and in case of usage var it will be irrelevant. In the second, even without var - in case of usage of development environments, instead of  - in case of refactorings one horse-radish the development environment types changes usages that is necessary. My judgement such. If this local variable is used only in one place in the code, and the name of this variable is obvious from a caused method, sense in this local variable any. As in your case, personally I would not began to do these editings, there it be no point. The only thing - as a result of your changes it was fixed and  type String that can be both desirable, and undesirable. So your editing here disputable but if you made - I would not carp. But if here this call was used in a method more than once my judgement, what to select local variable it is necessary. And if who of it did not make that when the method corrected - I just would carp and insisted that corrected shit. For otherwise will be rigid , and it repeatedly complicates both refactoring, and understanding.

17

Re: Local variables - angrily? Whether rights Fowler?

Hello, Artem Korneev, you wrote: AK> the Preference in favor of local variables very naturally helps to lower an amount such here problems - instead of here that chain of three calls, there there would be a variable virtualMachinesCount or something like. Completely on your side. But. The project manual on the contrary is possible is interested in the maximum quantity of code lines and in the maximum quantity of bugs. And in maximum . For thus it is possible to receive rollbacks from more powerful iron, and also to justify  an amount of people on the project. As a result they supervise over the big collective, it is the positive experience, hence they will have more salary. If I got rid from , and from me would demand  as remaining, I quickly enough would put in statement on dismissal in case to me would pay less, than the market salary increased on 2. For then  at the expense of love to an amount of lines of the code as that not so would be desirable me. And by the way, I repeatedly faced problems when the method size works extremely slowly. For example it can be transversed on all collection by the full search. And generally any method call can is godless to brake and be a bottleneck therefore the less it I cause explicitly, the better. Plus any tricks when the second call can return new value. Therefore at me refactoring extract variable for a long time on the automatic machine. If whom that does not suit it, my experience means is not claimed in the given place and me or it is necessary to search for new operation, or to overpersuade. I explicitly tell some things - or I do how it is accepted in the industry and as I got used, or I leave to devils. Can  it is necessary after return to put else and  nesting level, some factories of factories to do, not to write tests as much as necessary and . to use global variables, as much as necessary  as much as necessary. But when to me do the remark contradicting reasonable practice and force to do shit - I search for other operation, though and slowly. Truth while was not necessary to resort to such radical measures, others left just. And at ill-wishers the authority was less mine, all the same recent students.

18

Re: Local variables - angrily? Whether rights Fowler?

Hello, Artem Korneev, you wrote: AK> And what you think of Fowler's that recommendation? Fowler's recommendations - a remarkable thing. We read that it  and it is done strictly on the contrary. Works almost always.

19

Re: Local variables - angrily? Whether rights Fowler?

20

Re: Local variables - angrily? Whether rights Fowler?

All present is much easier that you  years write the code and suddenly there is a beginner who starts it hardly hardly to correct, variables to place, brackets and gaps to equal. I think a hint it is clear

21

Re: Local variables - angrily? Whether rights Fowler?

Hello, _ABC _, you wrote: _AB> Hello, Artem Korneev, you wrote: AK>> Following the burst to avoid local variables, 4 calls of the same method requesting the data at Azure REST API here become. It, at first, slowly, secondly, we with the test surroundings already exceed Microsoft limits on 15000 calls Azure API at an o'clock. But it was not noticeable, since the method is called getVms () and makes false impression of a lung "". _AB> , already it is quite enough of it to send Fowler's fans to a garden, to reflect what dogmas do not replace a brain, is not present?  argument "At me hands were combed to remove this heap  that I and made" absolutely not enough. To have talk about refactoring about  and temporary variables when under the pretext of refactoring functionality of the code changes, can there  any or still that? It should become separate  with a separate substantiation where productivity will be an overall objective. Fowler against usage of local variables which store "the result of an expression" and suggests them to carry out in separate methods, on mine it is very reasonable. It is necessary to search for resistance of other command not in Fowler and in other place,  simply the first that got under a hand.

22

Re: Local variables - angrily? Whether rights Fowler?

Hello, serb, you wrote: S> productivity will be an overall objective. Here with it it do not agree. Refactoring is not so mandatory influences productivity. Refactoring changes structure of the code and if the code changes for the purpose of increase  and improvings of structure of the code - in my opinion, it is quite sufficient reason for refactoring. A rewriting for the purpose of performance gain it frequently any more refactoring. Therefore as the behavior of the code changes. In the majority of applications of a class "" productivity generally business the tenth. All a little noticeable time delays go not because of local variables or a string concatenation, and because of slow  the data.

23

Re: Local variables - angrily? Whether rights Fowler?

Hello, Artem Korneev, you wrote: AK> Hello, serb, you wrote: S>> productivity will be an overall objective. AK> here with it it do not agree. AK> Refactoring is not so mandatory influences productivity. Refactoring changes structure of the code and if the code changes for the purpose of increase  and improvings of structure of the code - in my opinion, it is quite sufficient reason for refactoring. AK> a rewriting for the purpose of performance gain it frequently any more refactoring. Therefore as the behavior of the code changes. AK> in the majority of applications of a class "" productivity generally business the tenth. All a little noticeable time delays go not because of local variables or a string concatenation, and because of slow  the data. Then not clearly this "" was simply ugly or also braked? Here the Author: Artem Korneev Date: 13.02 00:20 wrote that it braked also you the changes  and variables it accelerated, what it ? And if it simply ugly that about its style start up those care who it permanently support. That all was uniform, instead of so that in one method we do one in the friend another, moreover and in different files everyone writes on the. Here  dictatorship than anarchy is better.

24

Re: Local variables - angrily? Whether rights Fowler?

Hello, serb, you wrote: S> Then not clearly this "" was simply ugly or also braked? Sent superfluous requests. But against 20-minute process of development new  in a cloud, anybody also did not note those three superfluous requests. S> you wrote that it braked also you the changes  and variables it accelerated, what it ? Yes I there did not want to go into detail of changes. And that I quoted in the beginning, was pure refactoring - saved result of a method in a variable and interposed that variable in some other places within the same method. The question was more on  - whether there is for people a difference in visual convenience of reading between a method call and a local variable. For me the difference is. Not that that I am direct big, but on reading of calls of methods, on sensations, I spend hardly more. Seeing nested calls I visually I start to assort brackets to understand, what parameter concerns what call. Seeing repeated method calls, I start to think, whether it is expected here that different calls can return different values, or it simply save-paste. Not that that I am direct a lot of time behind it led, but the code with a minimum of calls of other methods to me visually to read easier. S> and if it simply ugly that about its style start up those care who it permanently support. I with this code will need to work. I try not to use expression "spaghetti-code" and "noodles" in professional dialogue and thought to show to colleagues a bright example as their code can be reduced in one and a half-two time, simply generalizing repeating units, and met the active resistance to offered changes. S> that all was uniform, instead of so that in one method we do one in the friend another, moreover and in different files everyone writes on the. Here  dictatorship than anarchy is better. I push dictatorship. But I need to answer with deep arguments similar attacks that entered requirements to style of the code did not look as my personal whim, and have been justified from the point of view of convenience of support of the code.