Skip to main content

@PureFunction

1 reply [Last post]
sjasja
Offline
Joined: 2004-08-15
Points: 0

Ever seen a newbie coder (or even a seasoned one on an off day) write a statement like "someString.toUpperCase();" and thinking someString is now upper case?

There are methods whose return values shouldn't be ignored.

Random idea of the day: an annotation known to the compiler. When a program ignores the return value of a method that is defined with @PureFunction, the compiler (optionally) emits a warning.

Issues:

* Bloat! Bloat! Bloat!
* There are some semi-pure functions. Would Iterator.next() be pure? Usually you want to use the value but not 100% necessarily. C's lint uses casts to void to mean "I'm ignoring this on purpose"...
* Could also be the reverse: an annotation that means "it's ok to ignore my return value". But that ship has sailed; would cause warnings for all existing code. @PureFunction is non-invasive; you can annotate your methods any time or not mark them at all.
* @PureFunction is a misnomer; it does not mean no side effects. Opening a database connection surely has side effects but it would still be marked "don't ignore my value." Needs a better name...

Thoughts? Rotten fruit?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
alexlamsl
Offline
Joined: 2004-09-02
Points: 0

one can achieve this via Annotation Processing I suppose - and after implementing that and put that into use (together with Unit Tests, say) for a while to decide whether it's really worth it is probably a good idea ;)