Home Writing elegant code

Simplify your code

When writing complex algorithms, try and write the simplest and cleanest version of the code possible.

This can be challenging – the ideal is to simultaneously write elegant code with no redundancy while still being fully efficient. Sometimes, you can accomplish this ideal by making minor adjustments to your code; sometimes you need to rethink your entire approach.

Re-implementing convenience methods

Whenever possible, you should avoid re-implementing methods. Two common offenders are using list.size() == 0 instead of list.isEmpty() and using stringA.toLowerCase().equals(stringB.toLowerCase()) instead of stringA.equalsIgnoreCase(stringB).

If you're not sure if you're allowed to use a convenience method/if using it would count as advanced material, ask. As a good rule of thumb, if a method is listed on a cheat sheet from section, you may use it.