Killing time while I wait for my birthday drink(s). I am prowling around a large java code base, looking at code I regard as "good", and partly picking nits and partly re-envisioning it in Kotlin idiom.
It is fun, sometimes, to look at bad code from lousy designers. But I am enjoying looking at reasonably good code from a designer I have a lot of respect for. Feels like it's even more fun.
A f'rinstance nit: don't use inline if's to handle pre-configured optional steps in an algorithm. Use a Strategy constructed at configure-time.
Example: some matchers for incoming request paths want to strip trailing slashes from the path before attempting the match. This is either configured at constructor time, or shortly thereafter, and is a fire-and-forget configuation choice.
So in your code to do this, you write:
function match(input) {
if(strippingTrailingSlashes) {
input = // .. whatever that takes.
}
// ... test for a match
}
A better choice? Define a strategies for stripping or not stripping slashes. When the value is configured, instantiate one of those strategies. Now, when you call match, *always* let the strategy pre-cook your input.
function match(input) {
input = slashHander.handle( input )
// ... test for a match
}
Somewhat weak names, but it's the concept that's important here. What are you getting?
1) Your code for match is now non-branching. 2) Your tests for the strategies don't have to call whoever's doing the matching. 3) In some cases, you'll actually get a performance boost.
In some real-world cases, there are a half-dozen or more of these optional "pre-cook" or "post-cook" options. In that case, consider making the Strategy's be decorators, so you can chain them.
A serious case? The splitpane in JavaFx. It has an Orientation field, which defines whether we're positioning its children horizontally or vertically. The algorithm, which stretches across several methods, tests that Orientation value over and over again, a dozen times.
The thing is? That Orientation *never* changes during a run of the algorithm. (It can change during the lifetime of the object, but not during a run of the layout.)
But there are dozens ternary operators all through the code, effectively if's, that are saying orientation==Vertical?y:x. It makes the code incredibly noisy and hard to understand.
A strategy would make the code far easier to grasp and change. There are two strategies, one for VERTICAL, one for HORIZONTAL. They could simply *always* be called, and will always do the right thing.
(There's an even better answer, btw, having almost nothing to do with this topic. Have a Stategy that does/doesn't swap x and y on the way in and the way out. Now you only call that Strategy at the beginning and the ending of layout.)
• • •
Missing some Tweet in this thread? You can try to
force a refresh
I have been mulling, stalling, hedging, thinking, strategizing, so on and so forth, on a new project I've had in mind, for at least six months.
I think I'm about to pull the trigger on it.
And I'm not even gonna tell you what it is.
Because all the mulling, stalling, hedging, etc etc et fucking cetera had almost nothing to do with the geekery.
I make content for a living, or, more correctly, I make content for a non-living. I have project after project, three of them alive currently, to serve as the base and motive for my content.
I read in bars, or anyway, I used to read in bars, when bars were a place I could go to. I miss bars terribly, and I miss reading in bars, too.
I like bar culture. And, tho I am of course a junkie, I have spent many an hour in bars, dead sober, reading my book and drinking my Diet Coke.
I have a friend, Sandy, who was the bartender at a place that was once my local. And I went in two or three times a week. I'd be there for very long stretches, six or eight hours, even.
Avoiding fakes, I'mo telegraph this, cuz so many have asked, but the truth is February is my personal ebb-tide, and as much as I wish I could lay it out at the primer level, I simply don't have that in me right now.
Start by knowing who's awkward and who's not, and why. Identify which collaborators are awkward. You can read about awkwardness here:
Now go look at your function that needs a fake of that awkward collaborator. It needs that fake because the awkward call is surrounded by logic you need to test.
True story: Eighteen or so years ago, I had a gig rolling code at an engineering company. We were writing a windows app using Microsoft Foundation Classes to drive a TTY interface to a box of various radio hardware junk.
I was gigged in by a guy I'd taught a c;ass (in MFC) to, because he liked that I knew my shit, and he loved that I spoke openly about joy, right in the classroom. right in front of God and everybody.
G amd I got along great. We were both heavy smokers and committed drinkers and hardcore software geeks. We spent many an evening after work, just hanging out, talking about geekery, smoking cigarettes, and drinking like fools.
The Dreaming was the first album I heard from Bush. It is stunning. I mean, every take, it is stunning.
Bush is one of the greatest "I may in fact be hopelessly insane" singers of all time, but whackos are a dime-a-dozen, what's astonishing about her work is how she works to give you the *grounding* you need to hear the insanity.