Ad
  • Default User Avatar

    There are 6 code variations, all of them with removed lines

  • Custom User Avatar

    why didn't you remove the last two lines?

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    this one saves one characters over mine xd

  • Default User Avatar

    To begin with, I didn't tell that any method must have exactly one return statement.
    For maintainability reasons method has to have AS FEWER RETURN STATEMENTS as possible.

    'Object.equals() method uses multiple return statements'

    equals method in the Object class has only one line that checks reference equality. Basically, it is entry-level information that you should know by hard.
    Most of the classes in the JDK and different frameworks override default implementations of the equals method to define equality based on an object's state.

    For the purpose of explanation, I think equals probably is not the best illustration because it is short and simple.
    To describe a method that has more than one exit point but still remains to be well-readable let's consider a method, which implements some intricate algorithm, and may be it's a relatively long one. And it has the following structure:
    {
    if (eliminate one case) throw ... // that's the early kill and that's great
    if (eliminate another case) retrurn ... // and that's the early kill as well

    // MAIN LOGIC

    return ...
    }

    To conclude, in general, return statements in the middle of a method has to be avoided where it's possible.
    Because it can create confusion when return inclosed by multiple loops and conditions, as well as when return statements are present in both try and catch blocks.

    DISCLAIMER: I don't say no one writes like that. I say it is considered to be undesirable nowadays.

    Quick digression. If you think that JDK is written by the Gods and there are no pitfalls in Java you have a lot to discover.

    'how you would solve problems of that kind in general'
    In terms of clean coding, it's a naive way of thinking that there's some kind of uniform recipe that can be applied anywhere.
    Clean code - is a code that is EASY to read, but difficult to write.
    There are tons of books on this topic and if you expect to receive an exhaustive unambiguous answer in this comment then I'll say:
    Sorry silver bullets are out of stock))

    If you want to see how to solve this kata in an imperative way without exiting in the middle of the loop take a look at my fork.
    That's doable with a help of break statement or labels. Caution, both of them can make the code cluttered, and labels are actually discouraged to use for that reason.

    By the way, your dubious attitude to streams is still unclear to me.
    Streams are effective, lazily evaluated, very nice in terms of readability, and capable to solve most of the problems which traditionally were implemented using loops.
    If you are striving to write clean then code functional style is more preferable over imperative. And that is the strongest suggestion in my answer.

    Now you are free to ignore this information or explore it father on your own. I hardly can add anything.
    Good luck.

  • Default User Avatar

    Single exit strategy is a controversial discussed dogma. For instance the Object.equals() method uses multiple return statements. Just have a look at the source code. Perhaps you shall advise Oracle that you think they're using bad pracice and improve the Java code base with a better solution. Your approach may be good for these concrete kata, but it is indeed not a general valid way, to solve all problems of that type, as you can see.

    Hence, my question was not about linking connections to anything, but wanting to know, how you would solve problems of that kind in general, if something like IntStream was not accessible. Since clairvoyance does not exist, nobody can elaborate how your solution would look like. This is just a red herring argument. I would be grateful, if you could take two minutes to show a good way that works even for cases when Streams don't work, with a single exit point. And of course, without a break in the loop, because it violates your choosen strategy.

  • Default User Avatar

    If it was a replay to my comment can elaborate on your thought. I'm not sure that I've understood correctly what you are trying to convey.
    What is the connection between import statements and best coding practices?

  • Default User Avatar

    How would you solve it without any imports like IntStream?

  • Default User Avatar

    Apart from the weird formatting multiple return statement is not a best practice.

  • Default User Avatar

    sometimes I am so stupid it is astounding. I make things so much harder than it needs to be

  • Default User Avatar

    To enhance fastness, it would be better to convert the String to StringBuilder, do the replaces like we did with String, and then convert the SB back to String. But it would still remain in O(n), thus it may not be worth the effort, but is nice to know.

  • Default User Avatar

    Both solutions are in the same complexity class O(n). So it's not really a win to use a for loop, instead of just replacing several times. Your code has also a bad readability, and is therefore less maintainable in production.

    Our method will not be slow like you say, only somewhat slower than yours --> See big O notation: https://en.wikipedia.org/wiki/Big_O_notation

  • Custom User Avatar

    Yes, your solution is shorter. But I think that we (developers) should write faster code. Imagine that your method will execute 1_000_000 times in production every 1 minute. It will be slow.

    Also I think that this kata we can't solve with regex, because we have several replace templates.

    Sorry for bad English.

  • Custom User Avatar

    Correct, but its more concise than what you did with a boilerplate for loop. One line versus 11 lines on your code. Best way would of been to use regex and call .replace() once.

  • Custom User Avatar

    This will create 5 new String instances. Also this solution will make 5 loops for replace all characters.

  • Loading more items...