Friday, March 16, 2018

Key Value Pair Statements - Better for the eyes, but better performance?

There have been two challenges on Free Code Camp now that I went down the road of using if statements. It started getting ugly with all those if and else if statements. You could use a case statement for each of the if statements, the case statement could possibly be easier to read and possibly have a faster performance.

Below is an example for possible DNA strings:

function pairElement(str) {
var myArray = str.split('');
var returnArray = [];
for (st in str) {
if(myArray[st] == 'G') {
returnArray.push([myArray[st], 'C']);
}
else if(myArray[st] == 'C') {
returnArray.push([myArray[st], 'G']);
}
else if(myArray[st] == 'A') {
returnArray.push([myArray[st], 'T']);
}
else {
returnArray.push([myArray[st], 'A']);
}
}

But then I got thinking, what if there were a dictionary or map that would allow someone to look up the index and see what the value output is for the item. I believe I had seen something like that before so I started searching. Turns out a map is exactly what I needed.

This technique allows me to create a map, or a key value pair, that will let me look up the value I am looking up and see it's matching string. Your first key value set G: 'C' will look up anytime your key is G it will output C.

By splitting the input string by their characters, one you are then able to use that item as the lookup for your key and return the value you need.

function pairElement(str) {
  var pairMap = {G:'C', C:'G', A:'T', T:'A'};
  var myArray = str.split('');
  
  for(var st in str) {
    myArray[st] = [myArray[st], pairMap[myArray[st]]];
  }
  
  
  return myArray;
}

The code is much easier to read and I am guessing it will have better performance as it doesn't have to check each if/else if statements.

"Nothing is work unless you'd rather be doing something else." - George Halas

Wednesday, March 14, 2018

FreeCodeCamp intermediate algorithms

I recently started FreeCodeCamp. Okay, not recently. I have been taking my time completing the challenges and tasks, but it has been pretty good. I enjoy learning the JavaScript and I am hoping I begin understanding everything more. I will be honest, I have been going slow because I am coding all day at work using Angular. Angular is great and very challenging, so why do I continue doing FreeCodeCamp?

Perhaps I feel that it will give me a foundation on the background of Angular, after all Angular uses JavaScript, right? Maybe it will help with debugging? Plus, I started FreeCodeCamp before I got hired to my team that uses Angular, and I'm not a quitter. I am to the Intermediate Algorithm Scripting and, as always, I am finding many different ways to complete the tasks they as you to do. I know that I am not the most efficient coder and there are probably much better ways to code the tasks.

I would like to go back and look at all of the tasks I have already done to see if there is something I could do better, but who knows when/if I will find time to do it. But let's start on the challenge called "Diff Two Arrays". The task seems easy enough, compare to arrays and return a new array that only contains objects that are in just one of the original arrays.

For example
arr1: [1,3,4,5]
arr2: [1,2.5,6]

My call to diffArray should return
return: [2,4,6]

My first time around I used a for statement to check each object in array1 to see if array2 included that object. Then I did the same for array2. Not the most efficient way, I know, but it worked and it was my original thought (I usually try to do the task without using the hints to see if I am able to complete the task on my own free will).

function diffArray(arr1, arr2) {
  
var newArr = [];

for(var i = 0; i < arr1.length; i++) {
   if (!arr2.includes(arr1[i])) {
     newArr.push(arr1[i]);
   }
}
  
  for(var t = 0; t < arr2.length; t++) {
   if (!arr1.includes(arr2[t])) {
     newArr.push(arr2[t]);
   }
  }
 return newArr;
}


It get's the job done. I know that I use arrow functions at work to pull data from a database, so I thought why wouldn't that work here? FreeCodeCamp mentions that you can use Array.prototype.filter() so I went to explore this in the JavaScript context. I'm going to be honest, it took me a bit to figure out how to use arr1.filter and .concat(arr2.filter). But in the end the code is much cleaner. (although it was unnerving when the FreeCodeCamp gave me a warning: " 'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6')"


function diffArray(arr1, arr2) {
  
    return arr1
      .filter(ai => !arr2.includes(ai))
      .concat(
        arr2.filter(ai => !arr1.includes(ai))
      );
}

A much simpler solution. The filter takes from each array and will remove any object that is included in the second array. Then you use concat to add the filter from arr1 to the filter from arr2.

Sometimes our first response will get it working but sometimes we need to look a bit to find a better solution.

I will post some more of these, I have plenty of examples.


"Well done is better than well said." - Benjamin Franklin

Tuesday, October 31, 2017

Angular...a new language beginning

I have been doing a lot with Angular lately. I am really enjoying it. The entire time I should have been posting my progress, but no excuses, I haven't done it.

I will be posting my progress on Angular starting...soon :) . While you anxiously await I have started a real simple shopping tool (right now it is just the menu and navigation) that I will be updating. Don't worry, you can't buy anything and it won't take your money, yet. It can be found at https://myshoppingtool.firebaseapp.com/

Watch for more updates coming soon!


"They always say time changes things, but you actually have to change them yourself." - Andy Warhol

Tuesday, July 18, 2017

The failure to a win

My last post was about not being able to get some code to work. I declared it a failure. A failure that I should learn from. I am here, a couple weeks later, to say that I did learn something from the whole experience. I think most of us have seen the comic, or a similar comic as what I have below


That was exactly what happened except it wasn't a semi-colon. I was missing two words and an equal sign, Mode=TwoWay, where I was giving my binding information. In that particular XAML file, I was not using Mode=TwoWay in any other lines of code. However, I did use it on other pages. Why did I miss this one piece?

I came back to my solution a week later with new eyes. It took me about 15 minutes to see what my issues was. I add Mode=TwoWay and, presto!, my code is working as I expected. What was it about taking a step back from the code that helped me see what was missing?

The important lesson I feel I came away with, is that sometimes it is good to have a fresh set of eyes take a look at the code. In my case, it was taking a week off allowed me to get a fresh perspective, but we can't always do that at work. Yet, it can still be important to get a fresh set of eyes.

We look at code all day and may start missing pieces that would actually help solve our issues. Making sure we know when to ask for help and get another person looking at the code can be very beneficial.

Take that "week" off  and get fresh eyes to look at your code.

Here is a screen shot of my minimal viable product (I know the UI isn't great that will be coming...soonish). Now to see if I can get some services to the online movie providers (not looking so great, I have only been able to find an API to Plex Servers and the rest of the providers are either hidden really well, don't exist, or I am searching for the wrong things).

"It is better to know some of the questions than all of the answers." - James Thurber

Thursday, June 29, 2017

When frustration runs high...

Lately, I have been a little frustrated with the app I have been building in Xamarin.Forms. I am trying to use MVVM and I have most of it working, yeah! I just have one attribute that isn't not getting saved from the view. It is the only value that sits on a different SQLite table then all the rest. Do I blame it on that? Am I not calling the correct items in the view model?

Anyway, I have been stuck for more then a few days. And each day I don't make much, if any, progress. What happens? Frustration builds up. The first line where I say a little frustrated, that is a lie. I have been getting more frustrated. I shouldn't be getting too frustrated, I know that some frustration is good and will actually help you grow. I have been hearing a lot that failing is good and we learn from each failure. Is it time to chalk this one up to a failure?

Let's be honest, I got to the point the other night where I really just wanted to get the app to work. I did part of it in code behind (I know, I know, I won't be able to test it!) and got it partly working. You can now select the storage for the movie, but it won't save. I am missing something in the view model, but I already knew that. Using just the view model I was not able to get the data to populate or pull up the correct page.


Great! Now I can select the storage type and save my movie information! Bad news, it doesn't save the storage piece. I save the movie and come back into the detail and the storage type is missing. I need to go back and learn more about how MVVM is working. I have a cursory understanding, but not a full understanding. My app broke when I tried to add a second SQLite table into the mix. Take out the new table and you can have a list of movies, no problem. But, I also had an example of the one table scenario that used MVVM that I could model.

Maybe a failure now, but a failed project which I want to come back to after taking a step back and taking some time on another project to continue my learning, perhaps more about MVVM and a smaller project that implements it. Or, next time I am in the office I hope one of our MVVM experts are in the office as well and I can ask them. Would it be ok to use Skype to call someone up to ask help on a personal project?

The frustration may be good, but how much frustration is good? This is probably a question each person needs to ask themselves. It will be different for everyone.

"Nothing makes one feel so strong as a call for help." - Pope Paul VI

Thursday, May 18, 2017

Tracking Improvements

We all want to make improvements to ourselves. It is a long process that takes a lot of work and continuous effort towards our goals. Recently, I have been asking myself a few questions about my goals and the progress that I am making towards them.

The first question is, what are my goals? This is a question that I never have really asked before. I always knew that I wasn't going to be a truck driver like my dad, but beyond that I was always flying by the seat of my pants. It seemed to work out relatively well at first. I graduated high school and went to college. I can't even tell you how many different courses I took in college to find what I "wanted to be when I grew up." I decided finance. Well, I didn't love finance so I changed careers and now am a Software Engineer in Test. I like this job for the most part, but what is my end goal?

 I would say I want to become a full developer, this raises another question on is this a good long term goal for me? Great, I have an end goal for now. But what is the time frame for this goal? 1 year, 5 years, 10 years (better not be the later two). To reach this goal I have been setting intermediate goals. Here are a few of the goals I have been thinking of lately:

  • Build API
  • Build App
  • Deliver App to App stores
  • Speak at a conference

Great news! I have already completed the first one. I know that each of those goals have steps I need to take in order to reach them, I just need to figure them out. Not only do I need to figure the steps I need to take, but I also need to keep track of what I am doing to achieve these goals.

I recently saw part of how a person I look up to keeps track of his goals and self improvements. I look up to him as I believe he is very organized and able to keep up on everything, from sports (both U of U and BYU!) to family, and technology to his work. I can't even keep straight who the quarter back is for the U of U, how can I be expected to remember so many other things. I need to find a process that works for me that allows me to keep track of my daily activities, my goals (need to look at more and come up with a long term plan), the stuff I read, and other activities I feel will help me reach my goals.

The plan I saw was a checklist of sorts that had many different categories. I saw it and decided to try something similar in OneNote. I created different tabs, 5 year goal (nothing here yet), 1 year goal (becoming a dev and the ones you see above), monthly goals (did this for one month then forgot all about it), and daily goals. My daily goals are simple, like read a tech blog and learn some code. Really generic and I know in order to reach my long term goal of becoming a full dev I need to get more specific goals. That then shows my lack of what I don't know. What are the daily goals that will help me reach my long term goals? 

I have recently been teaching myself Xamarin.Forms using a course through Udemy and other online resources. I posted about my learning of MVVM the other day. Let's be honest, the MVVM "challenge" has been really hard. It is refactoring some code that I wrote early in the course. This let me to think that I don't know the basics as well as I maybe should. I had trouble separating out the responsibilities and seeing what each ViewModel would need in order to work correctly (it still isn't working, the app dies when I try to save and then won't start up again until I delete the SQLite Database that was created). I also took more then a few hints from the answers to find that in some parts I was doing well and had a good start and others I was totally wrong. 

I here that everyone uses Google to help with their work. But what goals do I need each day to reach my end goals and how do I keep track of them. There are some days where I am really good at keeping track of the daily goals. Other days, like Tuesday, I totally forget to even look at my goals and many of them don't get done. Questions arise such as how often should I look at the longer term goals and adjust them? I think this will be an iterative process. Find something that doesn't work and move to the next method. Keep trying, just like code, right? One day I will find something that works.

"Success is not a good teacher, failure makes you humble." - Shah Rukh Khan


Monday, May 15, 2017

Attempt at explaining MVVM

100 Days of Code is done. I may not be coding every day (last weekend we went to Moab and spend our days in Arches National Park and I didn't code) but I am still taking time to learn things. I am nearing the end of a Xamarin.Forms class on Udemy taught by Moshfegh Hamedani. I have one more activity left and then "Beyond the Basics" section to go. Then I will be a great Xamarin.Forms developer, right?

Well, I will have more tools to help me at least do some development in Xamarin.Forms. Today I did the section on MVVM (Model-View-View Model) pattern. I knew that using the code behind, like we did in all the examples, may not be the best way to code in XAML. We use MVVM in some of our applications at work and I thought this section would be great to learn for my own project and also so I can better understand what we are doing at work.

We hear that teaching is the best way to learn. And by we, I mean that I have heard that many times and again today in a video post on YouTube channel called Top 8 developer habits: Teaching - Fun Fun Function. Watching that today I thought, why not, let's blog about MVVM to help make sure I really understand what I learned.

What is the best way to describe MVVM? Well, it helps separate concerns of the front end and the back-end logic. This will ensure that the front end doesn't need to know the logic behind the data and the back end get's to do all the calculations and only give the data when it is asked for. Your view just needs to know what to pull and the model will just store the content.

Model: The model is a domain model that has both the behavior and the data. When you enter new data to be stored, the model will handle any transformations to new data and then store the data.

View: This is what the end user sees, so make it pretty. It isn't going to do much else.

View-Model: This is a layer of abstraction between the model and the views. The view model will make the calls to the model and send the data to the view to be shown. It also works the other way. It can take that data from the View and send it to the model to be stored. It will also know what to do when you change a setting or click a button (that doesn't change/add data to the model) in the View. It let's the model just know what to do with the data and the view to just continue looking pretty.

Quick lovely drawing that you probably won't be able to read:

Ok, why do you want to use MVVM? From what I have heard (I still need to do this on my own) it makes your code more testable. If you are using Xamarin.Forms and do all the code in the code behind, you can't test it. It is too tightly coupled with the XAML code and your tests won't be able to see what is happening in the UX layer. MVVM will also keep a separation of powers.

What could be a downside of MVVM? The amount of time it takes to implement MVVM may be overkill when you have a simple application or you never plan on doing testing (pretty sure this is unit tests, you will always want to have some sort of testing before you release to the wild). What is the size of the application it would be overkill? Good question, maybe a small to medium application would be fine using just code behind or using a model and view alone. Large projects you will probably want to use some framework that will allow for better testing.

That is probably an over simplification of the MVVM model, I think I am going to put some time on my team leads calendar tomorrow to discuss it with him and make sure I have it correct, isn't Ryan lucky?

"All great achievements require time." - Maya Angelou