Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Can Jadx reduce excessively long statements with nested ternary operators #2123

Open
DecompExplorer opened this issue Mar 11, 2024 · 1 comment
Assignees
Labels
Milestone

Comments

@DecompExplorer
Copy link

Describe your idea

Description:

Hi, I've observed that Jadx sometimes produces excessively long statements with nested ternary operators, which can impact code readability and understandability.

Here is an example:

The following code snippet is from org/apache/commons/codec/binary/Conversion.java in the project commons-lang

public static char binaryToHexDigitMsb0_4bits(final boolean[] src, final int srcPos) {
	...
	if (src[srcPos + 3]) {
		if (src[srcPos + 2]) {
			if (src[srcPos + 1]) {
				return src[srcPos] ? 'f' : '7';
			}
			return src[srcPos] ? 'b' : '3';
		}
		if (src[srcPos + 1]) {
			return src[srcPos] ? 'd' : '5';
		}
		return src[srcPos] ? '9' : '1';
	}
	if (src[srcPos + 2]) {
		if (src[srcPos + 1]) {
			return src[srcPos] ? 'e' : '6';
		}
		return src[srcPos] ? 'a' : '2';
	}
	if (src[srcPos + 1]) {
		return src[srcPos] ? 'c' : '4';
	}
	return src[srcPos] ? '8' : '0';
}

The corresponding code generated by Jadx:

public static char binaryToHexDigitMsb0_4bits(boolean[] src, int srcPos) {
	...
	return src[srcPos + 3] ? src[srcPos + 2] ? src[srcPos + 1] ? src[srcPos] ? 'f' : '7' : src[srcPos] ? 'b' : '3' : src[srcPos + 1] ? src[srcPos] ? 'd' : '5' : src[srcPos] ? '9' : '1' : src[srcPos + 2] ? src[srcPos + 1] ? src[srcPos] ? 'e' : '6' : src[srcPos] ? 'a' : '2' : src[srcPos + 1] ? src[srcPos] ? 'c' : '4' : src[srcPos] ? '8' : '0';
}

In this case, Jadx generates a single-line expression that represents the same functionality using nested ternary operators. While this condensed form is more compact, it sacrifices readability significantly. The deeply nested ternary operators can be challenging to parse and comprehend, especially when dealing with multiple levels of conditions. In this case, the change from the multiple if structure to the single-line ternary expression has decreased the code's understandability.

I think keep the multiple if structure OR format the nested ternary operators in the following way might be a helpful improvement. Thank you.

	return src[srcPos + 3] 
		? src[srcPos + 2] 
			? src[srcPos + 1] 
				? src[srcPos] ? 'f' : '7' 
				: src[srcPos] ? 'b' : '3' 
			: src[srcPos + 1] 
				? src[srcPos] ? 'd' : '5' 
				: src[srcPos] ? '9' : '1' 
		: src[srcPos + 2] 
			? src[srcPos + 1] 
				? src[srcPos] ? 'e' : '6' 
				: src[srcPos] ? 'a' : '2' 
			: src[srcPos + 1] 
				? src[srcPos] ? 'c' : '4' 
				: src[srcPos] ? '8' : '0';

The corresponding .class file can be found here

JDK version: openjdk 17.0.5
Jadx version: jadx-1.4.7.297-3599b248

@skylot skylot self-assigned this Mar 17, 2024
@skylot skylot added this to the TBD milestone Mar 17, 2024
@skylot skylot added enhancement Core Issues in jadx-core module code-style labels Mar 17, 2024
@DecompExplorer
Copy link
Author

Hi, I was just wondering if there was any progress?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants