I'm writing a Java method for a board game that should indicate if a move is legal and, if not, why. If exceptions are too much machinery for my students, is it reasonable to call it reasonForIllegality and have it return a String, null if it's legal?

Follow

@peterdrake Sure that technically works. But why teach students bad practices and the wrong way of doing it when you could teach them the right way (with exceptions). It really isnt a difficult topic and is kinda fundamental to java.

Β· Β· 0 Β· 0 Β· 0

@freemo (1) "really isn't a difficult topic" is relative. It's good Java practice to never put anything in the default package, but they certainly can't handle that when they're still figuring out what an object is. (Don't worry, they learn about exceptions before they graduate.) Choosing the right time to introduce things, especially when there's a wide range of levels of preparation in a class, is always a struggle.

(2) Student limitations aside, are exceptions the best choice? People on Twitter have also suggested an enum, an Optional, constants, or the empty String to avoid having a null lying around. What's the cleanest way to go?

@freemo Another issue is that some techniques (e.g., making fields private and having public getters and setters) only pay off for larger programs. I strive to introduce things just at the point where they are obviously useful.

That said, maybe this is a golden opportunity to introduce exceptions...

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.