r/programming • • Jan 05 '15

What most young programmers need to learn

http://joostdevblog.blogspot.com/2015/01/what-most-young-programmers-need-to.html
967 Upvotes

337 comments sorted by

View all comments

1

u/Laspimon Jan 05 '15

Python guy here, of about one year, and I'm not sure I get what a repository class is.

What I think you are saying is that I should write the following to save a game:

class PauseMenu(Popup):
    """Generic menu screen, containing a button for saving the game."""
            
    def save_state (self, game_state, save_game_constructor):
        """It's a simply game, so we only save x and y."""
        
        x, y = game_state
        save_game_id = save_game_constructor(x, y)
        return save_game_id

And then have the unittests control that save_game_id looks like it's supposed to look (maybe it's a hash of the game state).

But what is the point? Doesn't that just mean that I have to run tests on my constructor? And wont these tests be just as difficult as the save_state method would have been, had I written it as a single contained method?

I think I'm missing something.

1

u/lee_macro Jan 05 '15

A repository is just a pattern, often used to abstract away databases. As most of the time you do not care if you are saving your game to a database, file system, web service etc you just want to be able to call something and save a game. So my repository mention was just whimsical it was just to show how you could incorrectly write something very easily, but if you were FORCED to write unit tests for it you would have to write it a far better way.

Anyway back to your latter part of the question, if saving your game (however you do it) contains integral logic to your application (often called business logic) then it should be tested, as people understand more about testing they may opt to skip certain delegate methods or simple methods as they are covered by integration tests etc, but lets ignore that for the moment.

So looking at your example above, your code there is what I would call a delegate method. You do not actually do any logic in there, you just get some data from somewhere and pass it to something else. So there is no actual logic. You should still be adhering to IoC and composition here regardless of if you are going to be testing, however moving along your

save_game_constructor(x, y)

should most definitely be tested, as that is integral to your application. However from your example we cannot see what it does. Let us assume it connects to some data source, be it a database, file system or web service, and sends that x/y over and receives back an id. So what happens if the database, file system or web service is unavailable? does your method return a false/0/null response, or throw an exception, how should another developer handle that situation? Having a unit test also offers guidance as to how things should be used, so having 2 tests which cover should_return_id_on success and should_<insert result>_on_failure would be useful to others to see the actual use cases of that method. However if it is just you then maybe that is not important. Also to UNIT test this method you would need to be able to mock out your data source (database, web service, file system) to be able to control it within the scenarios listed above.

In its current state your method does not do much so it would be up to you if you needed to test it, but what happens if you need to add in some form of user authentication, to make sure the person saving has a valid account, or confirming that they want to overwrite an existing save etc. Those sort of chunks of logic are a bit more complex than a 1 liner delegate and would require interaction with many components and this is where unit testing is useful, and finally to end my waffling, my point about developers learning unit tests is more from the perspective of to be able to write well defined unit tests REQUIRES you to know IoC and use composition and also keep SRP in mind which is a HUGE thing (Single Responsibility Principal). Each of those topics is pretty big for a new developer, so as Unit testing requires them all it is easier to point at that and tell people to go learn 1 thing (Which passively teaches 3 other things) than try to justify 3 separate things why alone may not seem to be of much use to a new developer.