Showing posts with label Friday Challenge. Show all posts
Showing posts with label Friday Challenge. Show all posts

Friday, August 11, 2017

Friday Challenge #6: Which Way Should We Go?

Lets say you've got some decisions to make. You have a bunch of stuff to get done but you aren't so good at managing your priorities...so you create a system to keep your priorities in order.


Ok that's cool. But how do you prioritize those taks? Maybe you want to take care of the one that's the latest first - if there are any that are late. Or how about the most important? The one closest to due date that isn't late?


Make a Priorities class with a method called GetHighest that gives back the highest priority item depending on the aforementioned conditions.


Lets try this two ways:


1: Write an algorithm that takes into account all those ways to prioritize using 'if' statements.


2: Use a class for each way you want to prioritize and pass it into the GetHighest method. The class should have a Rank method that takes the array (or whatever) and gives back the ordered array (or whatever).
You should have 1 class for each way and pick the way in a different method.


Now go back to way 1 and add a new condition and ranking. And do the same for way 2.


Ok so maybe you have to add and if statement either way or use a map of conditions to class for way 2. But how about this...combine conditions. Or maybe chain filters...make filter classes to remove those that are late and those with low value...combine those two filters in different ways with the soonest due ranking. Now you can create different filter and ranking strategies based on different conditions...


Now if you have 2 hours to spare and the top priority task takes 4 hours, you can apply a strategy that takes into account how much time you've got, how many people are available, what skills are required, or whatever else you need...do that with procedural code and you'll wind up with a twisted mess to sort through anytime you need to add a way to do rank or filter! Go ahead...try it with way 1!


In this challenge you've exercised the strategy pattern and possibly the factory pattern if you've used a factory to retrieve the appropriate strategy.


A few things to try with the factory - use contextual information from the real world - time of day, number of tasks to prioritize, time available, etc. Re-use the filters in the factory to get percentages of tasks that are late and chose a certain ranking strategy based on the percentage...you can see how various strategies can be combined to create new strategies and even reused in other ways that a procedure cannot. This also works in functional programming, in fact it's how functional languages work best...map, filter, etc. The strategy is the map function you pass into map or the filter function you pass to filter, etc.

Friday, July 21, 2017

Friday Challenge #3: HTML 3-ways

Ahhh...web frameworks. What do they do for us? A lot! But do we really need them? Let's find out with this Friday Challenge.

This challenge has 3 parts (tip: try not to get too fancy and get bogged down on any one part).

Part 1: Build a web page with HTML and JS only (use some CSS if you like pretty things). The trick for this one is to use base JS only, no JQuery or anything like that. Do all DOM manipulation using JS only.

Part 2: Build the same thing (or refactor existing) using a templating tool such as Handlebars or for additional challenge build your own rudimentary templating tool.

Part 3: Build the same using a full-on framework such as Angular, React, Ember, Elm or whatever flavor of the day your feeling.

Build whatever you are comfortable with - a hello world if you wish. But if you really want to realize the value of the challenge - try something that interacts with an API that returns data (and handle the possible failure of the API call or no data response).

We do depend on frameworks for a lot. And yeah it's not always necessary to do so, but if you decide you'll have a go at not using one you may end up rolling your own. Depends on what you're going for...either way it's good to know what life is like on either side.

Friday, July 7, 2017

Friday Challenge #1: Data Access

Today, I'm going to do a whole different kind of post - a Friday Challenge! Here's how it works: I'll post a challenge, you take up the challenge (usually by writing some code) and post the results or a link to the resulting repository in the comments. Discussions ensue. We share information. I'll put one up every Friday if there's interest. It's fun and engaging, what can go wrong? With that, here we go:

First Friday Challenge.

Your Quest is to write data access code using 3 different "patterns". I'll try to be a language agnostic as possible, the one requirement is to use a SQL based database.


Background:

In software there are many ways to perform the same functional operation, some are better than others when it comes to change, re-use, and readability.

Scenario:

You have a `users` table with the following columns: id IDENTITY/AUTOINCREMENT INT, name VARCHAR(255). You have the user's id and need to lookup the name of the user.

Example:

Given the following users:

|id|name|
|1|Marquart Flitshema|
|2|Loriens Blakens|
|3|Ashima Kithra|

When I look up a user with the id 1,
Then I should get the name "Marquart Flitshema".

When I look up a user with the id 3,
Then I should get the name "Ashima Kithra".

Challenge:

Pattern 1: Everything in 1 class/method/function (this is actually an anti-pattern).

Pattern 2: Pass the SQL and some mapping class/method/function into a different construct (class/method/function) to handle execution. Could be a base class (abstract or not), could be a prototype, any other construct will do so long as it is distinct from the construct that handles the SQL string and the mapping.

Pattern 3: Pass an entire construct into a different construct which handles the execution (by passing the database connection into the first construct).


Each pattern should be consumed similar to the following depending on language:

Pattern 1:

db->GetUserName(1);
db.GetUserName(1);
db.getUserName(1);
DataBase.getUserName 1.

 Pattern 2:

  users->GetName(1);
  users.GetName(1);
  users.getName(1);
  Users.GetName 1

  OR

  users.->Get(1).name;
  users.Get(1).Name;
  users.get(1).getName();
  users.get(1).name;
  Users.Get 1
  |> user.name.
(getName (dbQuery getUser 1))

consider the advantages and disadvantages of both approaches.

Pattern 3:

db->Query(userNameQuery);
db.Query(userNameQuery);
db.query(userNameQuery);
Db.Query UsersQueries.NameQuery 1

OR

db->Query(userQuery)->Name;
db.Query(userQuery).Name;
db.query(userQuery).getName();
...

again consider advantages and disadvantages

Those are just examples in various language formats or pseudo-language formats. I tried to be inclusive, though I'm not as well versed in some languages (for a bit of language fun look up DerpCode). Feel free to use due dilligence such as null checking, exception handling whatever else you would like, but try not to muddle up the point too much with all that for now...don't let it get you off track is what I mean.

The code doesn't need to work completely, but if consumed by other code it should work (hint: you could use Unit Tests to drive the code and mock out the database but there should be some sort of `connect`, `open`, `close` and whatever else your SQL DB of choice normally has in the mock).

So that's it, the first Friday Challenge...go forth on your quest! If you need any tips, explainations, etc...feel free to put those in a comment and I (or anyone else who feels enlightened to do so) will do our best to get back to you as soon as we can. You should create a repo in GitHub, Bitbucket, JsBin, *Bin, someother online platform or whatever you do then share your work in the comments. Please don't link to file downloads, dropbox, etc...those aren't really for sharing code the way your should be sharing it for this.