Skip to main content

CodeSOD: A Tool for You

1 month ago

A junior dev on the team had a simple question: in Java, how do I correctly read user input from the CLI and act on it?

Now, we can wonder about a junior dev not having such basic knowledge, but maybe they're new to Java specifically. Who knows. But Robert's team has a senior on it who "likes to mentor". This senior has strong opinions about how to write software, extreme confidence in those opinions, and has somehow been around the company long enough that he knows where the bodies are buried and no one will fire him.

Here's his sample application for reading input.

import java.nio.charset.StandardCharsets; import java.util.*; public class ScannerTool { public static void main(String[] args) { Map<Optional<Character>, Map.Entry<String, Boolean>> map = new HashMap<>(); map.put(Optional.of('A'), new AbstractMap.SimpleEntry<>("You entered A.", true)); map.put(Optional.of('B'), new AbstractMap.SimpleEntry<>("You entered B.", true)); map.put(Optional.empty(), new AbstractMap.SimpleEntry<>("You entered nothing.", false)); Optional<Character> input; do { System.out.println("Please enter A or B:"); } while (!map.getOrDefault((input = nextCharacter()), new AbstractMap.SimpleEntry<>("", false)) .getValue()); System.out.println(map.get(input).getKey()); } public static Optional<Character> nextCharacter() { String l = new Scanner(System.in, StandardCharsets.UTF_8).nextLine(); if (l != null && !l.isEmpty()) { return Optional.of(l.charAt(0)); } return Optional.empty(); } }

Look, I know Java is verbose, but this is entirely next level. Giving this to a junior has to constitute an attempt to drive the youth out of the industry, because any sane person being told, "your job is to write this" would realize they've committed a grave error in their career choice and needs to escape now.

nextCharacter in this case actually returns the first character of the next line, not the next character. And if there isn't one, it returns an empty optional. Which, fine, we only expect one character per line of input for this exercise, so that's not wrong, I just don't like the name of the function.

Of course, using Scanner here is a wrecking ball fly swatter- that class is a full on parser for reading out common data types from a file. Worth showing to a junior, but a little out of scope of this question.

But the real overengineering is the map of maps- well, a map of map entries. The key to the outer map is an optional of the character that was entered by the user. The value is itself a map, the key of the map is the message we want to output, the value is whether or not the loop should continue. They've reinvented tuples using map entries. Which, in their defense, mainline Java doesn't have tuples (enterprise edition seems to have them in the javax namespace, but I'm not even certain those are meant to be general purpose tuples anyway). A quick search indicates that their absence is a perennial source of heated discussions in forums and Stack Overflow.

Robert writes:

It is remarkable in its creativity and execution and still has me in awe. When I tried to unpack it, my brain just started to pack up its things and left, haven't seen it since.

Bon voyage.

[Advertisement] Picking up NuGet is easy. Getting good at it takes time. Download our guide to learn the best practice of NuGet for the Enterprise.
Remy Porter

South Korea Says AI Agents May Have Been Used to Hack the Country's Banks

1 month ago
South Korean President Lee Jae Myung says there are signs AI models may have been used in recent cyberattacks against several major banks. "In some hacking incidents, signs have emerged of AI being used, causing considerable public concern and anxiety," Lee said during a cabinet meeting. "Please establish the circumstances swiftly and clearly, and concentrate personnel and resources on minimizing the damage." Reuters reports: South Korea's police have launched a full-scale investigation into the hacking attacks against commercial banks that led to a breach of customers' personal information, the Yonhap news agency reported on Tuesday. Shinhan Bank, KB Kookmin Bank and others had reported cyberattacks, the Financial Services Commission (FSC) said on Friday, while Yonhap reported that Hana Bank and Woori Bank also suffered breaches. Authorities have not yet disclosed details on what kind of AI tools were used in the hacking incidents or the full scale of the breaches. On Sunday, FSC Chairman Lee Eog-weon convened an emergency meeting with financial industry associations, regulators and executives from affected institutions, warning that the sector must respond with the highest level of vigilance. Financial watchdog the Financial Supervisory Service (FSS) and the Financial Security Institute shared data about 28 unique IP addresses and some country information linked to the recent hacking attempts with the financial sector, the FSS said on Tuesday.

Read more of this story at Slashdot.

BeauHD