New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
getProxyFromURI Extraction Refactor #1346
getProxyFromURI Extraction Refactor #1346
Conversation
YES!!! Love the concept of refactoring this out. I'm not sure I like the implementation though. Granted the code you're refactoring was previously a complete mess, but the use of |
I like the concept, but I agree with Fred, just use I'm also not sold on the amount of functional decomposition going on for relatively simple operations that are only done once - can you explain why you took this approach, and maybe we can find a better balance between the current code and the proposed changes. I want to see some more tests of the proxy behavior before we merge this. I'll add that to a PR I'm working on which should be ready early next week. |
The idea of Overall the The rest of the code I can explain in more detail. Any function that I have prefixed with The
Naming is subject to change for better, more expressive names, but what's there is okay. |
Overall what I did here was method extraction, from the large body of code that was |
The idea is a good one: However:
Right now I'd take a PR to split out the existing logic into a new file with minimal changes (and I'd probably name it |
I agree The existing code is monolithic and just nests branching logic when it needs to. Though it doesn't need to do it that way, and that's why I started breaking things out. That being said, I rather decouple things by default, sometimes a little over-engineered ;), than just leave things as they were. Overall, the whole point of breaking out the code is to refactor it to a "better" state. My suggestion would be to:
@nylen thoughts? |
Yeah, I agree there is a too much |
Yeah we can all agree that Im in the middle of applying the changes we just agreed on, I think the next revision will be much nicer, now that I can see everything in little pieces I can notice more similarities and conflate things. Thank you for all the feedback! |
@nylen @FredKSchott made some changes. Tell me what you think. |
No love :(. @simov @nylen @FredKSchott anyone? |
I really enjoyed this conversation 👍 I really like the fact that all of the code now is in one file, and I'm not sure that the I think your code gets a bit overly verbose, most of your functions (and they are alot), I think except two of them, are used only once. Do you really thing that this peace of code is ever going to be re-used in some way? Without being familiar with that part of the code (the old function) have too much local variables, also three levels of nesting Having too much function requires too much context switching and remembering what each and every function and tiny scope in it do. So you must strike the balance. Again I'm not familiar with that part of the code but try to look at it in terms of what's get done, not in terms of each and every single line. |
I appreciate the feedback, but I have a couple of concerns. I don't think there's a "Lines of Code to Proper Amount of Functions" equation and I really don't think that shortness of code justifies that its better. But I will contend that the only complexity I added to the getProxyFromURI function, aside from shortCircuit which is removed now, were function helpers. In conclusion I would say that each function is a good bite size, and can be debugged and understood easily. The true beauty is that the names to the functions are hopefully expressive enough to where one can read the the program and know what it is trying to do. Without knowing each internal implementation detail, which the original was almost the opposite, you would see all the details and then translate that to what's trying to do. What are your thoughts on that? Does that make sense? I could totally be wrong, but I feel that I am coming from a good place. |
The functions per code was exactly as your statement a few post above, when you said that you sometimes overly engineer things and rather decouple them - just a point of view. So how the added complexity can be justified? As for explaining me the encapsulation and stuff - do we really need that in this case? Hopefully I'm not getting too annoying with this, but you asked me in the first place :) |
These simple operations don't need names. We're not trying to prescribe coding style mathematically. However, I think this is a good rule of thumb: if a function is only referenced once and it is one line long, then it should always be inlined! I hope this will help explain why. Here's another way to give the operation of "get the http proxy" a name: if (uri.protocol === 'http:') {
// get the http proxy setting
return process.env.HTTP_PROXY || process.env.http_proxy
} This comment is unnecessary because it is already pretty obvious what is going on from the code. I could see adding a link to some spec or explanation that these environment variables are commonly used to specify proxies, but really, someone who doesn't know that has no business changing this code. Pulling this logic out into its own function has an additional major drawback: it requires the human reader of the code to switch context. Multiply this by 10 or so times of using this technique, and you have a piece of code that (to me) is very difficult to reason about because it now requires simulating a fairly complicated call stack in your head. I was the one who reviewed and merged the original PR for this change (#1096). I caught a bug with the line I get what you're going for here: decomposing this logic into functionally pure units which clearly describe what they are doing. We all know that decomposing things into functions is a good thing, and has lots of other benefits like testability, but this is too much decomposition. Practicality (the ability for a human like me with limited brainpower to quickly review an unfamiliar piece of code) trumps beauty (functional purity and declarative style). So, I'd accept a PR which makes minimal changes to the existing code, and really I'm not even sold on that - 3-4 levels of |
To be honest, I feel like you have good points @nylen. But let me pick your brain a bit more. That's overall the decision I made, that we're at a disagreement with, right? I'll make more minimal changes to the original code and get back to you guys. Thanks again @nylen @simov for your patience, I know I am stubborn, but I won't hold this up anymore. |
@seanstrom I like what you're trying to do here, but as a reader jumping into this new file (its changed a bit since I last saw it) I have trouble navigating and understanding it. Even though my only concern is with the main, bottom function it was still a frustrating experience having to jump up and down across the file to see what's going on. I wouldn't block this from being merged, in many ways this is good and clean code, and its certainly not bad code. But I did want to share my experience trying to understand it. |
@FredKSchott did you review the latest code, I just updated it a second ago |
Whoops! No I did not, looking now :) |
New solution looks perfect, imo. Awesome stuff @seanstrom! |
Hooray! @FredKSchott your cheers put me in a good mood! |
Ah, yeah this is good stuff, thanks. Leaving a couple notes now, none of which will block me from merging (assuming you will squash these commits together when we're done). |
} | ||
|
||
function noProxyZoneObject(zone) { | ||
zone = zone.trim().toLowerCase() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about calling this parseNoProxyZone
, then you can do ...
A couple of things I really like about this:
|
|
||
// if the noProxy is a wildcard then return null | ||
|
||
if(noProxyIsWildCard) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add space in if (
@seanstrom this code is looking good to me 👍 good job everyone involved. EDIT: Oh and btw, the end result is exactly 3 functions besides the exported one 😛 |
I wasn't sold on the refactoring originally but this is some pretty code. Thanks @seanstrom. |
getProxyFromURI Extraction Refactor
@simov hahahahaha i didn't even realize it mapped to what you mentioned early. |
The getProxyFromURI function seems like a good candidate to remove from the
request.js file. That file is bloated and this fits well as a module.
This is still in need of a few more changes, but I figured people can start reviewing.
@simov @FredKSchott @nylen review?