r/badcode • • Apr 09 '21

js Back to basics

Post image
877 Upvotes

94 comments sorted by

282

u/ExtraOrdinary_Drew Apr 09 '21

He got me with β€œthis is a very complicated function...Wikipedia says” πŸ˜‚

179

u/shrekthethird2 Apr 09 '21

I had been coding JavaScript for 13 years and I'm appalled.

catch(exn)?

exn??

Seriously???

193

u/NutsGate Apr 09 '21

Great variable names overall...

var x

var y

var z // oh crap, reached the end, now what

// nvm, Wikipedia says there are letters the other way too, phew

var w

var u

32

u/MysteriousGreenBean Apr 09 '21

In the past (though more in math rather than in code) when I was out of variable names after "z", I just started from the beginning. So it was x, y, z, a, b, c...

2

u/Blip1966 Apr 09 '21

Not "i,j,k" after z? Interesting.

10

u/i____7d Apr 09 '21

Reminds me of when I started programming the variable naming sequence was x y z r/w

Fun times man

66

u/itbengis Apr 09 '21

At least they try{ed

65

u/paulsmithkc πŸ‘¨β€πŸ« Apr 09 '21

Disregarding the fact that this could have been done with %, the math still isn't right...

w = (Math.floor(x/2) - (x/2)) * x

The correct formula for the modulus would be...

w = (x/2) - Math.floor(x/2)

46

u/SandInMyEverywhere Apr 09 '21

%

Your mention of Modulo took me back. I'm self-taught and learning it was such a game-changer. I don't get to use it much but feels so good when I do.

12

u/AdminYak846 Apr 09 '21

Yeah it's rare use but so satisfying when possible.

6

u/AspiringCake Apr 09 '21

Even better for testing divisibility by two, use x & 1. If it's odd (i.e. the binary representing x has a 1 at the end), it'll return 1. If it's even it'll return 0.

3

u/SandInMyEverywhere Apr 09 '21

Whoa. Had no clue about Js Bitwise AND operator.... The more you know.

4

u/Blip1966 Apr 09 '21

Use it a lot in progress logging. β€œ count % 100” only report every 100 lines or whatever.

41

u/DoYouEverJustInvert Apr 09 '21

fack, we have some remainders

5

u/Rudxain let let.let = 'let' Apr 09 '21

Fatal Exception: kernel panik, binary system can't handle odd numbers, please execute man cmd for more information... oh wait you have to restart your PC, please wait while we dump an exact copy image of your RAM to disk (yes, unallocated memory won't be ignored this time, so go make a cup of coffee while you wait an eternity)

66

u/sslinky84 Apr 09 '21
 ((n & 1)!=1);

82

u/FinalGamer14 Apr 09 '21

Hey don't scare people with bitwise operations like that.

28

u/[deleted] Apr 09 '21

!(n&1)

7

u/[deleted] Apr 09 '21

Come on. I know this is basically a circle jerk sub, but you're just being silly now.

4

u/Terrain2 Apr 09 '21

too many numbers and not enough symbols

[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]][([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]][([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((!![]+[])[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+!+[]]+([]+[])[(![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(!![]+[])[+[]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]()[+!+[]+[!+[]+!+[]]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]][([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((!![]+[])[+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+([][[]]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+!+[]]+(![]+[+[]])[([![]]+[][[]])[+!+[]+[+[]]]+(!![]+[])[+[]]+(![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(![]+[])[!+[]+!+[]+!+[]]]()[+!+[]+[+[]]]+![]+(![]+[+[]])[([![]]+[][[]])[+!+[]+[+[]]]+(!![]+[])[+[]]+(![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(![]+[])[!+[]+!+[]+!+[]]]()[+!+[]+[+[]]])()[([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((![]+[+[]])[([![]]+[][[]])[+!+[]+[+[]]]+(!![]+[])[+[]]+(![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]+([![]]+[][[]])[+!+[]+[+[]]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(![]+[])[!+[]+!+[]+!+[]]]()[+!+[]+[+[]]])+[])[+!+[]]+[!+[]+!+[]+!+[]+!+[]]+[+!+[]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[+!+[]+[!+[]+!+[]+!+[]]]+([][[]]+[])[+!+[]]+([]+[])[(![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(!![]+[])[+[]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]](([]+[])[(![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(!![]+[])[+[]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]()[+!+[]+[!+[]+!+[]]])[+!+[]+[!+[]+!+[]+!+[]]]+[+!+[]]+([+[]]+![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[!+[]+!+[]+[+[]]]+([]+[])[(![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(!![]+[])[+[]]+([][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+(![]+[])[!+[]+!+[]]+(![]+[])[+!+[]]+(!![]+[])[+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]()[+!+[]+[!+[]+!+[]]])())()

82

u/Cmgeodude Apr 09 '21

If !!w

Double negatives aren't not allowed.

52

u/MrFluffyThing Apr 09 '21 edited Apr 09 '21

I appreciate the joke, sorry you are getting downvoted.

For anyone actually curious about this, it's a way you can throw something to strictly boolean in javascript by an interpreter typecasting behavior. It technically flips the "Is false" check by overloading the variable to "is NOT something that is not this" for the variable, but it does so in a very strict manor. It's a way to get something that is truly true to result as true, something that is truly false to be false. Using !!undefined will convert the value for an undefined to be false instead of throwing an error. !!<value> equal to 0 or -0 will be false but 1 and -1 will be true. NaN and '' will be false but 'false' and 'true' will both be true. This is handy when checking a value that might be any sort of data type and you just want a check on raw input and know the ways this returns a value.

Edit: I had duplicate words while originally typing my reply that might cause confusion.

38

u/[deleted] Apr 09 '21

I somehow got confused about a concept I understand by reading your comment

7

u/[deleted] Apr 09 '21

very strict manor

so I guess it's not Wayne Manor

6

u/oreo_memewagon Apr 09 '21

Fun fact: the "object is true" function in the CPython API is literally described in the official docs as "equivalent to the Python expression not not o"

9

u/420TaylorStreet Apr 09 '21

using !! to typecast to boolean is something i frequently use tho.

20

u/Hafas_ Apr 09 '21

I prefer Boolean(w) instead of !!w. It's more readable and does the same thing.

18

u/NUTTA_BUSTAH Apr 09 '21

Ah you are one of the coders that like to write readable code

get that shit out of my sub

12

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

Image Transcription: Code


function isEven(x) {
  // this is a very complicated function
  // wikipedia says
  // An integer's parity is even if it is divisble by two with no remainders left and its
  // parity is odd if its remainder is 1.
  try {
    // we start by trying to divide by two
    var y = x / 2;
    // javascript doesn't have remainders so we take the fractional part
    var z = Math.floor(y) - y;
    // and mulitply it out to get our remainder
    var w = z * x;
    // now we need to make sure no remainders are left
    var v = !!w;
    
    if (v) {
      // fack, we have some remainders
      throw RangeError("we have some remainders")
    } else {
      // whew we have no remainders so that means it's even
      return true;
    }
  } catch (exn) {
  
    if (exn instanceof RangeError) {
      // ok we had a remainder let's get out of here
      return false;
    } else {
      // unknown exception;
      throw exn;
    }
    // just to throw you all off
    throw exn;
  }
}
console.log(isEven(1));
console.log(isEven(1.4));
console.log(isEven("2"));

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!

8

u/[deleted] Apr 09 '21

Is modulus not taught at school? I remember seeing the clocks explaining that 3PM is really 15 modulo 12.

5

u/Naeio_Galaxy Apr 09 '21

They wanted to fit to the wikipedia definition for fun, I guess

1

u/jan-pona-sina Apr 09 '21

I remember the like 2 java classes I took explaining how to write getters and setters before saying anything about modulo lol

10

u/NetherFX Apr 09 '21

Basics with babscript

9

u/[deleted] Apr 09 '21

By the way, seeing all the comments... I'd rather have an over-commented code to check after my colleague than under-commented.

4

u/[deleted] Apr 09 '21

return !(x%2); ...

4

u/danielolaviobr Apr 09 '21

What theme is this?

1

u/fuloqulous Apr 09 '21

Looks close to Atom's "one dark" theme. I'm not sure what it exactly is though

3

u/[deleted] Apr 09 '21

very complicated. my head is going to explode

4

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

I'm just happy I can finally understand this memes

2

u/[deleted] Apr 09 '21

Which font is being used?

1

u/themixtergames Apr 09 '21

Looks like Menlo

1

u/Rudxain let let.let = 'let' Apr 09 '21

I think this should be posted on r/shittyprogramming . Don't worry, I was confused too

1

u/PonchoVire Apr 09 '21

Seems legit.

1

u/sakunasi1 Apr 09 '21

literally me when i started

1

u/MrVesPear Apr 09 '21

This is.... something

1

u/Zimintre Apr 09 '21

return !(x&1);

1

u/Gloomy-Factor-5706 Apr 09 '21

A. C. Xxxxxxxxx

1

u/[deleted] Apr 09 '21

Boy that's cheeky

1

u/KingJeff314 Apr 09 '21
//just to throw you all off

1

u/frederik88917 Apr 09 '21

Honestly, don't these people get taught of the % operator??

1

u/undieablecat Apr 09 '21

I wanted to stop reading on line 9: JavaScript doesn't have remainders

1

u/mgudesblat Apr 09 '21

Throw in some async/awaits for good measure

1

u/dave_thenerd Apr 15 '21

God this problem / programming meme is so pervasive that I honestly think every programing language should have an 'isEven' and an 'isOdd' function built into its standard library.

I've really got to wonder how many terrible wasteful functions like this exist in in-use released software. An operation which should take at most 25-ish CPU cycles using something sensible like:

if(num % 2 == 0)

printf("IsEven");

or if your compiler is old as sh*t and doesn't optimize very well:

if((num & 1) == 0)

printf("EVEN!\n");

bloated with costly loops and useless branching drawing the run time into the thousands of clock cycles. This operation should be the first operation taught in every programing class/tutorial, yes even before operator '+' and '=' . Sick of seeing this crap.

0

u/WurschtChopf Apr 09 '21

Huh? Does this even compile?!
the last 'throw exn' is dead code?

1

u/Naeio_Galaxy Apr 09 '21 edited Apr 09 '21

Doesn't have to compile, it's JS. And it should be able to run, no ?

And yes it's dead code, but whatever

3

u/recycle4science Apr 09 '21

Strictly speaking, javascript is compiled, but it's compiled "just in time", so errors might not cause a problem if there is no way to get to them.

0

u/Naeio_Galaxy Apr 09 '21

Well... I'm not sure of what you name "compiling" then. For me, compilation is translating from a language to another. Javascript is an interpreted language.

The interpretor might translate from JS to an internal data structure (using tokens and things of the kind). And if they can't translate to this internal data structure, it's a syntax error. But obviously this code does not have any syntax error, so it would "compile".

But if what you mean is it "compiles" when it get to the instruction and execute it, then I don't agree... The interpreter simply interprets the instructions as a list of actions he has to do, and then makes them. It's the interpreter that do what the tokens specifies, and it doesn't create binary to be executed by the computer

2

u/recycle4science Apr 10 '21 edited Apr 10 '21

There's definitely some room for interpretation with these concepts. It's a fuzzy distinction. Factually speaking, though, javascript is what's called "just in time" (JIT) compiled. You can Google it. I'm not sure if this particular snippet will have any compilation errors, but compilation errors are possible in javascript.

Edit: like, the sequence of actions you're taking about -- converting javascript into an abstract syntax tree and then executing that -- is basically what happens, but that is called compilation.

https://en.m.wikipedia.org/wiki/Just-in-time_compilation

2

u/Naeio_Galaxy Apr 10 '21

Ok, yeah you're right. Moderns browsers compile bits of code just before executing them to gain time in the execution, in particular when this bit of code is executed many times with the same input types. Now I'll know ! πŸ™‚

About the abstract syntax tree, yeah I'm ok with calling that compilation but an error here would result in a syntax error and not into a runtime error, no ? And so to answer the original question "does it compile", I thought "there is no syntax error so I don't see why".

1

u/WurschtChopf Apr 09 '21

By compiling I rather meant, that this does not look like solid code. As you confirmed, this is dead code. So why the effort? Even with js
Should be able to run, no?
- good to know, thanks :D

1

u/Naeio_Galaxy Apr 09 '21

Oh ok !! Yeah, my guess is to was not meant to be solid code. Even the try...catch to check if there is a remainder is awful

So yeah I totally agree

-3

u/Ivanchez Apr 09 '21

That's not (Visual) Basics. That's JavaScript.

-33

u/aeropl3b Apr 09 '21

This isn't so bad? The worst thing I see here is it is in JS...but that doesn't necessarily make it bad code...

52

u/atzedanjo Apr 09 '21

the function could be reduced to

return x % 2 === 0

i think

12

u/RyanNerd shameless Apr 09 '21

Pretty much. But because it's JavaScript there's no type checking so what to do if a weird-ass string gets passed in?

It could still be wrapped in a try...catch but coding by exception is a rabbit hole that I try to avoid. I won't take a contracting job if they don't use TypeScript. Not worth the time chasing down bugs due to JS crazy collessing behavior.

15

u/auto-xkcd37 Apr 09 '21

weird ass-string


Bleep-bloop, I'm a bot. This comment was inspired by xkcd#37

4

u/paulsmithkc πŸ‘¨β€πŸ« Apr 09 '21

Strings get automatically converted to numbers when you try to do math with them like %

5

u/NiteShdw Apr 09 '21

Well I would add some defensive programming like verifying the input is a number (or can be converted to a number), isn't NaN , etc... But then yeah just % 2 that sucker.

1

u/noXi0uz Apr 09 '21

throw in typescript and you don't even need to do that

4

u/NiteShdw Apr 09 '21

Typescript doesn't remove the need to validate inputs. It's a compile time check, not a runtime check. This is especially true if you don't have strict mode turned on, are interacting with an API, or interfacing with JavaScript.

1

u/noXi0uz Apr 09 '21

Tyoescript should show an error at compile time if whoever calls this function might not pass a number to it. If you're dealing with data from an API and use any, then yes, you need to write type guards. But in most cases you want to use OpenAPI code gen, which generates interfaces for you, making type guards obsolete again.

3

u/NiteShdw Apr 09 '21 edited Apr 09 '21

Disagree. There's no strict guarantee that the API conforms to their OpenAPI spec. Maybe one of their devs makes a mistake and a field starts returning null instead of a value, for example. Or maybe they changed the spec and you didn't know and didn't update your types. Now TypeScript thinks everything is great when it's not.

TypeScript also just assumed every type you give it is accurate. Look at Definitely Typed. People submit PRs to that because someone else got the types wrong.

It helps, absolutely. It's great at dev time for lots of reasons. But it is absolutely not a replacement for runtime checks. I mean there's a reason there are libraries out there that take the TypeScript metadata and convert that into runtime checks.

TypeScript compiles to JS and so your running code loses all type safety.

Edit: the biggest lesson I've learned over more than 2 decades is to always expect the unexpected. Every line of code I wrote I ask myself "what could make this go wrong or fail? What assumptions am I making?".

1

u/aeropl3b Apr 09 '21

I see you talking about a Standard being terrible about types...have you heard of WASM? You can now compile real, type safe languages into code that executes in your browser! No need to keep up on the annual standard changes in the fine print on some change notes hosted in the dark web :p /s

0

u/aeropl3b Apr 09 '21

Actually, the only extra operations here are exception handling things. And there are good reasons to wrap operations in routines to handle exceptions, I do this all the time so divide by zero stuff doesn't crash my application...

The mod operation is that collection of operations unless it is modding by 2 (not every language optimizes this). Mod by two is a bit wise operation which is much faster than a regular mod.

x <bitwise and> 1 === 0

Sorry, don't know the JS syntax for bit and...

-1

u/atzedanjo Apr 09 '21

I'd assume JS would throw a native exception if the passed in value can't be casted to a number.

-9

u/MyAntichrist Apr 09 '21

It may have an unnecessary amount of lines but each step is well documented and tells not only what but also why something has been done. That alone makes it not bad code in my book.

2

u/AdminYak846 Apr 09 '21

Except that it is unnecessarily longer than it needs to be as any developer worth their salt will convert it to be shorter and probably remove the function entirely unless you need to check for non-numeric values being passed in.

1

u/MyAntichrist Apr 09 '21

I guess a lot of people got me wrong here, I would probably refactor that function too. That still doesn't make it bad per se, because of the points I've mentioned - the code is well documented and the intent is clear. And that is something a lot of senior developers still can't do right, and that I teach my juniors as early as possible so they don't belong in that category later on in their career.

Just because there's more elegant ways to solve a problem doesn't make every other solution bad. A simple `return x % 2 === 0` may solve the problem in one line but there's a couple of issues like non-defensive programming here.

0

u/[deleted] Apr 09 '21

[deleted]

1

u/simon_zyx Apr 09 '21

it is considered bad practice to assign implicit globals by not using let, const or var. It makes the code harder to read and maintain. Also this is only allowed in loose mode.

-13

u/Caelestos Apr 09 '21

Easy fix don't program in javascript

0

u/[deleted] Apr 09 '21

[deleted]

5

u/toi80QC Apr 09 '21

0**%** chance he knows...

1

u/Bjornoo Apr 09 '21

0

u/[deleted] Apr 09 '21

[deleted]

1

u/Bjornoo Apr 09 '21

I woosh anyone who deserved a woosh, and you sir deserves a woosh. I am the woosher of locksley. I woosh the rich and explain to the poor.

0

u/[deleted] Apr 09 '21

[deleted]

1

u/smuttenDK Apr 09 '21

Because people have just downvoted without explanation. The entire function can be replaced with a mod 2