Why using immutable restriction in programming is good

2016-09-18 00:00:00 +0000

I have developed software libraries for some years now and part of that have been learning to designing API’s for idiots. What I mean with that is; People with gladly use a software library without reading the full documentation for it, and then when do something which the API was never intend for. They complain that it is not acting as it is supposed to. One of the things I have experienced are people whom either expect and object to change or not change if it is parsed to a function or procedure. Therefore if either happens, they think the library is broken and needs to be fixed. This often happens when a user have not read the documentation for a function they are using. I found that a way to actually avoid this is by applying a restriction on the input given to a function. This is the restriction of saying this will not change or this is constant. Effectively making a object immutable, for a given function or and entire application.

So what does this mean? Well before we get started let me explain the concept of constant. I will base this in C++ but it can be applied in C#, Java and such, but it might need the application of other keywords. For instance in C# it is sealed and readonly. Now back to C++, in C++ we have two usages of constant I will explain but both using the keyword ‘const’. One is the application of making a variable immutable by applying the const keyword when we declare the variable.

const int x = 10;

Let us break down, we have a integer x, x is 10 and it is constant 10. What this means is that x will ALWAYS be 10 and it cannot be changed. Therefore if yourself or another developer uses x, you can always be certain that it is 10. This is very neat as there will be no confusion, when testing and function and its results. It also ensures that there are no discussion when you have to determine the value of variable.

The second usage of ´const´ is function based immutability. Just to emphasis I will show a function signature;

float avg(const int[] data, const int size);

What we have is the function signature for method calculating the average of integer values in an array. As you can see, I have applied the ‘const’ keyword to both data and size. This is to tell the user of the function that in the function ´avg´ we will never change either data nor size. Emphasising to the user that if he/she uses this function, the content of data and size will not change and are in good hands.

There is a third option of using const in C++, but I have never actively used it myself, but it is used to explicitly tell that a function cannot change member variables of a class and are only applicable when doing object oriented programming

To Emacs or to Atom

2016-08-26 00:00:00 +0000

So I will start at university again next week and by such my summer is over and so it is time for me to decide between Emacs and Atom.

First I would like to remind you why I decided to try out other editors. I started trying others editors due to RSI problems and I suspect one of the reasons for my RSI issues was Emacs and it strange key bindings. This let to that I wanted to try out other editors.

I started out with trying out both Spacemacs and Atom. Where I found Spacemacs impossible to setup at least for me and therefore I ended to only focused on Atom. Which I have already written a blog post about this topic, so see that please.

So what is the result and why is the result as is? Well I have actually decided on something I usually do not do. I have decided to continue to use both Atom and Emacs.

I normally never use two tools for the same thing, but I simply do not think I have spend enough time with Atom yet to dismiss, but I still prefer Emacs (surprise). But do really like Atom, and would like to get to know it even more. Because I think with a couple of months additional usage of Atom, it might actually become my preferred editor (if I can get the damn British dictionary to work).

That is actually basically that. The only difference I will be using Atom only at work and for my study development work. While I will continue using Emacs for writing my documents and books.

-Lars Nielsen

My take on Template Programming in C++

2016-08-05 00:00:00 +0000

So lately I have been doing a lot of C++ programming with heavy usage of template programming, which is a type of meta/generic programming. Which have inspired this blog post. Now I am not an expert at all, that would take years and this is not a guide to template programming. This is my take on the advantages and disadvantages.

So let us get started, what does template programming enable you to do? Well it enables you to make your code dynamic, in the sense that types are first determined at compile time, and not while you are coding. This means that we can create methods which can take different kind of types, depending on how it is invoked.

template<typename Type>
class vector
{
public:
    vector(uint32_t size);
    vector();

    void push_back(Type item);
    Type pop();
private:
    uint32_t m_size;
};

Let us continue based on the code example above, just so we have some common ground. We have a template of the following structure template<typename Type>, this is a type template which allow a type to be “dynamic” until compile time, where the type will be set in stone, and then type checking is performed. This type for of template allows us to create a vector like auto x = vector<int>(); or auto y = vector<double>();.

We are also able to create templates of variadic length, and based on a pre set type. But as this is not a guide, we will not dwell on this. But if you are into C++ programming take a look at here: cppreference:Template . Bonus info cppreference.com is an awesome reference database for C++ and I use it often.

So what is the advantages and disadvantages with using templates? Well like all generic programming, it often lessens the amount of code you actually have to write. For example can these methods using polymorphism

int add(int x, int y);
double add(double x, double y);
float add(float x, float y);
int add(int x, float y);

Be replace with a single method utilizing templates:

template<typename TypeOne, typename TypeTwo>
TypeOne add(TypeOne x, TypeTwo y)

But is this a good idea, to replace a set of type specific functions with a single template function? In my opinion both yes and no. No because if you are a newcomer to software development, you need to understand different concepts such as polymorphism and type compatibility, writing functions with specified types seems to be a good way to learn both in my opinion. Therefore I suggest newcomers to actually write out the code, you can always learn tricks for writing less code. But it is also a good idea, for the experienced developer it can be quite the advantage to write lesser code and by such become a more efficient and accomplished programmer.

However in addition to lessen the amount of code, it can also make it hard to get and overview of a codebase. If you as a developer is not familiar with template, heck not only templates, generic programming in general it can be really hard to get an overview over codebase using generic programming. Some would argue that if code is hard to write, it should be hard to read. I would argue that if code is hard to read, you as a developer wrote it wrong.

Another disadvantages with template programming is the header only approach. The functions I have written above, cannot be defined in a .cpp file, why I am not completely certain about, but it is annoying. But why it is annoying, well it is because it increase compile time. The smallest change in the code of a header file can/will result in a recompilation of an entire project. Which can take a lot of time if you are working on a very large project.

So is template programming a go or a no-go? Well after working with it, everyday for a week I can say that I love it, and would not be without it. I would like to recommend template programming for non-novice C++ developers.

-Lars Nielsen

Status about blog, book, projects, and life

2016-08-05 00:00:00 +0000

So if you follow me on twitter, you will know that I do check my email and that I have promised a status update. So let us get started with that.

First of I apologize for not posting more, but I have been very buzzy. It is not an excuse for not posting but rather a reason for why I hopefully will be posting more soon.

Status on my book

I have decided to change my approach to how I write a bit. Instead of forcing myself to write, I will be writing when I have inspiration, which seems to give a much better result. Also I will be writing more detailed character sheets, as this sometimes have put a stop to my writing, as I was not completely certain how a given character would react, and especially one particular character have given me troubles. But I am still working on the book, and so far I am happy about writing.

Status on projects

GOOD NEWS IrisPause is currently being tested by a couple of developers I know whom also have problems with RSI injuries and they seem to like it.

logJS has been site lined at the moment, not because I do not want to make it better, but because I am currently working on a benchmarking tool for C++. Which requires a lot of my attention at the moment. But Lars that project is not on your github page profile. Yes it is, it is just private until I have worked a lot more on the code and how to make proper profiles.

Status on blog

It is my goal to make a post at least once every second week from now on.

But that is not why this point is on the agenda, the reason is that I am currently working on a new theme. Again really? Yes, this is because I do not like the currently design, I think it is strange and it reminds a lot about another webpage I designed lately, and I would like to get away from this. The design should be more sleek and simple and less confusing elements.

Personal life

I have completed my first university degree, and now holds a Bachelor in Software Engineering, so that is good and I will start on my masters the second of September.

Hope to see you here soon.

-Lars Nielsen

Atom and Spacemacs the final chapter

2016-07-27 00:00:00 +0000

At the beginning of this I said I would use Atom and Spacemacs over the summer. For me the summer usually my summer is two months and by such I would have had plenty of time to really play around with the two editors, and realizing the different pros and conns of the two editors. However after a month one contestant is out, and that constant is Spacemacs.

So why is Spacemacs out? To be honest I have given up on making it work as it is supposed to. I have seen countless videos on youtube, read blog posts and reddit post about how to setup Spacemacs on Machintosh and OS X. However what ever I have tried, from a one of the above, have not worked. I do not know if I am useless or if my Spacemacs installation is broken, but I have given up on Spacemacs.

Now it is not the last time I will be looking at Spacemacs, I will continue to try and figure out what is actually wrong with my installation of Spacemacs and fixing it, for I do like what it brings to the table.

But does this mean that I will go back to Emacs? No it does not, it means I will switch completely to Atom for the rest of the summer at least. So far I must say I am very satisfied with Atom and it has done a lot for me so far in the terms of RSI. Though this maybe in combination with my usage of the RSI application Workrave.

I hope you will continue to follow my adventures with Atom and if it helps with my RSI

_-Lars Nielsen _

subscribe via RSS