Category: programming

  • a bit of refactoring

    In software engineering, “refactoring” a source code module often means modifying without changing its external behavior, and is sometimes informally referred to as “cleaning it up”.

    In particular, adding new behavior to a program might be difficult with the program’s given structure, so a developer might refactor it first to make it easier, and then add the new behavior. Refactoring is also a tool for removing bad code smells that exist in code.

    ~ Refactoring

    what is refactoring?

    Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior. Its heart is a series of small behavior preserving transformations. Each transformation (called a ‘refactoring’) does little, but a sequence of transformations can produce a significant restructuring. Since each refactoring is small, it’s less likely to go wrong. The system is also kept fully working after each small refactoring, reducing the chances that a system can get seriously broken during the restructuring.

    ~ Martin Fowler’s site on Refactoring

    Sometimes code gets messy over time, and investing some time not developing something new, but simply refactoring what already is there, really reduces a lot of time in maintenance results in a significant boost to productivity.

    But the most common obstacle for refactoring is “I’m too busy to do it!

    And so, given a programming task on some old complicated source code, there are two options. One is to straightaway start with the task – wrangling with timelines, struggling to debug and decrypt the code in order to complete a given task. An other is to spend time in refactoring and sorting things out first (with test cases) and then actually start with the given task. Of course there’s no ‘best’ solution but it depends on the situation.

    The first one seems to be the easiest. The second option may require some courage – because there seems to be the somewhat overwhelming risk that it might affect the entire system, introduce new bugs, end up as a dead investment if the refactoring cannot be completed succesfully… all kinds of difficulties!

    Socrates had said…

    An unexamined life is not worth living.

    Not sure exactly what he might’ve meant, but one guess is that he could be talking about refactoring one’s life.

    I’ve actually done that gradually over the past year or so, and have found that it has tremendously improved the quality of my life.

    For example, a lot of time was being occupied in driving around in traffic. I took up a new job (not the only reason of course) and moved to a new house to live in. Now my new workplace is just 15 minutes drive from home. This is not a single drastic event but happened gradually over time.

    One more thing is I gave up ambitions of climbing the corporate ladder gaining a better and better position, to lead other people and so on (and earn higher and higher salary of course!). Instead, I decided my priority was simply to become a “good developer”. Btw this is below the dignity of a lot of software engineers, because compared to a construction industry, programming is like the basic cementing job which freshers from college do. At first this seemed to be a total lack of ambition. But this makes me happy, and I have any interest in impressing anyone with a sophisticated designation 😉 And my new job respects this attitude.

    Another idea is that 4 to 7 PM is the most beautiful time in the evening, best spent roaming around in a park or on the terrace watching the sunset with my son, instead of sitting in front of a computer. Since I have an option to work from home (depending on the project team situation), I often leave for home around 4 PM and really have a nice outdoor evening either on the terrace. continue working from home when it gets dark. This balances working with the rest of the team in the office, and has an added advantage that it also gives me some extra overlap time with my American colleagues.

    The above ideas worked well for me. There are some other things I tried which didn’t work out. But I’m glad I did give them a try, and experimented with life.

    Maybe you may not have the same opportunity (to work from home, for example) but you may have other even better possibilities, if only you would examine with a fresh perspective. I would love to hear your experience if you’ve want to try/have tried out anything new.

    One thing that this speaker he himself is a world traveller travelling all over the place meeting so many people all over the world… had said “I pity you, you meet the same people – same family members – same colleagues – day after day” But I disagree (and also possibly disagreement was a reaction the speaker hoped to provoke). As long as one manages to avoid getting caught in a mechanical daily routine, one can see the newness in every day and in every person around them! 😎

    It made a lot more sense to begin with whatever I have, instead of wishing for something else. I started becoming more sincere about whatever I was doing, including my job. So rather than begin by changing external circumstances, I looked at how I could change the way I was handling them. The external changes happened as a natural consequence, without too much effort.

  • grails – hasMany does not generate dynamic properties

    This is a technical post, I should probably create another site for such things.

    Just want to document some problems I came across while trying out some examples from The Definitive Guide To Grails…

    class Bookmark {

        static hasMany = [tags:Tag]
       
        URL url
        String title
        String notes
        Date dateCreated = new Date()
    }

    class Tag {

            static belongsTo = Bookmark
           
            Bookmark bookmark
            String name
    }

    b = new Bookmark(
            url: new URL(‘http://grails.org’),
            title:’Grails’,
            notes:’Groovy-based web framework’)

    b.addTag( new Tag(name: ‘grails’) )

    b.save()

    These simply did not work, giving the error MissingMethodException, and basically the dynamic methods just weren’t being generated.

    To cut a long story short, here are some things that solved the problem:

    – In the Groovy plugin for eclipse – Eclipse->Project->Properties->Groovy Project Properties->Turn of the Groovy compiler.

    – Disable automatic build in Eclipse, which generates unnecessary classes, that overwrite the groovy dynamically generated classes.

    A cryptic error while running from Eclipse :

    Cannot execute native2ascii failed.
    This can be resolved by running grails runapp once, which does the conversion of native to ascii initially.

    Most of the above solutions were at nabble.com though it still involved quite a bit of searching!

    If these issues are not sorted out in the development environment,
    then one may consider using Eclipse only as an editor, using the command line for grails!

    A key point is that the hasMany relationship has been revised as described in GORM – Defining relationships, making addTag deprecated, changed to addToTags

    In case anyone has just typed in the code into a groovy console, it might not work as the classes should be defined as proper domain classes using create-domain-class target.

    Having programmed for over 10 years in C, C++, COM, DCOM, STL, MFC and .NET right from its pre-beta stage of evolution, as well as Java right from 1.0 in college days, I find Groovy on Grails defining the future of technology. Its based on a very impressive solid foundation (of JVM, Hibernate, Spring) Want to write in more detail later sometime but anyway there’s already enough available out there already!

  • software architecture revisited

    This is a very old writeup from a few years ago from my older website, which I came across recently again, and am reposting here… Now I have a lot more to say about this topic… but just putting in the old one for now…

    Once in a way, I feel software might be a career just to make money while the opportunity is there and then retire and do something else. Especially when we consider the well known related problems such as RSI, eye strain, back injuries, etc. not to mention the vast environmental pollution (plastic CDs, DVDs… esp now with rewriteable use and throw ones).

    After all, its sometimes hard to answer the question: how is this helping anybody or improving anybody’s quality of life? How does it make it matter to be involved in this application to keep track of a warranty of some printer that some customer had bought whom I personally dont even have a clue about. Surely there must be more worthwhile investments of time than this? Especially when you compare this with something like for example the medical profession, where doctors cure people’s diseases – or physicists who study about the our universe itself, or come up with something tangible that people actually do something with rather than sit glued to a computer monitor all day…

    In “Black Holes and Baby Universes and Other Essays” by Prof. Stephen Hawkings (reknowned scientist and author of A Brief History of Time), he says after a disease and a surgery he was paralyzed, speechless and completely immobile. He still wanted to continue his research.

    His communication was reduced to a person flipping through alphabets, and he would raise his eyebrows for the required one. He would write sentences this way – very very slowly.

    Then a software programmer sent him a device which would act similarly, and he could use his finger to indicate when the letter was reached.

    This was such a breakthrough that its evolution was inevitable… a laptop was replaced by a PC embedded into his wheelchair making him mobile. A screen was eventually replaced by a voice synthesiser – allowing him to speak again!

    This man written off by medical science, has been able to go on giving countless speeches all over the world!

    Ultimately it does come down to people who actually using whatever we are working on, who could be adversely affected at some other point in time and space in a manner in which we simply cannot imagine now, if we treat what we are doing right now even with the slightest negligience or indifference. But we get so bogged down with the technical details or stress or distractions or so on, that this very key aspect often gets totally forgotten.

    Incidents like the one related by Hawkings was a kind of a reminder to me on why I joined this industry in the first place… a belief that software, like everything else, does have its own place in the great scheme of things! 🙂

    ——————————————————————————–

    Some links… (with thanks to those who sent these)

    The Software Pyramid

    Its interesting what it says about Architects : “A few thousand visionaries sketch out entire systems to handle complex jobs.”

    Anyway on a different note… this is a beautiful relevant article … a MUST read for anyone with technical aspirations 🙂

    .NET Brings the Architect to the Fore
    As .NET helps advance Microsoft technologies in the enterprise, the role of the software architect becomes increasingly important.
    by Eric Lynn

    Other links…

    This article from Deccan Herald I particularly liked :

    Upgrade yourself from the world of coding
    How does one build a successful technical career? SUBROTO BAGCHI gives nine key factors.

    Passion at work by Prof. Arindam Chaudari

    The following I’ve meant to read but havent actually really read till now 😉 But have gone through them, they look quite promising, maybe worth bookmarking…

    Thoughts and experiences from an enterprise software architect – Jorgen Thelin

    Microsoft Architects Journal

    Independent analysis of Microsoft

    It all comes down to 1s and 0s

  • develop debugging skills playing Mastermind

    This was one of the really cool tips from Steven’s presentation. I used to play this game in my childhood when my cousin sister had gifted us this from the US. Now I am sometimes apalled when I see developer’s struggling to find simple problems in any code, and was wondering how I could help them improve this skill.

    As Steven (SQL training program) highlighted, this game really develops one’s debugging skills, and he strongly advocated this for every developer to play even at company time 😉

    You can give it a try – it’s simple, and I could find a nice one online.

    Below are the rules from google…

    —–

    Object of the Game
    The computer picks a sequence of 4 pegs, each one being one of any of six colors.
    The object of the game is to guess the exact positions of the colors in the computer’s sequence in as few guesses as possible. After each guess, the computer gives you a score of exact and partial matches.

    Rules
    1. The sequence can contain pegs of colors: red, yellow, green, blue, white, black
    2. A color can be used any number of times in the sequence.
    3. All four pegs of the secret sequence will contain a color – no blanks/empties are allowed.
    4. Each guess must consist of 4 peg colors – no blanks.
    5. You have 8 guesses to find the secret sequence.

    Scoring

    For each of the pegs in your guess that is the correct color and in the correct position, the computer will give you one small black peg to the right of that move. If you score 4 small black pegs on a guess, you have guessed the secret sequence.

    For each of the pegs in your guess that is a correct color in an incorrect position, the computer will give you one small white peg to the right of that move. Together, there will be no more than four small black and white pegs for each move.

    If none of the pegs in your guess is of a correct color, you will see no small pegs to the right of that move.

    Sample scoring:

    If the secret
    sequence is…
    And
    your guess is…
    Your score
    would be…
     
     
       

    Some detailed instructions if you like:

    http://www.pressmantoy.com/instructions/instruct_mastermind.html

  • Steven Feurstein's PL/SQL resources

    Steven may not have been a rock star but his presentation on PL/SQL Best Practices definitely ROCKED! Its been the most entertaining, informative and positively INSPIRING tech talk I’ve ever attended (and believe me, with 10+ years in the IT industry I’ve attended quite a few and given some of my own as well) 🙂

    I really really wanted his slide set to convey all those high quality ideas to my team mates in office, and was delighted to see he’s so generously put them on the net: SQL Training and presentations (Unfortunately I had some trouble downloading a couple of them) Though I took down a lot of notes, I just cant wait to get my hands… er mouse… on the main thing! 🙂

    His presentation really changed my perspective of PL/SQL programming, and the best part of it was packed with so many invaluable insights beyond the core topic!

    What really impressed me was the huge contribution he was making to the industry in terms of books, open source tools, presentations and other material which he’s made freely available on his website – enriching the quality of his own life as well as everybody else’s. At dinner time I asked him for an autograph, and he wrote me some very nice words “Dear Sanjay, May your code bring peace and satisfaction” 😎

    —-

    Also see: His younger sons art website: Chris Silva and don’t miss the artistic statement – just to appreciate the quality of life that’s possible.