Objection

RSS

How testers can make your code worse

I keep experiencing this: I’m unhappy with how we do things at work, I really wish we could do <x> instead. Then I finally get to try <x> and it turns out to be worse than the situation I was complaining about. I’ve complained about functions/methods being way too long (more than 5 lines each) I’ve complained about not having proper requirements before starting work. I’ve complained about not having dedicated specialist software testers. 

Now I’ve experienced all of these things I’m not so convinced anymore.  I find a codebase where nothing exceeds 5 lines frustrating to work with - especially for non-trivial software written by a large team. I find requirements documents counter productive and demotivating (talk in Norwegian) And I’m starting to realize that software testers can actually make your code worse and get you to focus on entirely the wrong things.

The most useful testers, in my experience, are the people who actually need the product you’re making.  They will often be quite happy to test your application too, as they are eager to see it finished, and finished in a way that suits their way of working.  If they aren’t eager to test your application, you should really be asking yourself if you’re making the right product.  When these people test your product, they will test if it makes their life easier. They test whether or not it gets the job done in a good way.  These are the most important things to get right.

Professional software testers on the other hand have a different focus. They start out with the requirements document. They have no need for the application under development, what is important to them is that it complies with the requirements. This is the definition of quality after all. After ensuring that the software meets the specifications, they do their best to make it fail, reporting any success they achieve a long the way.  If they were testing microwaves, they’d be putting animals in there, reporting all deaths as critical bugs to be fixed immediately.  If they were testing coffee machines, they would be pouring hot coffee in their eyes and reporting how your product can cause blindness. They input nonsense and complain that the output isn’t useful.

I can’t really blame them. They are there full time, doing nothing but testing. Testing a product they have never needed, and are unlikely to ever use again after the project completes.  They need something to do. And with shows like Jackass, Top Gear and Mythbusters so popular, who can blame these hard working souls for being inspired to do damage in as many creative ways as possible? 

It is a well established fact that most of the code in any application is practically never executed. Quite a lot is actually NEVER executed once in production. This code increases the overall complexity, increases chances of bugs, decreases maintainability, while giving very marginal, if any, benefit.  

These are the kinds of features that your software testers will spend 98% of their time finding for you.  You’re paying good money to get someone to find ways to make your software worse.  Instead of focusing on how the software is going to be used, focusing on the overall technical structure of the application, you end up focusing on “fixing” “bugs” that no sane user will ever encounter, or even see as a bug. As an example I once got the following bug report:

"When I click the context menu button on the keyboard [I didn’t even know there was such a button before I got this bug report!], a context menu appears with all menu items disabled. The context menu is only relevant for clicks on individual cells in the table shown, so no context menu should be shown when I click on the context menu button"

I swear that was a real bug report!  We ended up spending 15 hours “fixing” this “bug”.  Most of it was spent WTFing and asking ourselves and our managers whether this was really necessary.  A couple of hours here, another few hours there, it all adds up. More importantly though, the code fills up with ridiculous amounts of code that serves no important purpose at all.

My second concern with full time software testers, is that the developers themselves don’t think so much about testing, as they know they have dedicated testers to test for them. This leads to sloppy coding, and code that is hard to test at all.

I once had the luck of working on a project where we had a domain expert on the team with us full time.  He didn’t so much test our application, as use it. He used it as we were building it. Giving us invaluable feedback along the way, every day.  When he reported a bug, that bug was always something that stopped him from doing his job.  He was focused on how the application worked. Not whether it matched a specification, or whether it was physically possible to get the system to output nonsense. 

I say let the coders do the monkey testing. Make it the programmers themselves’ responsibility to ensure the system handles reasonable input ranges, say.  Send them to a TDD course, teach them FitNesse. Even if you don’t plan on using any of them.  It is important that your developers understand what it means and how important it is to test for correctness.  Once they understand this, and know how to do it, it doesn’t matter if they do TDD or if they use an acceptance test framework (they could just stick to normal unit test frameworks).  The point is that us programmers are more than capable of doing this kind of testing ourselves. Someone just needs to ensure that we take this responsibility seriously. 

One thing I did miss before using it, and haven’t regretted a second after starting, was doing automated testing. I love unit tests. I think most programmers worth keeping in your employment do too.  With a team of programmers keen on testing, you don’t need a team of software testers. At least not full time.  Invest time in domain experts who actually need your product instead. If no domain experts are interested in helping you out, you might be building the wrong product.  If management actually stops you from getting access to and input from domain experts or real users… you’re probably doomed anyway.

Nov 7

Not only are requirements documents worthless, they can be directly harmful. (10 min talk in Norwegian)

How I became an insensitive bitch

I read "Fuck you, I got mine" a few days ago and It’s been gnawing away at my subconscious ever since.  The author is so angry. She’s spewing out hate in all directions.  Toward men, toward women like me.  Her descriptions of the world of tech seem so foreign.  Her anger and hatred so misplaced. 

Then it occurred to me. I’ve been that angry too.  Just as deluded as she seems to be. Just as convinced that the world was against me. The only difference was that I didn’t have the self confidence to express my anger openly.  Also, I was 12.  

"Hey look, there’s that girl who’s so ugly"

No, I never got death or rape threats.  Never got beaten up or anything. But I got the message.  I was inferior.  And I was alone.  It all seemed so unfair and so random.  All I’d done wrong was to fall out with my best friend.  The girls in my class all played in pairs you see. When my best friend and I “split up” I was suddenly left all alone - she found a new best friend straight away.  Being alone in a group seemed to trigger some weird tribal vibe that made everyone shun me as if I were a leper.  

Me having no friends and actually being treated badly only lasted a few months.  And very very few people actually said anything mean to me.  But my perception of how people saw me was completely warped for years.  Those few assholes made me think everyone was against me.  I was angry.  I remember almost wishing that someone would try to attack me when I was on my way home alone from somewhere, just so I could beat the shit out of them :-D Never happened.  All those years of martial arts training to no use.

I’m not angry any more.  I don’t take things personally any more.  But that’s taken years and years of “training” if you will.  I’ve taught myself not to care.  So if I come across as an insensitive bitch, this is why.  It was either that or be depressed and angry. Not really caring that much can actually be quite liberating and fun. I know full well some people dislike me, but I really don’t mind.  I can be myself.  And I like myself the way I am.

Even so, I can’t really recommend this strategy to anyone.  The world would probably be a better place if people cared more.  Without a doubt, the best thing would be if nobody were made to feel inferior and excluded.  It’s horrible. I remember.

But the way to achieve this is not by spewing out hate. At the end of the day people in the tech community, more than in any other community, know how it feels to be left out.  Why not appeal to their better nature, make them understand how you feel.  They’re likely to have been there.

Attacking people makes them defensive, aggressive, more attached to their beliefs and ways.  When was the last time someone called you names and you thought “Hmmm, I think you have a point there, I AM a stupid bitch! Wow, thank you for that insight. I’ll rush off and change my ways immediately”.   It doesn’t happen often, does it.  

While I wish nobody had to learn the hard way NOT to care what others think, I DO hope that more people get to see the software community like I do.  The angry feminists are giving you guys a bad name.  It’s not fair and you deserve better.  You guys rock.  You’re the best. I refuse to believe that the concentration of misogynist assholes is higher here than anywhere else in society.  

You know what I hate most about working in tech? The fact that being friends with you guys can be a bit tricky. I’m surrounded by the most amazing people I really enjoy the company of, but if I ask you out for a coffee, or a drink or anything, it seems like a date and gets weird and people get the wrong idea. Spouses have to be involved. I practically only socialize in large groups.  Close friends can be hard to make when you’re the only girl in the office.  So I really hope we convince more girls to join us in tech.  I know we can.  But we’re more likely to get them by showing off our best sides, not our worst ones.  And if we want people to change, then encouraging good behavior is better than punishing bad behaviour. This is a pretty well established fact.  

So come on people, let’s go find some girls and show them how awesome we are :-D

If a girl can replace Hibernate in 600 LOC, just think what you could do…

Why you should write your app in java

My mother is American and my father is Norwegian, so I grew up with 2 languages at home.  I love languages.  I wish I knew more of them.  After a year in France, I did learn french to the point where French people asked me if I was Belgian. Quite proud of myself. I’ve lost it all now of course after years of neglect.  But anyway, I’ve learnt that some things are easier to express in one language than in another.  Some languages are easier to learn than others.  In English - nouns have no gender.  Which just makes so much sense. Why are Norwegian chairs masculine, while French ones are feminine? It’s just confusing and hard to learn.  In Norwegian we do have to deal with 3 genders for our nouns, but at least they are spelled sensibly.  We also don’t conjugate our verbs; I am, you am, they am.  We don’t care, everyone just am. Makes everything so much easier. We also have far more words to express different conditions of snow than your average language. Most importantly though, we make it really easy to express the fact that you just can’t be bothered to do something. Not ‘don’t want’ to, not  ’can’t do something’, just very ‘meh’.  ”Can not be bothered with” translates to “gidder ikke” Much shorter and nicer.  In short, if you’re writing a story about a group of Chronic Fatigue Syndrome sufferers who are stuck in a blizzard; Norwegian is the language for you.  

Or is it? As a Norwegian speaker, I would obviously go for Norwegian if I were to write that story. And if you would like to learn Norwegian, this might be as good a place to start as any.  But for the rest of you, sticking to a language you’re good at might be better option.  Even with its deficiencies, you will most likely create a better text in the language you know best.

Did Shakespeare ever write anything in French or German? Should he have? Maybe he did. I wouldn’t know as I, sadly, went to school in Norway. When young English students were reading Shakespeare in school, we were reading Petter Dass. 

I’ve been hearing a lot of complaints about java lately.  It’s verbose, it’s object oriented, it lends itself to creating factories, proxies and XML files galore.  I know, I hate those things more than anyone.  The English language has some serious flaws too, the spelling is ridiculous, the verb conjugation is unnecessary, and way too often it is used to express really stupid things.  But even with these deficiencies, you can still create beautiful texts, if you learn it well and use it wisely.

I’ve been writing java since 2006, but I’m learning new stuff all the time.  WeakReferences, ForkJoin, AtomicInteger/Reference/etc, CountDownLatch, Phaser, dynamic proxies, memory management tuning, all the new stuff in java 8.. the list goes on and on. Now that I’m starting to really understand how to use the language, I can focus more on what I’m making and less on the language and libraries.  And that’s one thing I miss in the software community.  A larger focus on what we’re actually making.  I want to create cool stuff, I want to make things happen. Sure the tools you use are important, but they aren’t the goal.  Maybe it’s a girl thing; I see a car as a way to get me from A to B. I see programming as a means to create cool stuff.  Guys (often) get so hung up on the car and the driving they forget the whole purpose of the thing. Similarly it seems that discussing language syntax becomes more important than creating something of value.  

Let’s face it.  If you suck at java, the solution isn’t to learn clojure or scala. By all means do, by the way. I sure would like to anyway. My point is just that learning a new language isn’t necessarily that much of a solution if it’s great products you’re after.  For that you need good programmers who know the language they are using.  Hell, I’ve seen a finite element analysis program written purely in VBA in Excel(!!!!) Was it the right tool for the job? Absolutely not! But I mean - WOW! How cool is that? If you know your stuff, you can do anything in any language.

Knowing many languages is cool, it has value, it’s fun, it’s a good party trick (for some definition of party) I personally want to learn as many as possible.  But does it beat true mastery of any one language? I don’t think so. 

Great authors are not renowned for the number of languages they know, but for the mastery of the ones they do, and the beautiful stories they are able to tell with them.

Sep 4

SO SQL (part II)

Hi again, 

For those who don’t know, I’m trying to convince the world that it is possible to connect to a database from java without a standard ORM like Hibernate. I have done so. And if I can do it, so can you. And you should. You’re not good enough, you say? Well then you definitely should.  At least if you want to get better.  To be a great programmer you have to practice writing code, your own code, design stuff, try things, fail. Try again, fail again. Laugh about it, learn from it.
And for all you architects and managers out there - if you want a good team, you need to give them enough space to write some of their own code.  Maybe not on the most mission critical system, but something. Why should you pay them to write stuff that might not turn out that well, that might not result in great business value? Because it will make them better at their job. That has business value. And besides, maybe they’ll surprise you. Maybe they are awesome programmers and you just didn’t notice, because of their personality (or their gender?).

Now I stopped in the middle of explaining my custom DB-code last time, let’s see how far I get this evening.  I have to repeat that I’m not presenting a universal replacement for Hibernate/JPA here, I’m just showing you what I’ve done and hoping you might find some of it useful. I hope you can see that it can be done, that it can be fun and actually perform better than the official standard.

First a little recap. I’ve written some code that helps me connect to a relational database from java.  So far we’ve seen how to select stuff:

List<Child> children = db.runSQL(Child.class, “SELECT * FROM child where child_daycare_id = ?”, asList(daycareId));

Or, if don’t have any left joins or tricky where clauses, you can do the following:

List<Child> children = db.select(Child.class,
        new OrderBy(nickname, ASCENDING),
        new Where(child_daycare_id, ” = “, daycareId);

(To be fair, I didn’t mention the ORDER BY clause previously, but I bet you can guess how it works :-) )

We can update like so:

child.setNickname(“Luke”);
db.update(child);

Insert like so:

db.insert(new Child(“Leia”));

I don’t have a delete function, because I haven’t needed one yet.  I’ve only written code I have needed so far.  On purpose.  I’m a strong believer in emergent design. By that I don’t mean that a good design will emerge by itself. I just mean that designing should be a continuous part of your development effort. You should be thinking about design every time you put your hands on the keyboard.  And your fingers should be on your keyboard as often as possible.  Writing code. Changing code. Deleting code.  Designing too much up front leads to complex systems.  If I had sat down and created a design for my database API, I would most certainly have added a delete function up front. I would also have added left joins, fancy where clause handling that accepted groups of where clauses with both ANDs and ORs joining them.  I would have gone to town!  I wouldn’t have got started on the actual application until months had passed.  Whereas here, I am delivering business value at the same time as I’m working on my DB code. I’m always amazed by just how simply you can solve things, if you just stick to the problem at hand.  And unless you’re building a generic framework (shame on you :-) ), you can just stick to the problem at hand.  

I have a confession to make though. I also support aggregate queries in my “select framework”.  Just goes to show how hard it is to avoid designing stuff you don’t really need. I haven’t needed any SUM or AVG or anything like that, yet I still support them.  Here’s the syntax:

class DayCareChildCount {
  @Column(column=daycare_id) private Long DayCareCenterId;
  @Column(column=daycare_name) private String DayCareName;  
  @Column(column=child_id, function=COUNT) private long childCount;

… getters, setters, whatever
}

See the last column annotation? I have a function parameter there.  If you supply this class to the select function like so:

db.select(DayCareChildCount.class);
it will 
give you all daycare centers, with their id, name and the number of children in each.  I don’t have any HAVING clause-support yet I am relieved to report.  If I ever need this enough that typing out the sql each time becomes a hassle, I’ll think about adding it. 

Something I HAVE needed a lot though is linking items with many-to-many joins together.  So I HAVE created a utility function for this.  

public link(Object from, Object to) {…}

and 

public unlink(Object from, Object to) {…}

These methods look for the primary key in each object, finds the many to many relationship table that links them, and inserts or removes an entry from that table.

This means I can link entries together without having to create mapping tables for my many to many join tables.  In context, these methods are used something like this:

public void insertNewEvent(Event e, List<Long> childIds){
    db.insert(e);
    List<ChildPK> childPrimaryKeys = childIds.stream()
             .map(id -> new ChildPK(id))
             .collect(toList());

    children.forEach(child -> db.link(child, event));
}

Oh yes, I’m using java 8, by the way. Forgot to mention that. Love it!  What I also love is that since I’m connecting directly to the database. I let the database deal with all my state. I have no session cache to maintain.  My app doesn’t need much memory at all. I mean why do we need caches? It’s to avoid doing expensive data retrieval more than necessary.  Now what is the purpose of relational databases? To make data retrieval inexpensive.  If your queries make sense, there is no need to cache the data. Just leave it in the database. The only lists of data I have, are query results that go directly out to the client. I was a bit disappointed in a way. I was looking forward to using the new java 8 lambdas for what they were worth, but quickly found out that SQL does most of that already.  Why would I need to iterate over lists to extract certain fields, or update things based on certain conditions? SQL does that already. 

Another cool benefit of not having loads of cached data all over the place, is that all the clients can get database updates really fast. For this purpose I have made a BigBrother class, that I use for spying on certain rows in the database.  When a user logs in, the following statement is run:
myChildren.forEach(child -> spyOn(child, childUpdateListener));

myChildren is a list of children the currently logged in user is linked to.  By spying on them, the childUpdateListener will be notified every time any data on any of them changes. If the child changes their name, all users who are logged in, will see that change instantly.  What is more, if anything changes in something that has a foreign key reference to that child; a new event is added, that they have taken part in, they have been assigned a new grownup, whatever, the childUpdateListener also gets notified.   Whenever I do an update, an insert or a link, my BigBrother class checks if there are any spies registered to either the primary key of the updated class or any of its foreign key values.  All spies found get called with a suspectAltered(Object update) callback with the object that has just been inserted or updated as a parameter.  Straight away.  I just love it when apps are fast. 

I would put more code in here if it weren’t for the fact that Tumblr doesn’t make it easy for me :-( Need to get a proper blog.  As I mentioned already, the full project will be available for all by the time I do my JavaZone talk on this subject the 11th of September 2013. You can look over the details then :-)

 

Update: here is the code: https://github.com/christingorman/baxter (please don’t spend too much time, unless you want a laugh :), on the front end stuff, I’ve never really worked with javscript, HTML or CSS)

And here is my javazone presentation: http://vimeo.com/74354408

 

 

 

 

Sep 2

SO SQL

I didn’t do any programming before university.  I wish I had.  I have no excuses. My dad got us an Amiga when I was quite young.  I spent countless hours in front of a computer as I grew up. But I was always a consumer. Never a creator.  Until university.  I remember coming home after my first year and a friend asked me:

So can you write programs now?
Yeah, I said.  
What kind of programs? He asked.  
Any kind, I said.  
What, so could you write, like, Tetris? 
Sure, I said. I can write anything.  

Programming is awesome.  Turns you into a kind of super hero.  With just a computer you can make anything at all.  The world is full of possibilities.  I loved that feeling.  The feeling that I had the power and the capability to create something worth while. 

I don’t get that feeling as much anymore.  In the java-world we see everything as a solved problem.  People, smarter than you, have already written solutions for similar issues. What’s more, they’ve made them available, for free, for anyone to use.  Hibernate, Camel, Spring…  Our only job is to connect them together, and voilà, a standards compliant system emerges (or so the story goes).  Sure we’re still making stuff. Burger flippers at McDonalds are making stuff too. But I don’t think they’re having too much fun doing so either.

Instead of using our minds to figure things out, we’re consuming manuals, each one several thousand pages long, and googling stack trace elements to figure out why things aren’t working (because of course they aren’t).  We’re passive consumers, cursing Gavin King as we look forward to the weekend.  

If only the end products really were that much better than if you’d written your own code, that might justify our suffering. But that’s just not true.  With all our standard frameworks, we’re making Big Macs, when we could have been making a delicious meal to remember.

I have previously tried to address the issue by highlighting the problems caused by using generic frameworks like Hibernate.  But focusing on the problem will only get you so far.  It’s the solution that needs highlighting.  So that’s what I’ll try to do this time.  I want to show you how you yourself can do what the frameworks do. Only better, because the solution will be tailored to your particular problem.  All with very little effort.  Most often it’s so easy, even a girl could do it :-D

First off I want to deal with database interaction.  Because relational databases are awesome! They are fast, and easily give you exactly the data you want without much hassle. You can aggregate the data, access it from multiple clients simultaneously, all with a query language that is so easy to understand, you can even show it to non-IT folk and they get most of it. Why would you want to abstract yourself away from such a great tool? I don’t want to hide the database behind an OO domain model, I want to access the database directly, taking advantage of all its wonderful features.  

Now  you have to keep in mind that I’m not suggesting a universal replacement for hibernate. I’m only showing you something I’ve done, that worked for me, hoping it will make you realize that you could do EVEN BETTER and make something truly amazing.  My solution is based on a proof of concept I did at work earlier this year. But as the domain at work is quite complex (the Norwegian housing benefits system), I’ve put together a simpler little web app using the same idea for database access.  It’s an app intended for daycare centers, to keep track of their activities and duties, and to facilitate communication between the parents and the daycare center.   

The most important technical idea in my app is that there is no domain model. Instead of working on a fixed domain model, I work against a fixed database schema.  I want my java code to relate to the database directly.  On the database’s terms.  Doing queries that make sense from the context you’re in.  Some times you only want the first and last name of a person, other times you just want their age. Other times again, you want to count the number of children who still use diapers. You don’t care about the details of any of them.  I have split my app up into the different “screens” the user encounters. One package per screen. Each package has it’s own set of query-result-objects - that work like your typical domain model would.  They have properties with getters and setters, and utility methods that apply for the current context.  I’ve gone for a very hibernate’esque way of mapping my objects up to the database, as I figured people are all pretty familiar with that kind of thing. Here is an example:

public class ChildName {

    @Column(column=child_id) private Long child_id;

    @Column(column=child_first_name) private String child_first_name;

    @Column(column=child_middle_name) 
    private String child_middle_name;

    @Column(column=child_last_name) private String child_last_name;

    @Column(column=nickname) private String nickname;

    …getters and setters and so on…

}

When doing a database query, it is fairly straight forward to populate objects with the correct data.  I use reflection to get the annotated fields, then set values from the result set based on the defined type of the database column.  Selecting data from the database can look like this:


public ChildName getChildName(long childId) {
    return db.runSql(ChildName.class, “select
          child_id,
          child_first_name,
          child_middle_name,
          child_last_name,
          nickname
          from child where child_id=?”, Arrays.asList(childId));
}

As you can imagine, “db” here is a class that deals with doing the actual jdbc-calls, with all the try catching and boring boilerplate.  

Already we’re starting to get there.  But I must admit I’m still not happy. At this point I’d be worried about all the sql statements that need to be maintained. What if I change the name of a column? Or drop one? The app would stop working and It wouldn’t be detected compile time. To solve this I’ve written a few lines to help me make all the simplest queries automatically.  The ones with no complex joins, nested queries, or tricky where clauses. Most of the queries in other words. Querying now looks something like this:

public ChildName getChildName(long childId) {
    return db.selectOnlyOne(ChildName.class,
    new Where(DatabaseColumns.child_id, ” = “, childId);
}

The query generated would be the same as the one above, but we didn’t have to write any of it ourselves. I can also join in fields from other tables without having to write any SQL. Let’s say I have the following class:

public class ChildInDaycare {
    @Column(column=child_id) private long childId;
    @Column(column=daycare_name) private String daycareName;

    ….
}

Here I’ve got columns from two different tables. child_id comes from the Child table and daycare_name comes from the Daycare table. If I try to select this class with the same method as before, the following sql gets generated:

"select child_id, daycare_name from child, daycare where daycare_id = child_daycare_id and child_id=?"

The code hiding in my db-class automatically finds the foreign key and joins the tables together.  How? Well I’ve put all the tables and columns in my database in enums. An enum for the tables and an enum for the columns. My column enum, defines not only the name of the column, but also what kind of column it is - PrimaryKey, ForeignKey, Version (will get to that) or just Field.  It also says which table it belongs to, and if it’s a foreign key, which table it relates to.  Here are the columns in my child table:

public enum DatabaseColumns {
   
    child_id(Long.TYPE, Table.child, PrimaryKey),
    dob(LocalDate.class, Table.child),
    c
hild_first_name(String.class, Table.child),
    
child_middle_name(String.class, Table.child),
    c
hild_last_name(String.class, Table.child),
    
nickname(String.class, Table.child),
    
color(String.class, Table.child),
    c
hild_daycare_id(Long.TYPE, Table.child, ForeignKey, Table.daycare),
    
child_version(Integer.TYPE, Table.child, Version),

   ….and so on
}

Tables are in another enum.  With these two enums, I create a simple weighted graph with tables as vertices and foreign key joins as edges. Then I just do normal graph traversal stuff to find the shortest path between the tables represented in the class specified.  It has to be a weighted graph by the way, because a many-to-many join involves a third table in between the two ones being joined.  I make sure a many-to-many join gets half the weight of other joins, so the distance between joined tables are equal regardless of join type.  Might sound complicated, but it’s all done in about 20 lines of code (I use jgrapht).

I have to explicitly write the SQL if I need anything other than cross joins,  of if I need nested queries, or where clauses involving the OR operator.  I could have extended the db-code to handle these things as well, but  I’ve already got about 500 lines of code in my “framework” and I don’t want it to grow much larger.  I don’t want it to do difficult stuff for me, as it’s the difficult stuff that I really need to control properly myself. 

I mentioned the column type Version, let’s talk about that now.  My code also lets you do updates with no sql (I’m lazy).  Say I want to change the name of a child:

child.setName(‘cool name’);
db.update(child);

There you go. Pretty painless. To make sure nobody else had modified that object in the meantime, I use a version column. The update statement will look something like this:

"update child set child_first_name =? where child_id=? and child_version=?"

If the version in the database is not the same as it was when the child data was retrieved from the database I throw a ConcurrentModificationException that should be handled in the app. As far as I know, that’s what Hibernate does too. 

So how about transactions? Well, I’ve put all the database interaction in separate classes that are “wrapped” in a dynamic proxy that starts and commits/rolls back a transaction before and after the method invocation.  So my business methods don’t have any transaction logic in them. Each servlet (I’m using servlets in an embedded jetty server) gets injected with a class to do its database stuff:

TimelineServlet timeline = new TimelineServlet(transactional(new TimelineCRUD()));

Where my TimelineCRUD class has methods that, for instance, insert events with attached images, linked to children and groups. Things that have to be run in a single transaction and committed together.  Even though I’m not writing much SQL myself, every SQL statement executed is initiated explicitly.  This makes it very clear when transactions are needed. In JPA, you don’t really see that you’d need a transaction. You just say entityManager.persist(child). You have no idea when the sql will actually be executed, nor do you really see how many queries and what kind of queries are created. It depends on the cascade type defined in the join annotation. And that’s the same regardless of where in the app you are. 

Speaking of transactions, they come in handy while testing as well. I have a test runner that connects to a real database, with the same schema as my “production”-database, but with no data in it.  Before each test it starts a transaction, then rolls it back at the end of the test, thus ensuring that the database stays empty.  You might say that it’s not a real unit test if you’re connecting to a database. Maybe not. But it sure is useful.  No mocking required, I test how the application really works, in a realistic environment. In my @Before method I insert all the data I need for my test, then I run my business code and ensure the resulting data is what I expect.  Here is some of my code testing my db-code:

@RunWith(DBTestRunner.class)
public class DBTest {

    ChildTableEntry baxter = new ChildTableEntry();

    ChildTableEntry zoe = new ChildTableEntry();

    ChildTableEntry dollyo = new ChildTableEntry();

    @Before
    public void setup() {
        … initialize the entries with some values…

        db.insert(baxter, zoe, dollyo);
    }

    @Test
    public void should_create_simple_select() {
        List<ChildTableEntry> children = db.select(ChildTableEntry.class);
        
assertThat(children).containsOnly(baxter, zoe, dollyo);
    }

This way of testing isn’t my idea, I should point out.  We did this in a project I was in a few years back and it worked really well. I thoroughly recommend it.  Takes a few milliseconds longer to run your tests, but your tests get so much better and they give you a lot more confidence that your app works the way it should.  I never test the details of the SQL generated, by the way.  I verify what the queries return, that’s what’s important.  If someone adds a space or changes the order of some where clauses I don’t want the test to break.

There is more! But I’m tired and need a break. Maybe I’ll show you some more stuff tomorrow? I’m writing this as preparation for a JavaZone talk I’m doing next week (11th of September 2013). See you there? I’ll have the whole project put somewhere you can get at it by then too. 

Update: here is the code: https://github.com/christingorman/baxter (please don’t spend too much time, unless you want a laugh :), on the front end stuff, I’ve never really worked with javscript, HTML or CSS)

And here is my javazone presentation: http://vimeo.com/74354408

 

Apr 7

The real reason we need more women in software

I just thought of something, very profound as usual.  Women are more complicated than men, right? We say yes, when we mean no and vice versa. We expect people to read our minds, instead of just telling them what we mean.  We’re better at multitasking, we’re more flexible, but also a bit more fragile. Am I right guys? Still, for whatever reason, you seem to be attracted to women.  This complexity must draw you in and fascinate you somehow.   

Women on the other hand tend to prefer men.  Screw the soft and the flexible, we prefer straight forward, stable and strong.  So here’s my thought: Does this apply to code as well?  Do guys prefer complexity in code to a larger extent than women?  Think about it, if a guy sees a hard coded value he shudders and instantly hides the value under at least 5 layers of abstraction.  When a woman sees one, she’s like: “Oh, great I can see what’s going on here” I was tempted to make a joke about how I like my code like I like my men… but somebody might get fired.  

I have absolutely no data to back up this theory, but I think there is something to it.  In my experience, women are more interested in the end product than their male counterparts.  As soon as men start coding, they often seem to forget that they are actually meant to make a product.  They get totally distracted by the code itself.  Instead of writing a solution for the problem at hand, they try to solve the problem of how to solve any problem. This is far more complex.  When women fail to see the beauty of this complexity we’re made to feel stupid.  We aren’t.  We just like it simple, reliable and to the point.  

People are always telling me that I exaggerate and generalize.  As if I don’t know this.  Of course I’m generalizing and exaggerating.  I used to be American for crying out loud!  So just for the record, I don’t hard code every value, quite few in fact.  And I know plenty of guys with a great ability to think practically about the problem at hand.  But I still think we could use an increase in software developers who care about the products we’re creating over the code itself.  I think women, on average, can deliver that.  

Dangerous jokes

At my first full time job I was asked to come along to meet a customer. He wanted us to spend a week developing a prototype, then if he liked it, he would possibly pay to have us develop a proper application.  I did my best and after the week was up he apparently told my boss that he was giving us the contract on condition that I would be the one working on it.  

I remember my boss and the sales manager coming in to tell me the good news: “Well done Christin! We won’t ask what you did, but well done!” *nudge, nudge, wink, wink*  ”Hahahahahaaa!”  We all laughed and went out and had a nice lunch together.  They were such great guys to work with. Always joking about everything. Nothing was sacred.  The office was always filled with laughter.  I loved it.  Why wouldn’t I?  Should I have been offended by their insinuations?  Why? They knew I was good at my job, they didn’t actually think I had slept with the guy to get the contract. THEY WERE JOKING.  And shocking as it may seem, females can actually enjoy jokes too. I’m one, and I know of several others.  

Later I got a job doing R&D for a large international oil company.  As you all know women are few and far between in the IT-industry, the more technical it is, the fewer women there are. There were very few women doing anything technical at the office. And which project do you think they put me on? Programming the central control system for land based seismic source VIBRATORS.  I thought that was hilarious! I was just dying to make jokes: “Well I hope you approve of test driven development! :-D”  But NOBODY joked about it.  Not a single word.  Now THAT made ME really uncomfortable.  I was confused - “Are they joking behind my back?” “Don’t they have any sense of humour at all?” “Can I joke about anything at all in this place?” I just can’t work in a place like that.  Where I can’t be myself.  Where people are afraid of making jokes.  I mean that quite seriously. If somebody had joked about the vibrators, if the culture allowed that kind of banter, I might not have resigned.  (The lack of jokes was of course not my main reason for resigning, but more fun  around the office might have made those reasons seem less pressing)

The worst part is, I don’t even get it - why on earth should women take offense at sexual jokes? Women like sex too (If you don’t, you’re doing it wrong) Romance novels sell like hotcakes.  I often wonder what would happen if guys at a Rihanna concert started behaving like girls do at Justin Bieber-concerts.  Women go completely nuts about good looking guys.  Why on EARTH should we be so up tight about men even mentioning anything sexual? Jokes about women being stupid - now there offense makes sense.  Sexual harassement, guys actually expecting sexual favors, there complete outrage makes sense.  But that’s not the issue here.  Let’s not confuse sex (and jokes about sex) with sexism.  I know they sound the same, but there is a big difference.  

It makes me so sad and angry to see that some women are trying to turn the whole IT industry into the kind of work place that I felt I had to get away from.  Well you don’t represent me, girls.  So there.  Maybe mine is a minority opinion, as usual. But there it is. For what it’s worth.  

The truck factor

I like writing. It helps me think, helps clarify my thoughts. I even think writing about coding has made me a better programmer. It has certainly made me think in a more structured way about my coding style.  I just wish I took the time to write more often. I think you should too.  And when you do, make sure not to do so alone.  You should never write anything all by yourself. You’re not good enough. No one is.  A team of about 7 people simultaneously co-writing should be about right.  Give or take a few.  ”7 people? That many?” I hear you ask. Don’t worry, it’s the only responsible way to write and it’s totally worth it.  You’ll see.  First you divide your main idea into a few main topics you want to cover. You then split up in pairs and start writing, making sure to switch partner and topic at regular intervals. That way everyone gets familiar with the entire document, and everyone gets to put their own little personal touches on the final text. The results are very charming. Another benefit is that if you get hit by a truck, nobody gives a shit, because you are totally replaceable. Yay! Everyone in the team also gets the opportunity to voice their opinions and have meetings upon meetings to discuss all the possible ways the document could be structured. The more meetings that are held, the better the document becomes. Obviously. Some people get motivated by  progress, and the feeling of getting things done. Not me. I mean how can you enjoy completing something without having discussed and documented it’s every single aspect and every possible way it could have been different, but isn’t and why those differences matter or don’t matter to whom and when they matter more and less etc. I get goose bumps just thinking about those meetings. So thrilling.  And even if you don’t like to spend all your day in meetings, you shouldn’t just think of yourself. You have to think about those who might have to maintain your solution in future. I mean imagine the disappointment of a newcomer to the writing-team on learning he would NOT get access to a wiki describing in detail how the document isn’t written and why it’s not written that way. He’d be devastated. We must think of him.

Now I know that not many authors work like this, but I can’t figure out why.   

After years of nothing but team work (that I’ve enjoyed by the way, and learned a lot from, despite my sarcasm above) I’ve just had the opportunity to work pretty much by myself on a new project and it was so much fun! I could just do stuff! I could make decisions and get things done.  At work!  It was amazing!  I had forgotten just how much faster I can do stuff when I’m alone compared to in a team setting.  

"But faster isn’t better", you might say. Nonsense. Of course faster is better.  Either, you finish early, it all works and everyone is happy. Otherwise you realize it doesn’t work, but you still have time to fix it.  Your next solution will then be based on real experience of the problem, rather than the plans made before you had even started working on the issue.  I swear I could implement stuff at least 3 times over in the time it would take a team (with me in it) to do the same. I don’t mean to say that "I’m awesome", but teamwork takes so long! You can’t just go ahead and do stuff, you need to agree about what to do first.  If you don’t, everyone will implement things differently and the code turns into a mess.

Some say all this planning and discussion leads to better decisions being made. I’m sure it does.  Maybe the chances of getting it right increase by 10% or so.  But with all the extra time it takes, you had better get it right, because there won’t be any time to do it differently should the chosen solution not work.  By cutting out the team bureaucracy you’re indeed exposing yourself to a bigger risk of failure, but you’re also decreasing the impact of it.  With all the time saved, you’re able to learn from your mistakes and actually have time to redo stuff.  Having 200% more fun all the while.

I’m not saying we should all sit in our corners and avoid each other all day. The best part of my recent experience was that I was still sitting with my colleagues, able to discuss issues, show them what I was working on, etc.  I would never argue that we should stop collaborating. I’m a big fan of pair programming even. But not everywhere and all the time. Is it so wrong to enjoy a bit of autonomy and private focus? Am I a bad person for wanting to work more like authors do -  with personal ownership of my work, but lots of peer reviews to ensure quality and knowledge transfer? Agile theory suggests I am. I suppose I’ll end in agile hell. Will you join me there?