3 ms·
Your mileage clearly varies, but I found the transformed example much easier to understand. While I suspected it was parsing a query string from the initial cod
by overgard 4y ago
Your mileage clearly varies, but I found the transformed example much easier to understand. While I suspected it was parsing a query string from the initial code, having that stated explicitly in the variable removed the guessing. I think the main problem is he just didn't go far enough, there was still more to deconstruct.
I suppose the function to parse the query string could have been better, its name isn't very descriptive, and the method with which it parsed wasn't very obvious either (I'd expect to get back a dict or a list of key/value tuples, not a list of strings)
I know a lot of programmers are against comments, but I also think this is exactly the kind of code where a comment is handy..., the purpose of the [-3:] part wasn't obvious to me at all.
- mike_hock 4y agoIt would presumably be evident from the context in which the `map` call appears what its purpose is.
- overgard 4y agoAnd yet, I know python and have 15 years of professional experience coding, and (in the limited time I wanted to spend on it), I still don't exactly know what he was trying to accomplish. So congrats if you got it, but I'm sticking with my claim that it's fairly non-obvious. I understand that it's pulling out the last three elements. I have no idea why it's pulling out the last three elements, what purpose that serves and why it's desirable. If those elements were placed in a well named variable, I suspect it'd give a hint as to why the code is doing that. Alas.
- mike_hock 4y agoThat's precisely what we should expect from the context of the map call, but that context is outside the scope of the example.
- aidos 4y agoFor me that's the issue with this whole example; it kinda misses the woods for the trees. # not interesting (also, use stdlib instead) def extract_url_params(url: str) -> dict[str, str]: params = url.split('?')[1].split('&') return = dict(param.split('=') for param in params) # This is the thing that needs explaining! final_3_param_values = list(extract_url_params(url).values())[-3:] By decomposing the problem in a more natural way, you can entirely direct the reader's attention to the thing that's causing the dissonance. For some reason it's just the the final 3 values they want (both weird requirements). Make that clear from the code!
- philipwhiuk 4y ago> For some reason it's just the the final 3 values they want (both weird requirements). Make that clear from the code! Yeh, I mean the name should really tell you what those expected to be. `hopefully_auth_param_values` is almost a better name