Grep N’ Reassign Technique for Removing elements from Array of objects


Hello folks, missed me? This is a quick post for JavaScript/jQuery rookies and intermediates. Its been a couple of years that I’m mucking around with the JavaScript library called jQuery. Moving my focus suddenly from desktop applications on C++ or even C# made me confused on what this client side scripting language has become; a magic wand or just a party stunt thingy. Because as a C++ developer, there are lot of things that you may feel a euphoria upon seeing it working like a charm with JS while the same were nightmares in those well mannered programming languages.

I just want to share yet another party stunt (or a spell) which I found when I was in a serious thought of how to remove an object from an array by matching some attributes with out using any loops. I cannot give a guarantee on its performance benefits, but take a look at it. I want to call it Grep N’ Re-assign (I didn’t invent it, so giving name is a bit melodramatic I know).

If I have an array items and I want to remove a group of objects, say the ones with quantity field is ’1′. So I would suggest the following way.

items = $.grep( items, function( e ) { return e.quantity != 1 });

That’s it! Instead of searching for the element by matching it manually using loops, we just filtered it using the grep function in jQuery library and re-assigned it to the same variable.

Tagged , , , ,

Unit testing a C++/CLI Wrapper


Normally the software projects I worked in never consisted of a proper unit testing phase. What we did all these years was not UT but IT ( integration testing ). Some people still oppose when I say you can’t unit test a code without completely isolating the method under test. This is probably due to their lack of knowledge or something related to the rocket science that I still don’t know. In either case, I love to direct them to this one book called “The art of Unit Testing by Roy Oshervoe”. In one project, I got an opportunity to study about unit testing and submit a report on whether it is feasible under the current workflow.The knowledge gained from that particular assignment, I consider precious.

What I’m going to talk about is not about unit testing in general. I suggest you to take a look at that book I mentioned to get a good picture of current unit testing methods etc; Ignore if you already know. During my assignment, while trying out different techniques like dependency injection in my project scenario, I come across a difficult situation. I was testing a C++/CLI wrapper for a module in C++.  Its basically an adapter that marshals the native C++ types in to corresponding C# types and vice versa. Wrappers normally have little or less logic. But, there are some ‘if’s and ‘while’s involved even though that counts as the conversion logic only. To ensure nearly a complete code coverage, I need to test those minute details too. Dependency to the module that is being wrapped is obvious. I need to get rid of it in order to test it.

A typical C++/CLI Wrapper

A normal dependency breaking scenario consists of two classes that are coupled together that we use inversion of control(IoC) to loosen the coupling. IoC is a process by which the assembler binds the object coupling at run-time. Normally we achieve this through Dependency Injection, where the interface or base class of the dependent class is injected via the constructor or setter functions. [Contd. See next page]

Tagged , , , , , ,
Follow

Get every new post delivered to your Inbox.

Join 215 other followers

%d bloggers like this: