Encourage to use 'assertions' in swt code base.

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Encourage to use 'assertions' in swt code base.

Leo Ufimtsev-2
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:

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.

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

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.
[hidden email] | http://DeveloperBlog.RedHat.com/

platform-swt-dev mailing list
[hidden email]
To change your delivery options, retrieve your password, or unsubscribe from this list, visit