Our Blog.

Expert thoughts from our software developers and design leaders.

  • Larry Morales

    Code review: Refactoring (part II)

    In part one I talked about what refactoring is and all stuff related to that. In this post I want to show you how refactorings are related to frameworks and give you a real example of their use.

    Frameworks were design to make your life easier in some way, and they should fit to solve your problems. Nowadays developers rarely build apps from scratch, we use frameworks according to our needs to speed up construction, testing and delivery. You realize that get used to a framework doesn’t mean you are except from live without code smells. You can actually apply refactoring techniques using the components and elements those frameworks provide.

    To give you an example, last week we were working in an app which was built using Ember.js framework, while we were in a manual code review activity, we found some duplicated code into three route components.

    So, in a nutshell beforeModel hook validates if an user has been logged in, and if not then it redirects to login page. There is obviously a duplicated code smell winking its eye, we need to move that code into “something" that allows us to reduce changes to this logic be replicated three times or many more if new routes are defined and need to be constraint by this logic.

    Remember previous post I mentioned code smells can be solved by using refactoring techniques? Move Method refactoring is the one I chose.

    Before I dig in it, a question emerges, where do I put that code i’m going to move?, here is where my framework understanding should be highlighted. Ember.js introduces concepts like Mixin and Services, which were my first options to use, but then I notice Ember uses routing hierarchy delegation and it is like implementing inheritance between routes. I took latter approach, the reason was because it was a routing problem and that piece of code should be also managed at the same level, there are not another components in the app that use that kind of logic so I discarded mixing and services. ApplicationRoute is the root parent of all routes so I applied move method refactoring to put my code into it. I started applying move method in one route, tested it and then move on the next one. Here is the result.

    The other routes don’t need to define this method anymore and it is also located in just one place.

    To conclude, there are a few things I want to highlight about this exercise.

    First thing is whatever framework, programming languages, even markup languages you use you are not except from introduce some code smells, this is something natural that happens most of time, the key is to learn to detected them and mitigate them using refactoring techniques.

    Second thing is you must have a basic understanding about the framework you use, get knowledge about overall components, what are their roles, many of frameworks were built following some architectural patterns so they can make your life easier, when applying a refactoring take in account framework components first, then see if it can be solved by using low level elements like classes, objects, etc.

    Finally, dedicate yourself at least 15 min of your day to learn about refactorings and code smells. For every code smell you learn try to find in your code if it exist, then apply the refactoring to mitigate it, this is the best way for training and put yourself into a different mindset. Practice and practice.

    Keep Learning, Keep Applying…

    Continue Reading
  • Larry Morales

    Code review: Refactoring (part I)

    Let’s imagine you want to visit a friend in town but you don’t know where he or she lives, so you call him/her and ask for his/her address. The address becomes your target place, it’s the place where you want to go. When you are about to decide to move, you think, what path should I take so it can lead me to my destiny?…So your options are probably based on less time path, or more secure path, or nearest path. After you decide to use a path, you move and follow it until you end at your destination (your friend’s house).

    That kind of thinking is a little bit related with the concept of refactoring. Imagine now that the situation is not to visit a friend but to extract a piece of code in your app. With that piece of code, you want it to live in a new method, the new method now becomes your target (the place where you want to go). So you think again, what path should I take to put that piece of code into a new method in a safe way?…

    Wait, safe way?, extract a piece of code can involve a lot of things, you could extract local variables which are used later in the method you extract your code from, code you extracted can also depend on some variables defined before from the code you extract, so this process should be done carefully as current behavior should be preserved.

    A refactoring is a prescription of a safe path you need to take in order to solve a particular problem in your code. I like to think about refactoring not as an activity where you remove duplicated code or make your code easier to understand in a deliberate way, a refactoring feels like a guideline, it is a step by step guide to transform your unhandled piece of code into a handled one in a safe way, without affecting code behaviour.

    Following the example I mentioned earlier, supposed that the code I want to extract is related to outstanding calculation:

    which path should I take? how should I extract that code? well, there is a refactoring called Extract method with the mechanics(path) to extract a piece of code into a method in a safe way. I use this refactoring almost every day and it has became pretty natural to me to apply. After applying Extract Method Refactoring my code looks like this:

    Look that code I extracted depends on customer variable. customer is just used to read some of its attributes (orders), so I passed it as a parameter to the function calculateOutstanding. However, outstanding variable is calculated and then it is used later in print details section inside printFor function, that’s the reason why it’s returned.

    Extract method is just one of many refactorings existing. Every refactoring has a template, if you click on Extract Method link mentioned before, you’ll see the first thing is a definition about that refactoring, and coming next you’ll see a before and after refactoring class diagrams, which means “go from this start point to this target point”, there is also a motivation to use the refactoring, mechanics(path or step by step) for applying it and some examples.

    Nature is full of balance and contrast…

    There is no existence of light if darkness would not exist, we would not talk about war if we don’t know what peace means, we inhale oxygen produced by plants to exhale carbon dioxide which feeds plants. So, we can not mention refactoring without talking about code smells. Refactoring would not exist if code smells do not emerge.

    In a simple way, code smells are those “things” that make you feel like there is something wrong in your code. When emerging, they can make your code hard to change or understand. Some of us recognised duplicated code introduced in an application. Duplicated code is considered as a code smell because of the impact it can cause when changes need to be done. You will need to introduce the changes as many times as the code is duplicated. Here is a list of some code smells you can probably deal with:

    Duplicated Code
    Long Method
    Large Class
    Long Parameter List
    Divergent Change
    Shotgun Surgery
    Feature Envy
    Data Clumps
    Primitive Obsession
    Switch Statements
    Parallel Inheritance Hierarchies
    Lazy Class
    Speculative Generality
    Temporary Field
    Message Chains
    Middle Man
    Inappropriate Intimacy
    Alternative Classes with Different Interfaces
    Incomplete Library Class
    Data Class
    Refused Bequest
    

    Code smells are out of scope for this post, I’ll talk about them in another entry, I think they are as important as refactoring. What I really want you to know is that refactorings help to mitigate a code smell, when a code smell is detected, there is at least one refactoring technique to make it disappear.

    Refactorings can be applied in an incremental approach. You start applying one small refactoring in a method, then a medium one in a class or object and after that you can apply a bigger one between two classes or components. This is an approach used in emergent design, the result for this process is to accomplish a more robust design which can suit to a pattern design. I recommend you take a look to Refactoring to Patterns by Josh Kerievsky.

    To conclude, refactoring (the verb) is an activity we as developers must consider to practice and learn more about it. Code smells are inherent attributes when developing software, they always seem to appear, learn to detect them and mitigate them using refactoring techniques require some practices and discipline, but the result is a code that looks cleaner, easy to change, easy to mantain and easy to extend.

    Keep learning, keep applying.

    Continue Reading
  • Larry Morales

    Colombia is getting mature and more competitive in software development

    In 2013, Organization for Economic Co-operation and Development accepted Colombia as a candidate to become an active member of this privileges countries group. If Colombia accomplishes all the requirements for membership requested by OECD, it would be part of “the best practices club”, where countries with better perspective and better performance in social and economic fields help each other to improve their public politics quality, which means more investment, more employment, more capability.

    There had been great progress in some areas along these two years. One of the greatest progress made in Colombia is related with IT investment. With help of ICT ministry and FITI, some programs were developed to help entrepreneurs to create businesses around IT technology, programs to capitalise IT professionals which offer free certifications, free scholarships for MBA and PHB degrees related to IT industry, I+D+I, just to name a few ones.

    An info-graphic emitted by ICT ministry for the last four months of 2014 shows how IT technology has improved its use and capability to coverage almost the whole country. Also, statistics from the same source show how GDP had behave in a decade thanks to IT investment.

    So things look good for Colombia in IT subject, but there’s a long road to follow and good quality software production has been the need nowadays.

    In Colombia software investment had grown up around 43% in the last decade, estimated in USD 6’200’000,000. Today, exists a lot of demands for software production in Colombia, many companies decides to offshore software production because of the lack of companies with good quality software production or because they are too expensive, So they decide to outsource with Indian or Chinese companies with some maturity in software production and maybe because they are cheaper ;).

    On the other side, the great news is that this situation is changing, we as local software producers have a big social responsibility on what we do and we’re doing a lot of improvement in quality of software production.

    Software Engineering Institute a.k.a SEI, emitted four months ago a report that keeps me motivated and dedicated to get better in this business (and also proud to be a colombian :), yeah I love my country). Colombia got the first place as the country with more software development companies which applies the best practices in software engineering in south america, according to the Capability and Maturity Model Integration a.k.a CMMI. This means Colombia is getting mature about the way it develops software and it’s making it more competitive in the business.

    This is a reality, Colombia is developing good quality software applying the best practices according to SEI. I hope the number of software development companies in Colombia with good quality software production keeps increasing as we follow a continuous improvement process, and maybe someday when Colombia becomes an active member of OECD would be prepared to be the reference in software development.

    Keep Learning, Keep applying…

    Continue Reading
  • Wawandco

    About our FIFA15® Tournament

    Last week we ended our internal, FIFA tournament, we play this tournament every year (2 years now), after all, what are we without the joy?.

    Results this year were very succesful, i’m glad to say that we had 8 teams and all the matches were played, which means FIFA on PS4 is gaining traction inside the team, ( huh… ).

    More than that, its very good to see how our culture starts to grow with events that may seem to be outside of the range of the technology-mind guys, but, want it or not, are creating a very nice space where we can joke, laugh and build connections outside profesional scope, which after all, are the ones that will persist.

    Congratulations to the 2015 FIFA 2015 Podium Bryan, Cris, and Mario.

    Continue Reading
  • Antonio Pagano

    EnvironmentAware and Deploying a grails app to Pivotal web services

    This week we were looking for a service to deploy a small grails application and find Pivotal Web Services, a platform that is built on top of cloud foundry, that caused our attention.

    Please don’t expect to have a long tutorial here, we will focus on a single error that may stop some of us from deploying grails to CF, you will find we are doing a lot of assumptions on things you should have installed on your machine, cf, grails and a lot more.

    In order to test a simple mysql grails application we did create a new grails project using grails 2.4.0, something like:

    grails create-app OurApp

    After that we followed Pivotal’s instruction on configuring grails, we did add:

     repositories {
        grailsHome()
        mavenCentral()
        grailsCentral()
        mavenRepo "http://repo.spring.io/milestone"
     }
     dependencies {
        compile "org.springframework.cloud:cloudfoundry-connector:0.9.5"
        compile "org.springframework.cloud:spring-service-connector:0.9.5"
     }
    

    Into our Buildconfig.groovy, and do the database bit as pivotal’s link mentioned. which caused us an error when trying to run the app, something like:

     java.lang.NoClassDefFoundError: org/springframework/context/EnvironmentAware
    

    [ 10 Ruby guys running now :D ]

    After about an hour researching we find that we needed to add:

     compile "org.springframework:spring-context:$springVersion"
    

    Under dependencies inside our lovely Buildconfig.groovy.

    At the end we did run

     grails prod war && cf push ourApp -p target/ourApp.war
    

    And wait for the deployment.

    Happy Deployment!

    Continue Reading
  • Antonio Pagano

    Experts are made not born

    As technology geeks we feel that we are born with unique super powers that allow us to achieve complex tasks such as writing code and building complex applications - wow!

    On the other hand we know (through experience) that to become an expert at something takes time, so why do we still think that we’re born to inherit that unique DNA that makes us experts in all things software development related?

    As a startup we are used to switching hats between tasks expeditiously, we’re not a company and culture where you do that one thing you are destined to do, at Wawandco our people grow organically and are capable of diving into most of the work we’re involved in for our clients, in a sense - we’re multi-functional individuals.

    In this fast hat-switching world where us startups live, due to the size of our teams, we’ve created a line of differentiation between admin tasks and software development tasks and people who fulfill these tasks, something interesting happened this week.

    This week we’ve been at full-capacity on our dev team (yes, I was busy developing too ), and some of our old gold clients needed us to do some html/css tasks, I’m talking about that kind of clients you cannot say NO because its not just another client but a partner (thats common here). We talked with our client and explained the situation, we told them that there was someone on our team that could do that job; to be candid this person had done some light HTML/CSS training in the past but never production ready work, our client agreed to test her and in just a day they got back to me informing me that they wanted her to work full-time on their project.

    That absolutely caught my attention, and proved to me that we’re going in the right direction - by showing our teams they can continue to learn and more crucially learn about things that are different from their day-to-day tasks; we completed the task that was assigned to us by our gold partner, and we continue fostering great relations with them, other than that I think we’ve learned something that is not to underestimate the people in our teams, after all we’ve done a great job hiring great people who are smart and want to grow.

    Our engineering teams in the company are happy and have learned a lesson about becoming experts - the lesson is that everyone with training and the proper dedication can become expert, is just about striving for perfection every step of the way!

    Continue Reading