Tuesday, October 18, 2016

Polymorphism Can Save Time and Make Code More Fun

After a day of working with some rather procedural code and shoehorning it into something a bit more OO, I've concluded that this code base would've benefitted from some SOLID OOP early on.

This code base takes a set of data in a tree-like structure and generates an html tree view based on certain attributes of each data point. The tree views are not basic like a file tree, but are based on that with more features. And it needs to display differently depending on where it's being rendered. User authorization for each data point is an additional consideration.

Reasoning about this, we can see how polymorphism can benefit us here. For instance (pun intended), the basic flow of walking the tree of data would not change, except for the fact that some business logic applies to whether a node should be added or not - could be several reasons a node would not appear in the view (e.g., security, choice, etc). Filters could be used to achieve this depending on requirements. Or a class which implements filtering. Either way we slice it, the tree of data needs to be walked.

While walking the tree, whatever is rendering the tree view can either construct it on the go, or construct/serialize in one go. For the latter choice, it can hold a collection which can be added to, or receive a collection. That collection would closely model the data tree, but with whatever visibility rules applied to it by any business logic classes.

Whatever is walking the data tree should then take in business logic as a dependency. The polymorphism part is that different types of nodes can be added to the tree view depending on the result of that business logic. I'm thinking of an interface that takes in the data and returns a TreeViewNode. TreeViewNodes are FolderNodes or FileNodes (leaf nodes). But have additional properties which control other aspects of the nodes.

Hey, sounds like we can do this better than procedural with functional programming too!

(buildview (map transformToViewDataList ( filter context dataList) )

But I digress...with OO it would have different implementations of the interface for different business cases, but the same recursion. Heck you could even have different implementations of tree recursion if needed -

nodeMapperFactory = GetFactory(context);
rootNode = GetRootNode(data);
treeWalker.WalkTree(rootNode, nodeMapperFactory);

nodeMapper = nodeMapperFactory.Get(currentDataNode);

viewNode = nodeMapper.MapNode(currentDataNode);


each(WalkTree(childNode) in children);


Monday, October 17, 2016

Quick Tip: Study OSS

OSS repositories abound. Just pick your favorite bits of open-source and study the code that makes it all go! There's a lot to be learned by studying the source code. Even more by pulling it local and running it. Make a few changes and experiment with it. Most will come with automated tests. Go ahead and make up a few of your own.

They've got all the info out there on how to get the source code locally and a bunch of other info here if you need:

Tuesday, October 11, 2016

Getting Political

This is not a political blog, it's a tech blog. However, I watched the presidential "debate" last Sunday and I'm feeling it this week. What am I feeling? I'm feeling a sense of ugh - like many who have described it as an ugly, dirty feeling. But why?

I'm trying to sort out why this feeling was put on all of us - why do we feel this way after this debate? I have reports from one coworker who's husband is a political junkie - he was ok to turn it off after five minutes. I assured her that it didn't get any better.

I want to start with the Trump campaign, since its an interesting campaign marked primarily by the strategy of making the OTHER candidates unlikable. It's a smear campaign, plain and simple. There's an unfortunate side effect to this strategy, the rest of us (meaning Americans) are getting smeared right along with the others.

With comments that allude to how bad things are, we all get dragged through the mud. Sure there are some problems...everybody's got their problems and its just nothing new. What I haven't seen is a Trump who really says "I know how to fix it". Where's the "champion for change" attitude? He has more of a blame game..."its all wrong and its their fault". A whole lot of it shameful whining, frankly.

Can't stand to see my country that I love and all of her people dragged through the mud and put down for Trump's political gain. I wanted to put a finger on that sort of negativity we've seen in the #debate and see what kind of person behaves that way. I found one article https://www.davidwolfe.com/12-traits-negative-people-avoid/
which lists out some traits such as:

They never apologize.
Blame others for their mistakes.
Hope to see others fail.
and 9 other unpleasant things.

I found these aligning right up with what we've seen from him over the years.

Then there was this blurb on pessimism https://www.psychologytoday.com/basics/pessimism
it said pessimistic people make better leaders. I do think you can't have a blindly optimistic leader, else they'd get taken by everything. But I can't help think too much pessimism makes for bad morale. And when morale is bad, people aren't going to get moving to get things done. I've played enough civilization games to know how that works - eventually cruel lords incite revolt. It takes a healthy mix of tough-but-fair to be a good leader.

Now Clinton...she's a pro politician. She's running a well-devised campaign. Giving measured answers. Skirting tough questions and always going back to her agenda - I'm more electable. Laughing off whatever is thrown at her to try to break her. Always veering back toward her agenda and away from danger. Answer the fucking question will ya...

I do believe she is playing for the certain votes, being too nice while she's at it. She did go after Trump a bit by saying he's not fit to be president. Maybe let those allegations come out in other ways a bit more. Just a bit weak on defending past actions (hers and others). Could use a bit more Joe Biden attitude...that "I've been there and done that, you peon" sort of attitude..."and another thing, you don't know shit so quit acting like you do"...you know, but without actually saying it. Now that's class!

The private email thing...makes you wonder what was there. I'm not an advocate for invasion of privacy. She was dealing with a whole lot of terrorist countries - could be national secrets, could be shady deals, could be dirty emails to and from Bill, could be campaign strategy. Don't mix work and private emails people! I hear this all the time where I work, IT tells everyone not to do this. Do people do it anyways? Yes they do. Should they? No they shouldn't.

All of this and there wasn't really much talk about the issues, of which the parties are SO polarizing on. I don't think it's sensible to push for so much polarization for political gain (pandering for votes). Nothing good will ever get done that way. But since campaigns are run on polarization, that's what we're going to have - we get a whole lot of black/white on issues that really have a whole lot of grey area. Guns, foreign affairs, personal liberties, national security, taxes, GDP, debt; all are highly complex issues and the political campaigns try to box them up as simple black and white in order to manipulate the public into voting for them.

Just take the gun issue in and of itself - fear of change, even responsible changes, to gun laws are so far polarized as infringement of rights that we can't have sensible laws in place to keep gun sales within the law.

iCan't balance a budget. Can't appoint a judge to the highest and most respected court in the world. All because of this black and white attitude the parties are using to control votes. What if you are one way on some issues and the other way on others? God forbid we should have thought for ourselves without ganging up with party polarization...we might actually find ways to solve the issues. Who gains from that and who loses?

So both candidates kind of suck. They've both made mistakes. One makes Americans and men look bad, the other has some skeletons in her closet. Either way you better believe every other candidate is shaking in their shoes hoping their own secrets aren't getting dredged up from the depths. People make mistakes, it just happens. Good people do bad things, bad people do good things. It's not all black and white. What's important is how we move forward and how we learn from past mistakes. Part of me can't wait to see what kind of crazy shit gets dredged up next, but the other part want to see a much more positive run for the White House - one with far less casualties.