+ 2
In line with Ludovico Iommi, it's quite common to check preconditions, or maybe some function or method where you want to ensure the parameters adhere to some rules (e.g. not null) instead of writing many ifs that then throw an exception we can use a single line assert. It's nice to have a keyword for a common problem. It's important to remember that many aspects of coding are semantic - there are many ways of doing a single job but there is usually a "best" way of doing it, using some specific syntax - classic example, for and while loops are functionally identicial but sometimes you would prefer one or the other in some scenarios
16th Aug 2018, 10:50 PM
Dan Walker
Dan Walker - avatar
+ 4
So... "assert" is basically the same thing as "if", but if the condition fails it throws an exception.
16th Aug 2018, 9:30 PM
Maz
Maz - avatar
+ 1
Sometime is useful to think to a piece of code being comply to this structure: some preconditions, the logic to be executed and finally some postconditions. This is the usual structure for a unit test in backend service of a web application: if postconditions are matched then the unit test is successful otherwise it is failed and some broken logic in the application needs to be fixed. A suitable way of implementing postconditions is using asserts instead of ifs. In general any time we need to check postconditions after the execution of some logic a good way of code it is using asserts.
16th Aug 2018, 9:54 PM
Ludovico Iommi
Ludovico Iommi - avatar
+ 1
JS is my best Language.... so i dont know about other language so far.
17th Aug 2018, 3:24 AM
MasterBroPro
MasterBroPro - avatar
0
semantic is to do with meaning. HTML is a good example - you could use different tags to produce use identical results, but there is usually a tag whose job it is for that task, so the tag conveys it's purpose (yes you could write all content in various divs targeted by CSS, but just use a p instead)
17th Aug 2018, 7:21 AM
Dan Walker
Dan Walker - avatar