wissel.net

Usability - Productivity - Business - The web - Singapore & Twins

Function length and double byte languages


Complexity is a prime enemy of maintainability. So the conventional wisdom suggests methods should be around 20 lines, with some evidence suggesting up to 100+ lines.

When I review code written by non-native English speakers, especially when their primary language is double byte based, I find methods in the 500-1000 lines range, with some special champions up to 5000 lines. So I wondered what might contribute to these function/method worms.

Let's compare:

function happyWeekend(me) {
    if(computer.isRunning) {
        projects.forEach(p => {
            if (p.isOpen()) {
                p.commit();
                p.close();
            }
            })
    };
    colleagues.forEach(c => {
        if (c.isPresent) {
            sayGoodBy(c);
        }
        });
    var availableFriends = [];
    allFriends.forEach(f => {
        var call = callFriend(f);
        if (call.successful()) {
            if (call.friendAvailable) {
                availableFriends.push(f);
            }
        }
        });
    var restaurantVote = {};
    allRestaurants.forEach( r => {
        availableFriends.forEach( f => {
            if (f.likesRestaurant(r)) {
                if (restaurantVote[r]) {
                    restaurantVote[r] += 1;
                } else {
                    restaurantVote[r] = 1;
                }
            }
        })
    });
    var restaurant;
    var curMax = 0;
    for (r in restaurantVote) {
        if (restaurantVote[r] > curMax) {
            restaurant = r;
            curMax = restaurantVote[r];
        }
    }
    // and so on ...    
}

Or this one:

function happyWeekend(me) {
    leaveOffice(me);
    var availableFriends = callFriends(me);
    var restaurant = pickRestaurant(availableFriends, me);
    wineAndDine(restaurant);
    hangOut(availableFriends, me);
}

I see these main contributors:

  • When the programming language (based on English) doesn't match your native language, you won't ?tell your story' in a breakdown manner, using simple statements as function/method names. Inventing useful function names in a language not your own affords more cognitive effort that just writing a long function. This is especially true when you can't name variables or functions in your native language - something that's prevalent for double byte character sets
  • Lack of a test first mindset: Even if you don't write a test upfront, you can ask yourself: How will that function be testable. Once you ignore that, functions grow larger
  • Not your problem: you are a contract developer and there's no code quality requirement/metric in place. You know that after a successful deployment you are off the hook and someone else has to deal with that code. Appreciation for your work is absent in your environment, so you stopped caring

Posted by on 2018-04-09 10:18 | Comments (0) | categories: Java JavaScript NodeJS Software

Comments

  1. No comments yet, be the first to comment