Invariant Properties

  • rss
  • Home

Deer In A Headlight Mode

Bear Giles | November 30, 2013

Ah, technical interviews.

It was a perfectly reasonable question but 1) we were discussing heaps so I naturally started thinking about the various things I learned in a graduate class in algorithms, e.g., how you rotate the tree as you add and remote values (read: check my copy of CLR) and 2) I’ve been studying Scala for the past few weeks and trying to learn how to “think in Scala.” We’ve covered trees and how you can manipulate immutable objects. We’ve covered Nothing (the bottom class) and how that’s different from Nil (null). So that means…

… so that means I had a nice case of brain lock. Fortunately I can babble well enough that people can still tell that I have a clue, it’s just really embarrassing to not being able to snap out an answer to a simple question.

At least I didn’t start the sample code with “def class …”!

The other fun question was synchronization. What can you say in a phone interview when you can’t see if you’ve lost the other person? For the record:

  • You want to use a static ReentrantReadWriteLock if possible since readers do not need to block each other. You only need an exclusive lock on writes (and read locks so the writer knows there are active readers).
  • If you don’t use (java.lang.concurrent) Locks then you should synchronize on a private static field in your class.
  • You want to avoid using “synchronized” on the method or a block (without using a target) because the default behavior is to lock on the class object. This can be used for all sorts of mischievous.

Two postscripts:

First, this is one reason why I’ve become such a fan of unit testing if not outright TDD. It’s not because I think everyone writes sloppy code, it’s because the more you learn the more you realize that you’ve made assumptions. It’s a lot better to add a test case (e.g., say, can a list contain a Nothing value like it can contain a Nil/null value?!) than to check the code yourself and risk someone else making an unsafe assumption later.

Second, for the same reason I’ve become a much stronger proponent of using the standard libraries whenever possible, even if it’s not the most efficient approach. It comes back to assumptions again – the people who write the core libraries have had a long time to think about all of the corner cases. Unless it’s a real-time system where performance is the absolute top priority the milliseconds added by an inefficient call are probably better than the hours if not days spent tracking down some obscure bug. But it’s not an absolute – a millisecond here and a millisecond there and 100 concurrent users and you have a sluggish system. It’s always an engineering and management tradeoff of what hurts the least.

Categories
java
Comments rss
Comments rss
Trackback
Trackback

« Security Fail Race Results »

Leave a Reply

Click here to cancel reply.

You must be logged in to post a comment.

Archives

  • May 2020 (1)
  • March 2019 (1)
  • August 2018 (1)
  • May 2018 (1)
  • February 2018 (1)
  • November 2017 (4)
  • January 2017 (3)
  • June 2016 (1)
  • May 2016 (1)
  • April 2016 (2)
  • March 2016 (1)
  • February 2016 (3)
  • January 2016 (6)
  • December 2015 (2)
  • November 2015 (3)
  • October 2015 (2)
  • August 2015 (4)
  • July 2015 (2)
  • June 2015 (2)
  • January 2015 (1)
  • December 2014 (6)
  • October 2014 (1)
  • September 2014 (2)
  • August 2014 (1)
  • July 2014 (1)
  • June 2014 (2)
  • May 2014 (2)
  • April 2014 (1)
  • March 2014 (1)
  • February 2014 (3)
  • January 2014 (6)
  • December 2013 (13)
  • November 2013 (6)
  • October 2013 (3)
  • September 2013 (2)
  • August 2013 (5)
  • June 2013 (1)
  • May 2013 (2)
  • March 2013 (1)
  • November 2012 (1)
  • October 2012 (3)
  • September 2012 (2)
  • May 2012 (6)
  • January 2012 (2)
  • December 2011 (12)
  • July 2011 (1)
  • June 2011 (2)
  • May 2011 (5)
  • April 2011 (6)
  • March 2011 (4)
  • February 2011 (3)
  • October 2010 (6)
  • September 2010 (8)

Recent Posts

  • 8-bit Breadboard Computer: Good Encapsulation!
  • Where are all the posts?
  • Better Ad Blocking Through Pi-Hole and Local Caching
  • The difference between APIs and SPIs
  • Hadoop: User Impersonation with Kerberos Authentication

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Pages

  • About Me
  • Notebook: Common XML Tasks
  • Notebook: Database/Webapp Security
  • Notebook: Development Tips

Syndication

Java Code Geeks

Know Your Rights

Support Bloggers' Rights
Demand Your dotRIGHTS

Security

  • Dark Reading
  • Krebs On Security Krebs On Security
  • Naked Security Naked Security
  • Schneier on Security Schneier on Security
  • TaoSecurity TaoSecurity

Politics

  • ACLU ACLU
  • EFF EFF

News

  • Ars technica Ars technica
  • Kevin Drum at Mother Jones Kevin Drum at Mother Jones
  • Raw Story Raw Story
  • Tech Dirt Tech Dirt
  • Vice Vice

Spam Blocked

53,793 spam blocked by Akismet
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox