Sedat Kapanoglu's Blog, page 2

April 14, 2019

PowerShell Accidentally Created A Nice Configuration Format

Setting up my Windows PCs from scratch is a pain in the ass. I wanted to automate some of the mundane tasks and it turned out to be a PowerShell script called “Genesis” mainly for my personal use.

I used PowerShell because it’s quite flexible, straightforward and its VS Code support works just great. The problem is that there are only two configuration file formats PowerShell could recognize and they are terrible: JSON and XML.

JSON And XML Are Not Good For Humans

JSON is a terrible choice for storing human-edited configuration. People have already written about it. It doesn’t support comments, you have to constantly juggle the syntactic elements like the comma after the last element in a list, configuration keys always require quotes even when not needed. JSON can be a perfect serialization format between machines, like an API response, but never for manually edited configuration.

XML is better than JSON in some areas: it supports comments and a human can easily add or remove elements without touching any other part of the document. However, XML is extremely bloated! All the repetitive and redundant characters, closing tags. I think it’s friendlier than JSON as long as you are not the person editing it. XML has had problems with security for a long time. It’s far from being a pleasant medium for human-edited configuration files.

Both formats have been used as a configuration format for decades of course. They are built in most of the modern programming frameworks. They were my only choice when developing the tool on PowerShell.

Alternatives Aren’t Any Better

There are some nice, human-oriented alternative configuration formats like YAML and TOML. YAML is complicated and ambiguous. TOML is mostly great but not visually hierarchical (I mean, you can indent it artificially but it feels out of place). I prefer visual hierarchy for my own configurations. Although there are PowerShell modules handling those formats I didn’t want my script externally depend on those.

Enter PSD1

PSD1 is the filename extension for PowerShell module descriptions. PowerShell modules are like NPM libraries that you can install on demand. A module’s PSD1 file contains metadata for that module. It’s basically a PowerShell script that defines a PowerShell object (PSObject), similar to JSON in that sense.

Although PSD1 is specific for module descriptions, PowerShell provides an API to load any kind of data from any PSD1 file using Import-PowerShellDataFile cmdlet.

Here is a sample PSD1:

https://medium.com/media/438bb3085265c2d1504ddff43ef855c9/href

It pretty much looks like a tame JSON, doesn’t it?

What makes PSD1 great? First, it doesn’t have any of the problems JSON and XML have:

It’s visually hierarchical yet it doesn’t require rigid syntactic elements like commas for multi-line items.It allows comments, both inline and block.It supports integer, float, boolean, string, array and map data types. It actually supports more than that, like bytes, characters, thanks to .NET, but those should be sufficient for a portable data format.Quote characters around field names are optional, unlike JSON.It doesn’t need \ characters to be quoted in strings, because quote character in PowerShell is `. That especially makes Windows-style file paths significantly more readable.Data types can be strongly specified, e.g. key = [string]3.It supports multiline strings and here-strings.The parser comes built-in with PowerShell. Conversely, I don’t think any other platform has a parser for these. You could get by using PowerShell parsers in C# but they wouldn’t be performant.

PSD1 is not fully a PowerShell script either. Using Import-PowerShellDataFile cmdlet doesn’t run the code in the script, only gathers the data, so it’s not subject to security vulnerabilities, unlike YAML.

In overall, PSD1 feels mostly like what JSON should have been with some weird quirks from the world of PowerShell, like the @ prefix in front of array and map declarations, the $ prefix in front of boolean values and alternating use of ( and { to denote arrays and maps respectively. Obviously, when writing PowerShell code, you feel at home with a format like this but anywhere else, it feels like meeting a love-child of JavaScript and PHP.

The Takeaway

PSD1 was a welcome surprise to me. I actually enjoyed how easily it could be edited and how readable it is. Although it wasn’t designed for generic configuration use, it feels like a solid alternative to JSON for PowerShell-specific scenarios. I don’t see it growing to be a YAML competitor but it certainly emphasizes what humans need in such a format. Who knows, it might eventually shape the future of the configuration formats.

[image error]
 •  0 comments  •  flag
Share on Twitter
Published on April 14, 2019 19:41

April 6, 2019

The Tablet That Has Never Been: Surface Pro 6

I have been following the Surface lineup and salivating at the design of the products since the launch of the first Surface tablet. It is a beautiful looking piece of hardware and promises a lot. I finally purchased one, the Core i7 model, last week and I returned it today. I wanted to write about my impression and my thoughts about the device, which I hope would help the team developing it at Microsoft and people who are considering buying it.

Pleasant Packaging

Make no mistake, I don’t care much about the packaging, but I must admit that great packaging can also be very assuring for a consumer. The packaging of Surface Pro 6 feels quite Apple-ish, like the useless but elegant vacuumed box design where you have to patiently wait for the box to fall on your lap with the help of gravity. Not for the impatient but I still liked the packaging. You feel like you bought an expensive piece of hardware, with a decent amount of instructions booklets included.

Sexy Hardware Design

Surface Pro 6 handles and looks great. It becomes quite cumbersome with the type cover on but without that, it feels solid, balanced and lightweight. It feels like you can’t damage it easily and it would recover from falls at a short distance without any scratch. I like the bezel, the kickstand and the location of buttons.

That’s the 2019 I’m talking about

The screen is magnificent: vivid colors, great resolution, great aspect ratio. The charger is magsafe. Almost everything’s great about the hardware.

But…

Unfortunately, the great experience starts to fall apart after you turn it on. Here are the troubles:

Setup Experience Lacks Elegance

The OOBE (Out-of-box Experience) flow is unnecessarily long and troublesome. First, the whole screen looks too crowded with too many icons about accessibility:

One of the first setup screens.

First of all, Cortana during the setup doesn’t feel good at all. It’s confusing, demanding, and unusable. It only listens to your voice at certain moments where you have to time your answers carefully. It can get you wrong easily if you’re not a native speaker like me. It repeats instructions on the screen unnecessarily. It basically makes me follow two things at once: the text on the screen and Cortana’s voice. Otherwise, I feel like I could miss something important. So that’s an anxiety generator at the start and just a nuisance later.

What should happen is a question like “Would you like to use Cortana during the setup?” along with other Accessibility options at the beginning and never show them later in the setup flow. You don’t suddenly start needing accessibility options anyway, you either do or don’t.

What if the setup screen looked like this? Much better. Although the caption “Is this right?” isn’t really assuring, it makes Surface look insecure about its assessment. “Choose your region” and “Next” instead of “Yes” would be much better in terms of usability.

Removing icons would increase usability significantly since I have to discover what each icon does and figure out if I need to click on one of those icons before doing anything. Actually, the “Microphone” icon should never be there even with Cortana-based setup; Cortana should always be listening (only during the setup obviously) like the most basic customer support hotlines.

When implementing Cortana into the setup, Microsoft probably bet on the Sci-Fi feeling of conversing with an assistant to setup your device but the friction it causes diminishes that value completely. Voice assistants are not novel anymore anyway, every phone sold comes with them. I’d actually remove Cortana from setup completely.

So the “expensive feeling” actually starts to crumble during the setup phase. You feel like you’re setting up a PC, not a high-end brand device.

You’d think we’d come a long way.Ambient Light Sensor Is Buggy

The Ambient light sensor on Surface Pro 6 actually detects its own screen light as ambient light and enters a feedback loop to a certain point. That makes the brightness always fluctuate in a dark room based on what kind of screen you open on the app. Something you’d never expect from an upscale tablet like this.

Not A Fan of The Fan

The Core i7 version comes with a fan (I didn’t know that) and runs the shit out of it. I never thought I’d dislike a fan on a tablet but apparently, it completely kills the mood of having a technological slab in your hands. It suddenly feels like a laptop without a keyboard. It is relatively quiet but it still feels cheap. And despite all the efforts of the fan, the device can get very hot. I mean, you-cannot-hold-it hot.

A-lizard-continuously-switching-hands hot.Windows Falls Short Of Being A Tablet OS

There are a tremendous amount of issues with Windows on a tablet and I am actually appalled. Because I love Windows 10, I’m part of its history as a former Windows engineer, and I thought it would work great on a tablet. Apparently, that’s not the case.

Most of the problems with the operating system stem from the ambitious goal of being two things at once: a desktop OS and a tablet OS. That makes the overall experience confusing, inconsistent and unpleasant. Make no mistake, I’m not an end-user at all. But, my single expectation from a tablet is a smooth and consistent experience. I shouldn’t think about how to perform a task, yet the remnants of the desktop experience forced me to do so.

When “Automatically hide taskbar” is enabled, it shows up randomly sometimes and it doesn’t go away. Touching the app doesn’t make it go away. That actually obscures the app’s menu items on the bottom of the screen and it’s impossible to fix. I must emphasize that this is after ALL updates are applied and the sixth generation of the product lineup. I should never have any problem like that.I had a similar problem with obscured UI by the window title bars which appear when you touch at the top of the app. That makes it extremely hard to use app’s UI if they are very close at the top. I don’t know if Microsoft guidelines state “stay away from the top of the screen” but most of the apps I used had UX at the top (Skecthable, Adobe Sketchbook, Baconit, etc).There is no simple way to go to start menu if you have “automatically hide taskbar” enabled. You have to either kill the app on tablet mode by swiping carefully from top to bottom or swipe up from the bottom of the screen to expose the taskbar and click on the start icon on the bottom left. It feels like Microsoft expects you never enable this option, but it comes enabled by default.“Back” navigation on tablet mode taskbar behaves like Android and has the same UX problem: It exits the app when the app doesn’t handle the back operation or doesn’t have anything to go back to. That is a serious problem because:Switching between apps is cumbersome. You swipe from the left edge of the screen and a “task view” shows up from the bottom (why not from the left?) and you select the app there. It doesn’t feel snappy enough, but more importantly, it shows “too much”. The “Task View” shows not just your current activities but past ones too, a feature of Cortana, which makes only the top left corner of the screen relevant and the rest, mostly useless if not frustrating.You see the remnants of the PC culture on the UI, like “Realtek Audio” tooltip on the volume control of your Surface device. “What’s a Realtek Audio?” a regular user would ask. It should simply say “Internal Speakers” for a Surface device, at least only when setting the volume. User doesn’t need to be exposed to the relationship between device drivers and OEM manufacturers. They don’t really care.There is no “slider-based” brightness setting on the action center. So to make the screen darker in a dark room you have to first touch “Brighter” there, get blind temporarily and then “Darker”. It’s quite ridiculous.There are branding issues in general. For instance, when I tried to “reset” the Surface to factory defaults it says “Reset this PC”, not “Reset this Surface Pro”. I think Surface brand needs to distance itself from the pool of generic PCs.Resetting to the factory settings takes too long because of the architectural limitations of Windows (the OS/apps and the data are mixed up together). Apple’s iOS handles this scenario smoothly, it wipes out everything in seconds. I started wiping out my Surface when I started writing this article and it finished just now. Obviously this isn’t really important, I mean, Android does that too and it gets away with it. But still, something that fell short of my expectations.The File Save Dialog UI of the apps are the desktop dialog which is very confusing on a tablet. You have all those small, intricate UI elements designed for the desktop.

A counter-example would be the Windows on Xbox One. It’s adapted to the console device perfectly. It’s smooth, seamless and it works great. Somebody needs to do exactly the same for tablets.

Surface App Is Underwhelming

The “Surface” app that comes with the device makes you expect an exploration tour around the features of your Surface device, like how you can actually switch tasks without Googling them, and some diagnostics features perhaps but it’s merely a pen configuration setting and some battery level indicators, nothing else. It’s extra disappointing because it’s displayed very prominently on the start menu too.

Wrong kind of foreshadowing.Surface Pen Runs On Batteries

I thought Surface Pen didn’t require a battery, I don’t know where I got that information but I learned that it actually ran on a single AAAA battery after opening the package. I had dissed Apple Pencil about how it required the user to charge it by inserting it into the lightning port, but I think it is actually worse to have replaceable batteries. Arguably, you can use rechargeable batteries, but that requires some extensive set up solely dedicated to those batteries.

Type Cover

Type cover is simply amazing, there is no question about it. It even provides a touchpad which is a natural instinct to reach for it when using a keyboard. The keyboard doesn’t feel as good as a laptop keyboard but it’s very close. It snaps easily into Surface and detaches easily too. I’m impartial about the fabric texture, had no issues with it but it felt like it might be harder to clean up in the long run.

Security

All data on your device is encrypted by default. I didn’t have a chance to test the face scanner with a photograph so I’m not sure how it fares but it works well.

Final VerdictQuick sketch of myself working on Surface Pro 6 done on Sketchable.

Surface Pro 6 is a great piece of hardware but lacks a cohesive tablet experience unlike iPad, or Android tablets for that matter. That in fact, might appeal to some as it can count as a lightweight laptop, but I believe the tablet form factor is an entirely different beast and Microsoft already provides two different series of laptops in the Surface lineup (Surface Laptop and SurfaceBook). I believe Microsoft needs to focus on the tablet experience on software and create a better, coherent experience for the users. That can easily make it an iPad-killer.

[image error]
 •  0 comments  •  flag
Share on Twitter
Published on April 06, 2019 16:57

June 12, 2018

Why isn’t ‘nil’ treated as ‘false’ in Go?

Another day, another question got downvoted, closed, drawn and quartered by StackOverflow moderators for obscure reasons. So here I am moving my answer here with some more elaboration.

The question was “why did Go creators choose to not treat nil as false?”. I think that it is a great question and can enhance someone’s understanding of certain concepts when explored.

First, we should ask about why references are evaluated to booleans in some languages. I think that comes from the time when C treated almost everything as integers. That meant a boolean type did not exist but there was a shortcut for using an integer variable as an expression to check if its value is zero or not.

int a = 5;
if (a) {
printf("a is non-zero!");
}

That would print “a is non-zero!”. The syntax made the language simpler and lighter. Booleans could be worked around by using integers and macros easily. There was no need to complicate the language. Elegant.

That came with implications, however, because pointers were essentially integers and could be used the same way for “null” checks, like our pseudo-booleans:

char *a = "test";
if (a) {
printf("a is a valid pointer!");
}

This syntax was handy for many scenarios because checking if a pointer was null or not was a quite common task. Developers started using it everywhere. Some programming languages took it to the extreme, like JavaScript which evaluated almost everything to a boolean, regardless how much it made sense.

Like every shortcut, it came with its own set of problems. First, what the expression !a means isn’t clear. Are we checking validity of a pointer, are we checking a value of a boolean or are we just checking if an integer is zero?

The ambiguity is especially is problematic when you are checking the value of a pointer to a boolean. What if you forget dereferencing it? There is nothing there to remind you of the error.

When you evaluate pointers to booleans, you assign the pointer to a boolean as a side effect. So it becomes a problem when assigning from a pointer to a boolean to a boolean too. Let’s assume Go had that:

a bool = true
b *bool = make(bool, false) // if Go supported this
a = b // should have been "a = *b"

The line a = b would not cause any problems with the compiler and a’s value would still be true. This would cause bugs and readability issues.

Similarly, if nil was a boolean value you could compile incorrectly written reference expressions without any errors.

a int = 3
b *bool = make(bool, false) // let's keep imagining
if a == 3 && b {
println("Problem!");
}

The code above would print “problem” incorrectly. The expression should have been a == 3 && *b , another missed bug.

Go people probably thought fewer bugs were better. I think they were right.

Why do people want this?

Go relies on explicit handling code for errors rather than exceptions. In order to check if a function succeeded or not you have to check if the error return value is nil or not and return early accordingly. So you have to write something like:

result, err := someFunc()
if err != nil {
return err
}

The code gets swamped with these checks. They hurt readability and get cumbersome at some point. I understand why people want a shorter syntax to write them.

Obviously, it would be much better if Go didn’t have this “tuple-like” return values for error propagation. The return value layout can change between functions so it’s impossible to determine a common structure for error propagation. Given the design constraints, they seem to have come up with an optimal solution.

In short, there would be no shorter way to write those error handling code without a nil-boolean shortcut and having such a shortcut would create many problems.

If Go had generics and discriminated unions, some elegant error handling mechanisms could have been devised. Without them, we are bound to the current way of doing things, for good reasons. And yes, we have to explicitly write a != nil every time because it makes our code better.

[image error]
 •  0 comments  •  flag
Share on Twitter
Published on June 12, 2018 15:48