Monthly Read Posts in Sep 2018

Programming Language

Strong types for strong interfaces

How to add a type wrapper for built-in types.

Besides using phantom template parameter to avoid alias, one can also use private inheritance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class NamedType
{
public:
explicit NamedType(T const& value) : value_(value) {}
explicit NamedType(T&& value) : value_(std::move(value)) {}
T& get() { return value_; }
T const& get() const {return value_; }
private:
T value_;
};

class Width : NamedType<int> {
public:
using NamedType::NamedType;
using NamedType::get;
};

Passing strong types by reference – First attempt

This post is the sequal of the above post.

The main problem the post tryies to solve is: how to make copy of NamedType values cheap.

However, I am conservative on using reference-wrapper as the solution, because doing this has to expose the lifetime of the wrapped object to public; after all, reference-wrapper is only a point per se.

Read More

给 ydiff 提了个 PR

ydiff 是基于 CLI 的一个 diff tool,支持 side-by-side 模式

前段时间在 surface 上 pip 安装后发现不能使用,直接提示找不到 SIGPIPE;看来这玩意儿压根没考虑过支持 Windows。

Read More

Git Invert-grep Bug

前段时间部门技术老板要求每个组统计每个研发同学在某个版本的千行 bug 率,所以第一步就要能够统计某个人在某个指定分支的提交信息。

这个很容易做,利用

1
git log d313a36e199^..HEAD --shortstat --author="Kingsley Chen"

Read More

Monthly Read Posts in Aug 2018

Programming Languages

CppCon 2015: Fedor Pikus “The Unexceptional Exceptions”

Handling exceptions is just as to handle errors using error code, exception is just a tool.

The key point is to maintain the program state in a well defiend state.

Turn error handling into resource management and use RAII to automate it; use explicit try…catch only when absolute necessary.

Bonus tip: avoid uses of pthread_cancel.


Pros and cons of functional programming

Pure function: avoid shared state; immutable structures; function composition.

Declarative: instead of answers the question ‘how to do’ in imperative style, it answers the question ‘what to do’; imperative relies on instructions while declarative relies more on expressions.

Cons of FP: not suitable for graph algorithms; major shift on mind patterns.


Understanding Shell Script’s idiom: 2>&1

General usage:

1
cat foo.txt > result.log 2>&1
  1. fd for stdout and stderr are 1 and 2 respectively
  2. a > b is a shortcut for a 1> b and 1 here is fd value for stdout
  3. you can use &fd to reference a fd value
  4. therefore, using 2>&1 would redirect stderr to stdout, and 1>&2 would do the opposite.

Read More

开了个新坑 ezio

前段时间分别在 Linux 和 Windows 下实现了一个简单的 TCP 网络框架之后,感觉一些必要的坑都踩的差不多了,可以开始动手写一个真正的跨平台的 TCP 网络框架了。

于是就有了这个新坑:ezio。

ezio 这个名字是某天跑步的时候突然想到的,有几重含义:

  • 致敬刺客信条里的 Ezio Auditore,寓意希望这个库能够和刺客一样,虽轻量,但需要时恰到好处,不含糊
  • 致敬(碰瓷)Boost.ASIO,因为两个名字发音非常接近
  • 第三个是朋友发现的,他把 ezio 念成了 e-z-io(easy io),于是赋予了一个新的含义

ezio 的目标是:提供同时支持 Linux 和 Windows 的轻量型非阻塞且具备扩展性(non-blocking and scalable)的 TCP 网络框架;针对 Linux 做性能侧优化,针对 Windows 做一致性开发体验的优化。

这个目标初看起来有点怪异,但是确实非常实际的做法。

因为 Linux 和 Windows 在网络编程上存在根本的范式差异性,所以为了提供一致的对外接口,必须要有所侧重和牺牲。Linux 目前占据了绝大多数互联网产品服务器系统的份额,而 Windows 则提供了可以说是当前最好的开发环境。

ezio 这个设计目标可以使得绝大多数业务逻辑在 Windows 上开发,然后在 Linux 上进行最后的部署。

Read More

Dealing With Multiple Types As a Category

Macro ENSURE() from KBase can ‘capture’ variables by outputing their content to the internal stringtream, provided the type of captured variables has overloaded operator<<().

Read More

Monthly Read Posts in July 2018

Programming Language

Clearer interfaces with optional

Partial queries with optional

optional<T> 介绍 & 简单使用例子


CppCon 2015: John R. Bandela “Simple, Extensible Pattern Matching in C++14”

一个轻量级的 pattern matching 库简要介绍。


CppCon 2015: Greg Miller “Time Programming Fundamentals”

Google 内部实现的一个处理 date-time 的轮子。

不过讲道理,这块内容还不如关注一下之前 monthly read posts 里出现的这篇 里提到的 date library。

看起来差不多会在 C++ 20 引入


When MSDN says NULL, is it okay to use nullptr?

Short anwser: YES.

Read More