Thursday, October 30, 2014

Today in JavaScript

A few things I learned today:


1. You can't push to an array and sort it in the same line, at least, not like this:
array.push(entry).sort();
It just tells you Uncaught TypeError: undefined is not a function.


2. If you try to push to a variable that you've declared, but haven't initiated as an array, you get an error: "Uncaught TypeError: cannot read property "push" of undefined".


3. You don't have to pass variables to functions for the functions to use and modify them. For example:

var testvar = 02;

var testfunc = function(){
console.log(testvar);
testvar = 3;
console.log(testvar);
}

testfunc();

console.log(testvar);

Simply results in this being printed to the console:

2
3
3

The 2 is testvar, being logged from within the function, even though it was not passed as a parameter.
The first 3 is testvar, being logged from within the function, after the function has modified it.
The second 3 is testvar, being logged from outside the function, which proves that it remains modified even outside the function.

To an experienced coder, this might seem like second nature, but to a beginner, it's important conceptual development. Scope!



4. Say you're making a function that you want to take two parameters: a number, and an array. You then call the function and pass it a number and another number. You think of the first number as the first parameter, and the second number as the only item in an array. The function will take this to mean that both of the things you passed it are actually part of the array parameter. So, make sure you use brackets when you're passing the parameter that's supposed to be an array.

Monday, October 6, 2014

File Structures

Android coding means manipulating a few different file formats. Chief among them are .java and .xml files.

In android, .java files are used to do two things:
  1. Allow the operating system to interact with your app
  2. Do the back-end/behind-the-scenes work, making the parts of your app interact with each other programmatically
On the other hand, .xml files are used primarily to 
  1. Create GUI components known as "layouts", and
  2. Define "resource" items, such as entries in lists, strings that give names to individual .xml components, colors that appear within the app, styles that apply to many .xml components, and more.
Today, I was tasked with creating a Settings screen for my app. What seemed like it should have been one of the simplest things I've done yet turned out to require (manual) modifications to or creation of at least 9 files. That's right. Nine files, one settings screen.

And people wonder why so many start to learn code and end up turning away.

The sad truth is that many of the existing code-learning resources don't deal with how file structures interact, instead tackling syntax within individual files.

As I tried to implement the Settings-screen solution shown in the Deitel book I'm working out of, I realized I needed to chart out the file structures to understand what was going on. Which files referred to which other files? First, I made lists of all the .java and .xml files that I thought were relevant to my task, adding them as I found them.

.java files

MainActivity
QuizFragment
SettingsActivity
SettingsFragment

.xml files (folder they're in)

activity_main (layout)
activity_settings (layout)
fragment_quiz (layout)
main (menu)
preferences (xml)

Then, I traced how these files interact, starting with MainActivity. As shown in the upper left, all .java files are enclosed in boxes. (It got a little messy.)


I only wrote down instances where one file referred to another file, as opposed to just calling a method (java), creating a variable (java), generating final content (xml), etc.

The result? I was able to fairly quickly filter out the files unrelated to the task at hand (QuizFragment.java and fragment_quiz.xml) and then create analogues to all the remaining files or pieces of files in my own project.

In other words, a settings screen was made. With one adjustable setting.

Next: Splitting the project into two Activities: One for login, during which the Settings screen will not be shown, and one to become active once the user has logged in, during which Settings will become available to view and modify.