Java utility class?

I have some code I put in the robotInit method that prints some data to the console log via println calls and sends the same data to the Shuffleboard and SmartDashboard via calls to their methods like SmartDashboard.putstring() and ShuffleBoard.getTab(). Our team would like to make sure this code is executed in every robot we build. However, it is about 50 lines of code or so, and asking every student to copy and paste them into their robots seems error prone.

What I would like to do is put the code into a separate class file, so that all the student would need to do is copy the file and add two lines to the Robot.java file, one to do the import and one to call the method inside the robotInit method.

Looking at how to do that, it seems like I could do it via a static utility class, but every time I look at how to do it what I find is another essay why they should not be used. But I don’t understand the alternatives well enough to see how to do what I want using them. Does anyone have any advice about how to do something like this in the general case? Our team will probably want to add more utility functions later on.

And …

https://oblog-docs.readthedocs.io/en/latest/introduction.html

The reason I don’t like utility classes is because they can become a collection of unrelated functions.

Are you truly only logging in robotInit? I’m wondering what useful info would be on the dashboard once and not be updated while the robot is running.

Are you truly only logging in robotInit? I’m wondering what useful info would be on the dashboard once and not be updated while the robot is running.

Information about build and deploy version and dates, git hash for the latest changeset, git branch, files changed. We have often found ourselves not running the code we thought we were running.

Good idea! I think this would be an OK thing to put in a static method in a util class. I would just consider that utility classes aren’t object oriented so watch out for the patterns you’re implementing, and keep the class focused on only logging.

Usually it’s OK to have static utility classes. However, to make things simple, your static utility classes need to stay simple. If you create a class called CommonUtil, people are going to throw a bunch of random stuff into it. If you have a class such as MathUtil or something similar, you can use it to do Math related stuff. (That’s a bad example because almost anything you could ever want is built into WPILib or Java itself).

We have a CtreUtil class which has some utility methods for the CTRE library. For your use case, you seem to have a few things that are less generic, such as calls to Shuffleboard. I think this option is fine, you just don’t want this utility class to get mixed up with unrelated functions (like a random math function). So maybe call this class something like InitUtility or DebugtUtility.

TDLR; Separate your utility methods into different classes based on what they actually do and it’ll be fine.

1 Like

I am having some difficulty in putting my methods into a separate class. I am afraid Java is not my strong suit.

My original stuff was something like

robotInit () {
printallfiles();
}

printallfile() {
printfile(1);
printfile(2);
printfile(3);
}

printfile(int filenum) {
println(…);
}

That is a very lose approximation.

I moved the printfile and printallfiles methods into their own java file, and no matter what I do VSCODE complains.

In the robot.java file I can’t seem to get the printallfiles method to be recognized. In the new file it is complaining because it says the getClass() method is non-static and cannot be called from a static method.

If that’s the actual code then the printallfiles Is not defined. But you said loose approximation.

To get a little more in depth.

In robot.java I have:

package frc.robot;
[…]
import frc.robot.utils.Splash;
[…]
public void robotInit() {
Splash.printAllStatusFiles();

in the file Splash.java in the utils folder I have:

package frc.robot.utils;
[…] (a bunch of imports)
public final class Splash {
private static void printStatusFile(String Filename) {
[…]
}
public void printAllStatusFiles() {
printStatusFile(“deployhost.txt”);
printStatusFile(“deploytime.txt”);
[…]
}
}

My biggest problem is that no matter what I do, printAllStatusFiles() can’t seem to be resolved in the robot.java file. Oddly, VSCode offers what I would expect in auto-completion, but once the line is finished it says things can’t be resolved.

PrintAllStatusFiles isn’t static

public class LogHelper {
public static void log(String s) {
System.out.println(s);
}
}

After monkeying around a bit and moving the file out of the sub-directory I got the problem of not finding the class fixed.

Thanks everyone. Once I get the code cleaned up a little I’ll post a link to the repo so anyone can use it.

1 Like

As promised, here is the final version of what I came up with. Take a look, try it out and let me know if you have any problems or suggestions.

Exmaplebot with splash screen logging enabled.

Thank for all the help. Have fun with it.

Looks like fun!

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.