Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[platform-swt-dev] Encourage to use 'assertions' in swt code base.

Hello folks,

This email is to encourage us to use assertions in SWT's code base. (because we don't use them at present).

I just want to give a brief overview of what/why/how:

What:
Assertions cause code to print errors if an _expression_ fails, or causes jUnits to fail. But they are conditional, by default they are disabled so they don't affect clients.

Why:
These are very handy for validating assumptions and they serve as a form of documentation/commenting.
As such, they make code more reliable because faults are found quicker.

For example, in Webkit.java, there are some calls that are only reached by webkit1 code and some by webkit2.
It's not obvious which code is ran by which version of webkit.
As such, I sprinkled the code with some assert statements. These serve as documentation but also trigger jUnit failures if reached.
Inline image 2

How:
SWT's jUnit tests have assertions already enabled by default. So these automatically trigger tests to fail if condition is not met.

However, for snippets or child eclipse instances, you have to enabled them manually during development. To do so, add '-ea' or -enableassertions to VM arguments:
Inline image 3

This will cause code to halt and stack is printed:
Inline image 4

Assertions come in two forms:
assert _expression_
assert _expression_ : msg;

Hope it helps.

Yes, I'll tryna add the above to wiki after Oxygen is released.



--
Leo Ufimtsev
Software Engineer, Eclipse team.
Toronto, Canada

Red Hat, Inc.
Leonidas@xxxxxxxxxx | http://DeveloperBlog.RedHat.com/

Back to the top