Saturday, April 9, 2011

More on Hg (Aka Mercurial for Dummies)

Remember that tutorial on Easy App Project Hosting?  Yeah well if you followed it, I basically tricked you into implementing Code Revision software into your project. It's a good practice since it enables you to

  1. Share your code easily
  2. Work with other programmers at the same time
  3. Fix any mistake by reverting back to old versions of files
  4. Keep you code backed up

And if you followed the tutorial to the letter, you're currently using Mercurial (or Hg) as your code revision control system. Allow me to explain myself further. IFF (if and only if for those math noobs) you don't care and want a quick crash course on Hg scroll down.

BIG WARNING: If you choose to use Google code for project hosting remember that anyone in the world can look up your code. Google code is only suitable for Open Source Projects

1. Share your code easily
  • For someone to take a look at your code its as easy as "hg clone [url]" 
    • This command will download the whole project located at [url] and make a local copy on the users computer. The user will even be able to make changes to the code and commit to their local repository.
  • Make branches so your work and your colleague(s) work is separated. 
    • It's still easy to see each other codes, especially if you use Mercurial Eclipse. To make a branch go to your project, right click on the project Team>Add Branch. To see the work of your colleague(s)on another branch right click on your project again and Team>Switch To
  • Wanna take a peak at the code from anywhere in the world?
    • Your code can be viewed directly from your Google Project website in case you brainstorming a problem and you want to refresh your memory.

Before I move to point #2 I want to point out that this sounds like a really generic attempt to promote these services. I'm not affiliated with any of these companies, I just love using these tools so much that I feel like blogging about it. I guess thats enough to classify me as a nerd hehe.

2. Work with other Programmers at the same time

How is this possible you might ask? Well Hg has this cool feature called merge. You and your colleague(s) can work on one thing at the same time (even in the same branch) and when it comes time to join your work together, you use the merge tool. The merge tool works like this
  1. Import the work you want to merge.
  2. Use the merge command. Either through a command line (hg merge) or by Team>Merge
  3. When there are conflicts choose which files/parts to keep. Some Scenarios include
    1. Keep file a ,b and choose your colleagues(s) c
    2. Keep file a, use your colleagues(s) b, and use method 3 from your colleague(s) c file instead of your own.
    3. Your colleague(s) is a genius keep all his files and cry yourself at night

3. Fix any mistake by reverting back to old file revisions.

Oops wrote 300 lines and I broke the build. Hmmmm I don't feel like going through all of the code again. It came out kinda messy. Geee I wish I could just turn back time and start over from where i left the code off yesterday....WAIT A DARN MINUTE! I can just right click on the said file and  Team>Revert to yesterdays revision of the file hehehe. Sweeeet! Now I can pretend to bug track while I look at some youtube videos....


4. Keep you code backed up

NOOOOOOoooooo! I accidently deleted the project yesterday after that crazy party....beer and programming don't mix....oh yeah I pushed all my changes up the google code project site. Now just File>Import>Mercurial>Clone Existing Mercurial Repository . Ok while that loads I can just.....oh its done.


Hopefully that illustrates why a modern programmer would LOVE code revision control. I know I do. Heres some helpful hints

----------------STOP SCROLLING HERE IF YOU WERE TOO LAZY TO READ MY POST----------

Quick Explanations of Commands:

Pull - "pulls" in new versions of the code
Update - overwrites old code version with new code version
Commit - save the state of your code to your local repository
Push - "push" all the commits up to your project hosting website
Synchronize - check to see if there are any new code revisions in the project hosting website
Merge - to merge you need to have different versions of the same file in your local repository -or- choose to merge with another branch.
Add Branch - pretty self explanatory...except I would like to add that branches are very useful

Also I'm starting to refer you to other cool programming blogs. I'll start with Kaloer. He's a very respected android developer and I hope to make blog posts as useful as his in the future. Click here for his blog


Wednesday, March 30, 2011

Beautiful Code: Pt. 1

***I'm starting this new series which will highlight code that I think is pretty/gorgeous/mind-blowing or all of the above. Generally it will be simple cases, and most of it will be Java only because of my limited knowledge of coding languages. Feel free to submit code to me (Using Pastie works best I believe) or to correct code highlighted in these segments.***

findPairs()

I'm making a dice game for android with a close friend of mine. The game is called Farkle and its highly addictive. A player needs to roll 6 die and choose the best combinations to score the most points. This method I just created will return the positions of other dice with the same value. The method came out very clean and I'm extremely proud of it. Even if it's extremely simple

public int[] findPairs(int index, int value) {

      int[] dirtyArray = { 0, 0, 0, 0, 0, 0 };
      int count = 0;

     for (int j = 0; j < dieValues.length; j++) {

            if (j != index && dieValues[j] == value) {
                  dirtyArray[count++] = j;
            }
     }

    if (count == -1)     return null;
    int[] cleanArray = new int[count];
    System.arraycopy(dirtyArray, 0, cleanArray, 0, count);
     return cleanArray;
}


Edit: Teehee made a mistake and just found it. Guess that's what happens when you post "Beautiful" code before testing. Note to self: variable++ returns the variable before it got incremented


Double Edit: And another one! It's perfect now (I promise)

Sunday, March 27, 2011

Another Reason for Admob Ad's to not load and the Virtues of the Logcat

The Admob library for Android can be a bit of a pain to get it to work sometimes. Today I learned a valuable lesson: Don't put padding on the Layout where the ad will go OR THE AD WON'T SHOW UP. Unless you know how to use the logcat for android, this will be a very hard problem to solve.

Since were on the topic of logcat  here's a helpful hint if you're using Eclipse. Instead of opening up a cmd (command) line to look at the logcat and typing in "adb devices" then "abd shell" then "logcat"just go to Eclipse

Window>Show View>Other> then look under the Android Tab and double click on the Logcat View And viola!