Enhanced your programming skill with Assert and Unit Test

Enhanced your programming skill with Assert and Unit Test

Sources

http://www.pgbovine.net/programming-with-asserts.htm
http://stackoverflow.com/questions/1375786/whats-the-point-of-nsassert-actually
https://www.youtube.com/watch?v=BbPPnxhcdHo

Why you need Assert in your program?

1/ What is Assert?
+ Assert is the way you make sure a condition in your program must be matched. Otherwise, the app will crash.

2/ What is the point of Assertion?
+ Using assert will guarantee your program always act like your assumption.

=> For instance, you write a function with a lot of input and in some situation, inputs mus be valid. Let's say input_1 must comes with input_2. In that case, writing if else and return value is not fit, because you have to handle a lot of return value & error

=> So that, you will use Assert to make sure your function is used properly. This is very important when you working in a team while writing functions for other people to use. Or even if you're working alone, write Assert will help you not make mistake when using your own function after a long time.

3/ Does Assert affect production code?
+ No, it's not. This is a good point. Because most of the cases Assert crashes will be figure out in development. If by a rarely chance, you missed a case in PROD, your app still not crashes.

4/ When you use Assert? and How?
+ When you're writing sth like a framework or a function for other team members to use. Then, you should put Asserts in your method to check the input, environment vars, etc. to make sure everything working as you expected
+ When you're writing a complex process. i.e: a process must be run on background because it's too heavy, then you make an Assert to check the current thread.

*** Notes ***
+ In my opinion, you should handle the flow after the Assert in order to make your program not crash in PROD
+ Should not use Asserts too much in simple things or things often to changes. Because you have to re-write them.

Why should you have Unit Test, at least for few important methods?

1/ It's true that writing unit test is painful and take a lot of time

+ Yes, writing unit test cost time, especially when you want to test all the possibilities. There are two ways to write unit test. One is Test Driven Development, while you write test before you write code. The other is you write test after write code to make sure your function working well.
=> The 1st way is not feasible when working in Startup or in a small team. I want to focus more on the 2nd way. while writing unit test just for some stable & complex function. 

+ By the way, it's not always hard to write unit test. If you're already structure your program well, every part working independenly, follow SOLID rules. In iOS app, I recommend MVVM model. Then, write tests is not painful, it's even fun : )

2/ Unit test make you confident about your code and easy to refactor function

+ If you're working startup environment or you don't have much of time to write the test properly. I think at least you should write unit test for some complex business function, which is stable and rarely to changes. You can write these tests after a time you develop it, no need to be write at the time you develop.
=> The point of this test is help you cover all the cases and make sure everythings is working well or it's can help you verify faster when a small changes happen.

Conclusion

1/ Using Asserts even if you're working alone. It will help you a lot even if you're working alone. It's make sure your function working with the valid inputs as expected

2/ Write unit test for complex function, at least write for them. It will help you easier to refactor later and make sure the bugs is not on your side. Even if the bug occurs, you can quickly narrow down them

Comments

  1. Despite the not-so-good use of English and some obvious points got overly explained, the overal tips are actually good and useful. Thanks.

    ReplyDelete

Post a Comment

Popular posts from this blog

How to add custom media codec to pjsip

Level up your log in React-Native with Reactotron

iOS Push Notification with HTTP2