Thursday, May 15, 2014

Why Enums? Example in TypeScript

it's been a while since my last post, this it not because I don't have anything to write about! I have just been busy. since my last post I have created a new site called TypeScript.today this is a weekly compiled neews of whats going on in the TypeScript world, so if you have anything to share, please give me a note.


In this post I going through what an Enum is designed to solve, and then how to writing a simple Enum. I also going to show the difference between constant and computed member types.

Why Enums Sometimes as programmers we want to express the idea that a variable will be used for a specific purpose and should only be able to have a small number of values--for instance, a variable that stores the current direction of the wind might only need to store values corresponding to north, south, east, and west. One solution to this problem might be to use a number and some define'd values:

 var NORTH_WIND: number = 0;  
 var SOUTH_WIND: number = 1;  
 var EAST_WIND: number = 2;  
 var WEST_WIND: number = 3;  
 var NO_WIND: number = 4;  
 var wind_direction: number = NO_WIND;  


The problem with this approach is that it doesn't really prevent someone from assigning a nonsensical value to wind_direction; for instance, I could set wind_direction to -1 without any complaints from my compiler.
 
var NORTH_WIND: number = 0;
var SOUTH_WIND: number = 1;
var EAST_WIND: number = 2;
var WEST_WIND: number = 3;
var NO_WIND: number = 4;

var wind_direction: number = -1;


And if I looked at the type of wind_direction, i would see that it's just an integer, and this can be hard to see that something is wrong.

So the idea behind enumerated types is to create new data types that can take on only a restricted range of values. These values are all expressed as constants or computed types

Constant members declared with an integer value is assigned that value. A constant member declared without an integer value is assigned the value of the preceding constant member plus one, or the value zero if it is the first member in the enum body. The values of constant members are computed at compile-time and may be substituted for references to the members in the generated JavaScript code.
So here is an example of a enum with constant member types.
 
enum Windirections{
North = 0,
South = 1,
West = 2,
East = 3,
none = 4
}


Enums in TypeScript also support making computed members Expressions, specified for computed members must produce values of type Any, the Number primitive type, or an enum type. The values of computed members are not known at compile-time and no substitution is performed for references to computed members. heres is an example of a computed member called SoutEast where I assign it the values of South and East.

 
enum Windirections{
North = 0,
South = 1,
West = 2,
East = 3,
none = 4,
SoutEast = South + East
}
Conclusion We’ve seen how enums in the TypeScript language can be used to allow to constrain the values a variable takes on. I shown how to make constant and computed enum members. Enums can make your program more readable by eliminating magic numbers.

Thursday, March 6, 2014

AX 2012 X++ compilation from a .NET developers perspective

The last month or two I have been “forced” to look at X++ and AX2012, and getting started writing X++ is easy, if you have a background as a .NET developer.

When you start writing and compiling code in AX, you quickly get frustrated as a .NET developer. Why does it not work, - my “compilation” did not throw any errors! It works when I run the job manually, but automatically I does not!

So I realized that there was some fundamental AX2012 that I did not understand as a .NET developer, and much of the frustration was due not understand how the AX compiler works. For many (if not most!) developers this might not be a problem at all, or at least to at some point. In my case it was from the beginning.

I will try to explain how I understand as a .NET developer how the X++ compiler and runtime execution works.

At a high level, the process of compiling and X++ is illustrated in this diagram.


When you are compiling in AX, you don’t compile directly into .NET CIL, it only compiles to p-code. CIL is generated by actively choosing to generate it - either incrementally or full (full generation takes maybe 10-15 min). The Compilation results in P-code are stored in the model store (SQL database). 

Depending on the entry points your code will either be executed either directly in p-code or in CIL. From P-Code you can also easily switch to CIL execution – which is what the application does in most of the more complex proceses. However much code is still executed I P-Code.

Compilation is a three-phased process requiring all code fragments to be received from database (model store). “the three-phased process”, can be read in greater detail in this post: http://bit.ly/1e0WM3I

Phase1: Limited compile only for metadata "class declarations and method signatures"
Phase2: Full compile for metadata and code bodies.
Phase3 to PhaseN: Recompile the error elements, until success or error stabilization.

I have tried for fun to create a detailed diagram, but sometime it’s just easier to tell by code, rather than a diagram :-) decide yourself. First you see the example

First I show a diagram and next a good C# pseudo code example.


C# pseudo code example, borrowed from the post bit.ly/1e0WM3I

// Phase1: Limited compile only for metadata
// (of class declarations and method signatures).
foreach(Element in AOT)
{
       Element.CompileHeaders();
}
// Phase2: Full compile for metadata and code bodies.
foreach(Element in AOT)
{
       compileSuceeded = Element.Compile();
       if (compileSuceeded == false)
       {
              ListOfBadElements.Add(Element);
       }
}
// Phase3 to PhaseN: Recompile the errored elements
// until success or error stabilization.
if (ListOfBadElements.Count > 0)
{
       while(true)
       {
              foreach(Element in ListOfBadElements)
              {
                     compileSuceeded = Element.Compile();
                     if (compileSuceeded == false)
                     {
                           NewListOfBadElements.Add(Element)
                     }
              }
              
              if (ListOfBadElements.Count == NewListOfBadElements.Count)
              {
                     break; // The list of elements with errors has been stabilized.
              }
              ListOfBadElements = NewListOfBadElements.GetCopy();
              NewListOfBadElements.Reset();
       }
}

So what is the different between p-code and CIL? In short: p-code is lazy regarding type-checking and CIL is strongly typed, the same way as when your C# code has been compiled to CIL.

P-code
p-code is the byte code, which been compiled from X++ by the AX compiler.
First when I started writing this post I was comparing p-code as JavaScript? Because of the less restrictive type checking at runtime than compiled CIL has, but this is not true! After some good discussions with Achim (Dynamics AX Technical Architect) and Stig (Consultant), I concluded that we are not playing around coding in byte code (P-Code) but you do in JavaScript. P-Code runs in interpreted mode on either the client or the server.

CIL
Batch jobs and the Business Connector always run in CIL code, and CIL can run on both the server, or the client. CIL runs on the CLR infrastructure, which is much faster than the interpreted p-code.

Be aware when using interpreted P-code
The last thing I want to mention in this post is to be aware when using interpreted P-code. It has less restrictive type-checking at runtime than compiled with CIL, and this can cause your problems. I have probably found an AX bug where a method requiring a sub-type argument but actually getting a super-type instead. This can work fine in p-code, if the code is not calling something that is sub-type specific, but would give you and error in CIL because it’s strictly typed. And this will raise an “unable to cast the super-type to a sub-type”.






Sunday, January 19, 2014

Preconf at the Warm Crocodile Developer Conference

The Warm Crocodile Developer Conference (WCDC) is normally 2 days, but this year Microsoft held a preconf the day before, so 3 days of geeking, speaking and drinking beers at nørrebro brewery, pretty nice!

Before I go into more detail of the 3 days I will give a special thanks to Thomas Jespersen from spiir to crash at his hotel room after a lot of beers sponsored by my awesome consultant Stig from copenhagensoftware.

There were 3 sessions at the preconf

Visual Studio 2013 by Luke Hoban, Microsoft
He came allround the Visual Studio, he talked about the new era of Visual Studio has changed from being single developer focus to be more team based, by adding more client + developer services running in azure.
Then he demoed a lot of "new" stuff in VS 2013.
like:
  • Created simple web project and showed the new way of choosing project type
  • Showed async on the mvc controller
  • Browser link and web essentials how use the browser as our designer.
  • And deployed the website to azure and attach the debugger

F# Applications: From domain model to user interface by Tomas Petricek, PhD student, University of Cambridge
Tomas talked a lot of F#, and I was very curious about this because I never written a single line of F# code before.
He started by being really fundamental by showing a few sites (fsharp.org) to look at when you start out, he also mention that there is a local F# UserGroup starting up in copenhagen. Last he show how to make a Simple application checkout counter. I was pretty amazed by how few lines of code you needed to write in his demo.

ASP.NET MVC & WebAPI alongside AngularJS by Scott Allen, CTO at Medisolv
In the last session Scott showed us how to make a Single Page Application with WebAPI, Angular JS and the Entity Framework 6, and using the new standard MVC 5 template.

After the sessions there were free beers at nørrebro brewery. Henrik my boss, Stig from Copenhagen Software and I started enjoyed a few beers while talking about what presentations.


Stig to the left, Henrik to the right

After the free beer's ended, Henrik went home. Stig and I enjoyed the rest of the evening with a few other guy's with some more beer's, thanks for a great day everybody!




Sunday, January 12, 2014

What does being an MVP means to me.

My MVP lead asked, if I would answer a few questions about being an MVP, and thought of sharing the answers on my blog.

So here it is.

Tell us a little about yourself.

My name is Ronnie, I am a senior .Net developer/Technical Lead and been working at Widex for almost 3 years now. I have played around with .NET since the betas; think it was called “asp.net webmatrix” 2000-2001. My passion for Microsoft technologies slowly grown since then.

What inspired you to be active in the community?

My “Microsoft” community activity started for about 8 years ago, thanks to the umbraco community. What inspired me was the feeling of being part of a family, and there always was a few people willing to help with your problems. The last few year’s motivation has been from TypeScript, Azure and ALM.
Being and MVP inspires me to be even more active spreading my passion of Microsoft Technologies.
It makes me proud to be a MVP, and acknowledge my work in different communities, it's an award I never thought was possible to achieve.

Brag! Tell us about something great you have been working on lately (either community-related or as a technical expert).

Since TypeScript was announced, I have had a lot of focus on that. Start writing a book, and in september I gave a talk “TypeScript kata: the TDD style” presented with Visual studio to Linux/php/opensource beer drinking geek user group (called brewww). We had some heavy discussions - they were not Microsoft fans! Nevertheless, in the end they actually began to see the potential of visual studio, and that it´s free J they will probably have an IDE bitch fight event later this year, I hope they will invite me, so I can convince them all to use visual studio!
The next cool thing that I want to do is making a visual studio extension like ncrunch, just for typescript.

What is, in your opinion, the greatest advantage of being a Microsoft MVP?

To know what the future of Microsoft brings to the table by being involved in the Microsoft product development process by attending the MVP PGI events and the MVP summit. Another great advantage is being part of a new community, and getting new friends that share some of your own passions.      

What would you recommend to people who aspire to be an MVP? 

To be an MVP the most important is to have passion for Microsoft technologies, and use a lot of time in the community by, answer questions, writing books, blogging, teaching, give talks at conferences/user groups or making some new cool products that make Microsoft technologies even better. In my opinion, a good blend of it all, but in the name of your passion to Microsoft Technologies.