Friday, May 10, 2013

Brownfield Projects Woes

Maintaining brownfield projects is a myth, especially if it reeks of technical debts. So they tend to be rewritten, and voila! a greenfield project!



5 reasons to use AngularJS in the corporate app world in order to avoid rewriting brownfield projects



And this is the coolness B-) On-the-fly unit test:



“By using Karma Test Runner given to you by the AngularJS generators in Yeoman, you can have your unit tests running all the time in the background, that way if you change anything and hit save, your tests will automatically re-run and you will know if you are breaking something, where you are breaking it, and if the tests are built correctly maybe even why you are breaking it.”

Saturday, May 4, 2013

Earworm, I kept humming the instrumental intro of Eat Bulaga

These past few weeks I keep humming the instrumental intro of Eat Bulaga while I'm coding, which only recently I was able to unlock the reason why. The instrumental intro of Eat Bulaga is eerily similar to Sing a Song.


Here's the line I keep humming on Sing a Song:


"Don't worry if it's not good enough" -- http://www.metrolyrics.com/sing-lyrics-lea-salonga.html


Very similar to Eat Bulaga's instrumental intro.


Subconciously, I'm singing a very developer-centric song. So the next time you hear someone humming Eat Bulaga, that doesn't automatically translate to them being an Eat Bulaga fan(not that I have a problem on people being a partisan of a given primetime show or TV network :)). For all we know, those developers are subconsciously seeking enlightenment on the quality of their work.


Code quality should be tempered with the reality of the deadlines. Code quality is important, but it's not a be-all and end-all of an application development. There's also this thing called Time-to-Market.


"Quality in development is important, but it has to be scaled appropriately" -- http://haacked.com/archive/2010/08/26/not-paid-to-write-code.aspx


Of course, code quality and time-to-market (that's deadline for us who are not on the frontlines) are not mutually exclusive. Both are achievable, when torn between those two, just remember that perfect is the enemy of the good. And there's the Pareto Principle that could guide us on achieving both.
And don't even tell you're a perfectionist. That's a humblebrag masquerading as a virtue. There are more people way more perfectionist than you, and that would reduce you to a mere humblebragger



So there's the feel-good tune of Eat Bulaga, the feel of the music resonates with developers ;-)


Happy Coding! ツ

XY Problem. If someone is looking for a fix on Y, ask them first: "What's the X?"

Nope, XY problem doesn't mean men are troubles lol


There are certain class of developers that are too enamored with their codes, that when they ask for help they will proudly show their codes first and ask help on those codes, rather than stating the problem itself first


So what is XY problem? That's the terminology for the scenario above.


"The X-Y Problem, as it is sometimes called, is a mental block which leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help. It often goes something like this:

* User wants to do X.
* User doesn't know how to do X, but thinks they can fumble their way to a solution if they can just manage to do Y.
* User doesn't know how to do Y either.
* User asks for help with Y.
* Others try to help user with Y, but are confused because Y seems like a strange problem to want to solve.
* After much interaction and wasted time, it finally becomes clear that the user really wants help with X, and that Y wasn't even a suitable solution for X.
" -- http://mywiki.wooledge.org/XyProblem


So the next time around, before you even show how awesome your coding skills are, remember first that other people's time and energy are important, don't let it go wasted. Tell your X first, before you even show your Y.


Friends don't let friends waste time or energy on fixing the Y when a Y could even be discarded and go for a better solution when X is stated first.


The best code is no code at all. Code, especially the longer ones, distracts us from stating the intent/problem of the application.



Some developers just love to provide solution, even if it meant not writing a single line of code. They'll just purchase a Microsoft Word and sell it to you with a nice markup for their troubles heading to a software shop, and sip Pina Coladas on the beach the rest of the day. Ok, a lot of context is missing here, that's why this paragraph looks weird. See the XY problem connection on this paragraph? Y often looks weird if X is not stated. For the X, head to this blog post: http://haacked.com/archive/2010/08/26/not-paid-to-write-code.aspx



Other good sources on XY problem:

http://blog.spitfiremanagement.com/2013/04/10/avoid-the-xy-problem/


http://daniel-lange.com/archives/69-The-XY-problem-in-IT-management.html


"The XY problem is when you need to do X, and you think you can use Y to do X, so you ask about how to do Y, when what you really should do is state what your X problem is. There may be a Z solution that is even better than Y, but nobody can suggest it if X is never mentioned." -- http://meta.stackoverflow.com/questions/66377/what-is-the-xy-problem

Thursday, May 2, 2013

The last vestige of me thinking in imperative manner

SQL is a declarative language, but there are some imperative code lurking in an SQL query. Case in point:

select d.*, p.product_name
from order_detail d
join product p on p.product_id = d.product_id

Did you spot the imperative code there?

I still think of the second table and first table in terms of inner loop being compared to an outer loop, hence p.product_id = d.product_id. I should forget how things work under-the-hood, forget the loops, and be comfortable with a more declarative-looking code: d.product_id = p.product_id

Wednesday, April 24, 2013

JavaScript alphabet song

If JavaScript has an alphabet or elementary song, I think this is the one:

1: function foo(){} foo(); // calls foo 
2: foo = function(){}; foo(); // calls foo 
3: ( function(){} )(); // calls the anonymous function

Objective C's C#'s lambda

So we are missing C#'s Where lambda, which allows us to generalize a filter pattern, instead of creating new function everytime we need a new kind filter. On my last post about C#'s extension method, we have defined a function that filters all the odd elements, and everytime we have a new kind of filter, say get all the even elements, we will need to introduce another method called evenElements, think of all possible variations of filter and duplicated amount of code in those functions.


How about instead of creating a new method whenever a new set of condition would be required, we generalize the method's functionality and allows us to pass any kind of filters.

Luckily for C# transitioners, Objective C has a lambda, which is called blocks.


So with blocks, it allows us to create building blocks for generalized routine, e.g. filter.


So instead of creating another method that filters all the even numbers, we will just create a generalized lambda method that allows us to pass any filter condition. First, we should create the signature of the lambda in the .h file:

-(NSMutableArray*) where: (bool (^)(NSNumber *)) expr;



This is the lambda parameter on NSMutableArray's where Category(Extension Method in C#'s parlance):

(bool (^)(NSNumber *)) expr;


In Objective-C, the method's parameter has the following syntax, open parenthesis, type, close parenthesis, and the name of the parameter. So the name of the parameter is expr. So our type is:

bool (^)(NSNumber *)


The caret(^) denotes that the type is a lambda type, and that lambda signature allows us to receive an NSNumber parameter, and allows us to return a boolean type. So that's just it. Can't take but notice that it's a bit ironic that given the Objective-C's messaging mechanism (where the message's parameter is denoted by a colon), its lambda syntax takes the function signature form (similar to JavaScript lambda or C#'s Func/delegate) instead of its usual messaging mechanism syntax. I think Objective-C lambda code will look very convoluted if its lambda syntax resembles Objective-C messaging rather than the simpler function signature.


Here's the implementation of the lambda signature above, note that we simply invoke the expr by adding an open parenthesis, parameter, and close parenthesis. Which is a homey function syntax, not the messaging style syntax of Objective-C

-(NSMutableArray*) where: (bool (^)(NSNumber *)) expr {
    NSMutableArray *x = [NSMutableArray array];
    
    
    for(NSNumber *number in self) {
        if (expr(number)) { // we invoke the expr lambda
            [x addObject: number];
        }
    }
    
    return x;
}


If you are curious, yes this doesn't work:
if ([expr number]) {
    [x addObject: number];
}


Then here's how we use the lambda implementation:

void demoArray()
{
    @autoreleasepool {
        
        NSMutableArray *items = [NSMutableArray array];
        
        [items addObject: [NSNumber numberWithInteger:42]];

        
        [items addObject: [NSNumber numberWithInteger:11]];
        [items addObject: [NSNumber numberWithInteger:5]];
        [items addObject: [NSNumber numberWithInteger:1976]];
        
        
        for(NSNumber *number in [items oddElements]) {
            printf("\n%d", number.intValue);
        }
        
        
        
        printf("All even numbers:\n");
        for(NSNumber *number in [items where: ^(NSNumber *n) { return (bool)(n.intValue % 2 == 0 ); } ]) {
            printf("\n%d", number.intValue);
        }
    
    }
}


Output:
All even numbers:

42
1976


We want to list all odd numbers less than 100?
void demoArray()
{
    @autoreleasepool {
        
        NSMutableArray *items = [NSMutableArray array];
        
        [items addObject: [NSNumber numberWithInteger:42]];

        
        [items addObject: [NSNumber numberWithInteger:11]];
        [items addObject: [NSNumber numberWithInteger:5]];
        [items addObject: [NSNumber numberWithInteger:1976]];
        
        
        
        for(NSNumber *number in [items where: ^(NSNumber *n) { return (bool)(n.intValue % 2 == 1 && n.intValue <= 100 ); } ]) {
            printf("\n%d", number.intValue);
        }
    
    }
}
Output:
11
5

Saturday, April 20, 2013

Objective-C's C# Extension Method

...is called Categories.


NSMutableArray+Selector.h:
#import <Foundation/Foundation.h>

@interface NSMutableArray (Selector)

-(NSMutableArray*) oddElements;

@end


NSMutableArray+Selector.m
#import "NSMutableArray+Selector.h"

@implementation NSMutableArray (Selector)

-(NSMutableArray *) oddElements {
    
    NSMutableArray *x = [NSMutableArray array];
    
    for(NSNumber *number in self) {
        if (number.intValue % 2 == 1) {
            [x addObject: number];
        }
    }
    
    return x;
    
}

@end


To use the categories:
#include <CoreFoundation/CoreFoundation.h>


#include "ThePerson.h"

#include "NSMutableArray+Selector.h"

void demoArray()
{
    @autoreleasepool {
        
        NSMutableArray *items = [NSMutableArray array];
        
        [items addObject: [NSNumber numberWithInteger:42]];
        
        [items addObject: [NSNumber numberWithInteger:11]];
        [items addObject: [NSNumber numberWithInteger:5]];
        [items addObject: [NSNumber numberWithInteger:1976]];
        
        
        for(NSNumber *number in [items oddElements]) {
            printf("\n%d", number.intValue);
        }
    
    }
}


int main(int argc, const char * argv[])
{
    demoArray();

    return 0;
}