Short update on my DSP project – Being REST-like in WebSharper

Hello everyone, I am extremely busy lately with a few other side projects, so I just wanted to make a small update on my DSP2017 project. I managed to find a few hours today to implement the foundations of a REST-like service in WebSharper. Why am I saying REST-like and not RESTful? Well, because my service only reaches level 2 on the Richardson Maturity Model. I will expand on the subject in a future post, I promise!

The implementation is far from being finished but I finally managed to make a few simple cases work. You can check the code on my GitHub repo under PizzaManagerRestApi.fs. Obviously, the service is about managing pizzas (in case you were still wondering: yes, I like pizzas!). The root URL is http://localhost:9000/pizzamanager/ and for now I implemented two actions:

  • Get the full list of pizzas using /pizzamanager/pizzas.
  • Get a pizza by name by using /pizzamanager/pizza/pizza_name.

You can check out the work in progress in the .gif below:

(click on the .gif to enlarge)

Continue reading

Programmers want to have fun, too!

      3 Comments on Programmers want to have fun, too!

Being a programmer can be very stressful at times. Incomplete specifications, hidden bugs that hit on a Friday afternoon, project failures, Youtube being blocked at work… There’s a lot of factors that can negatively impact the psyche of even the strongest among us.

Luckily, there is also a lot of resources out there that help us stay sane on a daily basis. Yes, programmers want to have fun, too! In this post I will share with you what are my regular go-tos and hope that I’ll get a few new suggestions in return. Let’s get to it!

The all-time classic – Dilbert

Dilbert has been a source of fun and inspiration for thousands of programmers over the years.
The series began in April 1989 and the author, Scott Adams, has been publishing new comic strips daily since then! That’s over 10000 of them. I reckon I read a good half of them and still browse the site on a daily basis.

Continue reading

Overview of WebSharper’s templating engine – Part 2, Data holes

This is the second post of our series dedicated to WebSharper’s templating engine. In the first and previous post, we introduced the concept of text holes, a very simple but rather limited way to inject content in your HTML using F#. Today we are going to cover a more advanced mechanism that allows to inject whole chunks of HTML at once: data holes. There are two ways to declare data holes in your HTML template file:

  • using the data-hole attribute to decorate a given HTML element. Your content will be injected underneath the decorated element as a new child.
  • using the data-replace attribute to decorate a given HTML element. In this case, the whole element will be replaced by the injected content.

Let’s go over the data-hole attribute first.

Playing with data-hole

I modified the AdvancedTemplate.html file a bit and added:

  • a new data-hole called ‘somedatahole’
  • a new CSS class ‘.someStyle’ that we will be using in a moment.

As usual, you can also browse the full code on my GitHub repo.

<head>
    <style>
        .someStyle {
            color: darkcyan;
            font-style: oblique
        }
    </style>
</head>
<body>
    <div data-hole="somedatahole"></div>
</body>
</html>

We also need to make a few modifications to our F# code so we can inject new content into the hole. This is done in the Main.fs file. Here are the important bits:

Continue reading

Help yourself and your programmers by writing better specs!

Hi, as promised in my last post where I wrote about the scary world of project failures in IT, I am back today to help you avoid getting yourself in a similar situation. How so? By giving you a few tips on how to write better specs! Being able to capture requirements that are clear for both business and IT is a vital part of any Software project, yet I feel this part is often overlooked. As requirements are meant to convey the whole meaning of a project, this of course causes a lot of issues down the line.

In this post I will be freely using the term specifications, or simply specs, to refer to the document(s) handed over to the programmers so they can start implementing the Software. Now let’s try to answer the following questions:

  • What are the signs that you might have insufficient specs?
  • How should the specs look like?
  • Who should write specs?
  • What should the specs be about?

What are the signs that you might have insufficient specs?

Below is an non-exhaustive list of signs that may show that your project lack consistent and accurate specs:

  • high technical debt. Technical debt can be born from inaccurate specs that lead programmers to implement things they do not fully understand or are not necessary, thus introducing additional complexity.
  • Missing or Incomplete documentation. Good specifications often translate into good documentation as they should reflect precisely what the Software actually does.
  • Failure to meet deadlines. You cannot expect programmers to implement a program in a timely fashion if you are not able to explain in plain English what the said program should do or not do.
  • Angry users and angry programmers. The former complain because the Software does not do what it is supposed to (or does it in a very unintuitive and inefficient way). The latter complain because they were not given a fair chance to do good work.

If you experienced any of those in the past, you might find some valuable information down there.

Continue reading

“Oh no, why is my software broken again?” A scary IT story

Today I am going to leave the technical stuff on the side and take a wider look at the Software industry. By reading the news you might get the impression that everything is nice and well in IT Land. Between success stories of startups worth millions or even billions of dollars and computer guys making a six-figures just by typing on a keyboard, it might seem like Software is the New Eldorado. Coding bootcamps and other programming schools are on the rise, promising that you will become a Web Developer in under 3 months! and live the millionaire life in just double that time.

Well, today I want to focus on the other side of the coin, and talk about a scary, yet very real aspect of our industry: project failures. If you type ‘software project failure rate‘ in your favourite search engine, you’ll find a lot of interesting statistics that seem to repeat over the last 10 to 15 years. Here are some of the crispy bits that I found:

  • only 39% of all projects succeed (delivered on time, on budget, and with required features and functions).
  • One in six IT projects have an average cost overrun of 200%.
  • 75% of IT executives believe their projects are ‘doomed from the start‘.
  • Only 2.5% of companies successfully complete 100% of their projects.
  • The United States economy loses $50-$150 billion per year due to failed IT projects.

That doesn’t look good at all. if the sight of those statistics makes your legs weak and palms sweaty, perfect. That was the desired effect. If not, you might want to read those once again. The message is pretty clear: Software development is hard. Anyone telling you otherwise is lying. It requires a lot to get it right. It takes very little to get it wrong.

Tell us why, Johnny! Why is this so hard to make software?!. Rest assured, for the past decades Mankind has spent a tremendous amount of time, energy, and money trying to answer this question. So what are the usual suspects? First, Let me give you my personal take on the matter (caution, touches of satire ahead).

Continue reading

Overview of WebSharper’s templating engine – Part 1, Text holes

In the previous Hello WebSharper post i intentionally left a few questions unanswered:

  • What is the role of the Main.html file in the project?
  • What solutions does WebSharper offer do interact with HTML content?

We will try to answer those questions in a series of posts dedicated to WebSharper’s capabilities.

General overview of the templating engine

WebSharper comes with a templating engine that helps programmers manipulate their HTML files from F# by using special attributes. Those attributes create holes in the HTML structure that can be filled with data using logic described in F#.

In our Main.fs file, we declared a few modules that we didn’t cover in the previous post. One of them is the Templating module that declares a new record type that represents the structure of a typical page from our website (check out the full code on the GitHub repo):

type Page = {
    Title : string
    Body : list<Element>
}

In our case, a page is basically made of a Title (some text) and a Body (a list of HTML elements such as div, h1, or p). We then define the MainTemplate function that will map our Page records to an existing HTML file:

let MainTemplate =
        Content.Template<Page>("~/Main.html")
            .With("title", fun x -> x.Title)
            .With("body", fun x -> x.Body)

Continue reading

Integrating the Polish surname guessing into our WebSharper website

After spending a few posts on F# itself, It is high time we went back to our old friend WebSharper. I decided to integrate the Polish surname guessing functionality into the website we created in our Hello WebSharper post. This is how it looks like now:

(click on the GIF to enlarge)As you can see on the GIF above, the server can answer in 3 different ways:

  • ‘A stranger has no name?’ if the user didn’t type anything.
  • A prompt to the user to enter his surname too, if he only typed his first name.
  • The origin of the surname as determined by the server, if the user typed both his first name and surname.

You can read more about the Polish surname guessing algorithm here and here. Below is the list of notable changes I made to the website to make it work.

PolishSurnames.fs

I extracted all the functions from AreYouPolish.fsx and created a proper PolishSurnames module (source code here). The most notable refactor was the addition of a proper record type to store the results of the computations:

type SurnameOrigin = DefinitelyPolish | ProbablyPolish | NotPolish

type SurnameStatistics = {
    Surname: string
    Origin: SurnameOrigin
    PolishDensity: float
}

Continue reading

Make your pizzas taste better thanks to the F# Forward Pipe operator!

In today’s post we will take a closer look at the F# forward pipe operator represented by the following |> symbol. We already saw this operator in action in my two previous posts about Polish surnames that you can find here and here. In this session we will go over:

  • The definition and basic usage of the |> operator
  • An advanced example written in both C# and F#, illustrating the expressive power of |>

Back to Basics

If we search for the definition of the operator in the source code, we can find this:

/// Apply a function to a value, the value being on the left, the function on the right
/// arg: The argument.
/// func: The function.
val inline ( |> ) : arg:'T1 -> func:('T1 -> 'U) -> 'U

The function signature can be a bit scary if you’re not familiar with F#, but in reality it is pretty straightforward. Let’s look at an example:

let add a b = a + b
let res1 = add 2 3 // res1 = 5

We just defined a new add function that takes 2 operators a and b and sums them. Then we execute this function once and store the result in res1, 5 in this case. But what if we used the |> operator to achieve the same effect:

let res2 = 3 |> add 2 // res2 = 5

Or, with a bit of extra formatting:

let res3 =
    3
    |> add 2

What happens is that we simply take the value on the left of the operator, and we pass it (or pipe it) as the last parameter of the function on the right of the operator. And that’s all we need to know. Nothing more, nothing less.

Continue reading

Are you Polish? F# will tell us (probably)! Part 2/2

In yesterday’s post we had our first glance at F# and how we would use it to answer the question that is on everyone’s lips: is your surname Polish? In today’s post (the 2nd and last one of this series) we will go over the rest of the script that you can find on this public gist.

We already went over the countCharCI function that returns the number of occurrences of a given character in a word. We can simply call the function like this:

countCharCI 'C' "ccc" // returns

What we’d like to do now is implement the concept of points, and more particularly this:
Each Polish letter will earn the surname 1 point. For instance, If a word contains 3 polish letters, we’d like to assign 3 points to it.

Handling Polish letters

I introduced the new computePointsFor function to help us achieve this:

/// Apply a function to all elements of a list, sum the results and multiply by the number of points
let computePointsFor func elems points word =
    elems
    |> List.map (fun e -> func e word)
    |> List.reduce (+)
    |> (*) points

The function returns an int and takes 4 parameters as input:

  • func is a function that takes two parameters of type ‘a and ‘b and returns an int
  • elems is a list of ‘a
  • points is an int
  • word is of type ‘b

You may wonder what are those ‘a and ‘b about. This is the F# way of representing generic types! In C#, We’d probably have T1 and T2 instead. As explained in the previous post, F# is able to infer all those types from their usage.

The second interesting thing here is, as I mentioned above, func is a function! As F# is a functional-first programming language, functions are treated as first-class citizens and you can pass them around as you normally would for any other parameter. This is of course possible in C# too, but it doesn’t feel as natural and simple as in F#, especially when you throw generic types in the mix.

Continue reading

Are you Polish? F# will tell us (probably)!

In my previous post I created a small Hello, World! web application using WebSharper. As F# is predominant in WebSharper, I figured I’d write a few posts about the language itself. I didn’t want to write yet another F# tutorial focused on theory (there are plenty of those out in the Wild), so I came up with a simple use case I could use as material for those posts. How about we wrote a small script that would tell the probability a given surname is Polish?


This would serve as:

  • a smooth and practical introduction to F# for those who don’t know it yet, with just a pinch of functional programming theory (nothing too complicated, I promise).
  • a short refresher for the others (myself included),
  • a unique opportunity to learn a little more about Polish surnames!

Let’s start then. First, let’s describe the problem a hand and what we want to achieve. Given an arbitrary surname, we want to know if it is either:

  • Definitely Polish
  • Probably Polish
  • Not Polish

The magical algorithm

In order to do so, I came up with a rather naive algorithm that would help us get the desired results. This algorithm is based on 3 rules:

  • How many exclusive Polish letters does the surname contain?
    The Polish language has a set of of 9 letters based on the Latin alphabet, but combined with diacritics. Here is the complete list: ą, ć, ę, ł, ń, ó,’ś, ż, ź

Continue reading