May 2007 - Posts

Why C# is Getting Functional? Cos for LINQ, OOP is choked! :)
25 May 07 07:27 AM | norman | 3 comment(s)

Functional Programming stuff are creeping in into C#. Some people like it, some people confused, some people discouraged.

 

Why do we need to add Functional Programming stuff into C#? Cos OOP principles get choked to do one thing C# would like to do, LINQ. Other ways are needed. And it’s Functional Programming way.

 

Implicitly Typed Local Variable

 

In C# 3.0, we have the implicitly typed local variable like this:

 

var i = 5;

 

So, we don’t need to define the type like this:

 

int i = 5;

 

Don’t get it wrong, this line of code will raise an error:

 

var i = 5;

i = “Booyah!”;

 

Why? Unlike the “var” you’ve heard from Dynamically Typed Languages, in C# 3.0, once a variable is typed you can’t change the type. In the example above, i’s type is inferred from the Right Hand Side and became an “int”. Once it is typed as an “int” it stays an “int”. You can’t change the type.

 

This capability (type inferrencing) comes from Functional Programming. Why it is needed in C#? Cos it’s handy to do Higher Order Function (function that takes or output functions as parameter) as we don’t need to define the type, let the expression body (code at Right Hand Side) infer the type.

 

Then, why do we need to do Higher Order Function kind of thing? So that we can pass around code (function) just like we pass around data. In other words, code/function becomes data! Code, just like data is an expression. Hmm, “Everything is an Expression”. Another Functional stuff. How C# does it? Thru “Lambda Expressions”.

 

Before we get into “Lambda Expressions” I’d like you tointroduce Extension Methods first.

 

Extension Methods

 

In C# 3.0, we can add a method to a type dynamically/at runtime. We can add method to a type not at compile time. We can add method to a type even if we don’t have the source code (class) of the type. How? Thru “Extension Methods”.

 

See the code below:

 

public static void MyFunkyMethod(this string s)

{

                                // whatever

}

 

Notice the keyword “this” in the argument part. See the “string” type next to it. Now System.String would have that “MyFunkyMethod” method.

 

All you need to do is import the namespace where the MyFunkyMethod resides, and you can do this:

 

String myString = “Booyah!”;

myString.MyFunkyMethod(); // See, MyFunkyMethod is added to System.String not at compile time of the System.String.

 

We can achieve such thing in OOP way thru Visitor Design Patterns. But it’s more cumbersome off course. Anyway, why do we need to add “Extension Methods” capability to C#? Cos we want to add methods to existing types in C#. Why do we need to do that? Read on!

 

Lambda Expressions

 

Remember my statement above? That C# will use “Lambda Expressions” to be able to pass around code (as expression) just like data? Actually Lambda Expression is evolution of Delegate & Anonymous Type. Here’s one example:

 

s => {return s+1}

 

that can be simplified as

 

s => s+1

 

This expression says, it takes “s” as input and the body part says it returns “s+1”. I hope you can do the delegate or anonymous type of that expression. It’s trivial. My point is, yes, you can do it in delegate or anonymous type way, but “Lambda Expressions” is more neat.

 

LINQ, putting it all together

 

Now, I will put all the pieces together.

 

With “Extension Methods”, I can add a method let say named “Where” to types that are Collection, but not in compile time. With Lambda Expression, I can pass code as argument to that method. And in Lambda Expressions, I don’t need to define the type as it is inferred.

 

So, I might have a code like this:

 

Where(s => s.Length == 5);

 

It takes an expression “s => s.Length == 5” as its argument. With Extension Methods capability, I can add this “Where” method to array, collections, List, etc.

 

I also can do other method named “Select” that is also receive a Lambda Expression as argument and I may have code like this:

 

Select(s => s);

 

Lambda Expression “s => s” is “s => {return s}”, simply returns the argument.

 

Now, see this code:

 

string[] names = {“John Petrucci”, “David”, “John Myung”, “Mike Portnoy”, “Normy”);

IEnumerable<string> expr = names.Where(s => s.Length == 5).Select(s => s);

 

Or you can do it in more aesthetic way:

 

IEnumerable<string> expr = names

.Where(s => s.Length == 5)

.Select(s => s);

 

This expression “names.Where(s => s.Length == 5).Select(s => s);”  is actually a query expression. Can you see it? It querys element of names (string) that has length equals to 5. It’s a query expression, but it’s strongly typed, and Intellisense can be used too. Not like a TSQL string “Select * from names where len(s)=’5’” that is a string. You can’t check the query syntax correctness at compile time. But in expression “names.Where(s => s.Length == 5).Select(s => s);” you can!

 

Then it’s up to us now to do the actual query in the “Where” and “Select” method. We can write code that simply query collections in memory, and also… yes, databases! Just like ORMs! Not just that, XML storage too. Etc.

 

Rely to API is not always a good thing. What if the implementation changed? What if the method name changed? API is more fragile than language keywords. So, Microsoft decided to introduce few new keywords that currently rely to the API I mentioned above.

 

So, the code becomes:

 

IEnumerable<string> expr = from s in names where s.Length == 5 select s;

 

You can also write the code in more aestethic way:

 

                IEnumerable<string> expr = from s in names

where s.Length == 5

select s;

 

The new Keywords are in bold.

 

As any other Best Practices, if the language already have the keyword to “represent” an API, use the language keyword and not the API directly. Cos API changes more often or new/better API might come. Let the compiler choose which API to be used. Newer compiler will always choose newer/better API. So, you rely to language keyword, not API. If new or better API comes, all you need to do is recompile your code with the newer compiler. No need to REWRITE your code.

 

Functional Programming way is used cos OOP just simply can’t do it

 

Here you go! You have LINQ, a very powerful long waited capability in a programming language. You can do queries to “in memory collections”, RDBMS, XML Storage, etc, in one UNIFORM way. The elegance of LINQ can only be achieved via Functional Programming Style. You can’t achieve it by only relying to OOP.

 

Yes, Type Inferencing, Extension Methods and Lambda Expressions are made to support LINQ. But, since you have it in C#, you can use them for other purposes too. You can do some Functional Programming ways in C#.

 

I’m not saying OOP is stupid or something. It just it can’t do some stuff where other approach can do. But then OOP still do wonders in area where it can do wonders. J

 

So, open your mind, folks. Get over your OOP obsession. Learn all programming paradigm!

 

PS: OOT. My favourite Functional Programming Language is still Mathematica from Wolfram Research. But now are looking into F#/ML  too.

Share this post: | | | |
Vista Campus Launch at Binus this Saturday
24 May 07 12:19 AM | norman | 1 comment(s)

I'll deliver a talk again at Binus. This time about Windows Communication Foundation. The event is "Vista Campus Launch".

  • Day/Date: Saturday/May 26, 2007
  • Time: 13:00 - Finish
  • Venue: Ruang H2B Lantai 2, Binus Kampus Syahdan, Kemanggisan. Jl. KH Syahdan.

Here's the complete line up:

  • Windows Communications Foundation
    Norman Sasono - Principal Engineer, Intimedia
  • IT Career & Business Opportunity in Web 2.0 Era
    Agung Riyadi - CEO Bataviasoft
  • Differentiated User Experience with Windows Vista                    
    Zeddy Iskandar - Academic Developer Evangelist, Microsoft Indonesia

See you there!

Share this post: | | | |
Apocalyptica
15 May 07 07:56 AM | norman | with no comments

Another non-technical post I'd love to do. Smile

What can I say, I like them.

Share this post: | | | |
Pics from IASA IT Architect Symposium
14 May 07 12:44 AM | norman | with no comments

Smile

Here are some pics from last IASA IT Architect Symposium in KL, Malaysia.

Share this post: | | | |
Filed under: ,
The case of Domain Specific Language
07 May 07 11:56 PM | norman | with no comments

Steven Kelly from MetaCase gave feedback to my earlier post.

He made few points that are interesting for me:

  • What IS exciting is when you can create your own modeling languages and generators, with good tool support for them.
  • One experienced person works hard to make a tool, so that others can be more productive.

So, rather than just USING a Domain Specific Language/DSL (like LabVIEW or Simulink I used as DSL example in my post, where I was just a user) , we can CREATE our own DSL. It means the game is to create our very own DSL. The goal is to increase productivity. Then, we need tools that enable us to create our own DSL. Microsoft VSTS is such a tool we're familiar with now.

But, if you look at Steven's comment, the punch line is actually in the middle of his comment: "And it's nice to see Microsoft and Eclipse getting interested in this area (although obviously you can't expect too much maturity in their toolset yet)". It means, the concept of creating our own DSL (more than just using DSL) too has been around also for more than a decade now. MetaCase MetaEdit+ has been around since the early 90's!

So, I like to summarize:

  • DSL has been around for more than a decade. It's not a new idea/concept.
  • DSL tool (tool to create our very own DSL) too has been around for more than a decade.
  • It means, Microsoft is NOT the inventor of DSL and Microsoft tools (VSTS) is currently not the most matured DSL tool (tool to create DSL). It also means, to those UML die hard, you can't blame Microsoft for DSL.

The UML vs DSL debate is dumb. Why? Can you convince those scientist and engineers that have been using LabVIEW & Simulink to stop using those DSLs, and start drawing Class Diagrams, Sequence Diagrams, Activity Diagrams, State Diagrams, etc. Smile Such an idiotic idea!

Share this post: | | | |
Visual Programming Language, Domain Specific Language, etc
07 May 07 06:18 AM | norman | 3 comment(s)

Were you amaze the first time you saw the design tool of Windows Workflow Foundation? Seeing that you can define workflows graphically? Where it gave you a more transparent and clearer way to program workflows? You found it a new "paradigm" or "style" in programming? Where you can program "graphically"?

I was not. So do other folks whose background is building physical systems such as Intrumentation and Control Engineering, physical systems simulations, etc.

I used LabVIEW more than ten years ago, and LabVIEW apparently has been around for 20 years now! Here's LabView at Wikipedia for your reference. Another example is Simulink. I used it almost 15 years ago. Here's Simulink at Wikipedia for your reference. As you can see, graphical programming, or in more general, programming graphically is not something new. They have been around for decades.

With LabVIEW and Simulink I program complex physical systems graphically. Simulink even can spit out "C" code if you want the simulink code used as simulations to be used in "real" physical system (production in enterprise language).

Now about Domain Specific Language. The Application Diagram, Logical Data Center Diagram, etc in VSTS and other DSL tools/languages. Some people are now busy debating UML vs DSL, etc. The way I understand it, LabVIEW & Simulink are Domain Specific Languages too. And I am happy with them as they help me. I don't care about UML vs DSL debate that some folks are fighting about.

As LabVIEW & Simulink has been around for decades, so, Domain Specific Language is another concept that I think is NOT new. The concept AND implementation where you can model your program graphically, build it as executable, specific for a domain have been around for decades.

Maybe they sound like "new" concepts for those who write programs only in C, C++, Java, C#, Delphi, Pascal, Fortran, VB, etc (most Enterprise developers). But to programmers who build physical systems, these concepts are as old as they do programming. These programmers have encountered various paradigm in programming (this is topic for another post).

I just like to point out something here, that programming outside the enterprise realm is also exciting and in some cases, they are way more advanced. Tools, languages, etc in enterprise systems merely just adoption (coping up) of those stuff that have been around for more than a decade in physical systems.

Share this post: | | | |
Software Architecture outside the Enterprise world
07 May 07 03:46 AM | norman | with no comments

We've been talking & focused too much about Architecture in Enterprise World. On last Regional IASA IT Architect Symposium in KL, if you haven't "got it", Grady Booch showed one example that can make you realize that Architecture does exist outside the Enterprise realm.

He showed the architecture of SONY ABIO Robot (a dog robot). It's various Architectural view: prototype, deployment view, logical view and objects.

From a user perspective, this is what you see, the Prototype (GUI in your LOBs):

And here's the Deployment View (Logical Data Center in your Enterprise):

Now, we're talking about Layers. I always suggest people to read POSA to really understand what Layering really is, and compare it to other architectural style such as Pipes & Filters or Blackboard. Reading PEAA only would not give you deeper understanding about what Layering is. You should really understand Layering in abstract, as a more generalized concept that can be implemented in various domain. Not just in LOBs or Enterprise Apps. Layering is more than just that boring three layers system: Presentation, Business and Data Access. It's just an instance of layering in implementation. Here's the SONY ABIO Layered Software Architecture:

So, I hope I make my point clear that Architecture does exist outside the Enterprise or LOB world. Then to really understand Software Architecture one must grab the concept in higher level of abstraction, something that is more general. Architecture for Enterprise systems can always be deduced from the more general principles in Software Architecture.

Once you understand this higher view of concept, Software Architecture in Enterprise Systems will look natural and easier.

Share this post: | | | |

This Blog

About Me

Syndication