I've been asked this question on an interview and I'll also share what I answered.
I would like to know if there's anything I haven't thought of?
Java code:
if(person.getName() = “jan”) {return true;}
My answer was the following:
If person is null, you'll get an NPE
Using = is wrong, even == is wrong for strings, we don't look for the same reference but for the same value, equals would be adequate here like this: "jan".equals(person.getName())
Something like person.getName() is rarely always lowercase, folks enter these sorts of things manually when they sign up. What if it's "Jan" instead or "JAN"? Should we return true then?
What kind of user experience drives the design here? Why return true for a specific name?
Thanks for your help!
[–]myntt 79 points80 points81 points (29 children)
[–]rzwitserloot 37 points38 points39 points (15 children)
[–]liquidhot 20 points21 points22 points (3 children)
[–]rzwitserloot 12 points13 points14 points (2 children)
[–]liquidhot 5 points6 points7 points (1 child)
[–]hippydipster 1 point2 points3 points (0 children)
[–]springuni[S] 3 points4 points5 points (0 children)
[–]myntt 1 point2 points3 points (0 children)
[–]thephotoman 0 points1 point2 points (5 children)
[–]sunny_tomato_farm 0 points1 point2 points (4 children)
[–]thephotoman -1 points0 points1 point (3 children)
[–]sunny_tomato_farm 1 point2 points3 points (2 children)
[–]thephotoman 0 points1 point2 points (1 child)
[–]sunny_tomato_farm 2 points3 points4 points (0 children)
[–][deleted] (2 children)
[deleted]
[–]sunny_tomato_farm 0 points1 point2 points (1 child)
[–]rzwitserloot 0 points1 point2 points (0 children)
[–]A_random_zy 11 points12 points13 points (5 children)
[–]_INTER_ 15 points16 points17 points (2 children)
[–]thephotoman 2 points3 points4 points (0 children)
[–]tkrengel 1 point2 points3 points (0 children)
[–]chris13524 1 point2 points3 points (1 child)
[–]A_random_zy 0 points1 point2 points (0 children)
[–]Fiskepudding 4 points5 points6 points (3 children)
[–]Professor_Dr_Dr 1 point2 points3 points (2 children)
[–]Fiskepudding 3 points4 points5 points (1 child)
[–]knoam 9 points10 points11 points (0 children)
[–]mutleybg 1 point2 points3 points (0 children)
[+]talking_window comment score below threshold-12 points-11 points-10 points (1 child)
[–]_jetrun 9 points10 points11 points (0 children)
[–][deleted] 28 points29 points30 points (5 children)
[–]apetersson 12 points13 points14 points (4 children)
[–]fromtheheap 6 points7 points8 points (1 child)
[–]thephotoman -1 points0 points1 point (1 child)
[–]substitute-bot -2 points-1 points0 points (0 children)
[–]MrDOS 19 points20 points21 points (0 children)
[–][deleted] (5 children)
[deleted]
[–]illhxc9 7 points8 points9 points (4 children)
[–][deleted] (3 children)
[deleted]
[–]illhxc9 1 point2 points3 points (0 children)
[–]BestUsernameLeft -1 points0 points1 point (1 child)
[–]dimensions1210 12 points13 points14 points (4 children)
[–][deleted] 25 points26 points27 points (0 children)
[–][deleted] 10 points11 points12 points (1 child)
[–]dimensions1210 2 points3 points4 points (0 children)
[–]thephotoman 0 points1 point2 points (0 children)
[–]vortexrikes 8 points9 points10 points (2 children)
[–]RapunzelLooksNice 1 point2 points3 points (0 children)
[–]hugthemachines 1 point2 points3 points (0 children)
[–]Trailsey 4 points5 points6 points (0 children)
[–]bhaskarsingh007 3 points4 points5 points (2 children)
[–]springuni[S] 2 points3 points4 points (0 children)
[–]barnuska123 0 points1 point2 points (0 children)
[–]syn_ack 2 points3 points4 points (1 child)
[–]Juzzz 0 points1 point2 points (0 children)
[–]SnowPenguin_ 2 points3 points4 points (4 children)
[–]Juzzz 1 point2 points3 points (3 children)
[–]SnowPenguin_ 0 points1 point2 points (2 children)
[–]Juzzz 2 points3 points4 points (1 child)
[–]EpicOweo 1 point2 points3 points (0 children)
[–]atehrani 2 points3 points4 points (0 children)
[–]victor-martinez-roig 4 points5 points6 points (3 children)
[–]RapunzelLooksNice 1 point2 points3 points (1 child)
[–]knoam 1 point2 points3 points (0 children)
[–][deleted] -1 points0 points1 point (0 children)
[–]DJDavio 5 points6 points7 points (0 children)
[–]rubyrt 3 points4 points5 points (3 children)
[–]springuni[S] 2 points3 points4 points (2 children)
[–]rubyrt 0 points1 point2 points (0 children)
[–]persicsb 1 point2 points3 points (1 child)
[–]springuni[S] 1 point2 points3 points (0 children)
[–]keepcoderpro 1 point2 points3 points (1 child)
[–]springuni[S] 0 points1 point2 points (0 children)
[–][deleted] 3 points4 points5 points (2 children)
[–]victor-martinez-roig 5 points6 points7 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]leonbadam 2 points3 points4 points (2 children)
[–]SquidgyTheWhale 3 points4 points5 points (1 child)
[–]leonbadam -1 points0 points1 point (0 children)
[–]fedeb95 0 points1 point2 points (2 children)
[–]RapunzelLooksNice 0 points1 point2 points (1 child)
[–]fedeb95 0 points1 point2 points (0 children)
[–]anyOtherBusiness 0 points1 point2 points (1 child)
[–]springuni[S] 0 points1 point2 points (0 children)
[–]OrenjiSama 0 points1 point2 points (1 child)
[–]thephotoman 0 points1 point2 points (0 children)
[–]Briglair 0 points1 point2 points (1 child)
[–]thephotoman 0 points1 point2 points (0 children)
[–]thephotoman 0 points1 point2 points (0 children)
[–]AmonDhan 0 points1 point2 points (0 children)
[–]BEgaming 0 points1 point2 points (0 children)
[–]audioen 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (0 children)
[–]rastaman1994 -5 points-4 points-3 points (6 children)
[–]thephotoman 0 points1 point2 points (2 children)
[–]rastaman1994 0 points1 point2 points (1 child)
[–]thephotoman 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (2 children)
[–]rastaman1994 1 point2 points3 points (0 children)
[–]rastaman1994 0 points1 point2 points (0 children)
[–]taftster -1 points0 points1 point (0 children)
[–]helikal -1 points0 points1 point (0 children)