Friday, April 29, 2016

Git Branch, Diff, and Merge for Noobs

Prerequisites: terminal basics, git basics (e.g. git add, git commit)

Branching helps you try out new features while leaving a copy of your existing codebase intact. This is nice for working on teams, because it lets each team member work on a different section of code, without them having to sift through the changes the other team members are making at the same time (as displayed by "git diff".)

Diff is for difference. When you tell your terminal " git diff 'master' ", you're telling git to show you the differences between your working directory (the branch you've checked out) and the master branch.

The process goes something like this. In the terminal, from your working directory:

git checkout -b "name_of_new_branch"

[Go to text editor, make changes to your code]

git diff "master"

At this point, the terminal summarizes the differences for you. A few helpful hints:

  • @@ marks one changed section. (In addition to the lines you changed, diff shows you the lines surrounding them, to give you some context.)
  • - marks the lines you removed (or made changes to)
  • + marks the additions or changes you made (on your new branch)

When you're done making changes (you've completed your new feature or whatever,) you switch back to the branch you were on before -- say, "master" :

git checkout "master"

And use "git merge [branch]" to apply your changes:

git merge "name_of_new_branch"

If you're done using the branch you made, you can delete it:

git branch -d "name_of_new_branch"

Rinse and repeat.

Monday, April 18, 2016

Grunt for Noobs

Prerequisite knowledge: Node.js, npm

Grunt is a JavaScript minification tool. It can take a bunch of separate .js files, pack them all into a single .js file, and make that file really small by doing things like removing comments and unnecessary whitespace, and turning long variable names into single characters.

Conveniently, this also serves to obfuscate your code, making it trickier to hack around in or repurpose without permission.

This can be useful when you've reached the stage where you have a bunch of code in lots of different files, and you want to put them all into one file to stick them on your server (this is part of deployment.)

I used the following video to learn Grunt. It gets right to the point with a nice variety of examples.



Sunday, January 3, 2016

My 8 Favorite Keyboard Shortcuts

The environment I use is probably important to know, here...

CMD + tab -- switch between windows/apps (IDE, Terminal, Finder, etc.)
CMD + w -- close tab or program
CMD + ALT + arrow keys -- snap window to left-half/right-half/top-half/bottom-half [via Spectacle]
CMD + shift + F -- fullscreen/presentation mode (nice for maintaining focus and reducing visual clutter)
CMD + R -- reload (in Chrome), compile/build/run (in XCode)
CMD + 1/2/3/... --  go to 1st/2nd/3rd/... tab
CMD + shift + N  -- new folder (Finder)
CMD + N -- new window

I expect this list to grow with time.

Got a favorite of your own? Leave it in the comments!

About this Blog

As I touched on in the header/description -- this blog is more of a personal notebook/reference than anything else. I'm not sure I expect it to be useful to anyone else, but just in case, and because it's convenient and fun to use Blogger for this, I'm publishing it.

What Environment Do I Use?

If this blog distracts or confuses you, please just close the tab and do something more productive.


Getting Hardcore: Learning C++

In taking my first real steps with c++, I'm learning from this tutorial. (A friend also suggested this one.) Here are my cliff notes. There's a lot of vocab, and some comparisons to the languages I "know" already -- Java and JS, primarily.

Chapter 0/1

instantiation (int x) --> piece of RAM set aside, and token "x" is assigned a memory location (i.e. location 140)

l-values : a variable is just one type of l-value. L-values are called l-values because they go on the left of assignment statements. (There are r-values too.)

1.3a

The :: in "std::cout" is a namespace symbol, indicating that the "cout" object resides within the "std" namespace. You can save yourself typing std::cout every time by putting "using std::cout" at the beginning of your program. (I wonder if there's a way to undo this entrance into the std namespace?)

"using declarations" vs. "using directives" :
declaration -- "using std::cout"
directive -- "using namespace std"
(Directives are more general, and can result in "ambiguous symbol" errors if you also create functions or variables with names that overlap)

1.4

function calls cause the current function to be interrupted by the other function -- what implication does this have for scope and namespace?

1.4b -- Style guide for functions ("one task" rule)

1.4d -- "The name of a variable, function, class, or other object in C++ is called an identifier." I.e., an "identifier" is the name of an object, and variables, functions, and classes are all just objects.

1.6

Whitespace is ignored, like in Javascript. Unlike in Python and Ruby.
Adjust your whitespace to make your code easier to read. Think spacing/alignment of comments, l- and r-values, etc.

1.7

Hoisting (like in Javascript) is only performed if forward declarations are used. In c++, a forward declaration, when applied to a function, is called a function prototype ("prototype" is also a Javascript term, but it doesn't mean quite the same thing there.) It's kind of like declaring a variable without initializing it, but with a function instead of a variable. You name the function and define its parameters, but you don't give it any logic (you omit the body.)

Forward declarations can be applied to more than just functions. FD's can be applied to variables and user-defined types, for instance. 

FD/FP's are also used to tell the compiler to look for a function outside of the current file but within the current project. In XCode, it looks like the compiler will look within the entire project, since I created a file called "secondary.cpp" (with header "secondary.hpp") that contained a function called "add(int x, int y)". The "main.cpp" file referenced this function with a forward declaration/function prototype, and the project successfully compiled, linked, and ran when I pressed cmd+R.