Google+ Badge

Thursday, March 20, 2014

Is it fast enough - the lost art of building fast programs

When I was writing programs for the stock market speed and memory usage were huge issues. You dare not leak any memory because 100,000 headlines coming your way in a 24 hour period with a memory leak was a sure way to crash a program.

Speed was also very important. In the news business being first, and maybe only, source to get a headline out was critical. It needed to be on the screen right away. Turns out accuracy from the source was of less importance, they could always correct things in the story body later.

After the stock market company outsourced everything to India I work on tamer programs that did not use real-time data. Of course you still don't want to leak memory and you want things to be fast but the thrill and stress of that waned considerably.

Writing mobile apps brought a lot of that back. Not only do you want to avoid memory leaks you want to have as small a footprint as possible. The CPU / GPU combination on a mobile device is much slower than the desktop you are developing on. Speed is again a big factor. No one wants taps and swipes to feel like molasses. You have much less screen space to deal with and you need to use it wisely. Everything was about speed, space and time again. The thrill was in the air. I never stopped coding like size and speed mattered but the importance bubbled to the top again and I loved it.

Right now I am helping out with a web application using JavaScript and AngularJS. I have used JavaScript with JQuery in the past and I find AngularJS to be big step in the right direction. The app really just needs to be fast enough to be usable. Simple request / reply / show it on the screen operations. Speed is in no way critical and really is just an afterthought if considered at all. We are also using a number of JavaScript libraries including Underscore.js.

As I use various methods in these libraries I wonder about their speed and efficiency. You do seem some mention of that when you query about Underscore array methods on Stack Overflow. I don't feel enough attention is focused here and I try to find and use the most efficient method for my code. Sure, it is not bad to make a new copy of an array to add or remove a few elements but if that array grows tenfold what happens?

It is so easy to look at code and think each line of code takes the exact same amount of time to execute. Here is this wonderful AngularJS controller and it is only 20 lines of code! Dang it must be fast because when we did something similar in JQuery it was 100 lines. Sadly reality does not work that way. Each of those 20 lines can be running all sorts of framework code. That one call to an Underscore method might be 50 lines in itself. Dynamic injection is happening. DOM changes are occurring. Watch methods are being called. Other controllers are receiving messages and running lines and lines of code.

It is wonderful when the framework takes care of the busy work. I want to focus on the business logic at hand. I love it when a few lines of code updates four separate areas of the screen. I have written multithreaded framework code to perform those types of actions. Set a variable to false and have everything that cares disable - the menu item, the toolbar button the right button menu option, etc. If the user of my framework put the call to toggle the setting between true and false in a loop it looked innocent but man a lot of stuff was happening in the background.

I still want to know what is going on behind the scenes. When I called that line what was the ripple effect? If I add an item to that collection does it create a whole new collection and return it to me or just add it? Did it walk the entire collection to add it to the end or is the push operation fairly native? Does it scale well or do I need to use a different object or collection or framework to do this job? Will the client have 10 of those or 1,000 of them?

Speed is hard to add but easy to lose. Optimizing early can be a waste of time. Not thinking about optimizing at all can kill a project. Right now I am trying to learn so many new things in the JavaScript arena it is hard to know if I am doing it correctly. As I write the code I try to look for the optimal way to do it. Luckily code reviews help point out when I missed a better way. Taking time out to find web references is also needed. I try to scan over all the available methods in a library even if I don't need them now just in case I need them later. I may not remember the exact name but if I remember there was a method I can go back and find it and give in the in depth evaluation pass to decide if it is usable.

Tuesday, March 11, 2014

Let's have some IDE fun

During any given day I might be in 6 different IDEs. Eclipse, IntelliJ, Android Studio, Xcode, AppCode, and Aptana. Generally I have an instance of Sublime text editor running too. I use them for the following things:

Eclipse - Java server side work (our REST services)
IntelliJ - Java one off utility work
Android Studio - Java Android mobile development
Xcode and AppCode - Objective C iOS mobile development (some Mac work too)
Aptana - JavaScript with AngularJS, bootstrap and other libs
Sublime - taking notes, small file edits including .gitignore and other system type files

At times it can drive you crazy. I use a Mac at work due to my iOS needs and depending on the IDE I might use CMD+S to save or Ctrl+S. Since I have a PC background and I switch to Ctrl+S at home in any IDE I am in when there. I screw up the keystrokes at times but generally have a feel for which IDE I am in at any given moment.

Eclipse is the old standby I have use for years. There are a ton of plugins for it to do nearly anything but you can't always get them to work as expected or even together. We just switched from SVN to GIT at work. Probably need to throw out the SVN plugins as I will not be using them and no real need for them to load up each time. Also the menu system gets a bit cluttered. Heck it might be a good time to start with a fresh Eclipse install as I also have the Android plug-ins running and I have switched to Android Studio for that work. Code completion works ok but is not spectacular. Refactoring works and has not crashed on me. The editor is fast and has a ton of features.

IntelliJ has a great Dracula dark theme built right in. I run a dark theme in Eclipse too but it does not theme everything perfectly. I handle one off programming in IntelliJ. Testing code bits, working on small stand alone utilities. I could do some of that in Eclipse too but I tend to have the server stuff open there and switching workspaces in Eclipse is slow as it is a full reload of the IDE. I could use workspaces in Eclipse and I do at times but our server stuff is rather large so I prefer to leave it sit by itself.

Android Studio is an IDE I like a lot as it is IntelliJ based. There have been some growing pains with the constant updating to the newest version of gradle but in general the IDE is superior to Eclipse. It has better code completion, a better preview window for your XML based layouts, shows the colors and icons in the gutter, makes it easier to convert string into the string table, shows the replacement string in code instead of the string ID and a bunch of other smaller bits of "thanks for doing that" areas. I would say that Eclipse is faster at building and getting the code on a device but I am willing to forgo that speed for all the other features Android Studio offers. It works well with Genymotion, the device emulator I use.

Xcode is my least favorite IDE of the bunch. I have to use it for iOS development but it is behind the times in areas like refactoring and its code completion tends to not be as helpful as other IDEs. Sure, you get a full list of what is possible but it does not move the most commonly used ones to the top of the list. It also crashes on me more than any of the other IDEs especially when it comes to refactoring. I have had it crash when refactoring a variable that only appears twice in a 20 line method. It makes you scared to use it. It was also terrible when it comes to using SVN. GIT is much better. I wish it would offer to automatically include needed H files instead of my manually moving to the top of the file to type them in.

AppCode is also from IntelliJ. I use it when I am doing heavy Objective C coding when I already have the UI in place. So far its refactoring has worked every time crash free. It has more intelligent code suggestions, if you add a reference to an object it will prompt to add the proper H file, it handles CocoaPods nicely. There are so so many areas where it is coder friendly. Sadly it does not have an Interface Builder replacement so you end up back over in Xcode to handle those chores. They sync together without a hitch and both use the same project files making moving between them relatively painless.

Aptana has some nice code completion aspects for CSS and HTML along with a solid JSHint tie in. Since our server build does not allow JSHint errors to pass it makes it so much easier to catch them before you push to master. It is Eclipse based but focused on JavaScript which means I could end up cluttering it with plugins but so far I have kept it down to the minimum I need for JS programming. I have used this IDE flavor for the least amount of time of those listed but with my heavier involvement in JavaScript I have been very happy with it.

Sublime is my go to editor. I leave multiple tabs open with "To Do" lists for each of the projects I have going at any time. This allows me to type in quick notes so I don't forget something because people tend to drift in and out of my office with suggestions the mobile app, to share some tidbit of useful information about the server, a GIT command or something else helpful. I have a file for general work info, one for Android and one for iOS info open all the time and then I open and close other files as needed. The color coding support in Sublime for a multitude of file formats is awesome. I also use it to pretty print XML and JSON when I copy / paste server responses out of the Chrome web tools window. 

It can fry your mind to shift in and out of so many programming languages and IDEs in any given day. Of course I try to keep it to a minimum but that is not always in my control. My preference would be to stick in the world of mobile doing Android and iOS development. Right now that is on hold so I am helping out on the JavaScript team. Lots of time on Stack Overflow and doing web searches as my brain does not have a solid cache of JavaScript programming idioms and patterns. Looks like Aptana it is for the next few months.

Thursday, January 16, 2014

Demoing with Netgear PTV3000 a projector and a Note II

I needed to demo the Android application I had written at a company all hands meeting. I came up with a couple of potential solutions as I only had a few days to figure this out.

1) Run in the Android emulator


Uses same laptop as you use for the PowerPoint.


People might consider this to be a cheating slideshow as you are not on a "real" device.
You have to manually rotate the emulator
Emulator can show crappy gradients and other visual effects

2) Use Google android screencast


Simple JAR file to run on laptop that is already showing PowerPoint.


Rotation not supported.
Only runs at 4 fps or so.
Scales output and it is not pretty
Can look like a cheat and not a real device

3) Use Micro USB to HDMI cable


Not very expensive
Shows you are actually using a device
Can charge phone at same time as sending video


Was not able to find one locally, would have to overnight
Tends to not be generic, different cables for different device manufactures
Can't walk around with phone as you are tethered to the display device

4) Netgear PTV3000 wireless HDMI 


Wireless, you can walk around with your phone during the demo
Auto rotates to match your device
Fast - I was able to watch a video on my TV
Works with a large set of Android and Windows devices
Proves the code is actually running on a device


Most expensive option but at $60 it was not a bank breaker
Does not work with iOS devices
Usage of HDMI to other adapters might not work

I went with the Netgear PTV3000 as a local Best Buy had it in stock. Setup up was simple, plug it in, plug in an HDMI cable and ask my phone to screencast. Worked the first time, worked every time. I tested it at home on my TV using my Note II, wife's S III and my Google Nexus 7 (2013 edition) without a hitch. Only one device is connected at a time. I was able to play games and watch YouTube videos. There is a small lag between your phone display and the TV display. The display on the TV is auto rotated as you rotate the device and both portrait and landscape appeared as expected. Landscape is the better use of screen space on a TV.

Since I have a Note II I was able to pull out the stylus and see the "hover circle" on screen as I held the stylus just above the screen surface. This is very handy in a demo as you can point out / circle where you are about to tap or point out interesting areas on the screen. Other times I have had to use a laser pointer to achieve something similar.

We wanted the output to show on both projectors at the venue. They had an odd setup and were not able to split the HDMI. We tried an HDMI to DVI adapter but the Netgear did not want to play that game. HDMI is a bit picky about copy protection stuff so I was not too surprised. A direct connect with an HDMI cable to the projector worked fine.

This is a wireless connection but it is a direct wireless connection. I was not using the hotels wireless connection at all which is how Apple Airplay works. This means we did not have to pay overpriced hotel wireless fees and I had the full WiFi N bandwidth between my device and this tiny unit.

I was able to stay connected from the back of the room which was a pretty good distance. No hiccups during the presentation. It can suck down your battery on your phone though as it is constantly sending its screen out into the air to be picked up. The Note II has a sizeable battery so that did not come into play and I had it charged up before I started the demo.

Very impressed with this small and cost effective device. The demo went smoothly. The connection between my phone and the device worked like a charm. Splitting the video was an issue we did not solve. The Note II with stylus was an excellent device for pairing and the stylus came in handy for a new purpose. I love the auto rotation matching as parts of the demo needed to be in portrait and some in landscape. I wish Apple would support the Miracast format.

We will be getting more use out of this device in the future. The kids thought it was pretty cool when I brought it home for testing. Seeing their Android games on the big screen was pretty fun for them.

The Netgear already had the latest firmware on it. I did not need to update anything. I did kick it into web server mode so I could look a the firmware version and other settings. I could have forced it into "press button to accept connections mode" but did not. I figured no one in the room would randomly connect to the device. The hotel staff took a picture of the device. They may buy one and rent it out for $75 a use and make their money back the first time a sucker, I mean client, needs it.

Thursday, December 12, 2013

How do you respond to "Who cares? It's going away soon anyway."

I am hearing this more and more "Who cares? It's going away soon, just hack in a solution!" Guess what? Nothing goes away soon. Unless the replacement is fully written and ready to ship and the users have no choice but to upgrade then it is going to be around.

What is your response when this dilemma is posed to you?

I try my best to avoid hacks. I want to use the best possible coding practices when possible. So, when are they not possible? Let's say one of your apps has been internationalized and localized. You are working on an second company app that has not been. It might drive you crazy to add yet another hard coded string to that app but doing it right and starting the whole internationalization / localization process is not going to pay off in this situation.

Maybe you are on an app that has styles mixed in to the HMTL instead of being in CSS files. Not hard to create a CSS file right? Nope but it might create large changes to the build / distribution process thus doing that towards the end of a release cycle might not be the best idea.

Maybe you end up in some Java code that only uses Vectors where is should be using List and ArrayList with generics. If the collections are localized then I would probably fix them.

I find it depressing when the development staff has given up and goes into hack mode because everything is about to be replaced with a new design. I know code outlives your every expectation.

I was working at a company three years ago that was converting from Java via WebStart in the browser to C# with WPF. For some reason the CEO hated Java even though it was half their income with the other half being the C++ version of the product. Java ran on Windows, Mac and Linux. I worked on the C# product too and left for other reasons than the switch. I don't mind C# although I did not feel it was the best solution here. They pretty much only wanted hack fixes to the Java stuff.

Three years later and guess what? Yep, the Java code is still running in the field. It is taking much longer to replace than originally thought. They have moved it to Java 7 to keep up with Oracle and bug fixes.

I have seen this happen way too often. With the web it is easier to kill something. If you replace all the JavaScript on your server the next time the user hits your web page they get the new code. They don't get the choice to continue to use the old stuff. Mobile is close. Most folks update their apps unless you toss in some new crazy permissions like read all my contacts when you are a simple game. Desktop is a bit harder. Using WebStart allowed us to force the upgrade. On the Mac you get update notifications which is nice. On the PC you pretty much get patch notifications from Microsoft and then it is up to the vendor to write their own. Adobe likes to patch stuff.

As a professional programmer I like to treat all my code the same, like it will live for a long time. I don't want to fall into the hack in in trap. Design it, write it, test it then check it in. I refactor things when method and variable names are getting confusing or no longer reflect what they do. I move things into new objects when the line count in current objects gets out of hand or where a new object or base class just makes sense.

Monday, December 9, 2013

Thought about dipping into Windows Phone dev but it requires Windows 8

I do Android and iOS programming already and I thought about dipping into Windows Phone just to test the waters. I have done C# WinForm and WPF development in the past so I know my way around Visual Studio and C#.

I did a little research and you must have Windows 8. I just am not in the mood to do that. I support my family and my extended family on all things technical. I use TeamViewer to remotely take over their machines to clean up toolbars - oh man do I hate toolbars - and other issues.

Every machine, as of last Saturday, that I support is runnings Windows 7. Finally they are all in sync. I don't want to move to Windows 8. It seems a big MS mistake attempting to combine touch screen and desktop into one platform. I have buddies who have used it and don't like it. It may be more end user friendly but it is less technical dude friendly.

Stinks, I probably could have had some fun playing around with Windows Phone development. Who knows, I might have converted on my apps to it and put it on the store. At this point I am saying NO to Microsoft. My hardware can easily handle Windows 8. I built the machine myself, which is what I have done for years.

If the upgrade was cheap I might consider running it in a VM. But MS charges quite a bit for an OS thus they are keeping me out of the experimentation phase. Real shame MS, you want more phone developers but things like this keep me away.

Oh, just in case you think "Well you bought a Mac for iOS!" Nope, I only do iOS stuff for work, not for my personal stuff, so I only do it on my work MacBook Pro. Heck I could probably Hackintosh my PC via a VM and get a Mac running cheaper than Windows 8.

Updating my first Android game. Taking on a different perspective

I wrote and published my first Android game in a week between jobs. Always good to take time off between positions so I took a week and learned Android and wrote a game. It made enough off ads to pay for entry fee into the Android market plus I learned the Android SDK and have been working mobile jobs since then.

The game had hard coded images thus it did not scale up on tablets or larger phones. I have been recoding the app over the past several months. First I redid all the graphics using Canvas drawing code. Consider them vector graphics instead of PNG files. I can pass in a size to the drawing routines and it will draw all the games pieces to fit. I have been testing on my Xoom, Nexus 7, Note II, Galaxy S3 and original Galaxy. Right now all the game pieces are drawing properly and the game code is in place. The family is playtesting the game.

I have some issues in the help screens that I need to clean up that I found late last night on the S3. Probably get that up and going this evening.

Next I want to add a multiplayer mode. This will not be playing on different devices and not be playing at same time on one device but one player attempting to solve the puzzle then handing it to the next player and the winner will be the one with the shortest time. I have a lot of ideas around this mode that I will work on once help is running smoothly.

Before I ship I need to integrate an ad network and test on all the devices again.

Great, that is all background but what about the perspective change? The first cut and release was all about speed. Getting the game working and out the door. Android tablets barely existed at the time. I tried to make stuff generic but was learning the SDK at the same time so things were hardcoded. Now I am weeding out all the hardcoding and really using the power of Android such as styles.xml, canvas drawing code, various simulators and more generic programming techniques.

At first it was really cool just to say "I have a game available from the Play Store!"Now I want to say "I have a game and it scales and plays on you device and you can play it with friends!"

The help system was confusing. People could not learn how to play. It is much better now as it uses the full game board to show you how to play instead of screen shots snippets in dialog boxes. Even doing that aided me in fixing other areas of the code.

I am taking my time. No need to rush, get it done correctly and test is out on a lot of devices. The game play is in place and the single player scoring system works. I am pretty happy with the graphics although I have been tweaking them a bit here and there. While the single player aspect is nice I feel the multiple player side of things is going to make a much bigger impact. People like to play together. A friendly competition is always a bit of fun. I hope this area keeps the game installed longer on devices and earning more ad dollars.

The more I play the game the more things I see to tweak. My son was playing last night and found an issue. Soon I hope to be playing against him. The more I play the more ideas I have for the multiplayer mode. My first thoughts are not the direction I am going to take. Some of it I figured out while doing the new help screens. It is amazing what you think up when you are forced to relook at the game.

I plan on splitting some of the code out into some other objects too. The main Activity is getting a bit large for my tastes. Most of the code is drawing things so I will be passing a Canvas into objects so they can do their part of the drawing but that is not a big deal. I won't hold on to it, just pass it in for the drawing to occur and be done.

So what is the game? Not going to say until it is ready for release. The current version in the play store, which is a few years old, does not run on a lot of newer devices.

Funny running it on my original Android phone, the first Samsung Galaxy that is now my son's primary device, and how small everything looks. It plays fine on that but once you play on the stunning display of the Nexus 7 everything else looks kind of crappy. Cool how everything scales up and looks fully detailed as I am not blowing up pixels but I am drawing to scale with all the pixels available.

A game makes you use parts of the SDK you never touch while doing business apps. I have not used sound or off screen images in a business apps. Most of those have multiple activities and fragments using standard Android view based controls. I have written custom Android controls that needed to scale but nothing with a ton of graphics. I have not need to access data from styles.xml programmatically in the past. The Android SDK has a lot of power and is very flexible when it comes to running on devices of various sizes.

If the app does well enough on the Android store maybe I will pick up a used Mac Mini and convert it to iOS. I think it would look nice on an iPad and I should be able to port over the drawing code pretty easily. I have done a lot of iOS business development so I already know my way around Xcode and Objective C.

Wednesday, December 4, 2013

Bought a Nexus 7 - my thoughts

NewEgg sent a coupon for 10% off tablets. It was time for me get a Nexus 7. I have a 3 year old Xoom that gets a ton of use around the house but I wanted something smaller, faster and with a better screen.

The Xoom was really starting to show its age. It was heavy and slow. Holding it took two hands. Sure, I would balance it with one hand but then a cat rubbing on my elbow or a kid knocking a knee into it and I would get bonked on the head or have it fall in my lap.

I was using the Nexus 7 (old style) my boss loaned me for some code testing at work. From that I knew it was the right size to be held in one hand. I also got to see how the multiple accounts worked. This is something else the Xoom does not have as they are no longer updating Android for it. I could root it and get something on there but I think I will leave it alone for now.

I had a number of goals for the Nexus 7. I want to read more and not just technical books but some biographies, non-fiction and sci-fi. I have read books on my phone and the Xoom. Again the Xoom is a bit big and heavy to hold for long periods of time plus the screen is just not that great. I grabbed some free books from Amazon via the Kindle app. There are a ton more free books than the last time I checked. It used to be the old classics but now they have a decent selection of newer books.

The screen of the Nexus 7 is impressive. I used the default Kindle font and sepia colors. The device holds in one hand as it just a bit bigger than a normal paperback book as far as height / width but obviously no where near as thick. You don't have to worry about losing your place and having the book close if your fingers slip or you drop it. I found reading on this device to be very enjoyable and plan to read a lot more books on it.

For gaming it is a really good size too. My younger son really likes it over the Xoom. Both devices get used nightly for looking up internet items, checking email, weather forecast, reading books and games. Setting them side by side really shows how much nicer the high resolution and brighter colors shine on the Nexus 7.

Being 3 year newer technology it is a lot faster than the Xoom. Bringing up Chrome on the Xoom and waiting before you can type in your search was painful. The internal browser was a bit easier but it did not share my bookmarks. On the Nexus Chrome starts up quickly and lets me get right to business.

Having multiple accounts is a boon and a bane. I like being logged in as me and only seeing my email and having my home screen look the way I want it. When I install an app it installs to my account. We are not sharing a game so my high score is mine. The levels I complete on games are mine. The bane? I have to install the app under each account if each account is admin based. I just want to pick from a list of installed apps and let them appear under certain users. Not a huge deal.

Battery life is good too. It gets used a ton and holds up well. KitKat installed a few days back. I have not noticed anything super spectacular about it. I did like the old color for the battery and time, not a big fan of white, but otherwise it has not caused any issues.

I enabled developer mode of course. Connected right up to my computer and I have been able to install APK files for the code I am developing.

So far I am very happy with the unit. It is the proper size and weight and the screen is really impressive. It is going to work for book reading, gaming and developing. 10% off was the icing on the cake.