Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting rid of magic numbers in Java

How do I get rid of magic numbers in java without declaring a massive amount of finals or static finals? Keep in mind looping, arrays are not allowed. It doesn't seem possible? Help appreciated. thanks.

Example code:

drawOval(5, 5, width, height);
drawOval(10, 10, width, height);
drawOval(15, 15, width, height);
drawOval(20, 20, width, height);
drawOval(25, 25, width, height);
like image 691
Larry21 Avatar asked Dec 02 '25 04:12

Larry21


1 Answers

Defining constants is really your only option. What's your opposition to using them? They are definitely worth their space in this context. Future developers would much rather see extra constants than be confused by what those numbers mean.

like image 114
Oleksi Avatar answered Dec 04 '25 16:12

Oleksi