Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
    Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Would you do this with strings?

 
Ranch Hand
Posts: 72
1
Python Java Linux Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
I wasn't going to ask the following question about some code I've seen yesterday but stumbling upon this question, I cannot resist

Here is some code I have inherited from a contractor that was hired (at £500 a day!) and left some time ago. It does work. So, it's lower on my priorities to address:

(I applied minor obfuscation here)


From 0 to 10, how "bad" is this code? I am not too worried about the repeating updateValue method - it does make usage of the method readable and it does make the method itself simple.
I am particularly interested in the "recycling" of the string object 'template'.

Can/should a java developer do this?
(again, it DOES work, the committed 'template' has all the values updated like the usage of 'updateValue' is trying to do)
 
Sheriff
Posts: 15952
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
The method has two responsibilities: 1. Take care of infrastructure concerns (file path and reading from file)  2. Delegating to ParseTmpl

My concerns would be that the delegating responsibility is not necessary (what's the point?) and that it's getting passed parameter values it doesn't actually use in the method itself, except for maybe turning an int life to a String before passing to ParseTmpl.updateValue(). Again, what's the point of doing that here?

There's also a dissonance between calling the method resourceCreate and calling the updateValue() method on ParseTmpl. Is this creating or updating? One of those names is lying.

Also, how is this method unit tested? I suspect you don't have a true unit test for this.

I would not do this like this.
 
Todor Kolev
Ranch Hand
Posts: 72
1
Python Java Linux Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
It has it's benefits and drawbacks.
Imagine having a lot of forms and you want to have a generic method of filling a field in those forms: updateValue
Then, since there are many form types and different ways to fill each form, one might want to just create a method that aggregates the usage of updateValue instead of exposing updateValue as raw usage in the parent code.
Also, the specific intention of filling this form that way way could stay constant, but the way the fields are updated could change (different field names, or maybe owner needs to also be populated as a new field, 'creator', for example).
You could address all of that in this method, instead of fishing for all the times someone tried to fill this form in that way and changing it in every instance there...

As for naming, it works out. You create a resource by picking up a template (empty form), fill that form by updating the empty fields and submit for resource creation.

Again, not so worried about the structure here. More about the recycling of the string object.
 
Junilu Lacar
Sheriff
Posts: 15952
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
If that's all you're worried about, what's the worry? It's basically four concatenation operations that are being done in a different method. Are you worried about performance? If so, then use a profiler. Otherwise if this is not called 50K times at one go, then what are you worried about?
 
Junilu Lacar
Sheriff
Posts: 15952
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
If you're doing multiple string concatenations (say 100s or 1000s or more at a time in a loop) then it's better to use a StringBuilder and keep appending whatever values you need to append. Then call the toString() method in the end to get the final string value.
 
Junilu Lacar
Sheriff
Posts: 15952
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
But to be honest, focusing on the reuse of the string variable instead of the structure and semantics of the method to me is like nitpicking on the knobs of a radio in a car that is not very well engineered. I think you've got your priorities mixed up.
 
Marshal
Posts: 70709
288
  • Mark post as helpful
  • send pies
  • Report post to moderator
Why are they using a String as the password? I thought that was insecure.
I presume ParseTmpl is some sort of map. Why does it have that method apparently marked static?
 
Todor Kolev
Ranch Hand
Posts: 72
1
Python Java Linux Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
to be honest, I never intended this to be it's own thread, it wasn't my doing.

I think nobody even read anything that I wrote so I am going to step out.

Concatenation? Where do you even see concatenation?
(don't answer, I've had enough of this)
 
Campbell Ritchie
Marshal
Posts: 70709
288
  • Mark post as helpful
  • send pies
  • Report post to moderator

Todor Kolev wrote:. . . Concatenation? Where do you even see concatenation? . . .

Thrice in line 2 and once in line 3. I don't know whether there is any concatenation of Strings in the ParseTmpl(...) method. Since I can't see any String concatenation in a loop, all the concatenation I can see is all right. In fact I have heard people say thatis the best way to concatenate Strings and other variables. As long as you're not in a loop.
There was an enhancement in Java9 that made concatenation in a loop much faster, but it is still a performance bottleneck once you get into iteration counts with 5+ digits in.
 
Junilu Lacar
Sheriff
Posts: 15952
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator

Todor Kolev wrote:to be honest, I never intended this to be it's own thread, it wasn't my doing.

I think nobody even read anything that I wrote so I am going to step out.

Concatenation? Where do you even see concatenation?
(don't answer, I've had enough of this)


I don't understand your apparent frustration. You asked a question and you got honest answers. I may be wrong but it seems like your frustrations come from not getting the kind of answers you wanted. What were you expecting then?

But if you've had "enough of this" I'll be happy to lock the thread to prevent further aggravation.
 
Junilu Lacar
Sheriff
Posts: 15952
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator

Todor Kolev wrote:to be honest, I never intended this to be it's own thread, it wasn't my doing.


The question you posed is not quite related to the String immutability and had the potential to hijack the original thread. That's why I split it off to it's own thread. Since you have indicated you want no further discussion around your question, I am locking this thread.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic