Wednesday, May 25, 2016

What It Takes to Code

One year ago, almost to the day, I received my first programming book. It was an intro to Python and I previously had zero knowledge of coding or how to program. Now one year later, almost two months worth of treehouse, five more books, and my first app soon to be pushed to my Github account, I think I've figured out what it takes to code. Note this is not about how to find a job as a developer or web designer nor what I think is the best way to go about learning how to code. But rather this is what I think is the most necessary quality of a programmer.

The 25 words or less version:
The insatiable desire to solve a problem no matter how long it takes and you will not give up until the void is satisfied.

The longer version:
Recently I have found closure to a bane in my life that has come and gone for the past six years. It is a completely different subject but I feel the situation is rather illustrative of my recent coding experiences. The situation was not a pressing problem, I couldn't change it, I didn't think about it 24/7, but sometimes it would still come to mind. Last weekend I was at a wedding and it is remarkable the change a 10 minute conversation can effect.

How does this relate to programming? I am still putting the finishing touches on the app I've been writing and as I said I will soon put it to Github once I clean it up slightly. I didn't think about the app 24/7. When I wasn't at my computer I couldn't code. However the problem was still there. Questions such as "Why can't I access this variable from other class? How can I send a list to another activity? How do parcelables work? etc." were going through my mind. When I couldn't figure something out and at the same time had to go to work and do other necessary daily things, the problem was still in the back of my mind and eating away at my sanity. I often thought of this xkcd comic as it seemed to apply to everything I was trying to do. I often thought I would never figure things out. Then somehow I would write a line of code, change a variable type, or change a method. Then, excelsior! it would run without errors! Thus providing closure to the coding issue I was trying to solve. So to revisit the illustration and apply it to coding: It is remarkable the change a 10 minute Google search or line of code or just some sort of coding "magic" can effect on my program, and uncoincidentally my subsequent emotional state.

This is less of a post about how to program and more about what your mindset should be when programming. I have been listening to the Code Newbie podcast and have been loving the fact that at this stage of my learning I am not alone in feeling I can't program or should quit and do something else. Other people learning to code have had the same issues and feelings that I have on a regular basis. But after all the work, late nights, and frustration the feeling of finally figuring out a problem, writing code that runs without errors, and seeing my app on a phone and working the way it should makes me want to get up and dance! (which I sometimes will do when I've spent too long a certain bug)

I've also been rather impatient lately and want to see if I can explain concepts, instructions, and stories in 25 words or less, as sometimes my attention span doesn't last too long. I don't know if it will always be possible to do so but I will try to provide a short and long version of what I want to put in this programming story. I find certain explanations of how some method should work are wordy and unclear and maybe I can do my part to solve that as I go so I can more easily understand them later.

Hope to have my app on Github by next week. Upward not northward!

Wednesday, May 18, 2016

Storing Data

Anything I've written so far has only been good for one instance of the activity in my app. The last course on the Treehouse Android track was about storing information. Every time another activity is stacked on a previous activity, the previous activity is paused and Android may stop and destroy some of the data to free memory. However there are two methods that can be overwritten

OnSaveInstanceState
OnRestoreInstanceState

OnSaveInstanceState bundles information that should be saved. To retrieve that information the method OnRestoreInstanceState would be implemented and in that method you would have to write the code to set the variables in the bundle to its respective member variable. This is absolutely necessary because anytime the screen orientation would change while using the app, android essentially stops and restarts the activity to display it in the correct orientation. It's interesting because in the onCreate method, Android Studio puts in a parameter for a savedInstanceState Bundle. So it will already expect to load previous information.

There is also a data type of SharedPreferences. I still need to experiment with this type, and I have some ideas for my first app I'm writing to try it out. A variable can also be assigned to a SharedPreferences.Editor. This can thus implement edit, putInt/String/etc., and apply. They are basically self descriptive. I may be slightly wrong in my current understanding, but this is how I understand it at present. For example a member variable mSharedPreferences is set to a constant that is a file location in the package. The string would be the package path plus ".preferences". Another member variable mEditor can then be set to mSharedPreferences.edit(). Then you can apply functions to mEditor, such as putInt/String/etc.(key, value). Then after all variables are "put" into the editor, you call the apply method to save it. The location the information is saved in the place initially set up in the mSharedPreferences variable. To live update a preferences edit, you would call notifyDataSetChanged() on say, a list adapter.

I hope I can reread this later and understand it. And if anyone is reading this, I hope it makes sense. If the last paragraph makes absolutely no sense, especially after trying it, I would appreciate a way to explain it better. The more I use these functions the more I will understand them. I'm still unsure of how to implement adapters for list views as I am currently still just copying and pasting code while adapting the variables to match each activity.

So far everything I've ever learned in programming, from last years Python and PHP/SQL to the current Java and Android courses, has been an amazing experience and I feel I've come so far. I'm really looking forward to what more I can do in the coming months. I think too that I should document how many man-hours I am investing in writing this. Then I can look back and know how much time it took to write my first app.

The constant earwigs of how to figure things out however is likely incalculable.

Friday, May 13, 2016

Binding, Views, Public, Static, and Frustration

I'm in the process of writing my first app. A way to arrange products so they can be showed to a potential customer. I'm doing it for a friend with a car detailing business so it is specialized to that. After I finish it for him I plan on generalizing it so it can be used for other businesses.

The second app walk-through in the Treehouse Android course is a weather app. In it you must send information from one page to another and display it in a list. This is the format I used to write the code for the car detailing product app. It should also be noted I'm using Android Studio and have the Butterknife API.

The biggest obstacle I had, spent 5 hours on, and stayed up util 1 am* trying to solve was binding variables to the xml items in the list view. I kept getting a 'Null Pointer Exception - Attempt to get length of a null array'(loosely quoting) and it said to possibly add @Nullable. It was a rough night as searched and searched but couldn't find anything that would make it work. I finally ended up writing the adapter code again and somehow it worked this time. I don't know how I fixed it as I merely duplicated other code and changed variable names, but in the end it finally worked. I was so happy to go to bed.

The following day a challenge I had was creating an array of different services. I wrote a class that would populate arrays of products for the different categories of services. However I couldn't access them from the main activity and I didn't know why. So I ended up just writing methods in the main activity. It worked but it wasn't as organized as it could be. Looking at other code I found if I wrote those functions to populate product information public and static instead of private, they could be accessed by other activities and classes. It was really a simple fix and I should have seen it. However the triumphant feeling of overcoming the challenge overpowered any feelings of inadequacy.

As of now the app functions correctly and only needs to be cosmetically enhanced. I would also like to add the following features:
  • Showing before and after photos of products offered
  • Adding and deleting products
  • A way to edit the product names and descriptions
Also instead of getting the product information from a class, it would probably be better to have a database in the app. However that is beyond my knowledge. I know some SQL and how to integrate it with websites and PHP but I don't know how to have it in an Android app. I do believe there is a course or workshop on Treehouse that discusses that.

* I realize if you are a programmer 1 am is likely considered barely a late night trying to fix a bug.

Wednesday, May 4, 2016

Git

I finished the Java track on Treehouse the other day. My last post was a review of that course and not much has changed since then as far as my viewpoint goes. I still think it is a very good course. And for a $25/month fee it is certainly a good deal. Plenty of knowledge, videos, and forums to learn as much as any beginner needs. Recently I've been highly disappointed with customer service everywhere so until now Treehouse has been a breath of fresh air when it comes to quality product for a good price and good customer service. For example the few times I've tweeted about my learning experiences they liked my tweet and commented on it.

With that said, recently a friend of mine asked me to make for him an app for his car detailing business. I told him awhile back I would do it as I've been learning programming lately. My newly gained knowledge of Java, and again working through the Android development track, I thought now is the time I can start on this product display app my friend asked me to make for him. If you have been starting to program lately I'm sure you've seen the term "Git" and always wanted to learn what it means. I had no idea what it really was let alone learn how to use it. Any Google searched just used terminology that was way over my head and I had no idea how to access anything. Quite frustrating.

That leads me to looking on Treehouse to see if they have a track for this and they do!! It is one of their shorter tracks, saying it is only a two hour course. I'd say it is pretty close to that as it took me about one afternoon to finish it. It was amazing. Very clear and descriptive on how to use it and how beneficial it can be! Of course Git isn't the only version control system (VCS) but it is likely the most popular. I would highly recommend this course if you plan on developing your own projects or obviously if you want to join Github. Incidentally the treehouse course explains how to use Github.

Since I searched awhile ago on how to use Git and couldn't find a set of commands and clear explanations of those commands, I wanted to compile a source of simple Git commands that one would use a lot. I don't remember exactly how I found this site, possibly in teachers notes on treehouse. After using Git for an afternoon I feel this is one of the best explanations of Git commands out there.

One last cool feature I found in Android Studio is that it has a way to use Git from the menu bar! That will certainly be useful in developing projects. I haven't looked but I'm sure regular IntelliJ and Eclipse have a way to have a repository as well.

Last words for this post if you get discouraged in learning how to code, don't give up! It will likely click after you use it some and write code into your own projects.