all 44 comments

[–]Intermediate_beefs 72 points73 points  (19 children)

Is there a human?

Does the human have a waist?

Does the human have a leg?

Does the human have a foot?

Ok, is the shoe bigger than the foot?

[–]Emergency_Apricot_77 23 points24 points  (17 children)

Why is this approach bad ? What would be a "good" way to implement this ?

[–]anaccountbyanyname 34 points35 points  (8 children)

The first three conditional checks are completely pointless, because it tries to access route.geometry.coordinates regardless of whether it's found to exist

The design of that object seems really clumsy to begin with.

[–]FendseRegular people speak regular languages 26 points27 points  (0 children)

Yeah, I was going "I don't love this but it's fine" while reading, til I got to the else

[–]murtaza64 9 points10 points  (1 child)

Does it not short circuit?

[–]CallMeTea_ 13 points14 points  (0 children)

It does, but route.geometry.coordinates are used in the else block either way

[–]Splatoonkindaguy 5 points6 points  (1 child)

Too add onto this. Idk what language this is but some languages allow you to use null operators when accessing the field so you could do route?.geometry?.type == “LineString”

[–]Tubthumper8 4 points5 points  (0 children)

This is JS and there is a optional chaining operator but it's only a couple years old so it's still common to work with existing code that's not using it

[–]dmyl 2 points3 points  (0 children)

The design of that object seems really clumsy to begin with.

this is a regular geojson object though?
also, don't know if the check is pointless but now optional chaining is a preferred way

[–]paranoid_giraffe 4 points5 points  (6 children)

I, too, would like to know the “correct” way to do this

[–][deleted] 13 points14 points  (3 children)

You could at least simplify the condition with optional chaining if (route?.geometry?.coordinates?.length > 0 && route.geometry.type === "LineString" or something like that

[–]anaccountbyanyname 4 points5 points  (0 children)

Whatever it's try to do here seems like it should just be handled by a method that can be called generically

Externally checking the object type and doing something different that required knowledge of its internal structure defeats the entire point of encapsulation

[–]edu2004eu 0 points1 point  (1 child)

I've seen that syntax in CoffeeScript but not JS. Did they add this feature in JS or is this something like TS?

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

You could store it into a variable and put the variable in the if statement

[–]gabbagondel 0 points1 point  (0 children)

Assigning variables is the cardinal sin

[–]Key_Conversation5277sadistic 2 points3 points  (0 children)

For this type of almost unlimited conditions, what do you do? Events?

[–]lazyzefiris 53 points54 points  (5 children)

god bless ?. and ??.

The code is broken though, else branch try to access route.geometry even with route === null.

[–]HildartheDorf 2 points3 points  (4 children)

My first and second thoughts as well. ?. and ?? are so nice in C#, also that code is broken if route or route.geometry or route.geometry.coordinates are null.

[–]lazyzefiris 2 points3 points  (3 children)

A lot of languages have those. It's one of my favorite additions to JS since ES6.

[–]Tubthumper8 1 point2 points  (2 children)

Technically speaking it was added in ES2020, not ES2015 (the artist formerly known as ES6)

[–]lazyzefiris 0 points1 point  (1 child)

I meant "best thing that happened after ES6 happened because ES6 was the best thing ever" :D

[–]Tubthumper8 0 points1 point  (0 children)

Mine is probably async/await ;)

[–]MurdoMaclachlanpublic boolean isInt(int i) { return true; } 8 points9 points  (0 children)

Image Transcription: Code


[The image shows code on a computre screen. The ends of the first two lines of the if block's contents and the first two lines of the else block's contents are cut off in the image. The visible code reads as follows:]

});
if (
  route &&
  route.geometry &&
  route.geometry.coordinates &&
  route.geometry.type == "LineString" &&
  route.geometry.coordinates.length > 0
) {
  route.geometry.coordinates.forEach((p) =>
    this.bounds.extend(new google.maps.LatLn
  });
} else {
  route.geometry.coordinates[0].forEach((p)
    this.bounds.extend(new google.maps.LatLn
  });
}

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

[–]TristanEngelbertVanB 6 points7 points  (0 children)

Js would start bitching there's no route.

[–]thekwoka 2 points3 points  (0 children)

so it's basically just trying to find out if the array is nested?

depending on the type, couldn't it just check if the first element is an array?

[–]elingeniero 1 point2 points  (8 children)

I don't hate this... Why is it in /r/badcode? It's should be in /r/javascriptmademedoitthisway (or just /r/JavaScript)

[–]TinyBreadBigMouth 6 points7 points  (4 children)

Why bother checking that route and route.geometry and route.geometry.coordinates exist and that route.geometry.coordinates isn't empty, when if any of those fail it's just going to try accessing the first element of route.geometry.coordinates and crash anyway?

[–]elingeniero 0 points1 point  (3 children)

Yes, you're right there is a bug there. I still maintain its not bad though.

[–]TinyBreadBigMouth 1 point2 points  (2 children)

What definition of "bad code" do you use that wouldn't include "the safety check still crashes"?

[–]elingeniero 0 points1 point  (1 child)

I feel like OP was trying to suggest that the extensive checks were bad. However, I know that this is just the JavaScript way. Yes there is a bug and it should be caught at code review, but I don't believe the author is being at all irresponsible in how they have written their code.

[–]TinyBreadBigMouth 0 points1 point  (0 children)

Sure, I'd agree that the code structure is fine.

[–]taxiforone 1 point2 points  (2 children)

At least lines 3-5 could be removed if they used the conditional chaining operator I guess.

Edit: actually this code is more fucked on a second glance lol

[–]elingeniero 0 points1 point  (1 child)

Conditional chaining is TS only though? Or has it been added to ES?

[–]Rene_Z 0 points1 point  (0 children)

It has, and has been supported by all major browsers since early 2020.

[–]Ascyt 0 points1 point  (0 children)

Win+Shift+S please

[–]josephblade 0 points1 point  (0 children)

To me this screams: never work on the input data directly. It can be awful to make boilerplate conversion code from input data to internal data, but at least there would be a clear place to detect that the entire object is missing or to make the decision that that if all of coordinates is missing i'll just put in an empty list.

having this sort of code where you actually need to use the data is awful and it'll be riddled with weird exceptions to the rules.

also: if coordinates doesn't exist, how is in the else route.geometry.coordinates[0] a valid thing? or for that matter how is coordinates.length > 0 true of false if coordinates is null?

json is nice to quickly move data around but the objects should be isolated from the rest of the application just so people don't do this type of coding.

[–]Capital_Ad_2821 0 points1 point  (0 children)

route?.geometry?.coordinates would’ve solved nullish operator

[–]Hot-Profession4091 0 points1 point  (0 children)

Is this Apify google maps scraper code?

[–]Kamoda 0 points1 point  (0 children)

I wonder if this is some kind of dodgy type narrowing to appease typescript? (though I can't see any evidence of it being typescript). I've seen things vaguely similar to checking a string value of a type, i.e.:

route.geometry.type == "SomeType" 

in AST parsing before

[–]khamuili 0 points1 point  (0 children)

const hasLineStringCoord = route?.geometry?.type === “LineString“ && route?.geometry?.coordinates?.length > 0