Saturday, September 19, 2015

External Javascript Resources in Ionic Applications

Fun Fact: If you're using an externally referenced Javascript file in your Ionic browser, it will 404 out due to the default content policy on Android phones. This happened to me recently while I was working on an app that uses the Google Maps API to geocode locations.

Running the app in your browser via `ionic serve` does not reflect the issue - it appears only on actual devices. When I built the app and installed it on my phone, it showed me a very helpful blank screen.

I connected the Chrome debugger to the Ionic webview on my phone, and pretty soon tracked the problem down to the Google Maps API js file erroring out. A quick Google search revealed that the best way to get around this issue is to run `cordova plugin add cordova-plugin-whitelist`. The Whitelist plugin allows you to load third party Javascript files as part of your Ionic app. I'm surprised this isn't officially documented anywhere in the Ionic documentation - it seems like a pretty crucial issue to me!

Monday, August 3, 2015

Error opening file '{{path}}/difftar.gpg': Input/output error


While using Ubuntu's built-in backup tool, which uses Duplicity, I recently got the following error:

Backup failed
Error opening file '/media/yoshee/FreeAgent Drive/Backup/duplicity-full.20140514T064948Z.vol2132.difftar.gpg': Input/output error


After some research, I discovered that the best way to fix this on an NTFS drive is to boot into Windows - using either a Windows Live DVD or an actual machine you have with Windows installed - and run the following command in the command prompt:

chkdsk D: /R

Where D is the letter of the drive you want to check. /R asks check disk to fix and resolve any issues it finds.

Make sure you are running Command Prompt as an administrator. 

The process took six hours for me on my external hard drive, but once it had run through, I was able to complete my backup from within Ubuntu with no issues.

Wednesday, January 21, 2015

ASP.NET UserControl.Visible = true failing

Today I came across a situation where I was trying to set an ASP.NET UserControl to be Control.Visible = true, and try as I might, the call was failing. Here's what I did in the immediate window in Visual Studio.

MyControl.Visible = true;
MyControl.Visible; // output: false;


I almost bashed my head against my keyboard. It turns out that 'Visible' behaves like a property, not a field. You can set it to true, but it only returns true if the control is actually visible on the page. In my case, the control was not visible because I had set

ParentControl.Visible = false;

on Page_Load. I was trying to set MyControl.Visible to true on postback, but since ParentControl had not been explicitly set to true, MyControl.Visible was still returning false. I set

ParentControl.Visible = true;

et voila! MyControl.Visible immediately returned true.


Lesson: if you're trying to set your control's Visible property to true, and it's still returning false, check the parent controls and see whether those might not have their visibility set to false.

Wednesday, December 31, 2014

Takeaways: Tuesdays with Morrie

I recently read Tuesdays with Morrie by Mitch Albom. Apparently, it's a bit of a staple in the American education system, but I hadn't read it until now. My initial thoughts are thus: in one sense it was a bit of a rehash - most of the ideas Albom brought up are already familiar. However, he brought them up in a stunningly, emotionally moving way that was an incredible reminder of these ideas.

One of my coworkers described Tuesdays with Morrie as bringing up interesting ideas, but not really describing what to do with them. I disagree - the book might not explicitly describe what should be done with its ideas, but it does show you. It shows you via Morrie's thoughts, speech and his relationships.

Here's a quote from the book that describes the purpose it serves:

"We don’t get into the habit of standing back and looking at our lives and saying, Is this all? Is this all I want? Is something missing?" He paused. "You need someone to probe you in that direction. It won’t just happen automatically."
Tuesdays with Morrie attempts to probe us in this direction. It is the final thoughts of an old Professor who's dying a slow death of ALS (of ice bucket challenge fame), as written by one of his former students (Albom) who visits him once a week in the days leading to Morrie's death.  Morrie's visible and repugnant decay is well-captured by the book, and it also captures his cheery and positive attitude through this dreadful disease along with the final thoughts of a dying man, rendered with the clarity that only death can bring.

One of the most important questions Tuesdays with Morrie wrestles with is the meaning of life.

The Meaning of Life

Here's what Morrie says on the subject:
"So many people walk around with a meaningless life. They seem half-asleep, even when they’re busy doing things they think are important. This is because they’re chasing the wrong things. The way you get meaning into your life is to devote yourself to loving others, devote yourself to your community around you, and devote yourself to creating something that gives you purpose and meaning."

So what is important? Morrie describes three things:
  1. Loving relationships
  2. Good community
  3. Meaningful work 

"Devote yourself to loving others, devote yourself to your community around you, and devote yourself to creating something that gives you purpose and meaning. “You notice,” he added, grinning, “there’s nothing in there about a salary.”"

Most psychology research guides us towards these same three things. Things like money are important, but only as far as they take you above the poverty line. Once you have a roof over your head and food on your table, the correlation between money and happiness stops. Focus on these three things. Let's talk about them:

Loving relationships

The most important of these is your relationship with family. Blood flows thicker than water, and your family will be there for you when no one else will. Here is a key quote from Tuesdays with Morrie that stood out to me:

"The fact is, there is no foundation, no secure ground, upon which people may stand today if it isn’t the family."
Oftentimes, we tend to replace family with a fascination with career, or we work on increasing our wealth. Wealth and a meaningful career are good in their own right, but there is something that only your family can provide. Even friends are a poor substitute for family.

"It’s what I missed so much when my mother died—what I call your ‘spiritual security’—knowing that your family will be there watching out for you. Nothing else will give you that. Not money. Not fame."
Morrie was dying of ALS - a slow, dreadful decay - and Albom, looking at him, wonders this:

"Yet when I looked at Morrie, I wondered if I were in his shoes, about to die, and I had no family, no children, would the emptiness be unbearable?"
Do you want to die surrounded by friends and family? Or do you want to die alone?

Death is always around the corner, and you never know when it might trip you up. I have a close great aunt who's very old, and she had an unmarried daughter who until recently, lived with her and took care of her. My great aunt slowly transferred all her assets to her daughter's name, expecting to pass on soon. This past summer, we learned that her daughter had breast cancer. It was a particularly vicious strain of the cancer that tore through her body, and three weeks after the diagnosis, her daughter had passed away.

If you've been putting off getting in touch with your family, stop. Get in touch with them now. Pick up the phone. Send them a text, put in a call, write them a letter or an email, whatever you're comfortable with. "Don’t assume that it’s too late to get involved," says Morrie, and he's right.

It might be awkward if you haven't talked to them for a while, or if you got estranged for the wrong reasons, but you've likely done more awkward things for less important reasons. Don't let this slip. If you think they've wronged you, or that you have a right to keep distant, consider this:

“Mitch,” he said, returning to the subject of forgiveness. “There is no point in keeping vengeance or stubbornness. These things”—he sighed—“these things I so regret in my life. Pride. Vanity. Why do we do the things we do?”
There is wisdom in a dying man's words. Why do we do the things we do?

One of the most important relationships you can have is that with your spouse. It can be the most powerful and fulfilling relationship you'll ever experience, and it can also be the most devastating.

The most important thing to do when getting married is to choose right. You and your spouse need to share similar values. You don't need to be the same person - it's okay if one of you loves Chinese food and the other hates it - but you need to have the same attitudes towards important things in life - like, honesty in the relationship, how much you'll emotionally depend on each other, the values (or faith) with which you'll raise your kids, attitudes towards each other's family. These things don't usually change with time.

This is the great filter through which you must evaluate potential spouses, but once you're done, the work isn't over, it's only just begun. You'll never have to work harder to keep a relationship than that with your spouse. Luckily (or hopefully) you should both be equally invested in the relationship, and willing to put in the legwork.

Morrie gives us three things to watch out for:

"If you don’t respect the other person, you’re gonna have a lot of trouble. If you don’t know how to compromise, you’re gonna have a lot of trouble. If you can’t talk openly about what goes on between you, you’re gonna have a lot of trouble. And if you don’t have a common set of values in life, you’re gonna have a lot of trouble."
The values we've already talked about, and those are the most important because they are difficult to change. The rest of those can be learned. Watch out for these caveats, and try and build openness and respect. Relationships are about give and take so learn to compromise - for example, if you insist on going to a Chinese place once in a while, it's okay, but try and choose a place where there's something on the menu your spouse will be willing to eat.

Good Community

There's a strong positive correlation between more religious/spiritual people and life expectancy, but any Psych 101 textbook will tell you, this is more because of the communities these people are part of on account of their faith, rather than because of the faith itself. We need to find such communities to be part of. The best way to become part of a community is to give to them. Morrie says:

"Giving to other people is what makes me feel alive. Not my car or my house. Not what I look like in the mirror."
Relationships in which you give are important to you because they make you feel needed. Remember that this is an even exchange - you might be giving your time, or a friendly ear, or even money, but you get in return a valuable sense of fulfillment. A conversation between Morrie and Blom goes as follows:

“The truth is, you don’t get satisfaction from those things. You know what really gives you satisfaction?”
What?
“Offering others what you have to give.”
You sound like a Boy Scout.
“I don’t mean money, Mitch. I mean your time. Your concern. Your storytelling.”

Sometimes all other people want is to be noticed. Morrie tells a story from his early adulthood when we was researching a mental hospital. A patient there would everyday just go to a hallway and lie on the floor, unmoving. The staff eventually learned to ignore her and would step around or even over her as they went about their business. The young Morrie was moved, and he went up to her and started talking to her. Eventually, he convinced her to get off the floor - it turned out that the only reason she was doing it was so that she was noticed. In the end, that's the driving force for most of us - we just want to be noticed.

Go out of your way to notice people. There's an art to this, encapsulated perhaps in this sentence: 

"When Morrie was with you, he was really with you. He looked you straight in the eye, and he listened as if you were the only person in the world."

Meaningful Work

Work is good because it gives us a sense of purpose. That sense of purpose helps us be fulfilled. However, in order for this to be true, that work has to be meaningful. Whether you're a software engineer who has moved up to management and hates it there, or an insurance agent who rips people off and hates themselves for it, or a corporate drone who is tired of playing office politics, if you don't have meaning in your work, you won't be happy.

Our culture drives us to go up the corporate ladder, but Morrie says: 

"Forget what the culture says. I have ignored the culture much of my life. I am not going to be ashamed. What’s the big deal?"
Find work that makes you happy, and do it. We already established that money is only good for taking you above the poverty line. Once you're above it, money has no effect (perceived or otherwise) on happiness, so as long as the bills are getting paid, what does it matter what you do? Don't let inertia cramp you for the rest of your life.

"And you have to be strong enough to say if the culture doesn’t work, don’t buy it."

Final thoughts

I want to close out with the following statement from Morrie that really stood out to me:

"Do what the Buddhists do. Every day, have a little bird on your shoulder that asks, ‘Is today the day? Am I ready? Am I doing all I need to do? Am I being the person I want to be?’"
Have you done what you need to do? This doesn't just mean backpacking across Europe, it also means having a plan in place to take care of your spouse and kids or any other dependants (including your parents) if something happens to you. Do you have life insurance? Any other documents your survivors might need to know about? Did you write a will, so that your estate isn't locked up in court if something happens to you? Protect your loved ones by thinking beyond yourself. Your death might not be slow and decaying like Morrie's - you could die in a car accident tomorrow.

Don't shy away from the thought of your own transience. If you embrace it, it might just make life more beautiful:

"You see that? You can go out there, outside, anytime. You can run up and down the block and go crazy. I can’t do that. I can’t go out. I can’t run. I can’t be out there without fear of getting sick. But you know what? I appreciate that window more than you do."
Live life. Don't be afraid. Learn to love and be loved back. Life is defined by the people around you and the relationships you develop - not by money and cars and big houses. You can't take money and cars and big houses with you to grave with you, but when you die, "Death ends a life, not a relationship." You might be gone, but your relationships will survive you.

If you haven't read this book already, I highly recommend it: Tuesdays with Morrie: An Old Man, a Young Man, and Life's Greatest Lesson. If you're more into Audiobooks, here is a link to the Tuesdays with Morrie Audiobook.

And here's a non-affiliate link.


Wednesday, December 17, 2014

Takeaways: The Healthy Programmer

I recently picked up The Healthy Programmer by Joe Kutner. The 8+ hours of programming are beginning to take their toll on my health with various headaches/eyeaches, so I wanted to see if Joe had any insights into what I can do to stay healthy. Here are my key take-aways from Joe's book:

Walk Walk and Walk Some More

One of the big points that Joe stresses is the importance of walking. He tears through the current "standing desk" fad sweeping through the nation. Numerous studies can be cited which state that standing for 8 hours a day can be as bad for you as sitting 8 hours a day, albeit in different ways. The best thing we can do for our bodies is to take frequent breaks in which we walk. Walking gets the heart pumping, which is the source of the health benefits associated with walking. 

Walking is also associated with better cognitive function. We learn and remember things better while walking, which is extremely important for programmers. 

There are three areas of improvement I drew from the Healthy Programmer:
  1. If you get stuck on a difficult problem, take a walk. Walking will jog the brain and help it get out of its current rut of thinking.
  2. Take a walk either before or after learning something new. This will help you remember it better.
  3. Take a quick walk every 25 minutes. 
For that last bullet point, take advantage of the Pomodoro Technique. There is a Pomodoro Timer is available for Mac and Windows. I installed it on my work computer, and it looks like this:

You can set that little popup to always hover on your screen, and it alerts you when time runs out. When you get the alert, simply get up, walk around for a few minutes, look outside the window (to rest your eyes), and then reset the timer and get back to work. Using this technique will have remarkable effects on your health, and it will significantly lower your mental fatigue and increase your productivity.

Take care of your eyes

Imagine not having your eyes. It's a terrifying thought. Taking care of your eyes is important, and as programmers, we're already at a disadvantage because we stare at screens for 8+ hours a day. 

The Pomodoro Technique described above is a good way of giving your eyes the rest they need. Everyone knows they're supposed to rest their eyes and look in the distance every 15 or so minutes, but when you're in the middle of tracking down that obscure bug, an hour or several hours can go by before you remember to look up. The Pomodoro Timer is a good way of forcing yourself to look up and away more often. 

If you think there's a productivity cost, think about how productive you would be if you lost your eyesight. Your body is important. Take care of it. 

In addition to the technique above, I've also put a post-it note on my monitor with the message: "BLINK!" People tend to blink less while staring at a computer screen, so every time I see this note, I stop what I'm doing and blink several times for a few seconds. 

I've also lowered the contrast and brightness of my monitor so that it matches the brightness of my office. I've also put the display settings to "warm" - per Joe, red hues are better for the eyes than blues. 

Visualize breaking bad habits

Imagine yourself breaking through your bad habits. For example, if you know you go to the vending machine for chocolate at noon everyday, and you've decided you're going to stop doing that, then visualize several times over the course of the morning how you're going to not go to the vending machine at noon. If it helps, visualize yourself doing something else at noon - maybe going to the water cooler, or going to a coworker's cubicle, and then do that other thing. Once you've done it a few times, your chocolate urge will be significantly lower come 12PM.

Life should be a series of small improvements

I loved this point. "Set a new personal record everyday," says Joe. In many many small incremental improvements, we can make dramatic improvements in our lifestyles over the course of weeks, months, and years. Think about your daily routine. Is there something small you could do that would improve it? Seize that thing and fix it. Spend a few days making sure it's stamped out. Then pick on the next thing you want to improve and repeat.


I do recommend picking up the book I got these tips from - The Healthy Programmer (disclaimer: affiliate URL). It's not the best book in the world (it doesn't claim to be), but it does have some valuable nuggets of information that make it worth the time and energy spent reading it.

Friday, December 5, 2014

A Story about YAGNI - You-Aren't-Going-To-Need-It

Recently I was working for a project on a client where they wanted the project to be Live for a certain portion of time during the day - so for example between 9AM and 2PM, or 12PM and 3PM, or so on and so forth.

While implementing this feature, a thought struck me - what if the client wanted to have a period that ran past midnight - so for example between 11PM and 2AM? At this point, the intelligent thing to do would have been to pick up the phone and call the client to ask. Instead, I thought to myself - well, even if they haven't asked for it now, they might need it later. Besides, I could build a decent set of unit tests and use them to catch all the edge cases, right?

Wrong.

What ended up happening was that I spent many hours writing the unit tests to capture the many edge cases that came with crossing the midnight deadline during a time period. I spent even more time writing the code to deal with them. The project went over-budget thanks to that extra time I spent, and the client never ended up using the added feature. After a few months in production, the client scrapped the project (albeit for unrelated reasons). 

Lessons learned: unless you're very strongly confident that you need to build something for the future, don't do it. Don't fall prey to what-if's. Remember: YAGNI - you aren't going to need it. And if you do need it, you can build it later, when it's accounted for in the budget. Finally, don't be afraid to pick up the phone and ask a question. It makes everyone's life easier.

Monday, November 17, 2014

Postback not happening on ASP:LinkButton with Adblock Plus

I recently had a problem while working on an ASP.NET website where I had a page doing a postback on an ASP LinkButton, except in FireFox with AdBlock enabled, the postback just wasn't happening. Clicking on the button resulted in nothing. When I examined the markup on the page it looked like this:

<a id="ctl00_ctl00_MainContent_Content_btnReserveTickets" class="btn" href="javascript:__doPostBack('/* postback param*/','')">Buy</a>

It looked like the Javascript initiating the postback should have been firing. I put a breakpoint i that __doPostBack function, but it didn't get hit. There were no console errors either. I racked my mind and googled around with no luck. The only potential solution I could find - to add a CausesValidation="false" attribute to the LinkButton - didn't work.

Finally I posed my question on StackOverflow and walther over there gave me a hint that led to the solution. He suggested that I look for AdBlock's blacklisted keywords that may have been attached to the name or attribute or something of the LinkButton, such as "ad" or "facebook" or so on and so forth.

In the end, I discovered that there was a function adding a click handler to the LinkButton that was sending a Google Analytics event, as follows:

$(anchorEl).click(function () { 
                        ga('send', 'event', 'outbound', 'click', anchorEl.href, {
                            'hitCallback':
                              function () {
                                  document.location = anchorEl.href;
                              }
                        });
                        return false;
                    }); 

It turns out that once this click event gets attached, and because it's Google Analytics, all javascript on the LinkButton gets stripped off!

In the end this blogpost guided me to a solution: I wrapped the click event binding above in the following if statement:

if (ga.hasOwnProperty('loaded') && ga.loaded === true) {
    // bind event
}
This means that if AdBlock is ever blocking Google Analytics from loading, the anchorEl's click event is never bound to that Google Analytics event (binding it successfully would be useless anyway, since Google Analytics isn't loaded), and AdBlock doesn't strip the Javascript from my LinkButton.