Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Code Questions #19

Closed
qwo opened this issue Dec 16, 2014 · 14 comments
Closed

Code Questions #19

qwo opened this issue Dec 16, 2014 · 14 comments
Labels

Comments

@qwo
Copy link
Member

qwo commented Dec 16, 2014

This is good code but is a bit hard to read.

Any reason why ternary operators instead of if/else or a switch case in this instance?

@ryayak1460

https://github.com/Code4HR/hrt-bus-text-my-bus/blob/master/app.js#L36-L55

@qwo qwo added the question label Dec 16, 2014
@qwo
Copy link
Member Author

qwo commented Dec 16, 2014

also on a thread about code questions

https://github.com/Code4HR/hrt-bus-text-my-bus/blob/master/app.js#L138

is this finding the midway distance between here and richmond?

@qwo
Copy link
Member Author

qwo commented Dec 17, 2014

@ghost
Copy link

ghost commented Dec 17, 2014

Okay, here we go:

  • Any reason why ternary operators instead of if/else or a switch case in this instance?

Because I love functional programming and prefer expressions over statements to the point where I would use ternary operators in excess to ensure type/value safety, despite legibility problems. In other languages, the same semantics doesn't have such an awkward syntax.

Also, in this particular instance, it reduces code duplication and the need for a local variable.

It's a disease. You can absolutely change it if you prefer.

  • is this finding the midway distance between here and richmond?

Yes.

  • also i think the long lat point points to antarctica.

The coordinates actually do point to Richmond, the HRT bus API reverses latitude and longitude from what you'd normally expect.

  • I also got a key(?)

Sweet! I'll have to add that when I get home tonight. 😺

  • so yay?

\o/!

@qwo
Copy link
Member Author

qwo commented Dec 17, 2014

functional

upon first glance i thought the equivalent was

if(help)
  ... 
if(address)
   ... 
if(stop) 
   ... 
else(error) 

but i was wrong and the evaluation is as if i was expected but in a ternary syntax so i dont think a rewrite is necessary. Its something new for my eyes.

if(help)
else if (stop)
else if (address)
else (error)

Second question is why is it distancing between here and richmond? i'm curious whats the logic between that.

is this finding the midway distance between here and richmond?
>Yes.

@ghost
Copy link

ghost commented Dec 18, 2014

I forgot the exact reason, but the whole purpose came from trying to find the closest stop, and so I chose Richmond as an extrema in the fold operation.

@qwo qwo changed the title Style of ternary operators over traditional Code Questions Dec 18, 2014
@qwo
Copy link
Member Author

qwo commented Dec 18, 2014

this regular expression is scary

https://github.com/Code4HR/hrt-bus-text-my-bus/blob/master/app.js#L79

@ghost
Copy link

ghost commented Dec 18, 2014

:feelsgood:

@qwo
Copy link
Member Author

qwo commented Dec 18, 2014

so i wrote the test

"Should not fail on 150 Granby St" and it fails! mm i probably have to stub that out and find out why that particular POI fails. any ideas?

@qwo
Copy link
Member Author

qwo commented Dec 18, 2014

i also feel if i write against stop numbers, if those stops or routes change the tests break.. but that could be good? i dunno false positives. what is a reliable way to test?

@ghost
Copy link

ghost commented Dec 18, 2014

Well, it sounds like you want to test live data, so you'd use a functional test for sure, so I would choose a stop which would have a small likelihood of actually changing or going away. Other than that, I have no real good suggestions.

EDIT: Oh wait... you'd write tests for both stop numbers and addresses, no matter what. Same suggestion above applies, without further knowledge of how to improve those kinds of tests.

@ghost
Copy link

ghost commented Dec 18, 2014

Also, I'm absolutely aware that that regular expression will terrify anyone who observes it. I had no other immediate way of how to determine whether the citizen entered a stop, address, or garbage, and I want to try and adhere to LANGSEC (this speech got me into the concept) for these particular kinds of situations.

@kmcurry
Copy link
Member

kmcurry commented Dec 18, 2014

Hairy regexs are AWESOME and I hate them.

On Wed, Dec 17, 2014 at 10:49 PM, Ryan Y. [email protected] wrote:

Also, I'm absolutely aware that that regular expression will terrify
anyone who observes it. I had no other immediate way of how to determine
whether the citizen entered a stop, address, or garbage, and I want to try
and adhere to LANGSEC http://langsec.org/ (this speech
https://www.youtube.com/watch?v=3kEfedtQVOY got me into the concept)
for these particular kinds of situations.


Reply to this email directly or view it on GitHub
#19 (comment)
.

Kevin Curry
Virginia Beach, VA

3Scape begins @ http://3Scape.me

@ghost
Copy link

ghost commented Dec 18, 2014

👍

@qwo
Copy link
Member Author

qwo commented Dec 18, 2014

Hairy Regex do things like this
isAddress Function

address="150 Granby"
 /^\s*(\d*)\s*(([a-z]+[.]?|\d*(1st|[23]n?d|[4-9]th))\s*)+(([a-z]+[.]?)\s*,?)?\s*(([a-z]*\s*)*,?)?\s*(V(irgini)?a)?\s*$/i.test(address)
>true
address="150 Granby Norfolk Va 23507"
 /^\s*(\d*)\s*(([a-z]+[.]?|\d*(1st|[23]n?d|[4-9]th))\s*)+(([a-z]+[.]?)\s*,?)?\s*(([a-z]*\s*)*,?)?\s*(V(irgini)?a)?\s*$/i.test(address)
>false

@qwo qwo closed this as completed Jan 8, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants