Apr 21 2011

Boolean Arguments

I find that boolean arguments don’t read that well when revisiting the code a little later.

doBackup(true);

This is easily fixed with languages that allow keyword arguments, like Python:

doBackup(notifyOnSuccess=True)

However, in languages without, such as Java, I’ve found it preferable to create two constants for true and false, named for what they will be used for.

private static final boolean DONT_NOTIFY = false;
private static final boolean DO_NOTIFY = true;

doBackup(DO_NOTIFY);

It’s kind of like the suggestion in Code Complete to use Enums instead of booleans.

  • #programming
  • #java
  • #python
  • #booleans
  • #code style