My Profile Photo

Andrea Maglie


Software Engineer, guitar player. Founder of GDG (Google Developer Group) Venice.


Don't waste your time coding - part 1

Life is too short: please, don’t waste your time writing code! I’m not saying that you don’t have to code, but many people tend to waste too much time typing on the keyboard instead of producing code. Mee too.

Following examples relate to Android’s world, but that can be easily adapted to other languages and programming tasks.

Aliases

Android developers, how many times do you have to call the command adb? If your installation directory is something like

/opt/android/sdk

maybe you’re going to write /opt/android/sdk/platform-tools/adb many many times a day.

Don’t do it! Instead add the android /opt/android/sdk/platform-tools/ directory to your path or create an alias so you will only need to write adb!

I’ve also an alias for writing only logcat instead of adb logcat! ;-)

Some other alias examples:

# android
alias amstart='adb shell am start -n'
alias amkill='adb shell am kill'
alias media_scan='adb shell am broadcast -a android.intent.action.MEDIA_MOUNTED -d file:///mnt/sdcard'
alias packages='adb shell pm list packages'
alias install='ant debug install'
alias install='./gradlew installDebug'

# svn
alias svnlog='svn log | less'
alias svn_ignore_edit='svn propedit svn:ignore . --editor-cmd nano'

# git
alias revert='git checkout --'

Snippets and Templates

Every modern IDE has the support for snippets and templates.

Explore the built-in snippets and templates included in your IDE and use them as an example to write your own. You can create small blocks like a try/catch implementation or generate an entire class.

I’ve changed the default Android Studio template for try/catch block including exception logging. Go to File -> Settings -> File and Code Templates, select Catch Statement Body and change the default implementation with:

Log.d(TAG, "got exception", ${EXCEPTION});

If you use Robolectric you can change the default Junit4 Test Class implementation with the following, so annotations and basic imports are added automatically:

import static org.junit.Assert.*;
import static org.fest.assertions.api.Assertions.*;

import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

@Config(emulateSdk = 18)
@RunWith(RobolectricTestRunner.class)
public class ${NAME} {
${BODY}
}

In Eclipse templates are under Preferences -> Java -> Code Style -> Code Templates and Preferences -> Java -> Editor -> Templates.

If you prefere Sublime Text you can find some useful Android Snippets in this repo: http://github.com/ribot/SublimeAndroidSnippets

comments powered by Disqus