Want cleaner code? Use the rule of six
41–50 of 352 posts
Re: Want cleaner code? Use the rule of six
#42 s.split('?')[1]
.split('&')[-3:]
.map(lambda x: x.split('=')[1])
Unfortunately, that's not how Pythons map(), len() and such were designed.Re: Want cleaner code? Use the rule of six
#43Clean code is not just a few rules about how to write a line. You can write nice lines that still don't make sense and amount to shit code
Re: Want cleaner code? Use the rule of six
#44I don't necessarily agree with the step of putting the code in a separate function; that often works, but just as often makes it so that the code can't be read top-to-bottom anymore which hurts readability. In this case there's, I think, a better alternative; the equivalent-ish code in Ruby for the example code here would be something like this: values = s .partition('?')[-1] .split('&') .map { |key_value| key_value.…
Re: Want cleaner code? Use the rule of six
#45I don't necessarily agree with the step of putting the code in a separate function; that often works, but just as often makes it so that the code can't be read top-to-bottom anymore which hurts readability. In this case there's, I think, a better alternative; the equivalent-ish code in Ruby for the example code here would be something like this: values = s .partition('?')[-1] .split('&') .map { |key_value| key_value.…
Re: Want cleaner code? Use the rule of six
#46Re: Want cleaner code? Use the rule of six
#47Re: Want cleaner code? Use the rule of six
#48We break everything down and then we reach one of the most difficult problems in software engineering: Coming up with good and short names for all these extra intermediate variables and functions.
Here, "query_params" means "extract the last three query parameters, raw (i.e. not unescaped and not broken into key-value pairs)." The transformation shown makes precisely nothing more readable or easy to understand. "The second argument to map()" is just as easy for your brain to group into a black box to be analyzed later as a call to an opaque "query_params" function that you need to read the implementation of to really understand what the code is actually doing.
Of course sometimes it's the best solution to just extract local helper functions, especially if the actual function just becomes too unwieldy and/or the helpers are called from more than one place, but in general I try to extract things that do something more general than the thing I'm extracting it from and have an interface / a purpose that's easy to understand and describe on its own.
To stay with the example, actually extracting the query parameters would be a generic, extractable utility. Half-extracting the last three parameters because the function I'm writing needs precisely that for some reason, is a local helper function, and I'd only extract it if there's a good reason, certainly not to make an already trivial function no easier to read.
Re: Want cleaner code? Use the rule of six
#49Give mysterious things room. In this case the most mysterious is [-3:]. That, together with the split, should have it's own line or maybe even multiple (function declaration, comment).
Right?! That should instead be -len("foo") or -NUM_PREFIX_PARAMS