Grade 10 Computer Science · Objects

Common mistakes with objects

The wrong assumptions recurring in everyone learning this subject.

Code that runs perfectly, yet in the wrong place

A method inside a class always returns the right result, and no test fails. And still it is a mistake. How can code that works be wrong, and which one short question exposes it before it is even written?

The four mistakes below are not mistakes of carelessness. They come from “class” and “object” sounding alike, and from the syntax hiding part of what happens. Whoever recognises them once usually does not repeat them.

If the method does not touch the data, it belongs elsewhere. If an internal value is needed, add a method that returns it.