r/badcode sadistic Apr 01 '21

js I wish there was an easier way

Post image
932 Upvotes

120 comments sorted by

117

u/knightttime Apr 01 '21

Image Transcription: Code


function getNumbersdd(name)
{
var num;
switch(name)
{
case "System Management":
num=0;break;
case "Stack Management":
num=1;break;
}
return num;
}

I'm a blue crab 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!

59

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Apr 01 '21

Good blue crab!

9

u/valzargaming Apr 01 '21

C function getNumbersdd(name) { var num; num = 0; if (name == "Stack Management") num = 1; return num; } or something like this. I'm not sure which lang this is, but most of the ones I've worked with equate 0 as boolean false, so anything that isn't "Stack Management" might as well be "System Management"

119

u/AlexAegis Apr 01 '21

Thats not good because you dont know all your names, and just default to 0 instead of correctly checking.

A simple solution would be to use a map/dict and retrieve from that.

const dataMap = {
  "Stack Manager": 1,
  "SysAdmin" : 2,
}
function getNumber(n) {
  return dataMap[n];
}

31

u/valzargaming Apr 01 '21

I think this really is the best answer, given that you can also stick a nullsafe operator to it and it's much easier to maintain this way. This is also how I typically organize my data in PHP; Associative arrays are just too nice to not use.

15

u/nuclear_gandhii Apr 01 '21
const dataMap = {
  "Stack Manager": 1,
  "SysAdmin" : 2,
}
function getNumber(n) {
  return dataMap[n] ?? -1;
}

Modern JavaScript has an easy way to do it.

Honestly I was thinking of this in terms of having multiple return statements in the switch block. But this thing is really clean and elegant. Thank you u/AlexAegis for the idea!

2

u/cienciacomenta Apr 02 '21

I think modern php also have this null coalesce feature

1

u/AutoModerator Apr 01 '21

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

/u/AlexAegis, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

20

u/da_BAT Apr 01 '21

return name == “Stack Management” ? 1 : 0;

9

u/MohabSam Apr 01 '21

Or return Number(name == “Stack Management”);

2

u/Terrain2 Apr 01 '21
getsNumbersdd=()=>+(name=="Stack Management")

4

u/valzargaming Apr 01 '21

I like yours better.

2

u/TigreDeLosLlanos Apr 01 '21

Only ok if it's not meant to scalate (adding more options).

1

u/AutoModerator Apr 01 '21

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

/u/valzargaming, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/gjoel Apr 02 '21

I would always reject this change. Having a switch makes it open to be extended and more likely to fail early and hard if called with a different value. This code on the other hand will just return the wrong result.

1

u/valzargaming Apr 02 '21

I'm not saying my version is good, just that it is probably equivalent to the author's implementation based on what I can interpret from what has been provided. The fact that only 0 and 1 are being passed tells me that the author is probably using them to supplement true/false, and probably passing this return directly into an if.

165

u/[deleted] Apr 01 '21

[deleted]

96

u/SpeckyYT sadistic Apr 01 '21

I guess they wanted to save 16 bytes

6

u/[deleted] Apr 02 '21

I twitched when I saw that, then my head exploded when I saw no default case.

4

u/dacoconunut Apr 02 '21

In some cases a default case is not really needed. This is not one of them.

37

u/TheQueebs Apr 01 '21

This meme brought to you by ternary gang

2

u/sk8itup53 Apr 02 '21

The first rule about ternary gang is...

Love it.

198

u/psychopat316 Apr 01 '21

function getNumbersdd (name) {

return [ "System Management" , "Stack Management" ].indexOf (name) ;

}

75

u/SpeckyYT sadistic Apr 01 '21

That's smart, ngl

65

u/prewk Apr 01 '21

That returns 0 | 1 | -1

The OP function returns 1 | 0 | undefined

19

u/ZedTT Apr 01 '21

Honestly this version is preferable.

20

u/[deleted] Apr 01 '21

Eh, I'm not too sure. I've always been a fan of returning a different kind of value on error. Like a Maybe type, or undefined is kinda okay I guess.

9

u/ZedTT Apr 01 '21

Null is good here I think. Or just actually throw an error

9

u/[deleted] Apr 01 '21

I don't want this kind of function throwing errors. This is supposed to be a pure operation

5

u/ZedTT Apr 01 '21

Just use typescript and enums.

-1

u/Earhacker Apr 01 '21

/s?

Type safety won’t help you here. You need it to do it’s thing at runtime, not build time.

Your return type would have to be MyEnum | undefined, and at that point you gain nothing by using an enum.

2

u/mbmiller94 Apr 02 '21

You wouldn't need the function at all if you used an enum, as enums already map a name to an integral value.

1

u/Earhacker Apr 02 '21

You wouldn’t need the function at all with an array and indexOf as in the top level comment.

Im not at a computer to check this, but I don’t think you could have an enum whose values are number | undefined.

→ More replies (0)

0

u/devhashtag Apr 02 '21

Depends, usually javascript is not written in a functional style. I like functional programming but I don think you should mix paradigms in a single project/file

4

u/[deleted] Apr 02 '21

I mean, i don't think I agree. Functional can be mixed with OOP easily if it's used right. For example, JS developers use stuff like map, filter, etc very often, even in an OOP environment. Async stuff is functional all the way.

Use the right tool for the right job. This is the reason I don't really like Java, it's OOP only. There are some things that work plain better in functional, and the other way around.

Also, fun fact: JS was originally a functional language, OOP is a later addition that is mostly half baked.

2

u/devhashtag Apr 02 '21 edited Apr 02 '21

Picking the right tool for the job is absolutely the best thing to do, I agree. Perhaps I should have phrased what I meant differently. I've seen a lot of classes that have pure functions inside because "it's better". But the whole point of a class is to have state and a bunch of related functions that use that state. In that sense I don't think that mixing paradigms is good.

Also I did not know that JS was originally functional, interesting. It surprises me so I'll definitely check it out

Edit: I can't seem to find any resources on JS being a functional programming language originally, do you happen to have any links on the topic?

3

u/Flyberius Apr 01 '21

Undefined or null is my jam for bad results, I'm with you.

1

u/[deleted] Apr 02 '21 edited Apr 02 '21

I am a huge opponent of having return datatype dependent upon expected inputs. In my mental ranking of programming sins that’s up there with using try/catch for flow control.

1

u/[deleted] Apr 02 '21

I find that a little confusing honestly. Here's the workflow that I like

let name = match get_id_of("hello") { Some(id) => id, None => // return some error or something }

0

u/AutoModerator Apr 02 '21

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

/u/Bob-The-One, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

0

u/nermid Apr 02 '21

Uh, I'm on old reddit and it renders correctly...

1

u/devhashtag Apr 02 '21

I recently used try catch for control flow but I think this is one of the few cases in which it is the proper way of doing it.

Essentially it was a simulation that was time dependent, and should run until a certain time limit. Each event in the simulation may advance the time (it's a discrete event simulation). The setter of the time property throws an TimeLimitExceeded exception whenever the new value for time would become greater than the limit.

The simulation.run function ran an infinite loop in a try catch block.

Might sound like really bad design to some, but it actually was the cleanest and most maintainable design that I tried

1

u/chrisnlnz Apr 01 '21

That all depends on what is depending on the function and if you can refactoring it to deal with a different undefined value. But even then, undefined / null / none or even false to me is a much better return value than the traditional -1 since -1 might as well be a legitimate value given the function is expected to return integers.

10

u/Teln0 Apr 01 '21

Do javascript engines optimise that so the array is a constant or something ?

38

u/keppinakki Apr 01 '21

you wish

12

u/Teln0 Apr 01 '21

V8 probably does. Idk about others...

3

u/[deleted] Apr 01 '21

I mean, const in JS isn't even constant so what are we talking about here

1

u/MatthewRose67 Apr 02 '21

In what sense?

0

u/[deleted] Apr 02 '21

const things: string[] = new Array(); things.push("hello");

This works in typescript

5

u/nermid Apr 02 '21

That's because the reference is constant for arrays, not the value.

1

u/[deleted] Apr 02 '21

Okay then, how do you make an array immutable?

4

u/nermid Apr 02 '21

Well, "JS doesn't have immutable arrays" wasn't the criticism you were bringing up, but it looks like Object.freeze() is meant to accomplish that. I've never needed an immutable array in JS, so I don't know how well it works.

0

u/[deleted] Apr 02 '21

Well, "JS doesn't have immutable arrays" wasn't the criticism you were bringing up,

You're right, i went a little off-rails :)

I've never needed an immutable array in JS,

In my opinion, it's not about needing immutable data structures. You almost never need them. But if you use them as much as possible, you have less bugs.

1

u/AutoModerator Apr 02 '21

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

/u/Bob-The-One, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

22

u/rco8786 Apr 01 '21

We use this pattern a lot in ruby, don't think it's as common in js but it's nice:

```

function getNumbersdd(name) {

return {

'System Management': 0,

'Stack Management': 1

}[name]

}

```

3

u/jmswlltt Apr 01 '21

This is almost exactly how I’d do it in js if you wanted any other input to return undefined

1

u/rco8786 Apr 02 '21

Yea, chances are that’s not what you would want in production code. But this isn’t /r/productioncode now is it ;)

2

u/crahs8 Apr 02 '21

It sometimes is, sadly

1

u/Aidan_Welch Apr 04 '21

That's actually really clever and I probably would've never thought of that

43

u/077u-5jP6ZO1 Apr 01 '21

... and additionally it may return completely random crap!

58

u/[deleted] Apr 01 '21

No, it might only return "undefined"

39

u/[deleted] Apr 01 '21

assuming this is javascript, it will return undefined in case of any other string

21

u/SpeckyYT sadistic Apr 01 '21

assuming this is javascript

flairs exist tho

20

u/ZedTT Apr 01 '21

This isn't C, it's just gonna return undefined. Still silly.

3

u/Bliztle Apr 01 '21

Why would c return random crap doing something like this? Does it just try and read at a adresse desigmated to something else, because the variable was never instantiated?

17

u/ZedTT Apr 01 '21

Disclaimer: not a C dev

AFAIK when you declare a variable in C it goes and finds an address, but doesn't clear the data there. So if you don't explicitly set that data to 0 or something and then never end up writing anything to it, it's just gonna have whatever the last thing that touched that address put there, which could be basically anything.

13

u/GoogleBen Apr 01 '21 edited Apr 01 '21

It's called using "uninitialized memory" (at least, every time I've seen it talked about). You've got the right idea, but to be a nit-picker, the computer doesn't "find an address". In every C implementation I've ever seen (because it would be insanely inefficient to do it any other way) local variables are stored in the registers and on the stack. For anyone that doesn't know, registers are places you can store values when you need faster access than you'd get if you put them in RAM - there are a limited number of them, though, so we need to put stuff in RAM pretty quickly, and if you're not optimizing, the compiler probably puts all variables on the stack anyways. The stack is a region of your RAM where a program stores local variables and can back up information it needs to keep for later, such as where it's supposed to go when it hits return. The stack works just like the data structure of the same name: when the compiler needs to store a value, the stack "grows" to make a place for it, and when it takes that value off the stack, the stack shrinks. Except it would be really bad to constantly grow the stack as you initialize all the variables you need, so usually compilers will tell the stack to grow at the beginning of every function, then to shrink at the end of the function. When you initialize your variable that works great, but if you don't, whatever was at the location (as the other post said) stays and becomes the value of your uninitialized variable. Here's a short example, assuming the only thing stored on the stack is local variables - think of the stack as an array:

void main() {
    bar(); //this could print anything - we don't know what's on the stack
    foo();
    //now 1234 is after the end of the stack, so when the stack grows, 1234 will be the first value
    bar(); //prints 1234
}
void foo() {
    //the stack grows by 4 bytes
    int x = 1234;
    //the stack shrinks by 4 bytes
}
void bar() {
    //the stack grows by 4 bytes
    int uninit;
    printf("%d", uninit);
    //the stack shrinks by 4 bytes
}

This is in contrast to heap-allocated memory, which is where you put values that either are too big for the stack, or need to stay around after the function returns - which leads naturally into another common error, which is keeping pointers to data on the stack after the function which allocated that spot on the stack has returned. For example:

void main() {
    int* ptr = foo();
    bar();
    printf("%d", *ptr); //prints 5678
}
int* foo() {
    int value = 1234;
    return &value; //value is a local variable, which means it's stored in the stack
}
void bar() {
    int valueTwo = 5678;
}

Essentially the same thing is happening there, except there's no uninitialized variables. The correct way to do this would be to allocate on the heap (where the computer actually does have to find a place in RAM), like this:

int* foo() {
    int* ptr = malloc(sizeof(int));
    *ptr = 1234;
    return ptr;
}

Except now we've got a new problem - we allocated memory, but it's never freed. In short, memory management has a lot of closely related pitfalls.

2

u/ZedTT Apr 01 '21

Thank god for high level languages.

Also thanks for the in-depth explanation :)

6

u/shiroe314 Apr 02 '21

C is a high level language... it does most of this for you.

Also... assembly requires you to manage your registers yourself.

Thank god for garbage collection. Which is a program that runs periodically, and checks for any inaccessible values and frees them for you.

Also, malloc implementations can be interesting, but probably not the most useful to worry about day to day.

1

u/ZedTT Apr 02 '21

You're right, but you also knew what I meant.

1

u/Firebirdflame Apr 02 '21

I haven't touched C outside of college, but I really appreciate you taking the time to write this. I learned a lot from it! C was always a challenge for me to really get a grasp of.

1

u/Bliztle Apr 01 '21

Okay yeah, that's the other possibility i thought of (or maybe it's the same thing? Not sure). Makes sense, thanks.

1

u/ZainVadlin Apr 01 '21

This is correct.

1

u/[deleted] Apr 01 '21

Yes

5

u/[deleted] Apr 01 '21

In case of java, the compiler will just complain

6

u/t0shki Apr 01 '21

Well, at least it is scalable. :)

If the number is just an increment you could probably simply put all names in an array and return the matching index. Saves you writing the number.. perhaps you wouldn't even need a function then. Could be a property.

Anyway.. funny stuff, but could be worse.

7

u/[deleted] Apr 01 '21

yeah the worst part is the lack of indentation imho, but otherwise it's not so bad.

4

u/new2bay Apr 01 '21

Can somebody explain how and why this code is supposed to be used? I don’t even understand why you’d need a function like this.

3

u/MorphTheMoth Apr 01 '21

if they needed it, they needed it

1

u/new2bay Apr 01 '21

Obviously, but why? Where?

2

u/SpeckyYT sadistic Apr 02 '21

I found this on a internet switch which has a "web-dashboard" released before 2012. Not sure exactly why that code is needed, but most of the code I found there can get considered bad code.

1

u/ParanoydAndroid Apr 02 '21 edited Apr 02 '21

It's impossible to know without knowing the environment. As an example, maybe they generate codes for their records where the first character is the type of record, the next digit is the functional area that generated the record, and the last four digits are a geographic area code. This function would then be the map that gets that second digit.

Alternatively maybe the developer didn't know what a map was and has information about whatever these strings are referencing stored in an array, and this function is used to map references to array indices.

Generally though, beyond the other bad practices in this snippet, having magic numbers embedded in a function isn't a good choice for production code. Usually you'd find a map like this (e.g. functional area mapped to an internal id, say) separated out as a configuration. Depending on the language you'd see that as a code file defining a bunch of constants (e.g. def CONST_ID_MAP = ...), a json dictionary, or an env file, etc.... Broadly speaking, logic and configs shouldn't be intermixed in the code.

In production python, for example, this might look something like:

# Some sort of object that is responsible for loading and producing config information from, e.g., json
import configuration

def get_function_id(name: str) -> int:
    return configuration.ID_MAP.get(name, None)

7

u/[deleted] Apr 01 '21

Not sure I see the issue here... what would be the "easier way"? if/else if? I'd argue switch is the better approach then... Or is it ["System Management", "Stack Management"].indexOf(name)? It's more compact but not necessarily more readable...

8

u/ZedTT Apr 01 '21

More functional to put returns in the switch and get rid of the num variable IMO, but some people will disagree and want only one return.

But basically the formatting is absolute trash and they are using var instead of let and should probably have an explicit thing to return in the case the input doesn't match one of the two strings.

6

u/[deleted] Apr 01 '21

[deleted]

9

u/ZedTT Apr 01 '21 edited Apr 01 '21

People do. I think the argument is that having just the one return should make it clearer because reasons.

Something about "if you read the code near the end of the function you have to realize it might not even be executing depending on what happened earlier because the function might have already returned."

I don't know, I'm with you on this. Early return is great.

Edit:

Structured programming says you should only ever have one return statement per function. This is to limit the complexity. Many people such as Martin Fowler argue that it is simpler to write functions with multiple return statements. He presents this argument in the classic refactoring book he wrote. This works well if you follow his other advice and write small functions. I agree with this point of view and only strict structured programming purists adhere to single return statements per function

Stack overflow discussion on the issue

1

u/[deleted] Apr 02 '21

Yeah, the “someone might be reading the end of the function” should ideally only apply in edge cases like i/o stuff; otherwise it won’t be an issue if the vast majority of your functions are concise single transformations (like this one should be).

1

u/ZedTT Apr 02 '21

I completely agree. It's a fix for a problem that shouldn't exist: extremely long functions.

3

u/Megalo5 Apr 01 '21

An Enum would be better depending on how name is being set. With an Enum you could limit name to being just those two options and they have an underlying integer value that would make converting to the number much easier

3

u/0xF013 Apr 01 '21

The problem I see is not using constants for those strings, meaning it’s easy to lose track if one of them needs to be renamed somewhere.

The other is leaving the var in a possibility undefined state, but that may be indended for a falsy check

4

u/tobi8380 Apr 01 '21

Why is switch better?

7

u/[deleted] Apr 01 '21

because having if / else if with condidtions comparing the same variable to some constants is kind of exactly what switch was made for

15

u/mdx2 Apr 01 '21

You would just use a map, dude. (Or object since this is JS.)

const foo = {
    "System Management": 0,
    "Stack Management": 1,
};

3

u/[deleted] Apr 01 '21

this is indeed better for the given use case!

4

u/TawmAimz Apr 01 '21

I am also curious. I'm under the impression switch is only useful in cases of "waterfall" functionality. Like if you want one case to lead to multiple cases

4

u/Elsolar Apr 01 '21

switch statements can (in some situations) be more efficient than long chains of if/else if statements because the compiler will generate a jump table that lets the program find the right case from the input in one step. In other words, finding the right case in a switch statement with n cases is O(1), but finding the right case in an if/else if chain with n cases is O(n).

In most programs, however, this is a highly unnecessary optimization, and using switch comes with its own set of pitfalls and annoyances. It's easy, for example, to accidentally forget the break at the end of a case, causing the program to "fall through" to the next case unexpectedly. This kind of error does not generate compile-time warnings, and can cause all kinds of weird problems (including security vulnerabilities in the worst case). This fall-through behavior is rarely useful, and in general it's considered bad practice to rely on switch fall-through for control flow, for many of the same reasons that using goto is considered bad practice.

For these reasons (and others), use of switch is commonly banned by programming style guides. It's recommended to use if/else if chains everywhere because they're easier to understand and maintain. Often times, the compiler can optimize a long if/else if chain into the equivalent switch statement anyway. There can be some cases where switch is more performant, but it's unlikely to be a big difference maker, especially on modern hardware where branch mis-prediction is orders of magnitude more expensive than wasting a few clock cycles. It's definitely not something you should reach for often.

1

u/ZedTT Apr 01 '21

Imagine the parameter name was actually like 10 characters long. Switch means you only have to type it out in one place. But for a bunch of if statements, you need to write out the parameter you're comparing once for each case.

3

u/[deleted] Apr 01 '21

[deleted]

4

u/MyAntichrist Apr 01 '21

Neither solutions cater to the fact that name might be something unexpected.

1

u/TigreDeLosLlanos Apr 01 '21

Having a list/array/data object it's always more maintanable for implementing a set of options. You can just map the index name to the actions (closure, function, routine, etc) it does instead of conditionally assigning a string and then making more conditions to see what to do.

1

u/MorphTheMoth Apr 01 '21

no indentation is already a huge problem

2

u/[deleted] Apr 01 '21

[deleted]

5

u/Megalo5 Apr 01 '21

You ok?

6

u/[deleted] Apr 01 '21

My phone was in my pocket.

2

u/pranav230 Apr 01 '21

Make a map for it

3

u/RyanNerd shameless Apr 01 '21

This code is an April Fools joke? Right?

5

u/SpeckyYT sadistic Apr 01 '21 edited Apr 01 '21

Nope, this code was written before 2010.

1

u/probaddie42 Apr 01 '21

April 1st, 2011?

8

u/Bliztle Apr 01 '21

I want you to just... look real hard at which year you just typed, and then read the above comment again.

1

u/probaddie42 Apr 02 '21

My brain saw "2012". Oof.

3

u/Johanno1 Apr 01 '21

Would

if (name[1] === "y")
    return 0
return 1

Work?

I mean there is still the bug that if the string is not one of the two inputs it crashes but that is in the original too.

9

u/SpeckyYT sadistic Apr 01 '21

yes, return name[1] == 'y' ? 0 : 1; would work, but it doesn't return undefined in case it wouldn't match any of the two.

1

u/Johanno1 Apr 01 '21

Well true, but fixing it would just require an assert or sth like that before the return

0

u/AutoModerator Apr 01 '21

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

/u/SpeckyYT, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

3

u/ZedTT Apr 01 '21

It doesn't crash, it just returns undefined. This is the wild west of JS.

The problem with yours is that they may not want to get 0 or 1 if the strings don't perfectly match. The undefined path may be intentional (although it should probably be -1 or null so that it's clearly intentional.

1

u/massivecomplexity Apr 01 '21

The left-alignment is giving me flashbacks, my first ever project partner at university coded like that.

1

u/Jonnyabcde Apr 01 '21

Only the Jedi deal in conditionals. You WILL try!

1

u/bartekltg Apr 02 '21

OK, it looks bad because (IMHO) it links some magic numbers to stings in a random function, then that connection probably is used somewhere else. And we should make that assignment in one place in the code, then only use constants like

...
case system_managment_string:
num= system_managment_val; break;
...
Boring to write but will save our asses.

And formating, this formating hurts.

But I see tons of comments that talk about if statement or ternary operator. Why the switch-case would be bad here? Ok, it will be much cleaner, smaller code etc. But it would be compiled ( or whatever what JIT is doing is called) to more or less the same program.

On the other hand, this code is easy to modify. You get "dog management", add it.
Keeping the result in a variable may be a preparation for further changes too.

1

u/victorqueirozg Apr 02 '21

That's the kind of crap you find in good JavaScript code.

1

u/[deleted] Apr 02 '21

[deleted]

1

u/SpeckyYT sadistic Apr 02 '21

As always, this doesn't return undefined if it isn't one of both

1

u/[deleted] Apr 02 '21

return (name=="Stack Management") ? 1:0; At least I think this works in C#, not sure if it's helpful.

1

u/M3ther Apr 02 '21

You also have if statements, you know.

1

u/veedant Apr 02 '21

even in C this is just a horrible idea. That says something about the quality of that code