Writing C-style Java Programs
This article represents only my personal view, welcome to leave a message to discuss

Feasibility

Java is said to be a pure object-oriented programming language, so can you write procedural-oriented C-style code in Java?
There are no pointers in Java, but there are references, so the answer is obviously yes.

The difference in style

Let’s start with two pieces of pseudo-code.

C style

struct Foo foo;
change_foo_value(&foo, 0);

Java style

Foo foo = new Foo();
foo.changeValue(0);

This also shows the difference between the two design patterns, object-oriented and procedure-oriented.
Both styles are good, right?

Problems during develop

If a method needs to return two values, how should I write the program?
Let the requirement be: execute a shell script in the program and get both the return code and the output.

C style

int exec_script(char *script_path, char **output) {
	int exit_code;
	// Business logic
	...
	return exit_code;
}

Java style

static class Result {
	public final int exitCode;
	public final String output;
	public Result(int exitCode, String output) {
		this.exitCode = exitCode;
		this.output = output;
	}
}

static Result execScript(String scriptPath) {
	int exitCode;
	StringBuilder outputBuffer = new StringBuilder();
	// Business logic
	...
	return new Result(exitCode, outputBuffer.toString());
}

What if we don’t want to define a new class for this method in Java?
Maybe we can take a page from C:

static int execScript(String scriptPath, StringBuilder output) {
	int exitCode;
	// Business logic
	...
	return exitCode;
}

Or this:

static String execScript(String scriptPath, int[] exitCode) {
	StringBuilder outputBuffer = new StringBuilder();
	// Business logic
	...
	return outputBuffer.toString();
}

Thoughts

This reminds me of the Vala programming language from the GNOME community :D


Last modified on 2022-03-26