Friday, 25 January 2013

Databases deserve a bit of respect

I've recently had the pleasure of being on a database administration course in Euston held at Learning tree, which by the way is still my favoured course provider since the quality of the presenters seems pretty high.

Anyway, the reason that I felt I had to make this post is that as a developer I always think about the code, what objects represent what aspects of the domain I am solving a problem for. I think about system performance, ease of maintenance, how readable the code is e.t.c. however since I almost always use an object relational framework of some kind not only am I mostly shielded from the way in which queries are constructed but I am almost totally separated from the actual fundamentals of the database itself.

One thing that a lot of care used to be given to was the design of a database, but now with very fast machines hard drives and all that stuff we can pretty much get away with slapping a load of tables together and let the database server handle it, and for the most part it does this very well and only in fringe cases does it require special attention but I think we/I should get back to basics from time to time and realise that the database behaves ok but could be spectacular thus relieving the rest of the system from quite so many performance enhancements that are there because unbeknown to me the database is underperforming.

The DBA on the course let slip a very simple sentence that interested me "when doing bulk imports in MSSQL Server you can greatly improve performance by switching to bulk_logging." This whole area spawn thoughts about the difference between log files and datafiles and how indexes are created and used and what the difference between an extent and a page is and how fragmentation can occur and be fixed. All these things are simple common sense to someone versed in databases but other than for academic purposes I've never had to look deeper than the surface, so think no is about time I did before I forget it all.

I have a dream, that one day all developers and database administrators will be treated equally.

Monday, 17 December 2012

Naming conventions that suck

I've been using C# for while now but I've spent most of my career to date using java in enterprise development. The naming conventions for java, the official ones from Sun that is, seem to be mostly logical and there is a well defined getter/setter access naming for beans that fits nicely I especially like that boolean member variables have a getter preceded by 'is' because it makes sense.

There is, or at least doesn't appear to be the same level of common sense with C# instead there seems to be a clinging on to the past. At least most people no longer put sz at the front of zero terminated strings or i in front of integers like back in the C/C++ days which lets face it were quite some time ago and things have moved on so get over it.

When I see code where interfaces are preceded by 'I' or member variables that are prefixed with '_' it makes the code look childish, it's what I'd expect an old school programmer/hacker from yester-year who has refused to expand their own understanding would do.

In the days of C the naming conventions were there because there were no clever IDE's so the quickest way to convey to the user what a variable was was to include something in the name which were generally kept short (I don't know but there may have been a limit on variable name length). The fact the conventions such as 'I' for interfaces and '_' for member variables is retained in a language that lets you define a variable as 'var' and let the compiler infer the type smacks of hypocrisy and those that follow it are simply refusing to grow beyond their limit understanding.

I for one am looking forward to the day that variable names contain spaces as the norm like they can in Groovy because it makes sense to be able to convey simply what the purpose for a variable is as it no longer matters how many characters are used in the name so be expressive.

A strange convention that I started using then found to be wonderful for allowing me to scan read code was to put a space after the opening parenthesis and a space before the closing parenthesis, it sounds odd but the little box that the parameters are now in really can be easy to spot whereas you can't quickly scan down code where there is no space because it seems harder for the brain (mine at least) to see the fact that the parameters are separate from the method name.

Just remembered another stupid convention used in C#, capital letters for methods and capital letters for properties. This seems like an opportunity missed since methods are usually verbs they'd fit nicely in the middle of a sentence whereas properties tend to be concepts or nouns that are more likely to be names therefore why not lowercase for methods and upper case for properties. Ok, so it's not that stupid I just prefer lower case methods as capital letters tend to stop the flow of my brain due to decades of teaching it that a full stop followed by a capital letter signifies the end of a section that the author deemed worthy of signifying.

Thursday, 13 December 2012

Machines that learn

I wanted to design a machine that learns, always have ever since I saw wargames with Matthew Broderick. Trouble is it ain't easy. Humans are very good at seeing patterns and I wondered if this has anything to do with the ability to learn. Maybe if we see patterns we're able to apply previous experience to similar situations. On another note I was thinking that a tree of decisions where each node is itself a tree creating thing. This would tie in with the idea of CNN where there is a bias feedback loop between nodes. Ok so I am starting to like the idea that each node in a tree is itself a tree creating thinking machine, I like the idea that all this tree has knowledge of is what ever is below it i.e. its children and its childrens children e.t.c.

Sunday, 18 November 2012

What to do?

I've got a load of ideas buzzing round my head at the moment so thought I'd slap them down on here so I'll be forced to actually see it through!

  1. I have a gps unit that works with the mtkbabel program, however it also has bluetooth connectivity that I've never used before. My plan is to write some j2me app that can use the bluetooth and this unit to give gps to my really old mobile phone.
  2. I want to make a javascript based drawing application that produces svg graphics, which I am only starting to play with but finding really cool.
  3. combine 1+2 so that the svg is a scaled image that the gps coordinates can be plotted on.
  4. make a good window switcher for windows 7 because all of the ones I've found so far suck donkey's balls.
  5. finish making my algebraic compression algorithm combining the fact the a rubicks cube can be put into millions (if not billions) of positions but you can get from any one to another with just 23 moves. Fractals can make infinitely complex patterns from relatively simple rules. the path from one piece of data to another can be plotted by some mathematical formula, I think genetic algorithms could be used to discover the best fit formula.
  6. I think I could take gpx and transform it to svg via some clever xslt, that'd be swish.
  7. fed up with there being no 100% standards compliant browser I've decided to make my own, however I've decided to make the entire thing a cluster of modules that can all be pulled out and replaced. This has a good side effect in that I want to describe the outcome of javascript programs as simple operations to be performed on the browser, these can be described as a series of simplistic commands much like idl or bytecode. This in turn means that no one is stuck with using javascript if they don't want to they can make a module that interprets whatever language they want into idl. Since the language will only effect the browser the sandbox will be the browser, plus a web page that uses a module that the user doesn't have can actually supply the module to the user and it will be automatically installed just for the browser. The other thing is that I like the pipeline model of IIS (and the chaining of command line apps in *Nix based machines) so I want the entire thing to be a cool little pipeline where each module effects the document as it flows through the pipe and is presented to the user :)
  8. make a world simulator, objects with gravity, collisions e.t.c. all with a view to simulating a clock mechanism and then make the mechanism a reality.

There are other things but these are the first few I can think of and want to get cracking on.

Monday, 8 October 2012

Tree Searching

It has been a very long time since I've written anything on here, what with having a baby daughter to look after and spending most lunchtimes keeping fat at bay in the gym (yes I am aware it's 80% diet, trouble is I don't eat enough, post for another time.)

Anyway I have 10 minutes before the end of my lunch break and looking round my desk can see an interesting note on tree searching.

imagine you have a tree of

                  a
                 / \
                b   c
               /\   /\
              d e  f  g
             /\ /\ /\ /\
            h i jk lm n o
I know this diagram is messy but I have 10 minutes and don't want to leave the text editor yet.

Now image the way in which this structure is expressed is as a map for example

a: b, c

would mean a has children b and c, you could then have an algorithm that would expand the first (left most) item in a list and depending on whether the expansion is stored to the left or right would determine if we had a depth first or bredth first search. I remember when I first saw this idea back in the 90's when playing with Haskell (Hugs98 if anyone's interested) and thought it was cool.

lets expand and store at the front, this would give a depth first search, ooh one other thing to note, leaf nodes are sent to the back since there is nothing to expand on.

  1. a
  2. bc
  3. dec
  4. hiec
  5. iech
  6. echi
  7. jkchi
  8. kchij
  9. chijk
  10. fghijk
  11. lmghijk
  12. mghijkl
  13. ghijklm
  14. nohijklm
  15. ohijklmn
  16. hijklmno
now if we expand the nodes but store them at the back of the list
  1. a
  2. bc
  3. cde
  4. defg
  5. efghi
  6. fghijk
  7. ghijklm
  8. hijklmno
no more expansions as we've reached the depth, so in some pseudo code it could be

expand([x:xs]) => expand[x] : expand[xs]

expand([x:xs]) => expand[xs] : expand[x]

it looks so simple :)

anyway back to work.

Thursday, 21 June 2012

Some cyclists are dickheads

A cyclist tried to play chicken with me, who was in a car, I kid you not! This is the sort of cyclist that gives the rest of us a bad name, here's how it played out. in a 40mph limit I am behind a van doing ~25-30mph obviously looking for an address or something, I see that in the distance on the other side of the road there is a cyclist but that is all for what looks like miles. I move out around the van approaching 40mph at which point I notice the cyclist is now moving towards the centre of the road, bit odd since he hasn't signaled that he's turning right any way I complete the overtake and move back over to the left and on the road in front of me the cyclist is shouting and pointing at his head like I did something wrong! I ride my bike a lot and I think it's because of arrogant pricks like this that some motorists cut me up, it's a sort of do before it's done to you response because they expect EVERY cyclist to do something crazy. What an arsehole!!

Friday, 9 March 2012

Wandering mind

The human body is amazing, it tries to keep all parts operating optimally.  If one particular part is stretched to its limit the body will try to compensate in preparation for the future.
I have no real evidence from this other than observing people that go to the gym.  What I mean is someone who goes to the gym and stretches their arms to the limit for example by lifting heavy weights will generally see an increase in the size of their arms the more they do it.  I am starting to think that the same may be true of the brain, the more you use it the better it gets.
I take this one step further by saying that because I work a very sedentary job I have to exercise my muscles outside of work, and when I was a youngster working in a factory I used to have to exercise my brain outside of work by learning languages e.t.c.

I think this is a good practice and think that maybe not enough people do this kind of exercise, just a thought.

Wednesday, 29 February 2012

Are we in a democracy?

Wikipedia defines democracy as;
Democracy is an egalitarian form of government in which all the citizens of a nation together determine public policy, the laws and the actions of their state, requiring that all citizens (meeting certain qualifications) have an equal opportunity to express their opinion.

This got me thinking about whether I live in a democratic country and according to this definition the answer has to be no.  I do not have an equal say in what happens, even the person that is supposed to represent me and my best interests isn't the person I voted to represent me, nor could I guarantee that they would have my best interests at heart.

Here's why
The things that are important to me and matter greatly to me don't effect most of the people who work in Westminster.  Most have never had the joy of getting electricity via a card that has to be topped up like a pay-as-you-go mobile, or had to make the choice between a new pair of shoes or filling the car with fuel.

I guess what I am trying to say is that the people in charge in Westminster have a different agenda, this is why there are new laws past to protect the already rich with lower inheritance tax and stamp duty e.t.c. and yet they can vote to close libraries and hospitals and post offices because there is another one only 10 miles up the road.  This is fine if you drive and can pay for the fuel as the politicians obviously can but for those that can't it may as well be a million miles up the road.

I recently read an interesting article in Dodgem Logic#2 (I know there are now 8 issues I am trying to catch up) that suggested a random selection of the real public should be chosen to govern for a limited time only before swapping, the reasoning is that the decision making politicians would be more inclined to make decisions that are beneficial to the general public as they would soon be rejoining the general public.  The only bad point in the article was that it held up Greece as the shining light of this principle and frankly the Greek government balls the whole lot up!  The article did raise the point that anarchy isn't a bad word, it simply means without leaders, i.e. everyone is seen as an equal.  This sounds a bit like the foundations of Marxism/communism, I know they are different but no one has succeeded in explaining the subtle differences to me yet so I view them as synonymous with each other and anyone who has read Animal Farm (George Orwell, not the porno) will immediately think "...but some are more equal than others".  There in lies the rub, it takes a great deal of self control to not want to post yourself as the leader, is it human nature, can it be avoided or are we doomed to always be a people led rather than equals?

My Utopia
My idea of a utopian land would be one in which everyone was free to think/do/say and learn whatever it is they wish to as long as it didn't interfere with someone else.  I think this was the general idea behind 1930's pursuit of happiness but I am not sure.  Anyway it would be nice if the goal of every human was to better themselves for humanity rather than the acquisition of wealth or power (vaguely remember Capt. Picard saying something similar).  It does sound a little far fetched at the moment, but doesn't it also sound good, and something worth aspiring to.  I sure as hell think so which is why I view a day in which I haven't learnt something as a day to regret and I don't want to live a life full of regret, who does?

Saturday, 7 January 2012

An Idea I am toying with
While I wait for some photos to upload to Google+ I thought I'd jot down an idea that I've been playing with for some time now.  But first, don't you think it's great that Google+ lets you upload full resolution pictures, it makes for a wonderful offsite backup facility!

Back to my idea;
For a long time I have worked with people that all have their own views on the way in which code should be formatted, these formatting styles only effect the way the code is viewed and have no effect on the way the code functions because white space is ignored in the languages I have used in industry unlike python which is undoubtedly a good language IMO.
The way I decided to solve this problem was to store metadata along with the code, basically a program sits between the developers IDE and cvs that alters the code the developer writes and stores it in cvs as XML the view of the code is then determined via some xslt that the developer has decided upon.

I extrapolated this idea along the inevitable evolution and decided that by being able to apply metadata to classes, methods even code blocks, if cases e.t.c. I could get the vm that was going to run the code to use this extra information to decide how to priorities the calculations in order to make concurrent programming far simpler.


I'll keep you posted on how it goes this entry was just so that I am forced to make a start on an idea I've had for a good few years now!!! How time gets away from you and these ideas never come to fruition, just like the energy generating compost heap idea I had when I was 12, I still hold out hope for that some day :)


Sunday, 23 October 2011

Single Sign on

Single Sign on
This post is a bit of an experiment, rather than like most of my posts where I come up with an idea and document it once complete I am going to blog about my progress getting this to work as I go, a sort of impressionist blog, which is most likely the way they were originally intended to be done.

What is SSO?
The way I see single sign on, which may not be the right way to view it, is that a user in a company has to interact with a multitude of computer systems, each of which requires some form of authentication before access is granted and single sign on means that a user once authenticated in one system will auto-magically be authenticated in the others without being prompted for a username and password or whatever.

The first method that I am looking at is Kerberos, the way I think this works is that a user signs in using whatever means at their disposal (card reader, password or biometric scanner) then that user has a token that is given to any other systems that require authentication.
The premise here is that there is a single secure token provider that every application within the enterprise/corporation then trusts.

JAAS the java way
Being a Java developer on enterprise systems I would have been lucky to have not had to have some experience with using JAAS the java authentication authorisation service.  This is a pluggable system like much of the JEE components where any particular vendor can make their own implementation, and as luck would have it there is already a Kerberos version available.

JBoss
I am going to use JBoss (likely version 4 but maybe 7 as I kinda like where that's going) as my application server, I may show using GlassFish too, plus I may also show it being used in tomcat just for kicks.

Step 1
In order to actually test these things out I need to actually have a kerberos system setup, as I tend to use Linux the instructions for setting this up will be linux orientated.

I followed these instructions for installing and setting up a kerberos system.
I rapidly hit a stumbling block with the first pre-requisite of the installation
Before installing the Kerberos server a properly configured DNS server is needed for your domain
D'oh! I don't have a DNS server, so I went to get one and set it up.  Doing a quick google I found these instructions which are for Ubuntu and as I am using a Debian system thought it would work.  I didn't bother following any of the reverse DNS lookup instructions as (a) I am lazy and (b) I want to move on quickly from setting up a dns.  The only thing to note at this point is that nslookup gw didn't give any results.

Back to installing Kerberos
So now that dns seems to be installed time to go back to the kerberos instructions.

Everytime I try  kinit steve/admin I get

kinit: Cannot resolve network address for KDC in realm "RLJASSOCIATES" while getting initial credentials

I altered the instructions for both dns installation and kerberos installation so that the domain is RLJASSOCIATES.
I found a forum that states;
"each host's IP address must reverse-resolve the canonical name."
so I guess it's time to go back to the DNS setup instructions once more, damn it serves me right for being lazy in the first place!

Ok the DNS setup doesn't work right off the bat but I did find a nice little utility to test called named-checkzone which I can use to check the zone files for correctness.  Needless to say they weren't.  Well I've been over those DNS instructions many times now so I can be absolutely sure my setup matches and guess what...they don't work so I guess they're crap!

I think there might have been a missing entry in the RLJASSOCIATES.db file so I added these lines
ns        IN        A        192.168.1.34
@        IN        NS        ns.RLJASSOCIATES
and that seems to allow nslookup gw to work.

however running sudo kinit steve/admin still results in the error
kinit: Cannot resolve network address for KDC in realm "RLJASSOCIATES" while getting initial credentials
Setting up krb5.conf
I remembered that after the first fault with the kerberos setup I uninstalled it, then when I re-installed it I was never asked the same questions about default domains once the installation was complete, this may have been the route cause of this last failure.
Essentially there is a file called /etc/krb5.conf that seems to list settings that are relevant for different domains, so I took a guess at what to write and put this in;
    RLJASSOCIATES = {
                kdc = rjohnson-acer.RLJASSOCIATES
                admin_server = rjohnson-acer.RLJASSOCIATES
                default_domain = RLJASSOCIATES
    }
I have no idea if this is correct or not apart from when I ran this command kinit steve/admin instead of it failing with the usual error message shown above it prompted for the password, which I typed in and ... nothing happened which still means no error :)

The next fault to get over in the kerberos instructions is
#> kinit steve@RLJASSOCIATES
kinit: Client not found in Kerberos database while getting initial credentials
 Once again I have no idea what is causing this to happen, so on to professor google.
Not actually sure this is an error as the list of principals is now
kadmin:  list_principals
K/M@RLJASSOCIATES
kadmin/admin@RLJASSOCIATES
kadmin/changepw@RLJASSOCIATES
kadmin/history@RLJASSOCIATES
kadmin/rjohnson-acer@RLJASSOCIATES
krbtgt/RLJASSOCIATES@RLJASSOCIATES
steve/admin@RLJASSOCIATES
The big issue is that it says I am not entering the correct password for kadmin/admin and yet I know damn well what I entered so this might be an issue with the re-installation I tried earlier ooops!

JBoss
I am going to start with a vanilla installation of jboss-4.2.3.GA, simply because I wanted to start with a version 4 and this was the latest 4 I found on jboss community site, don't worry I'll show the same kerberos stuff on jboss-7.0.2, I would want to use 7.1.0 as this is the version where remote ejb calls are supported but it doesn't exist yet tee hee :)

Since JBoss comes with a jmx-console lets try to secure the access to this web app with kerberos.

The current setup for security on this app is;

   <application-policy name="jmx-console">
      <authentication>
          <login-module code="org.jboss.security.auth.spi.UsersRolesLoginModule" flag="required">
              <module-option name="usersProperties">props/jmx-console-users.properties</module-option>
              <module-option name="rolesProperties">props/jmx-console-roles.properties</module-option>
          </login-module>
       </authentication>
    </application-policy>

So in order to use the same login that I want to use for kerberos I've created a user called steve and I've added that to the props/jmx-console-users.properties.
Stupidly the first time I added the new user I forgot to also add him to the roles properties file, the trouble is you can't sign in but you don't get any error messages in the jboss log which was a bit annoying.

Introducing Krb5LoginModule
I've never used this login module before so it's going to be a learning experience :)
I found some documentation for Krb5LoginModule and just decided to run with it.

So I changed the login-config.xml to read

    <application-policy name="jmx-console">
       <authentication>
          <login-module code="com.sun.security.auth.module.Krb5LoginModule" flag="required">
           <module-option name="debug">true</module-option>
          </login-module>
       </authentication>
    </application-policy>

As you can see I've only set a single option, this is just so I can see what happens, remember I have no idea.


Hooray
It looks like I was successful, the usual prompt popped up so I logged in as Steve and hey presto got this in the log

10:38:00,893 INFO  [STDOUT]         [Krb5LoginModule] user entered username: steve
10:38:00,978 INFO  [STDOUT] Acquire TGT using AS Exchange
10:38:01,056 INFO  [STDOUT] principal is steve@RLJASSOCIATES
10:38:01,057 INFO  [STDOUT] EncryptionKey: keyType=3 keyBytes (hex dump)=0000: FE 75 E3 7A 1A 29 7C A8  
10:38:01,057 INFO  [STDOUT] EncryptionKey: keyType=1 keyBytes (hex dump)=0000: FE 75 E3 7A 1A 29 7C A8  
10:38:01,057 INFO  [STDOUT] EncryptionKey: keyType=23 keyBytes (hex dump)=0000: 3D 8C CB 99 50 CC 9A 13   69 12 6D AA BF E7 C6 4E  =...P...i.m....N
10:38:01,058 INFO  [STDOUT] EncryptionKey: keyType=16 keyBytes (hex dump)=0000: 79 75 9B A7 67 EC BF 2F   7A 32 C1 C8 9E AB 57 0D  yu..g../z2....W.
0010: 5B B3 3D D0 64 F4 34 4F  
10:38:01,058 INFO  [STDOUT] EncryptionKey: keyType=17 keyBytes (hex dump)=0000: 87 17 D5 4C 61 35 48 4D   D4 8B 14 58 91 E5 E8 AE  ...La5HM...X....
10:38:01,078 INFO  [STDOUT] Commit Succeeded


Actually do SSO
So far we have a working test environment setup and have been able to make a call out to Kerberos to authenticate a user.

Although this is a good start to the experiment what we don't have are;
  • a way of automatically sending a username to do the login, this wouldn't just be a username but would need to be the TGT.
  • a way to set the Subject to the user signing in, currently we only have the principal.
I should have read the documentation as we really do have the subject of the person doing the login this is bundled up in the Credentials of the login itself, so score!!
The Connector 
The way that I want to pass the name through rather than prompt for it is to use the java system property user.name this seems like the easiest thing to do for this simple test.  Essentially the way I see this working is that the name is used to retrieve a TGT from the local cache if there is one, and of course if there is one then they are authenticated and this TGT can then be passed from app to app.  In order to do this before a call reaches the application running on the application server I intend to use connectors (or valves in tomcat) which means I need to brush up on my JCA knowledge first...more on this the next time I have some free time to add to it.

Lets take a step back
Before getting too far ahead of ourselves I think it's time to explain what exactly Kerberos will entail, much of the information about Kerberos in an easy to understand  style can be found on the MSDN site but here is the super abridged version of events.
The way my system is setup is that the is a server called the KDC (kerberos distribution centre) which has two roles.  The first role is upon request a valid user is given a ticket to get tickets (cunningly disguised as a TGT), then when a user wants to access a service they use the TGT to get a service request ticket.  Finally with this ticket the user authenticates his/her self with the service they wish to use.

The stage that our test system is at is that when a user requests the resource at /jmx-console 


Done...I think
Not sure if this is done now but it seems to be which is strange as I've set virtually nothing up?


login-config.xml
    <application-policy name="jmx-console">
       <authentication>
          <login-module code="com.sun.security.auth.module.Krb5LoginModule" flag="required">
        <module-option name="debug">true</module-option>
        <module-option name="useTicketCache">true</module-option>
        <module-option name="doNotPrompt">false</module-option>
        <module-option name="useKeyTab">true</module-option>
        <module-option name="useKeyTab">true</module-option>
          </login-module>
       </authentication>
    </application-policy>
Now when I log in to jmx-console it seems to authenticate me when I have a service ticket already.

Firefox
I also discovered that if you put about:config into firefox and then filter on negotiation you can set network.negotiate-auth.trusted-uris; to the domain kerberos authenticates against et voila ! .RLJASSOCIATES

But is it SSO
The simple truth is I don't know at the moment I haven't tested it and this is because I am not sure how to test it, so lets look back at square 1 what was the criteria;

  1. not be able to access resource (/jmx-console) if I am not authenticated.
  2. authenticate via the KDC.
  3. once authenticated don't get prompted for authentication again.
I think that I have managed 2 of the 3 above points, however the third is a partial victory.  I don't get asked for authentication when I visit the resource in a second tab of the browser but this is simply the typical cookie authentication, when I use a different browser I still get asked for authentication and it is this step that I need to get rid of in order to claim SSO works.  The trouble is I am not sure how to do that with the tools that I have used so far, I think I might need to write my own LoginModule or something. 

One More Piece of the puzzle
I've not had much time to look into all of this stuff at the moment and tidy up this rambling blog post, this link looks like it'll be quite useful.
http://download.oracle.com/javase/1.4.2/docs/guide/security/jgss/single-signon.html 
http://download.oracle.com/javase/1.5.0/docs/guide/security/jgss/tutorials/JGSSvsJSSE.html
http://download.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html 

Thursday, 13 October 2011

Exceptions (again)

The argument that keeps on giving :)
This is the fairest assessment that I think I've ever made of exception (specific to java, possibly).

Exceptions are a very good way of having more than one return type on call. Originally they were invented so that constructors could inform the caller of something rather than having to have a special "error" type of every class.
Handling exceptions early isn't always the best idea, code where an exception occurs may not be best suited to remedy it as the lowest level tends to be a generic handler of something.
Take for example the File API, if a file can't be found it is highly unlikely that the File object should be made to handle every failure adequately. Maybe it should simply return a null, but how then do we know the file couldn't be found rather than it couldn't be read? These 2 scenarios should be handled in very different ways (1) file not found: get the user to pick another (2) read error: try again X times then fail. Scenario 1 requires user interaction so bubble the exception up through the code so the user can pick another file.
There is also an argument against interpreting an exception at a lower level and sending specific "error" style replies up the chain.
  1. Multiple levels in the call stack will have to handle error return states.
  2. The returned objects would each have to have an error version created i.e.
    public A getA()
        public B doB()
            public C getC()
    
    each return type A, B, C would have to have an error version created, plus each call would have to handle this.
  3. There is already an adequate mechanism to repeatedly send exceptional responses up the stack regardless of method return type.
  4. Sometimes it is impractical to escape from a normal flow and return a meaningful error, the use of try catch blocks greatly simplifies this.

Also
Also the idea that exceptions are errors is flawed, exceptions are an
exceptional flow, the clearest use of this fact I think is the Thread
usage of InterruptedException. When things are running concurrently (even
pseudo concurrently) a message should be sent at any point during the flow
and the way to handle this is with exceptions.

The problem is that with legacy old shit (and I mean it is shit) does
the biggest sin of all which is to catch ALL general exceptions and simply
throw it without adding a cause to it. No attempt is made to handle the
exception where it is relevant which means the final resting place of a
thrown exception is the client which is why some people believe that the client
shouldn't handle them because 9 times in 10 the client is the wrong place
to handle them.

Sunday, 19 June 2011

IT Confidence

Developer confidence / arrogance

I had a sudden realisation about why most developers come across as arrogant. Ok, so in a lot of cases it is because they are, but I am always accused of being arrogant (mostly by my wife) so decided to look into why that was.

During a developers career they are always trying to sell their solution to a problem, usually without any evidence, the manager in charge has to be convinced that your idea is the correct solution over and above all other solutions in order for you to win the right to implement it. I think this is where it all stems from you have to have ultimate confidence in an idea and it is this confidence that could be observed as arrogance.

Friday, 3 June 2011

Religion

I recently attended a christening at a local church.

Things I noted were that religion is a very good form of control, it uses some clever psychological tricks to reinforce its ideals such as chanting and mob vilification. It is a commonly understood fact that people when acting as a group rather than as free thinking individuals are capable of vilifying something that is different from themselves for the simple reason that its not them. Another time when this psychological trick was used was by Adolf Hitler when he managed to turn a country against a race via clever propaganda and the mob reflex of human beings.

By acting as a mob in this way a persons individuality is damaged, keep it up for long enough and it is destroyed, when a persons individuality is destroyed they then become automatons, very susceptible to suggestion almost regardless of the nonsense of the suggestion.

I used to attend church as a child however I was lucky enough to have very intelligent and sensible parents that neither wanted to push me away from religion or enforce their own doctrine on me. They wanted me to experience religion and for me to make up my own mind, this is all that can be done if a child is to retain their individuality and it encouraged independent thought and reinforced the belief that I should question everything. I don't think I was liked in church for the fact that I would ask questions and follow them through to their logical conclusion without prejudice, malice or preconceptions. I will always remember one particular discrepancy in the logic of the bible “if god forgives all sins then why does hell exist...it's for people that have committed sins...which are presumably forgiven so they no longer have sin...they will always have sin...so what does it mean to be forgiven...it means your sin is removed...so god forgives all sin and it is removed, so why does hell exist...it doesn't work like that...so god doesn't forgive all sin?...yes he does...so hell is empty...no hell keeps all sinners e.t.c. Keep following these arguments through to their conclusion again without emotion and you come to a few plausible conclusions 1. that religion is full of discrepancies, flaws and illogical leaps that are nonsensical. 2. you need to have a leap of faith!

A leap of faith, this is religious parlance for ignoring what you know to be true in favour of mob rules.

A nice quote in Dracula is something like "faith means convincing yourself of something you know not to be true actually is." - I'll try and find the actual quote later

It is this “leap of faith” idea that is very clever when it comes to control, as explained before when under the control of mob mentality you freely adhere to the reality that is put before you and the more ridiculous it becomes the more likely you are to reject it, but not if you are made to bring yourself into question which is where the “leap of faith” comes into its own. People already indoctrinated into religion will shun those that question things rather than accept what they are told at face value, which makes those people doing the questioning into the vilified culture which they don't want to be so weak of will take a “leap of faith” and those that don't serve to reinforce the beliefs of the rest of the mob.

Religion is a remarkable control mechanism that works best on the masses which is exactly what it was invented for.

The problem occurs when the original people who developed the control are no longer around and the followers are left to try and make sense of things. Without someone to give direction the discrepancies are fudged until a new leader takes over. This can and has caused factions to form while the mob lurch around like a hydra with a head missing, the scary thing is that there is no control over the leaders that gain control and hence fanaticism is allowed to take hold.

This could all be remedied if everyone simply retained their own individuality. As I said before I judge these things without emotion, I truly have no feeling good or bad towards religion. In certain circumstances it is a good thing it provides support groups and if everyone made a promise to be nice to one another the world would be a better place but sadly no such religion exists (although Buddhism comes pretty close).

Saturday, 12 March 2011

QCon London 2011

Once again I went to London, and once again it did not disappoint :) this is by far the best meeting of the minds that I have the pleasure of going to. Although I must admit I find the choice in flavours of cake a bit weird, chocolate and lime ?!?!

I heard some interesting talks, I wish that I had the confidence to ask questions when I am there as there are many people I would like to pick the brains of. Maybe next time I'll pluck up the courage to ask why "facts" displayed by two different talks were in complete opposition or why the facebook team seem to think the problems they have faced are unique to them.

So on with my break down of some talks I personally attended, the following is a series of opinions. The thing about opinions is they are mine and you almost certainly won't agree 100%.

Wednesday

Why Don't We Learn?

This was an enlightening, informal chat with the audience that explained the principles of learning combining many ideas from the psychology field rather than IT. The speaker Russ Miles was enthusiastic and fluid not having to read from slides and making sure that his voice projected. A brief mention of the placebo effect was the only item that wasn't accompanied by facts, a simple read of some Ben Goldacre could solve that.

Software quality - you know it when you see it

Once again a very practised talk that conveyed in a short time the fact that you really do know good code when you see it, or rather you can tell when there is bad code. Erik Dörnenburg produced a slick series of graphical views of code that can make a series of issues clearly visible even to inexperienced personnel that don't code for a living. Such things that we all know are bad such as monolithic classes a high degree of coupling as well as pointless interfaces.

Learning and perverse incentives: "The Evil Hat"

This was one of those choices that I was unsure of at the time, I needn't have been. The speaker Liz Keogh was a brilliant speaker, she mixed up real life experience and sarcasm just the way I like it. The actual content of the talk was essentially carrot works better than stick and if you set a kpi prepare to have it gamed! She cited one example of a kpi being gamed that rang true with circumstances that I've had the misfortune of coming in contact with. One of her friends said he had a new project manager that had told him that the project was to be listed as "in trouble" and that he was calling an emergency meeting. The project was meeting deadlines and bugs were minimal so he couldn't understand why it needed saving. Liz asked a simple question, "are pm's bonuses related to rescued projects", bingo, the pm would get a bonus for simply saying a project was in trouble so that he could be seen to swoop in and "save" it.

Agile Operations - optimising the business on shell script at a time

I am going to be slightly controversial on this one, so be warned. Dan as always was a brilliant speaker as was his co-host (I resist the term side-kick) Chris Read. However the talk didn't really do it for me, the comedy was good but over-played. I will admit that it's probably just me that doesn't yet get it as Dan North himself admits he didn't "get it" straight away. Yes it is nice to be able to deploy an image across multiple servers at the press of a button and send changes out in a similar fashion, but this doesn't seem new to me as it seems obvious.

Java without GC Pauses

Gil Tene was a comfortable speaker, I did get the impression from his demeanour on stage that he was beginning to get tired of having to explain the same things over and over again and people not getting it. I have to admit that this is the talk I had wanted to see the most all day, I like the clever stuff that happens at a low level thus my love of the JIT e.t.c.
The solution to the compaction problem seems so simple once explained, it uses virtualisation so that rather than change physical memory during compaction (the long part of a full GC) you alter the vector table of memory. The only issue that this has is that Azul's GC (C4 I think) does require being installed in unison with some virtualisation server...as I understand it anyway??? This idea seemed so obvious and if the charts are to be believed it is very effective so it seems a no brainer that virtual-pointers become the next change for the linux kernel. Loved this talk immensely, however on day 3 of QCon it was bought into question.

Thursday

Testing for the unexpected

This talk given by Ulf Wiger was a surprise for me, in a pleasant way. To be honest I had no idea which presentation took my fancy and as it is always good to learn something "totally" different from normal I went along. I didn't really know how we possibly could test what was not expected and in walk quicktest. Essentially you fill a series of tests up that are then filled with essentially random data. I tend to do this as a matter of course so the fact that there is a framework for it probably means I am missing a trick.

Data Architecture at Twitter scale - Nick Kallen

I can't really say much but it was a good talk, no surprises also no exceptional solutions to problems. The fact that I've written less about this talk doesn't mean it was less brilliant than the others it was great maybe I'll add some details later :)

Releasing Fast Code: the DevOps Approach to performance

I can't remember this one, hmmm need to review my notes

Better is Better

This was a somewhat strange talk. It was billed as being a bit of a rant and to be fair to the speaker that is exactly what it was. I did feel a bit sorry for the speaker as he was new to public speaking and had fallen into the same traps that all new comers do, slightly mumbling and stumbling. Things weren't helped by the fact that he moved off mic as he paced on stage. All this is rather negative at the end of the day it was his personal experience as a story, I found this quite interesting as I do enjoy hearing about the history of computing. The history of computing is very important at the moment as we seem to be re-living it, I'll blog what I mean on this later.

I went to another talk on Thursday but can't find the details for it unfortunately.

Friday

High performance web applications in Haskell - Steve Vinoski

I had high hopes for this talk, sadly totally unfounded. The speaker was very arrogant and childish, never passing up an opportunity to bad mouth another language. These "my language is better than yours so there nerr nerr nee nerr nerr" talks drive me mad. I feel like grabbing the guy shaking him and saying "Ok I get it you hate anything that isn't your language, tell me why yours is good not why any other is bad." these people say this language is more concise without realising that there is context to this claim for example recursion makes for very concise code in a lot of cases but not all plus there are other concerns to a language readability, maintainability, performance (that's right sometimes functional is not faster than procedural sometimes it is), plus there is always the expertise of the people looking at the code.

Anyway I digress this guy got off to a bad start, he did reign it in once he concentrated on the idea of the talk, however I waited to hear some Haskell my functional language of choice only not to hear any he did mention Erlang and throw some (badly formatted) slides containing very basic Haskell up but there was no depth to it. I actually tweeted a bad thing which I don't like doing as concentrating on the negative is not good, unless it's funny :)

HTML 5 Design/Development Tooling (+HTML and Flash)

Not really a talk this one, more of a sales pitch from the Adobe guys. I liked seeing what can be done but would have liked to have seen what was going on underneath the tools if that makes sense. I'll admit I am no longer into web design and GUI's as I find the server side more interesting so have fallen behind with HTML5 seeing what can be done in this talk has prompted me to learn about it especially as the company I work for use flex to produce their GUI. My work colleague who joined me for this presentation was not interested in the talk as all of the tools being demo'd he had already seen so sorry @DavidArno but I did enjoy it to a point. There is however the danger that QCon talks will start to become more about selling stuff than pure knowledge sharing which would be a terrible thing to happen. On that note there seemed to be more talks around a product (twitter, facebook, dynatrace, atlassian, adobe, azul ...) than last year so maybe the knowledge rot has already started.

Performance Tuning for Java Applications

First of all this room was far too small for the number of people that attended the talk, it got really hot and at one point I had to leave to prevent myself passing out only to return to find someone sitting in my seat which I had left my belongings on to inform anyone that I was going to return, some people are dumb.
The speaker was great, it felt like an informal chat about how he tests products at Atlassian I don't know if it is a cultural thing but most Australians and Australian companies seem to foster a very easy going style that I respond well to George Barnett spends his time at Atlassian performance testing and really knows his stuff. I was however concerned at one point though as the charts he showed for heap size were in complete contradiction of the ones the Azul guy showed on the first day. George said if you have the memory available then use it in the heap and showed the performance increase even when the complete heap wasn't being used, whereas Gil Tene showed that compaction increased in stop-the-world length of time to run which had a marked effect on system performance. The only thing I can think that could make them both right is that the applications they used for testing were utilizing the heap in very different ways. confluence and jira for example should have short lived classes of small size so that even if they live long enough to reach old space the gc could easily remove them with concurrent mark sweep and not have much compaction whereas Gil may have used a system with irregular classes and life expectancies. This is something that George made clear at the start of the talk he was ONLY using Atlassian products and that anyone who blindly applied his performance tweaks to their system deserved every bit of failure they got, unless the system they were tuning was an Atlassian product.
Following on from this I did learn that in order to performance tune an application you have to know the application, how it uses memory and indeed what the current performance is before making any changes otherwise how do you know if you are making any improvements.

Using a Graph database to power the web [Rick Bullota & Emil Eifrem]

I did really enjoy this talk, it didn't expose me to anything new but was nice to hear that a few areas of mathematics that I enjoy are bubbling up to the surface graph theory and set algebra. The talk could have been more weighted to neo4j than thingworx as I was interested in the technology used more than they was a single solution was applied, but case studies are a way to learn I suppose as long as you remember each situation carries it's own unique attributes.

HTML5 and the dawn of rich mobile web applications

I didn't hold out much hope for another html5 talk given the sales pitch Adobe talk from earlier, but boy was I wrong.
This guy (James Pearce) was so enthusiastic and enthralling that you couldn't help but fall in love with the subject matter every bit as much as he obviously had. He started by demo'ing how we have learnt that the web is not just sitting at a desk and that it isn't just a change in screen size that we need to take into account but the whole interface and what visitors to sites want to do. He cited some good and some unexpectedly bad examples of where this is not done before showing some simple code and explaining more about offline html applications. This was a fast paced exciting talk I was blown away especially as it was a subject that over the past few years I've shown less interest in.

#qconlondon

Wednesday, 5 January 2011

3D stole my sequel :(

I am a geek and I love films so it's a no brainer that I love Tron, so how excited do you think I was when I heard about the sequel.

I have now seen the sequel and it was nice, but no smash hit.

Pros and Cons

Pro, plus or good point Con, bad point :(
Jeff Bridges as always is fantastic 3D was rubbish and actually a gimmic that took away some of the magic, Tron consists of massive contrast of bright colour and blackness. Sadly when your wearing 3D specs that act like sunglasses this fact is somewhat muted.
CGI is amazing (maybe too good, if that's possible). I like Daft Punk, but the music should be secondary to the action, supporting it and adding that extra depth and not a lead role in it. The music for the first quarter was intrusive and frankly didn't fit what was going on.
Garrett Hedlund was much better than I had anticipated Some parts were so camp I thought I was watching a sequel to Flash Gordon.
Paying tribute to the original especially the line That is a big door To make up for a lack of story we were subjected to a number of travelling periods, moving from one place to another where nothing much happened, presumably showing off some of the superb cgi that has been employed.
Some of the dialogue was a bit trying at times. I have to tread carefully here as the bits in question may have been good actors ad-libing. But using words like my bag and trip in an 80's style down with the kids manner was a bit gag enducing.

With all that said this wasn't a bad film, neither was it a great film, I left the cinema feeling "blah" about the whole thing.

maybe I expected too much but I am fed up of films consisting of pretty people not generally acting very well and letting gimmicks like 3D or CGI take the place of good story telling!

Monday, 20 December 2010

Fan Boys

1. What is a Fanboy
A Fanboy is someone that holds one particular thing be it a manufacturer, programming language or producer in high regard to the exclusion of all other alternatives without a logical justifiable and measurable reason.

2. Why I dislike Fanboys
I am a scientist by heart and a software developer by profession, this makes analysis my bible and the search for explanation my pilgrimage. Fanboys seek to get in the way of this quest for no obvious reason other than lack of interest in intellectual discovery and experimentation, this to me makes them unworthy of sentience.

The reason I make this seemingly harsh conclusion is that the thing separating us from beasts (I say the thing although there are many) is our ability and intonation towards discovery of facts based upon previously acquired knowledge in the same manner, rather than surviving on our instincts as beasts do we improve upon past breeds by education and investigation.

This is at odds with the fanboys stymied regards for investigation.

Tuesday, 19 October 2010

Access properties as if they are constants

Goal
Sometimes it is nice to be able to access properties from a properties file in the same way as enums are accessed within Java. By this I mean that you can simply use the '.' (dot) notation and you get exactly what you expect.

for example with this enum

enum Colours { 
    BLUE(1), GREEN(2), RED(3);
    private int num;
    Colours( final int i ) {
        num = i;
    }
    int getValue() {
        return num; 
    }
}

you can expect that
int iCol = Colours.GREEN.getValue();
will not only compile but also give the correct number to iCol.
Now imagine that the colour blue is no longer to be represented by the number 1 but the number 7 instead, you would need to re-compile you enum in order to get the correct number.

Very Simple Solution
now take this code
enum KFType {
    type( "type", ConversionType.INT ),
    words( "words", ConversionType.STRING ),
    //example in overriding the getter
    special( "special", ConversionType.INT ) {
        @SuppressWarnings( "unchecked" )
        @Override
        T getValue() {
            Integer t = super.getValue();
            return (T) Integer.valueOf( t.intValue() + 10 );
        }
    };

    private ConversionType c;
    private String tag;
    private static final Properties keyFile = new Properties();
    static {
        try {
            keyFile.load( new FileInputStream( "path to props" ) );
        } catch ( IOException ioe ) {
            ioe.printStackTrace();
        }
    }
    KFType( final String t, final ConversionType con ) {
        c = con;
        tag = t;
    }
    T getValue() {
        return c.convert( keyFile.get( tag ) );
    }
}

@SuppressWarnings( "unchecked" )
enum ConversionType {
    INT {
        Integer convert( Object o ) {
            return Integer.valueOf( o.toString() );
        }
    },
    STRING {
        String convert( Object o ) {
            return o.toString();
        }
    };
    abstract T convert( Object o );
}

The disadvantage here is that in order to get any changed values from the properties file the jvm will need to reload the enumerated type which normally means a restart (I think). The big advantage here is that you can now use properties from properties file, or an xml file as if they were code.
I have also shown that you can perform some sort of transformation to the values read from the properties file, here I simply add 10 to the number read but there is no reason why we couldn't wrap text in an http url or something similar.
Look at the output of this
@Test
public void testEnum() {
System.out.println( ((Integer)KFType.type.getValue()) + 10 );
System.out.println( ((Integer)KFType.special.getValue()) + 10 );
System.out.println( KFType.words.getValue() );
System.out.println( KFType.special.getValue().getClass() );
System.out.println( KFType.words.getValue().getClass() );
}


12355
120
these are words
class java.lang.Integer
class java.lang.String

this is when I use the following properties
type=12345
words=these are words
special=100




Monday, 11 October 2010

Kindle

Kindle I am probably the last person to provide his verdict about the Kindle, but I don't care here it is anyway. I have for sometime been looking for a good way to read books when not in my house. Being a developer I find that in order to read about a subject area I do normally tend to read several books side by side which is great when at a desk with plenty of space to spread out but not so great when on the move. I was quite surprised by the cost of the Kindle the only other device to come close in my opinion was the sony eReader and that was at least four hundred quid when I last checked. My kindle was £149 and it came with free 3G access to the kindle store plus I could actually browse the internet. It's not great at browsing the internet, but that's not what it's for. One thing it does do very well is that it has an 'Article Mode' that is great when reading wikipedia entries. The most important thing is that I could read it all day everyday when I was on holiday without getting eye strain or needing to recharge it. Whilst I was sat on the beach in the sun reading happily away it gave me great satisfaction to see people trying to read iPads and failing because of the glare from the sun and running out of power at inopportune moments like as soon as they get to the airport tee hee. All in all brilliant bit of kit, if it was any bigger then I wouldn't be able to put it in my suit pocket so I hope this never changes in future.

Sunday, 22 August 2010

Goddamn UbuntuOne

Ubuntu One promised so much, I could sync my files between all of my machines and pickup stuff from different machines when in foreign countries e.t.c. Shame it fails to deliver! Ubuntu One keep notifying the world of their new features and future proposals which would be very nice, if they actually had the current features working. I have lost count of the number of times the contact system has been down, and the number of times notes and files don't sync up. Now the latest failure is the fact that it sees fit to go tits up and then replace my files with ...u1conflict thus rendering them useless to me. I thought it might be a bit of fun to use git and have the repository on my ubuntuone, hey I know this is not what git is intended for but thought it would be a simple way to clone to other machines in different localities. I was very wrong on this one, very wrong. After the initial ubuntuone sync on a new machine something had gone wrong and the source files failed to be copied over, oh well I thought I'd just do an old fashioned git clone over ssh which failed soon after it started with a simple failed message. When I looked at my file system .git was now .gitu1conflict, as this was a clone I simple deleted the folders ready to try again, this time though git wouldn't connect. So I logged into the original machine only to discover that ubuntuone had sync'd up with the original machine after I deleted the repository from the second machine, so the bidirectional sync meant that I had just lost all of my git projects. I am not a happy bunny!!! I should have kept backups even though they were just simple disposable projects I used for learning new stuff, so annoying.

Saturday, 31 July 2010

Alonso, loser

a racing driver that seems unable to win a race without some form of cheating. He whines like a bitch with a grazed knee when someone is in front of him, and then to STEAL the race from his team mate 12 months after an accident that could have ended his carer is disgraceful. To win that race would have meant so much more to his team mate than to him, and after all of the words about helping the team the points gained by that shocking let through make no difference to the GP leader boards. A man that holds formula one with such contempt should himself be held in contempt. Alonso should be ashamed and so should Ferrari

Hooray, He's learnt

I am impressed by the way that Alonso has turned himself around, at least in my opinion. Lately he has been congratulating those that beat him, thanking his team for being good and generally being magnanimous in defeat. Also to be honest with his new opinion he seems to relish the fact that he will be (or rather is) able to compete against these people using driving rather than underhand politics.
Never thought I'd hear myself say it but he's turned around and I find him a much better racer for it, of course he still has to keep doing what he's doing to prove himself but so far so good.

Sunday, 2 May 2010

Exceptional Exceptions

Exceptions

Exceptions and exception handling isn't something that should be difficult to do, and nor is it in Java. However the trouble with exception handling in Java is that no one can agree on a sensible method for handling them.

It is for this reason that I have written what I believe to be the most adequate way to handle exceptions, and the reasons for making these decisions.

I feel that the best way to get my point across is with some simple, all be it contrived examples. I am from this point on going to assume that anyone reading this document is familiar with the concept of a try..catch..finally block

Lets start with a bit of background, there are Throwables, checked exceptions, runtime exceptions and errors within java. The easiest way to categorise these and a some what glossy high level view is to say that every exception is a Throwable, so we shall ignore this semantic verb for now. A checked exception is an exception that a method may throw when it is called, by being a checked exception it gives the user of the API a sort of heads up letting them know that they should handle the exception in whatever would be the appropriate manner given the context in which the API was being used in. Then there are runtime exceptions, these aren't listed as possible outcomes of calling a method but they can be caught and dealt with if the API user believes it to be worthwhile, for example if the opportunity for an array to be incorrectly accessed is a possible outcome of a method being used then perhaps some programming logic can be put in place to mitigate the problem slightly, this sort of issue is usually an indication that a better design should be put in place so that, for example, an array is never incorrectly accessed. Then there are errors, it is true to say that these more often than not should never be caught as they tend to be things that are not recoverable such as OutOfMemory which means the status of the JVM can no longer be relied upon.

Good practice when designing an API means that the usage is examined from the perspective of a typical user, I prefer to use Use Cases to examine the problem from the users perspective. For anyone not familiar with this method it simply means putting together a story of the course of events of a user using the particular API call. The useful thing about use cases is that there is a specific section set aside for what should happen as an exceptional case which can then be related directly to the implementation of the solution.

Example

As an example lets imagine a simple system in a hotel lobby that is used to lookup the room that a person is staying in by using their name as the search criteria. The Use case might look something like;

  1. type name of guest
  2. hit search*
  3. system displays the room number
Immediatley at point 2 we can think of an exceptional case, when the guest is not present in the hotel, this would result in an API that looked much like
public Integer getRoomForGuest( String guestName ) throws GuestNotFoundException;
Now behind the scenes the getRoomForGuest method may be accessing files, or a database or some other magical method of storing guest against room numbers, the point is that the user of the API shouldn't care about the fact that a FileNotFoundException or a SQLException may occur as this betrays the inner working of a method. Imagine the system is upgraded in the future from using a file system so that now it uses a database, if the API used to throw FileNotFoundException or IOException it would surely now have to throw a SQLException. Therefore through having a checked exception that is tighly coupled to the underlying implementation when the method implementation is altered the developer would have to alter every location where this method was called, and if the API was being used in another company their code may stop working which could result in them having to re-design/implement a new solution and this added cost could convince them to stop using your API. A friend raised the point that this could cause an exceptions true cause to be lost, I took it for granted that if anyone catches an exception they will as a matter of course add it to any exception being thrown as the "cause". Another point to raise here is that we should throw any exceptions that we really can't deal with. In the example above you could safely assume that an adequate response to not being able to find a person in the directory due to a FileNotFoundException or other IOException would be to say the person could not be found to the user. However many real life calls are not this simple, for example a with a different call it may be acceptable to inform a user of FileNotFoundException and other prescribed obviously solve-able exceptional cases but any other IOException that occurs should be considered more serious. In this case I would make the method call throw CallSpecificException (not the real name just an example) and IOException. The idea here is that the caller of our method would catch the specific exception and most likely tell the user but anything else would be dealt with differently most likely throwing to a higher authority in keeping with the idea of escalating exceptions that our caller can't directly deal with. Read on this idea should become clear.

I hear many people argue that we should not use checked exceptions instead make everything a runtime exception. I find this a little ...well...foolish, you see by having checked exceptions an API designer can give a hint to its user that here is something that could go wrong, or be out of the ordinary and you should think about how to handle it. The designer of the API is surely going to be more knowledgeable about the types of things that can go wrong during the operation of their code. If we were to replace these checked exceptions with runtime exceptions then the API user now has to either instinctively know what could possibly go wrong within someone elses code, which might be closed source and undocumented which means the only hint of what exceptions may occur is the method signature which now no longer has any mention of exceptions, this is why it seems a nonsense to turn everything into a RuntimeException.

Throwing and Catching

Another issue with exceptions seems to be when should they be caught and when should they be thrown, some people believe code should catch every Exception and some people believe you should catch nothing!!!Crazy surely we want to deal with exceptions that are pertinent to the code which has caused it and throw or escalate any exceptions that we weren't expecting and hence have no clear knowledge on how to recover from.

Lets look at the first option, catching every exception. By deciding that you want to catch every exception must mean that you intend to write code that can suitably handle every possible exception, that's fine until it isn't, that is to say in the typical mundane run of the mill processing, exceptions that you expect could happen are dealt with exactly as expected. However programs have a tendency to change like the shifting ice of the North sea, and with this change comes brand new exceptional cases that were not part of the original design. So where does this leave us? We are now catching an exception that we don't know what to do with, so our program responds by logging the problem and continuing as normal. The problem could be a simple one for example couldn't find a config file or something that any user should be able to rectify, or worse it could be a catastrophic failure that could be avoided if only the right people were notified, for example an upstream service has failed preventing a circular queue from being processed and thus will start to wrap around after X number of failed calls anyone handling a large throughput can understand just how serious a problem such as this can be. Many people still believe that the best, and sometimes only thing to do with exceptions is to log something exceptional occurred and continue. This denies the intent of exceptions they are not all meant to indicate that something went wrong in the program but merely that a path other than the norm should be pursued within the use case.

The next option is to catch nothing, i.e. throws Exception should be written at the end of each method signature. I see this as lazy coding, and not the good kind of lazy that leads a developer to automate repetative processes, but the bad kind of lazy that turns into a birds nest that someone else will have to unravell at a later date. The only thing this kind of coding goes to serve is to provide fodder to the WTF Blog. So should we never throw exceptions at all? Simple answer of course we should throw exceptions, but only when the design intends it to. Consider the throwing of an exception to be the escalation of an exceptional case, everything is being executed within the stack and the higher up this stack the more prevalent the process. What I mean by this is that in general the higher up the stack we get the more likely we are to want to shout about an exception happening, by thinking about this process it must surely seem obvious that if your code can't immediatley address the exceptional case occuring then it should let a higher authority know about it. Take the earlier queue underrun issue, you API deals with a small portion of the overall system that relies on the queue, your code doesn't know how to deal with this problem so it should escalate the issue by throwing it.

No one should see a Stack trace

This is both true and false, if no one was ever to see a stack trace there would be no need for them. In fact seeing a stack trace is very useful in solving the issue. I think this mantra has come about due to the fact that most developers are lazy when it comes to putting in simple handling code, we see it all the time an NPE occurs on some web server somewhere and the user gets shown a stack trace. The trace is useful to a developer (usually) but pointless to someone who is simply trying to buy something from the site. It is incredibly easy to redirect the user somewhere to say there has been a problem please bare with us while we try to fix it whilst at the same time sending the stack trace to someone that knows how to deal with it. The fact that this doesn't always get done is simply laziness.

...or at least only see a meaningful stack trace!

Not all developers were created equal, not all developers know how all parts of the systems they work with function, not all developers have the necessary skills to fix all problems a system can and will encounter during its normal operation. It is for this reason that 1st, 2nd and 3rd line support even exist. What might mean something to a 3rd line guru might as well be a foreign language to a 1st line and sometimes vice versa. By throwing specific checked exceptions at an appropriate level the correct trace can be sent to the person (or system) that can handle the exceptional case in the most appropriate manner. Working on large international systems it makes sense to have context sensative logging that will send stacks from a US system to the US support rather than to an Asian support desk or the user.

Conclusion

We all want to make systems that are good, that we can be proud of and say I don't think anyone could do a better job, it is for this reason that some forethought must be put into what exceptional cases are going to occur that I can deal with and anything my code can't deal with should be escalated. It is lazy to catch all exceptions with a catch (Exception... that simply logs what went wrong and continues.

Monday, 1 February 2010

a sign of things to come

I love Sun Microsystems always have done, they gave us java which has allowed me to accomplish so much with computers. I used to feel all warm inside when I drove past the Sun building on the M3 near Fleet, and I even loved their little Sun Spots as a bit of academic fun. However now good ol' Larry has his mits on it I just don't feel the same about it. Even the oracle icon leaves me feeling ... well nothing really it evokes no emotional response what-so-ever. Now I have tried to register a copy of Glassfish v3 and got this, simply by requesting my id. This shouldn't happen, have the people at Sun lost their pride, or have the lovely people from Oracle already taken their soles to dark place? Who knows, who really cares other than geeks like me.

HTTP Status 500 -


type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.NullPointerException

note The full stack traces of the exception and its root causes are available in the Sun GlassFish Enterprise Server v2.1 logs.


Sun GlassFish Enterprise Server v2.1

Wednesday, 6 January 2010

British people annoy me

I am really really really fed up of people moaning about Britain being the only country to suffer with snow. I was at a bus stop the other day and a woman was telling me about how the rest of Europe was fine and functioning as normal in the weather. The reason this angered me was because I happened to be standing in the bus stop with a few friends who had that day come over from the Nederlands and they said it was the same over there and Germany too. I hate it when people make up there minds about something like this and tell anyone who will listen that their opinion is fact, when it blatantley isn't, 5 seconds of searching and I found this article that PROVES the bogus reporting. http://www.dutchnews.nl/news/archives/2009/12/snow_causes_traffic_problems_i.php Imagine my surprise when the news channels (in the uk) also do all that they can to sensationalise the whole situation, for example a woman was reporting infront of a massive 6ft snow drift by the side of the road, however as the report wrapped up the camera did a 360 view and (a)where she was standing was the only area that still had snow and (b)the 6ft snow drift had been man made by people clearing the road of snow and dumping it all in one place. Why can't people be honest!

Tuesday, 25 August 2009

Waste

This is one thing that has really started to get on my nerves recentley, waste!!! People waste time energy and food and it really annoys me. Food! My number 1 pet hate that people waste, and the wasteful habits that are activley encouraged by super markets. My wife buys strawberries, she likes strawberries, yet she has a small appetite. She knows I don't eat them and she also knows that she won't eat more than 1 punnet in the time it takes for them to go off, and yet if there is a buy-one-get-one-free offer on she will walk away with 2 punnets. 1 will get eaten and the other will simply go off and be dumped straight into the compost bin. I've confronted her on this before as to why she is getting 2 punnets when 1 will more than suffice and the answer is she's getting more without spending anymore which you can't argue with. So the farmer that sells his fruit at a lower and lower price because supermarkets are giving them away only suffices to increase the material in my compost bin and reduce the amount of money the farmer gets for his fruit. This lays the blame squarely at the feet of supermarkets...I wonder what true, real and bonefide advantage this gets them?? Another one is food that does simply get thrown away, if you find yourself repeatedly having to throw food away as it has gone over then you are simply overestimating how much you need, or taking advantage of all of those BOGOF offers needlessly. The other thing is when you finish a meal and there is plenty left over is your first response to bin it, or to store it with the intention of turning it into something the following day. I must admit I am a bit of a dustbin for food but some of the best flavoured food I've had has been meals made the day before that have been aloud to marinade in the sauces for a full day strange but true :) Energy Energy being wasted is another bad one, this one carries a more hidden cost not just the environmental one but the fact that most energy is coming from fossil fuels that are for the most part controlled by some rather unsavoury characters and giving them more power is never a good plan. Take new televisions the bigger they are the more energy they tend to require some of the larger plasma tv's draw phenominal amounts of electricity. But there is a simpler energy wasting thing that gets on my nerves and that is this. I recentley bought a new tv, not massive as I live in a small house and big tv's in little houses look shit quite frankly. Anyway I set it up watched it for a bit then, when it came to switch it off realised there was ONLY a stand-by off pressing the button on the tv had the same effect as pressing the power button on the remote, it always used to be in times gone by that you could stand-by with the remote but you could actually switch the thing off as well therefore stopping it from drawing any stand-by current from the wall socket. This means the only way to switch it off properly is to switch it off at the wall which is less than convenient or else get another gadget to switch the plug off remotley but why should I when it was perfectly fine for tv manufacturers give proper power switches!! Time Here is a neat one, people waste so much time it is inconceivable, especially when using computers it would seem. I have heard of people ignoring a faster way of doing a task ( in Word especially ) because they (a) don't know (or want to know) about it (b) don't have "time" to investigate it or (c) their just too plain dumb and stuck in their ways to alter the process that they currently use. Well heres my answers to (a) if you use a tool for X hours a day wouldn't you want to be able to impress someone with just how quickly you can use it, (b) looking into something for an hour that will cut a repeated job down by 10 minutes will have repaid the initial investigation cost in just 6 repetitions (7 if you include the initial learning cycle) and (c) where do you start with someone who lacks the enthusiasm or ambition to better ones self?
 
Stack Overflow profile for Richard Johnson at Stack Overflow, Q&A for professional and enthusiast programmers