| 

.NET C# Java Javascript Exception

4

Improving code

TL;DR;

Things I learned:

  • long one-liners are hard to read and understand
  • split up your code into small, easy to understand functions
  • less “plumping” (read infrastructure code) is the better
  • get indentation right
  • “Make it correct, make it clear, make it concise, make it fast. In that order.” Wes Dyer

Why should I bother?

Readable code is:

  • easier to debug
  • fast to fix
  • easier to maintain

The problem

Recently I wanted to implement an algorithm for a project we are doing. The goal was to create a so-called “Balanced Latin Square”, we used it to prevent ordering effects in user studies. You can find a little bit of background here and a nice description of the algorithm here.

It’s fairly simple, although it is not obvious how it works, just by looking at the code. The function takes an integer as an argument and returns a Balanced Latin Square. For example, a “4” would return this matrix of numbers:

1 2 4 3 
2 3 1 4 
3 4 2 1 
4 1 3 2 

And there is a little twist if your number is odd, then you need to reverse every row and append them to your result.

After I created the my implementation, I had an idea on how to simplify it. At least I thought its simpler ;)

First attempt - Loops

Based on the description and a Python version of that algorithm, I created a classical (read “imperative”) implementation.

So this is the C# Code:

public List<List<String>> BalancedLatinSquares(int n)
{
 var result = new List<List<String>>() { };
 for (int i = 0; i < n; i++)
 {
 var row = new List<String>();
 for (int j = 0; j < n; j++)
 {
 var cell = ((j % 2 == 1 ? j / 2 + 1 : n - j / 2) + i) % n;
 cell++; // start counting from 1
 row.Add(cell.ToString());
 }
 result.Add(row);
 }
 if (n % 2 == 1)
 {
 var reversedResult = result.Select(x => x.AsQueryable().Reverse().ToList()).ToList(); 
 result.AddRange(reversedResult);
 }
 return result;
}

I also wrote some simple unit tests to ensure this works. But in the end, I really didn’t like this code. It contains two nested loops and a lot of plumbing code. There are four lines alone just to create the result object (list) and to add the values to it. Recently I looked into functional programming and since C# also has some functional inspired features, I tried to improve this code with some functional goodness :)

Second attempt - Lambda Expressions

public List<List<String>> BalancedLatinSquares(int n)
{
 var result = Enumerable.Range(0, n)
 .Select(i =>
 Enumerable.Range(0, n).Select(j => ((((j % 2 == 1 ? j / 2 + 1 : n - j / 2) + i) % n)+1).ToString()).ToList()
 )
 .ToList(); 
 
 if (n % 2 == 1)
 {
 var reversedResult = result.Select(x => x.AsQueryable().Reverse().ToList()).ToList();
 result.AddRange(reversedResult);
 return result;
}

This is the result of my attempt to use some functional features. And hey, it is much shorter, therefore it must be better, right? Well, I posted a screenshot of both versions on Twitter and asked which one the people prefer. As it turned out, a lot of folks actually preferred the loop version. But why? Looking back at my code a saw two problems by looking at this line:

Enumerable.Range(0, n).Select(j => ((((j % 2 == 1 ? j / 2 + 1 : n - j / 2) + i) % n)+1).ToString()).ToList()

  • I squeezed a lot of code in this one liner. This makes it harder to read and therefore harder to understand.
  • Another issue is, that I omitted descriptive variable names since they are not needed anymore. Oh and I removed the only comment I wrote since this comment would not fit in the one line of code :)

So, shorter is not always better.

Third attempt - better Lambda Expressions

The smart folks on Twitter had some great ideas about how to improve my code.

The first step was to get rid of the unholy one-liner. You can - and should - always split up your code into smaller, meaningful code blocks. I pulled out the calculateCell function and out of that I also extracted a isEven function. The nice thing is, that the function names also working as a kind of documentation about whats going on.

By returning IEnumerable instead of lists, I was able to remove some .toList() calls. Also, I was able to shorten the code to create the reversedResult.

Another simple step to improve readability is to get line indentation right. Personally, I don’t care which indentation style people are using, as long as it’s used consistently.

public static IEnumerable<IEnumerable<int>> GenerateBalancedLatinSquares(int n)
{
 bool isEven (int i) => i % 2 == 0; 
 int calculateCell(int j, int i) =>((isEven(j) ? n - j / 2 : j / 2 + 1) + i) % n + 1;
 
 var result = Enumerable
 .Range(0, n)
 .Select(row =>
 Enumerable
 .Range(0, n)
 .Select(col =>calculateCell(col,row))
 ); 
 
 if (isEven(n) != false)
 {
 var reversedResult = result.Select(x => x.Reverse()); 
 result = result.Concat(reversedResult);
 } 
 return result;conditional
}

I think there is room for further improvement. For the calculateCell function I am using this ?: conditional operator, it allows you to write very compact code, on the other hand, it’s also harder to read. If you would replace this with an if statement you would need more lines of code, but also have more space to add comments. Functional languages like Scala, F#, and Haskel providing this neat match expression that could help here.

Extra: How does this algorithm look in other languages:

Python

def balanced_latin_squares(n):
 l = [[((j/2+1 if j%2 else n-j/2) + i) % n + 1 for j in range(n)] for i in range(n)]
 if n % 2: # Repeat reversed for odd n
 l += [seq[::-1] for seq in l]
 return l

I took this sample from Paul Grau.

Haskell

Haskell: pic.twitter.com/P5rFqvgvgA

— λx.x Carsten (@CarstenK_Dev) 26. Juli 2018

Thank you Carsten

.net web csharp asp.net javascript html5 asp.net-mvc jquery css iis
10 Meinungen
0
One of the best things about the Write My Essay code review method is that it gives everyone suggested a place to get more penetration and learn adjacent the way. Whether you’re resembling to share your knowledge or learn more, the code review method can help you and others better recognize the codebase, languages, frameworks, and best methods.
0
Students want to get good marks in their academic career but there are some difficulties they are facing in assignment writing. We create a platform Professional Assignment Services for all those students who are weak in their writing skills. Our customer support is available 24/7 to answer your query.
0
Now and then as educators we have a reasonable thought of what we'd prefer to see accomplished; at others we don't and should not. On account of the previous we may be attempting to an educational nursing assignment writing service program, have a session or exercise plan with clear goals, and have a high level of power over the learning condition.
0
If you wonder how to write a film critique and need some assistance with it, you should read our guideline which will help you. Our writing specialists will provide you with detailed and effective guidelines on how to write a movie critique essay in the best way.
0
یک نکته ای که در انتخاب آپارتمان مبله در شیراز وجود دارد این است که با توجه به ملاک هایی که مدنظرتان است آپارتمان را اجاره کنید. مثلا برای عده ای متراژ خانه مهم است و برای عده ای دیگر موقعیت مکانی آپارتمان که نزدیک به مراکز خرید و جاهای دیدنی شهر باشد. در شیراز مبله انواع آپارتمان های یک خوابه، دو خوابه، سه خوابه و بزرگتر وجود دارد. مسافرین عزیز میتوانند از میان گزینه های فراوان، آپارتمان دلخواه خود را انتخاب کنند.
0
ترجمه تخصصی، ترجمه تخصصی انگلیسی به فارسی، ترجمه تخصصی فارسی به انگلیسی، ترجمه ارزان و دانشجویی فقط و فقط با ترجمه آنلاین
0
Visit our online store.
Don’t miss our special discounts
https://aixonne.com/
0
what a nice coding It is too helpful for me Thanks for sharing.
Research Paper writing
0
Writing a research paper is equivalent to watch the horrifying movie alone in the middle of the night. The internet is the eligible option to find out the relevant content, but definitely, plagiarism is not acceptable. Some students spend hours for reading irrelevant content in the hope of searching the treasure, but totally wastage of time. Few students find relevant material which is needed to complete their research paper from different sources, but unable to combine all the material in a logical way that explains the professor you actually aware what you are talking about. It’s not surprising that due to company credibility, the number of students is come to Masterpaperwriters.com to pay for research paper.

Pay Someone To Write A Research Paper
0
Sometimes, it may be extremely difficult to choose an effective topic for your paper.
There is some history essay examples that you can implement when writing your own essay.
Schreibe einen Kommentar:
Themen:
iis css jquery asp.net-mvc html5 javascript asp.net csharp web .net
Entweder einloggen... ...oder ohne Wartezeit registrieren
Benutzername
Passwort
Passwort wiederholen
E-Mail
TOP TECHNOLOGIES CONSULTING GmbH