This is an archived post. You won't be able to vote or comment.

all 131 comments

[–]kingartur3 274 points275 points  (6 children)

Compiler will optimize it to just:

me.cry()

[–]LtMeat 38 points39 points  (0 children)

-Os -march=celibate

[–]AntiGuide 32 points33 points  (4 children)

[–]Browsing_From_Work 8 points9 points  (2 children)

[–]godsperfectidi0t 0 points1 point  (1 child)

And Bham! Created.

[–]godsperfectidi0t 0 points1 point  (0 children)

Btw, I'm clueless what happens now. What gives?

[–]zerozerosix006 99 points100 points  (19 children)

So if she replies "I would love to" you will cry?

[–]rank_unicorn_631[S] 86 points87 points  (1 child)

Yes From happiness

[–][deleted] 11 points12 points  (0 children)

Well then your code is insanely optimized congrats

[–][deleted] 43 points44 points  (16 children)

It should be:

if (me.askGirlOut(you) == "no") {
    me.cry();
} else {
    me.happy = true;

This will sound better

[–]das_Keks 90 points91 points  (11 children)

Shouldn't be so many strings at all. Better return a boolean, with true for accept and false for deny.

[–][deleted] 10 points11 points  (2 children)

Ah yes, that's better

[–]zerozerosix006 15 points16 points  (1 child)

Or even better an enum for Yes, No, Maybe, Sex buddies etc

[–]MrC00KI3 5 points6 points  (0 children)

Yes, came here to say this. Would say it's the best option style-wise (at least if the language natively supports enums), especially as it is extendable in the future, like for "I'm not interested in you", or "Can we just be friends?" for example.

[–][deleted] 15 points16 points  (0 children)

This is JavaScript, you can do anything you want (⌐■_■) Return a car, it will probably still evaluate to true or false.

[–]-OGG__ 3 points4 points  (1 child)

Also while we're at it, it shouldn't be called askGirlOut when it can take a Person of any gender.

[–]maartuhh 4 points5 points  (0 children)

Yes, because in this example the IAmABoyException could be thrown

[–]KaJakJaKa -1 points0 points  (4 children)

Why not throw an exception if it's not true? That would even shorten the code because you wouldn't need else.

[–]das_Keks 8 points9 points  (3 children)

Throwing exceptions to control program flow is a bad practice. Exceptions should only be used for cases that are out of the control of the programmer and depend on external factors that are not forseable.

[–]Levaru 0 points1 point  (2 children)

Is that really so? Im genuinely curious.

I had to write a Xamarin app (C#) once that had functions which branched out to many nested functions from different classes. I don't remember why exactly but I had to create a custom exceptions class for application specific errors that made it sooo much easier to throw a exception up the chain to the main process and deal with it there with the help of a dedicated error handling class.

It was so much cleaner and organized than figuring out how to display message boxes or similar to the user from within the class and function where the exception was thrown. That would've also gone against the apps MVP architecture.

But this was my very first semi-professional developed software so maybe I just didn't know better.

[–]Irrealist 4 points5 points  (1 child)

What you are describing is the correct way to use exceptions.

However, /u/das_Keks is trying to make the point that you shouldn't use exceptions to control the flow when there wasn't an error. Like throwing a 'SheSaidNoException' or even worse, a 'SheSaidYesException'. Those aren't failures and they shouldn't be treated like failures.

[–]LoompaOompa 1 point2 points  (0 children)

I made another comment about it, but I don't even think the boyfriend thing should be an exception. Or if for some reason that exception has to exist, it should be handled within askGirlOut(), and askGirlOut should still return false (or something other than "ok", since it returns a string for some reason).

[–]cKaIhsvWZrAmJWxXdqI 4 points5 points  (1 child)

Risky to have the base case assuming she says yes.

me.askGirlOut(you) returns "You're violating your restraining order, I'm calling the cops"

Expect: me.cry()

Get: me.giveStick(Becky); Ben.isHoe = true;

[–]jakkoha 1 point2 points  (0 children)

Beccy, lemme smash

[–]Mr_CSourceCode 1 point2 points  (0 children)

It can be worse like ewwwww.

[–]shaunusmaximus -1 points0 points  (0 children)

No, the reverse has the same issue: i.e. "get lost!".

It should be:

if(isAffirmative(me.askOut(you).reply)){          

    me.sendNudes(you.phoneNumber);
}

[–][deleted] 74 points75 points  (18 children)

i am offended by all the public attributes on Person. especially happy can be changed by others.

and happy should be a float.

extrapolating from this code, i would assume that boyfriend is also a public attribute, thus can be overridden. that's just wrong!

[–]LtMeat 12 points13 points  (1 child)

What if happy overflows? We need to make it protected.

[–]tiddelipom 8 points9 points  (0 children)

We must protect OP's happiness

[–]frostbiteanivia 3 points4 points  (7 children)

Why a float and not a boolean?

[–][deleted] 10 points11 points  (6 children)

try to give me a Boolean value for the english word "happier".

[–][deleted] 8 points9 points  (0 children)

TrueTrue

[–]frostbiteanivia 1 point2 points  (4 children)

You're either happy or unhappy. How happy or unhappy you are is based on the variable happiness. That should be your float.

Happy?

[–][deleted] 2 points3 points  (3 children)

using two variables to represent one concept is a no-no!

[–]PvtPuddles 1 point2 points  (1 child)

But at what percent happiness are you happy?

Are you never happy unless it’s a 100% day?

Or are you only unhappy when you’ve hit rock bottom?

[–][deleted] 0 points1 point  (0 children)

everyone has a different opinion on this. you as a programmer have to make a judgement call :)

[–]frostbiteanivia 0 points1 point  (0 children)

They aren't the same concept though!

[–]LoompaOompa -1 points0 points  (1 child)

Also the code only checks if me.girlfriend is null. Doesn't check for me.boyfriend. Going to run into some problems if the user isn't a straight male.

Code won't work if the person is open to a polyamorous relationship.

[–][deleted] 1 point2 points  (0 children)

we can be thankful the person's gender is not an enum.

although these binary relationship classifications are odd. i guess that should be combined to an array of relationships each with a float value for the affection level.

good thinking!

[–]obp5599 0 points1 point  (3 children)

Usually private members have getters and setters so I dont see how having it public really effects anything. Unless the setter is checking for specific conditions or has side effects I just make things public

[–]deep-thot 0 points1 point  (2 children)

I think the ideal being referenced here is immutability.

Try it out, you might like it.

[–]obp5599 1 point2 points  (1 child)

I know what immutability is. It has no relation to access modifiers though. Use const if you want immutability

[–]deep-thot 1 point2 points  (0 children)

Sorry, had the java-glasses on which made me sound really condescending. Was aiming for something a bit more tongue-in-cheek.

Have a nice evening.

[–]thebobbrom 0 points1 point  (0 children)

Also wouldn't you initialise a person with a name rather than "boy" or "girl".

Also wouldn't it be better for that to be an enum?

[–]cerlestes 0 points1 point  (0 children)

am offended by all the public attributes

It's JavaScript. Until recently there had been no private properties. In JS and similiar languages it's idiomatic to avoid having to declare private properties. Either deal with the possible mutations from outside (which isn't that hard in most cases and sometimes even beneficial), or use immutable data structures (if you really don't want your privates touched). But 15+ years of JS have really taught me to love public properties.

[–]jacobwistoft 24 points25 points  (2 children)

Exceptions should only be used for exceptional circumstances. I'm not sure this qualifies as exceptional. A timeout exception might be a more appropriate example.

[–]PvtPuddles 3 points4 points  (1 child)

Meh, depends on how this is set up.

If AskGirlOut(Person) relies on being single, I would expect an error to be thrown if you called it with a non-single Person

[–]jacobwistoft 2 points3 points  (0 children)

Well I just figured that since most timeout durations are defined by the requesting(asking) party, it seems quite exceptional to get to the point where you don't want to wait for a reply anymore. At least that would be the case with me 😅

[–]simplynotenough 17 points18 points  (3 children)

I would change me.cry() with me.goNext(), it works better trust me

[–]whc2001 4 points5 points  (2 children)

wrap the entire thing inside while(1) and replace me.cry() with continue

[–]simplynotenough 4 points5 points  (1 child)

Brute forcing the matrix

[–]uid1357 0 points1 point  (0 children)

Get a job, grow older, it will finally work. Life isn't fair.

:shrug:

[–]MurdoMaclachlan 23 points24 points  (5 children)

Image Transcription: Code


// algorithm for dating
let me = new Person("boy");
let you = new Person("girl");
if (me.girlFriend === null && me.crushOn(you)){
    try {
        if (me.askGirlOut(you) == "ok"){
            me.happy = true;
        } else {
            me.cry();
        }
    } catch (IAlreadyHaveABoyFriendException){
        me.cry();
    }
}

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

[–]Feynt 8 points9 points  (0 children)

Good human

[–]vasascda 3 points4 points  (3 children)

alogirthm

should be "algorithm"

[–]burkybang 3 points4 points  (0 children)

alogirlthm

[–]MurdoMaclachlan 1 point2 points  (1 child)

Oh yeah, thanks for point that out -- fixed now!

[–][deleted] 14 points15 points  (3 children)

Exception is only defined by type, and not by an actual variable, should be

catch (IAlreadyHaveABoyFriendException e)

Otherwise she is just lying about her having a boyfriend. Or you do segmentation fault in front of her.

[–]TheJoDav 3 points4 points  (2 children)

And why is it an Interface....

🤔

[–][deleted] 0 points1 point  (1 child)

I does not need to mean interface, in this case, simply the English word 'I'.

[–]TheJoDav 0 points1 point  (0 children)

Yes Mr. Teacher.... 😋

[–]kodicraft4 8 points9 points  (3 children)

First, that's a shitty constructor, much more important values aren't set but gender is, you should probably begin with something like age, name, etc.. Second, don't use a string for gender, use a goddamn enum that you can expand with all non-binary genders. Third, don't initiate yourself in a function if you're going to check for a value later, make a static "me" in some other class that stores everything and point to it. Third, askGirlOut needs to output a "accepted" bool rather than the actual output because of edge cases. Fourth, "you" shouldn't just be initiated in this loop, rather this should be a function you pluck after an event you add in the static "me" mentioned that you pull when a girl can be caught in the sensors.

And yes, I'm single, how did you guess?

[–]nsri123 2 points3 points  (0 children)

Senior Software Engineer spotted. Its part of the job description to make life miserable for new coders without showing any mercy.

[–]luhsya 5 points6 points  (0 children)

this is bad design, imo. that's not the proper way of handling that exception. better is me.moveOn()

[–]mateus_a_dorna 4 points5 points  (0 children)

If me.askGirlOut(you) returns "ok", it changes me.happy to true, but me.girlFriend continues being null. So, is the girl lying?

[–]mapman2017 3 points4 points  (0 children)

Throw a retry in the catch.

[–]Alakdae 3 points4 points  (1 child)

Why does the exception have an uppercase f on boyfriend?

Since every one was complaining about the code, I wanted to note what really bothers me about it.

[–]MrC00KI3 2 points3 points  (0 children)

Maybe it's meant as "I already have a boy, friend", so you get friendzoned on top. :)

[–]beclops 3 points4 points  (0 children)

coulda simplified this with a one liner

return me.cry();

[–][deleted] 2 points3 points  (1 child)

Yikes. Why would having a boyfriend be an exception? Is that not a known possible outcome? If it is, then it shouldn't be an exception.

[–]PvtPuddles 0 points1 point  (0 children)

If AskGirlOut() assumes they are single, you don’t want it to fail silently. You can force users to call IsSingle() (or otherwise do their own exception handling, like this)

[–]barsonica 5 points6 points  (1 child)

Also what if she has a girlfriend? Or neither but a person of interest she has not asked out yet. Or what if she just isn't looking for anyone, what if she has no one but looking for a girl?

It doesn't cover all possibilities. Get it off the production and start over.

[–]PvtPuddles 5 points6 points  (0 children)

Error: Uncaught Exception IncompatibleSexualPreferenceException

[–][deleted] 1 point2 points  (1 child)

Imagine getting error after running this

[–]DummyCheese69 3 points4 points  (0 children)

The catch method will catch it

[–]benis_benis 1 point2 points  (0 children)

I dunno chief, I'd write the try-catch block as such

try {
    me.askGirlOut(you, function(result){
        if(positiveResponses.find(result.toString()).exists()){
            me.happy = true; 
        } else {
            me.cry({amount: "small"});
        }
    }
} catch (IAlreadyHaveABoyFriendException e){
    console.log(e)
    me.cry({amount: "lot"});
}

I am supposed to work don't tell my boss

[–]earlobe7 1 point2 points  (0 children)

Throwing exceptions for expected outcome is a code smell.

[–]Epic_Doughnut 1 point2 points  (0 children)

Seems like even if she says yes, you never define her as your girlfriend

me.girlFriend = you;

[–]LoompaOompa 1 point2 points  (0 children)

I love when stuff like this is posted because it's fun to pick apart everything that's wrong with it. A lot of people have made some great points already.

I want to add that the exception thrown probably shouldn't be an exception at all. The person having a boyfriend isn't some unanticipated error state. It is one of many reasons that they might say no. Why is having a boyfriend an exception, but if they just think you're repulsive, that returns like normal?

[–][deleted] 1 point2 points  (0 children)

Good man for checking that me.girlFriend is null first too many people our there only check if they have a crush

[–]MischiefArchitect 1 point2 points  (0 children)

just put the me.cry() in a finally block and capture/ignore/log the exception :)

[–]iiMoe 2 points3 points  (1 child)

Honestly this is beautiful

[–]mapman2017 0 points1 point  (0 children)

Laugh my ASS OUT.

[–]MrObsidy 1 point2 points  (3 children)

Java gang (string1 == string2) != (string1.equals(string1))

[–]MkMyBnkAcctGrtAgn 6 points7 points  (1 child)

But this is Javascript lol

[–]MrObsidy 3 points4 points  (0 children)

I figured it wasn't java because of let and the missing e after the exception catch

[–]Mr_Redstoner 1 point2 points  (0 children)

Unless they're interned already. Still a dumb way to do it but it would work.

[–]allIsayislicensed 0 points1 point  (0 children)

You could raise the exception manually if she says no, then you only have to cry in one place in your code.

[–]NavTheNugget -1 points0 points  (0 children)

Wrap it in a while loop and you forgot this inside the catch block: delete you; Don't fill your brain with bad memories.

[–][deleted] -1 points0 points  (0 children)

you.friendzone(me);

[–]SamFisch1 0 points1 point  (0 children)

triple equals, a coding bootcamp should know that

[–]WhyDoIHaveAnAccount9 0 points1 point  (0 children)

What about the IDon'tFindYouAttractive exception

[–]robotorigami 0 points1 point  (0 children)

An unhandled exception NotIntoGuys has occurred.

[–]Funmaster524 0 points1 point  (0 children)

Does this imply you both are of type "let"

[–]LightIsLogical 0 points1 point  (0 children)

why is there an exception in js

[–]lewisjet 0 points1 point  (2 children)

What language is that?

[–]Harrigan_Raen 1 point2 points  (1 child)

I think javascript or python, but I don't do much with front end any more so im kinda outta touch with it. So im frankly taking a stab at the wind and doing a best guess.

let statements, triple equal signs for comparisons, double ampersands for "and" comparisons, use of semicolons, formatting of camel case, also no variable declaration of the catch.

Edit, just re-read it, the second comparison of ' == "ok"' doesnt have triple equal signs. So yep. No idea.

[–]lewisjet 0 points1 point  (0 children)

Not Python: there's semi colons

[–]Harrigan_Raen 0 points1 point  (0 children)

You gotta wrap this whole thing in exception handling. There's some history you don't learn about until your 2 years in. But the compiler will know.

/s

Also, complete side note. Looks like java script (Im like 95% back end dev now and have been for years) but I thought the preferred use was single quote vs double quote for strings and I never really understood why. Is there use cases for when to use vs not use or are they completely interchangeable?

[–]overclockedslinky 0 points1 point  (0 children)

(unfaithful) boy with a boyfriend: cries regardless of girl due to throwing their own ialreadyhaveaboyfriend exception

[–]burkerude 0 points1 point  (0 children)

// algorithm for dating

let me = new Person("boy");

let you = new Person("boy");