Jesper de Jong wrote:That code is not very complex at all, so I wonder why you want to change it to lower complexity. Are you using some static code analysis tool that tells you that this method has a high runtime complexity? That's because there are many possible combinations of one or more of the parameters being empty or not. In fact, since there are 3 parameters, there are 23 = 8 possible combinations. In practice, this is not a problem at all, because the condition for each of the parameters is not dependent on the others.
If it really bothers you, you can make separate methods out of each of the if-statements. But I wouldn't worry about it.
Paul Clapham wrote:First using the technical term "runtime complexity" to mean something different than its normal meaning. He could have said "amount of code" and be better understood. Although if he had said what he meant, it would have looked silly because he would have been telling you not to increase the amount of code in order to reduce code duplication. What Jesper said is a good response to that
Paul Clapham wrote:Second, premature optimization. The whole method you posted is just a small amount of code which doesn't use any external resources. Its runtime is going to be essentially zero -- maybe a couple of milliseconds at most. And it's only going to be used once in a process which is going to take much longer than that. So spending any time at all on considering whether to optimize it for speed would be a complete waste of time.
Paul Clapham wrote:Your first two if-statements test the same condition so they could be rolled into one if-statement.
Paul Clapham wrote:Presumably "increase run time complexity" involves comparing your code to the previous version
Paul Clapham wrote:unless perhaps their definition of "run time complexity" is different to what my understanding is.
Henry Wong wrote:That topic did *not* earn any cows. If it had, and since you are the only poster on that topic, the cow would have been yours.
Henry Wong wrote:I am assuming that you noticed the cow in the forum listing?
Henry Wong wrote:The forum listing's cow rating is the rating for the whole topic. This includes the total number of cows earned by the topic, along with other factors, such as the number of times the topic has been viewed. Arguably, it is unfortunate that the cow icon was used for it ... but oh well.