UWP HackerNews Reader - OOP

The HackerNews Reader app is a project I'm using to practice and beat new concepts into my head. It's not quite proof of concept level - because I understand the concepts; it's a playground of sorts.
Originally it was a place to practice TDD and implement the now named Hotel pattern.

After reading Elegant Objects my mental model of how to write code has changed. It's almost as mind altering as Fred George's OOP Bootcamp. It would have been if I'd read it first; but I'm not sure I'd have gotten as much from it if I'd read it before the bootcamp.

Now that I have; I wanted to practice this style. I'm utilizing it at work for our UWP app; and I need to practice on my own to understand HOW. This is new; and the less floundering I do at work; the better that product will be.

This is my 10th post written for UWP and this is where everything pretty much changed for me in this pursuit.

The repo for the existing work for [HackerNewsReaderUwp] has been replaced by HackerNewsReaderUwpEo.
This new repo was started from scratch. No existing code used. I even grabbed the URL's fresh from the HackerNews Github.

I'm not 100% what everything looked like at what point. I iterated very quickly. Rewriting or throwing away entire classes at, mostly, a whim.

I started very imperative; step by step operations:

string jsonString = await new HttpClient().GetStringAsync("url was here");
JObject jObject = JObject.Parse(jsonString);
string title = jObject.Value<string>("title")
title.Should().Be("some title value")

Then I started teasing out objects.
I created a GetClient which performs a GET. This implements the Client interface (sorry C# standards; I'm not a fan of the I prefix)
A this point Client looked a lot like.

public interface Client
    Task<Text> Text();

The process to slowly tease out classes and create an interface for everything didn't take long at all.
It's daunting though.
I spent a few days shying away from diving in because I wasn't, and am still not entirely, sure how to do it. But from this short cycle of being able to throw the same level of functionality in such clean and simple code... I'm sold.

This is the way to do it.

I'll cite Yegor's book as the primary mental shift into this Object Oriented Programming Paradigm; but it's not his book alone.
I had to be primed. By the desire to do better, the recognition that I can be better, and by the drive to improve my code quality. The other books I've read have helped plant seeds. Design Patterns, Implementation Patterns, Refactoring, Refactoring to Patterns, Clean Code, The Clean Coder... There's a lot of books I've read that have all added a little bit to the mix. Yegor's finally showed how all those tie together.
Now that I've seen how the Object Thinking model works... Hell yea! I figure I'll re-write android soon. Shouldn't be hard... Even though it's got a few extra features.

This is mostly to say that; having gone through a mental shift of how to do software development; It's a far faster, better, clearer process. I hope to never use "easier" as it's quite challenging to think this way. It's much harder than "do x, do y, do z". It's abdicating control to some other pieces... sure you wrote them; but you can't see what's being done RIGHT THERE.

Once this style becomes reflex; it'll be easier to produce things. It'll be easier to think like this; overall it'll be easier; but the mental shift to it is challenging.

I'm going to the extreme side of it; which helps beat it into my head. Regardless of how well I can transform code into this OOP; I need to be able to identify when something ... smells.

Once you can detect the smells, you'll know there's something to look for.

I didn't expect it to be so quick and easy to get to the same point I struggled to get to in the previous model. I'm not sure why - There's a lot less structure. There's less for each layer to be doing. I don't have a network layer... My DefaultStory knows we only want to get it once; so uses the ClientCacheStory... which... I don't like. It's got a compound name. That's a bad. BOOOO!

I'm gonna go refactor this into a cache; and a client. ... OK; about 10 minutes later and I no longer have a compound class. I have a class that makes the call; and a class that caches the response. I could optimize a little further if I find I need to; but in the current world; I do not.

Simpler classes; easier to understand; easier to manipulate... This is where "reusable code" is created.

Show Comments