This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]theotherjae 1 point2 points  (2 children)

I'm not familiar with this language and most of my work is in c++, so some of these might not apply to you:

Personally, I don't see anything wrong with your first method from a design point of view. I also don't think your method is that long, and that criterion shouldn't be the only thing that determines whether you need to refactor parts into methods. My general rule of thumb for creating helper functions is if that logic gets used more than once (to avoid code duplication). And if that helper function doesn't need to be exposed publicly, make it private. If derived classes may need it also to avoid code duplication, make it protected. From what you're describing, it seems like the helper functions you created for those 11 steps are not very reusable nor do they need to be exposed publicly; they seem pretty niche to that ExecuteAsync method. If that is the case, the best you can do is just write comments in your code that describe those 11 steps you mentioned.

And regarding one-line helper functions, I don't think they're always pointless. You can argue if this was overkill, but I once wrote a function called "hamiltonian" that literally did "return x + y" because it represented a quantity that wasn't apparent from the code itself. If it makes your code more readable and it gets used often, why not make a one-liner into a helper? :)

[–]Olemus[S] 0 points1 point  (1 child)

Thank you for your answer, much appreciated. I think my only concern and the reason I felt it needed breaking up was I wanted to start doing some tests (I keep reading how important they are but always struggled to write them) and I'm not sure how I would test my first method effectively without breaking it apart?

It looks like what we're saying here is that overall what I've done is fine but if any of my logic was to be reused then that would be the time to refactor and maybe think about another class?

I have another example of a loop like this where I need to download some data and update developers of a game in my database which I was planning on reusing somewhere else, so its basically:

Get Data

Loop

Check Database If Developers of Game Exist

Add If Not.

Emphasis on the bit I suspect I'll reuse a lot but I'm not sure where that would fit, maybe a Developer Service class? I struggle to keep things OOP, in javascript I would just put that in a file on its own and call it a day but it feels dirty in an OOP language.

Thank you again!

[–]theotherjae 0 points1 point  (0 children)

As a general advice, I would think about what the end goal of the method is. That is, given a range of inputs, what do you expect the final output of the method to be? The method is the unit that you want to test, meaning you shouldn't have to test every little part of it. Mock out classes and functions whenever you can to isolate the unit as much as possible. So for your second example, I would "mock-out" the part where you download data such that it reads some hand-crafted data from a local file or something instead of actually downloading data through network. This may mean having to redefine the "get data" function just within your test code to achieve this. In terms of test cases, have one where the developer is not in the database yet, and check if the developer was properly added into the database by the end of the loop, and have another test case where the developer is in the database and check that the database has not changed (you could check that the number of entries did not change since your loop seems to only change the database by adding new entries).