The Getter Setter Getter – The Daily WTF

Date:

Share:

Today’s Java snippet comes from Capybara James.

The first sign something was wrong was this:

private Map getExtractedDataMap(PayloadDto payload) {
    return setExtractedDataToMap(payload);
}

Java conventions tell us that a get method retrieves a value, and a set method mutates the value. So a getter that calls a setter is… confusing. But neither of these are truly getters nor setters.

setExtractedDataToMap converts the PayloadDto to a Map. getExtractedMap just calls that, which is just one extra layer of indirection that nobody needed, but whatever. At its core, this is just two badly named methods where there should be one.

But that distracts from the true WTF in here. Why on Earth are we converting an actual Java object to a Map? That is a definite code smell, a sign that someone isn’t entirely comfortable with object-oriented programming. You can’t even say, “Well, maybe for serialization to JSON or something?” because Java has serializers that just do this transparently. And that’s just the purpose of a DTO in the first place- to be a bucket that holds data for easy serialization.

We’re left wondering what the point of all of this code is, and we’re not alone. James writes:

I found this gem of a code snippet while trying to understand a workflow for data flow documentation purpose. I was not quite sure what the original developer was trying to achieve and at this point I just gave up

Source link

Subscribe to our magazine

━ more like this

What You (Want to)* Want

November 2022Since I was about 9 I've been puzzled by the apparent contradiction between being made of matter that behaves in a predictable way, and the...

‘She landed through her uncomfortableness and gave me a chance’: After weeks of patience, a once-scared mama cat finally starts to trust the hooman...

Sometimes, the Cat Distribution System delivers in the most unexpected places. While checking on a quiet cemetery, this caretaker discovered a tiny, furry mystery....

Taylor Swift’s Style | PS Fashion

While each product featured is independently selected by our editors, we may include paid promotion. If you buy something through our links, we may...

Olive & June The Builder Gel Mani System Review At Home

"Because gel had done so well, but we knew people were like, 'How do I get stronger, longer nails that are mine? How do...

OpenAI Data Shows Hundreds of Thousands of Users Display Signs of Mental Health Challenges

OpenAI claims that 10% of the world’s population currently uses ChatGPT on a weekly basis. In...