5

It reaaaally annoys me when my business logic is sound but the data is corrupted.

For example, find duplicates in a HashMap<String>.. but I didn't take into account the input could contain a space either before or after.. so I end up wondering: if a HashMap only contains unique keys, how come the count of items in the map is the same as the count of the input keys?! Well.. spaces were the culprit.

"12345" != "12345 ".. and therefore the Map sees it as two distinct keys..

What an annoying bug.

Lesson learned: 1) Sanitize input first and never trust it. 2) Never make assumptions

Comments
  • 3
    " business logic is sound "
    Can't be that sound if it doesn't work.

    " Sanitize input first and never trust it "
    Good lesson to learn.
  • 0
    Where are you from?
    „its sound“
  • 0
    @just8littleBit Its sound laaaa, proper decent lad.
  • 1
    That's just how maps work, it's not "corrupted" data. "12345" and "12345 " are two different things and should map to different values.

    Definitely not sound business logic if you expected differently.
  • 0
    @RememberMe My business logic detects if my input has two equal entries. I don't consider something with a space as valid input. A space doesn't belong in my business domain, for my input should be a list of strings that have only a hash but no spaces.

    I should have written a comparator/compareTo, perhaps.
  • 0
    Just for clarification, what does sound mean in this context? There are literally 42 translations for sound
  • 0
    @delegate212 it’s not used in the yo though, is it?
    I thought it’s an Irish think only.
  • 1
    @EaZyCode grand, cool, okay.

    Like: had fun with that guy, he’s sound.

    Or: if you give me n€, that’s sound,

    Or: if you can’t make it tonight, that’s sound.
  • 0
    @CaptainRant You should've stripped spaces within the presentation layer, that's what you should've done. Don't bloat business logic with input sanitization.
  • 0
    @EaZyCode It's in the general context, as in:
    That my code should work in all scenarios. That the logic of the routine is valid and also works in all cases. If the logic is valid (it checks for all duplicates and only works with non-duplicates), that doesn't mean it's sound (works in every situation, with every input). My mistake was assuming my code would work in all given situations as they appeared narrow to me (vague and imprecise thoughts).

    This video should provide some clarification on what I mean:
    https://youtube.com/watch/...
  • 0
    @Lor-inc Yes, I know that, but sometimes you're Mr. backend and you don't have control over what the frontend people do.
  • 0
    @CaptainRant I mean, normally backend splits to presentation and business logic in larger codebases.
  • 0
    @Lor-inc I know - and I have that. I'm in charge of the MVC in my project but I'm not in control of the input (the actual files that get dropped into my UI etc).
  • 1
    This is part of the learning curve. The one thing you can't control are people doing data entry.

    You're on the wrong track however. Sanitisation should be a last resort. Validate instead.
  • 0
    @CaptainRant I see. In any case, I think you should sanitize input the moment you get it. If you can't trust frontend or pres, add an intermediate "frontend fixes" layer that does everything frontend should.
  • 0
    Sanitize input first is a lesson learned? I hope you haven't missed all those other lessons!
Add Comment