assertions

Is Belt and Braces programming good practice or just introducing needless complexity?

拜拜、爱过 提交于 2019-11-30 13:13:10
问题 I was wondering whether using a Belt and Braces (Suspenders) approach to programming - and to data validation in particular - was good practice or not. This came about from the following example. I was creating a form and I added Listeners to all the fields which should mean that the OK button is only enabled if all the fields in the form have valid values. I was then writing the code which was run when the OK button is clicked. The pessimistic side of me decided that Belt and Braces never

assertAll vs multiple assertions in JUnit5

蓝咒 提交于 2019-11-30 07:19:32
问题 Is there any reason to group multiple assertions: public void shouldTellIfPrime(){ Assertions.assertAll( () -> assertTrue(isPrime(2)), () -> assertFalse(isPrime(4)) ); } instead of doing this: public void shouldTellIfPrime(){ Assertions.assertTrue(isPrime(2)); Assertions.assertFalse(isPrime(4)); } 回答1: The interesting thing about assertAll is that it always checks all of the assertions that are passed to it, no matter how many fail. If all pass, all is fine - if at least one fails you get a

Do you use assertions? [closed]

ぃ、小莉子 提交于 2019-11-30 05:00:36
This is not really a "question" so I'm making it CW. The assert Keyword is great! It should make, feel your self more confident with the code you wrote, but, until today when I was creating a small test class ( < 20 lines ) I realize a never use it since it was introduced. Heck! I barely use logger which is very useful indeed, but it wasn't until today I realize I don't use assertions. Do you use assertions? If no, what's the reason? I was taught to use lots of assertions back in the 90s and they made great sense. It's good defensive coding. However, I think this has now been superceded by

How to programmatically enable assert?

落花浮王杯 提交于 2019-11-30 04:25:20
How can I programmatically enable assert for particular classes, instead of specifying command line param "-ea"? public class TestAssert { private static final int foo[] = new int[]{4,5,67}; public static void main(String []args) { assert foo.length == 10; } } This was a comment to @bala's good answer, but it got too long. If you just enable assertions then call your main class--your main class will be loaded before assertions are enabled so you will probably need a loader that doesn't reference anything else in your code directly. It can set the assertions on then load the rest of the code

Why should I use asserts?

六月ゝ 毕业季﹏ 提交于 2019-11-29 19:25:39
I never got the idea of asserts -- why should you ever use them? I mean, let's say I were a formula driver and all the asserts were things like security belt, helmet, etc. The tests (in debug) were all okay, but now we want to do racing (release)! Should we drop all security, because there were no issues while testing? I will never ever remove them. I think most of the guys that claim that removing something comparable to asserts never profiled their code or the asserts were absolute displaced. I've never seen any real performance advantage especially regarding the 80 / 20 rule. So, am I

Writing robust and “modern” Fortran code

ぃ、小莉子 提交于 2019-11-29 18:41:43
In some scientific environments, you often cannot go without FORTRAN as most of the developers only know that idiom, and there is lot of legacy code and related experience. And frankly, there are not many other cross-platform options for high performance programming (C++ would do the task, but the syntax, zero-starting arrays, and pointers are not compatible with some people). So, let's assume a new project must use Fortran 90, but I want to build the most modern software architecture out of it, while being compatible with most recent compilers (Intel ifort, but also including the Sun/HP/IBM

Why this regex assertion doesn't match?

故事扮演 提交于 2019-11-29 17:20:10
I'm trying to just preg content between html tags, I'm trying this simple assertion pattern and I don't understand why it doesn't match this string. <a href=http://url.com title="link">this is a ling</a> (?<=<a.*>)([ \w]*)(?=<.*\/a>) Debuggex Demo Lookbehinds on debuggex (PCRE, Javascript and Python) cannot be of variable width, meaning that you can use (?<=<a>) which has a fixed width (3 characters) but not something that can vary in length (?<=<a.*>) (can have 3 characters, or 4, or 5, etc). The regex simply is not valid but debuggex tells you that there is no match. 来源: https:/

Checking two boundaries with Jasmine (between matcher)

百般思念 提交于 2019-11-29 08:17:41
问题 In Jasmine, there are toBeGreaterThan and toBeLessThan matchers. What if I want to check an integer value in a specific range? Is there anything like toBeInBetween matcher? Currently, I can solve it in two separate expect calls: var x = 3; expect(x).toBeGreaterThan(1); expect(x).toBeLessThan(10); 回答1: You can run the boolean comparison and assert the result is true : expect(x > 1 && x < 10).toBeTruthy(); Also, there is toBeWithinRange() custom matcher introduced by jasmine-matchers: expect(x)

assertAll vs multiple assertions in JUnit5

拜拜、爱过 提交于 2019-11-29 02:58:17
Is there any reason to group multiple assertions: public void shouldTellIfPrime(){ Assertions.assertAll( () -> assertTrue(isPrime(2)), () -> assertFalse(isPrime(4)) ); } instead of doing this: public void shouldTellIfPrime(){ Assertions.assertTrue(isPrime(2)); Assertions.assertFalse(isPrime(4)); } The interesting thing about assertAll is that it always checks all of the assertions that are passed to it , no matter how many fail. If all pass, all is fine - if at least one fails you get a detailed result of all that went wrong (and right for that matter). It is best used for asserting a set of

C++ error-codes vs ASSERTS vs Exceptions choices choices :( [closed]

↘锁芯ラ 提交于 2019-11-29 02:25:43
Code In question I have heard (and regurgitated) the C++ exception mantra on both sides of the fence. It has been a while and I just want to centre myself once more, and this discussion is specific to the code I have linked (or low level classes such as containers) , and it's dependencies. I used to be a defensive and error_code using C programmer, but it's a tiresome practise and I am programming at a higher level of abstraction now. So I am rewriting a container class (and it's dependencies) to be more flexible and read better (iterators absent atm). As you can see I am returning enumerated