Saturday, September 3, 2016

Custom unstructured data types and types' reuse

In 1972 three well-known computer scientists wrote a book Structured Programming, where they mentioned about custom unstructured types:
All structured data must in the last analysis be built up from unstructured components, belonging to a primitive or unstructured types. Some of these unstructured types (for example, reals and integers) may be taken as given by programming language or the hardware of the computer. Although these primitive types are theoretically adequate for all purposes, here are strong practical reasons for encouraging a programmer to define his own unstructured types, both to clarify his intentions about the potential range of values of a variable, and the interpretation of each such value; and to permit subsequent design of an efficient representation.
(...) Such a type is said to be an enumeration, and we suggest a standard notation for the name of the type and associating a name with each of its alternative values.
type suit = (club, diamond, heart, spade);
type year = 1900 .. 1960; 
type coordinate = 0 .. 1023;
(...) We therefore introduce the convention that a .. b stands for the inline range of values between a and b inclusive. This is known as subrange of the type to which a and b belong, (...)
Ole-Johan Dahl, Edsger W. Dijkstra, C.A.R. Hoare, Structured Programming, A.P.I.C. Studies in Data Processing, No. 8, 1972, p.97

Some of the languages indeed support subrange types, e.g. Ada and Dephi. Nevertheless, modern popular languages like Java and C# do not support unstructured custom subrange types.

In addition to this there is a huge misconception about a type declaration and its actual meaning. For instance, an Array accepts an Integer as index argument with a range [-2^31, 2^31], so we declare support for -1,-2, ... index values. At the same time only non-negative integers are accepted, i.e. [0, 2^31]. For an honest and clear code it should be an Array[NonnegativeInteger].

Moreover, commonly used structured and unstructured types does not limit to numbers. Nowadays an email is always presented as String:
function SendEmail(String email, String emailBody){ ... }

but we actually mean
function SendEmail(EmailAddress email, String emailBody){ ... }

Making an assertion that an instance of the class/structure EmailAddress is always valid we do not need to check the string every time with IsEmail() helper or an attribute of DataAnnotations. It can have a function EmailAddress TryConvert(String email) for convenient conversion from string.

Other common places for apparent improvements:

  • Name checking of a String for a Firstname and a Lastname across whole solution with the same restrictions, probably with no more than 256 special characters and at least 1 character, can be changed to what a programmer really means: class/structure Name.
  • class Description with common rules for contact messages, order's comments, feedback messages, etc.
  • Measures: degrees, coordinates, weight, etc.
  • Date and time Integers up to 2147483648 (2^31) for a year, a month, a day, an hour and a minute look inconvenient for real use and most of the systems after all will throw an exception by taking large numbers for these types.
  • Money There is no a banknote with minus $100 value, but there can be a notion of 100$ debt.

With the help of Design by Contracts most of the checks can be done during compilation time.
For interoperability with other systems custom types should be built up from primitive and simple types, like integers and strings, and be convertible from these types.

Sunday, June 19, 2016

Power of the Market

At first sight this post seems to have nothing in common with IT. On the contrary, IT is a part of the following idea: the less people and enterprises are controlled by a bureau or a government, the more desirable or advantageous position in financial terms they have.

This concept was presented in 1980 by Doc. Milton Friedman in his TV series "Free to choose". He is an economist, winner of the Nobel Prize (so called the Nobel Prize for economic achievements). In his first episode Friedman talks about "The power of the market". Here is the short description of the episode:

Some may be appalled by his ideas, others can be enthusiastic about them. In the early years of the foundation of the USA settlers were free to pursue their own objectives. America was a land of opportunities. People came here from all over the world to settle down in a new place. There were no permits, no tariffs, no red tape to restrict them, and many people thrived on a new market.

To show modern example of a free market Doc. Friedman visits Hong Kong. There are many overcrowded, sticky, hot factories and shops. It might be seen as an appalled place to work, however, for the workers it is the best place they can find at their current position. If they do not agree they can always change their job, nobody forces them to work at these factories. They know that these conditions are not forever, they will find something better after learning new skills. Hong Kong is a special economic zone, there is no duties on imports or exports. The goods are coming to the port from all over the world. Thousands of people try to escape from Communist China, cross the border and come to Hong Kong for the real opportunities, better wages and freedom to speak, freedom to write, freedom to buy what they want. The number of permits to visit Hong Kong is restricted for the Chinese, and consequently many of them risk their lives and die in attempt to get a better life.

Milton states that from the historical point of view "The freer the system has been, the better off the ordinary people have been".

As an example of voluntary collaboration Friedman takes a lead pencil and states that there is not a single person in the world who can make the same pencil. Remarkable? Just watch how he proves this statement in the first episode.

In the second part of the video the leading businessmen (including the Motorola CEO) and the leading politicians (including a Congressman and a Governor of Delaware) discuss Friedman's ideas.

I genuinely recommend to watch this video. Enjoy it!

Saturday, May 28, 2016

Computers in USSR | Kazan

Computer science and development evolved in parallel in England, USA and USSR. There are several cities that took major role in the first Soviet computer era: Kiev and Kharkiv in Ukraine, Minsk in Belarus, Moscow, Penza and Kazan in Russia. Due the World War II all vital factories and universities were moved from the west (Kiev, Minsk, Saint-Petersburg, Moscow) to the east, including Kazan city. Therefore, Kazan became one of the major technical and scientific centre in the Soviet Union, and the city was chosen for building the first serial computers in USSR.

In 1953 the Communist Party approved a request to build a computer factory in Kazan to produce electronic devices for the military agency. The factory was ready for large manufacturing in 1959 and the government asked to build two M-20 computers.  The architect was S.A. Lebedev, who already had experience in designing and building computers in Kiev and Moscow, however, they were mere specimens and the newborn task aimed to build the series of computers.


M-20 is the first serial computer in USSR. It was built from vacuum tubes (valves) and the first two machines occupied the whole level of the factory. The vacuum tubes require colossal energy and emit a lot of heat. You can find the parts of this excellent machine only in the Kazan computer museum, which is based and supported by Margarita Badrutdinova.  She has been working with computers in the factory from its first days.

View of M-20 computer, 1962 

Parts of M-20 computer,
top - keyboard, bottom - 2 bit triggers from vacuum tubes

In the end of 1960 the computers were ready, successfully tested and moved to the customer, which was the military agency. The engineers did not know what kind of calculations and tests they were successfully performed. As history showed the second M-20 machine was used to calculate the path for the space rocket and this same computer was used to launch the first human space journey with Gagarin in the 12th of April, 1961!
M-20 is named after the 20 000 operations per second, during the five years there were built 63 sets of the machine.


The next machine "Setun" was developed in Moscow by Nikolay Brusentsov and build in Kazan in 1961. The machine got its name by the river "Setun" near Moscow State University. It had three-state logic, which was quite novel at that time and still is a unique serial implementation in the world. Instead of two-state logic "true-false" and bits "1 and 0" Setun had operations with trinary states "true-false-maybe" and stored trits "1 0 -1" in the memory. Some of the benefits of three-state logic are: the program size and data is 1.6 times smaller than two-bits representation, there is no need to take special bit for the number sign, additional branch for the if-else switch.
Setun computer

Parts of Setun computer,
at the bottom in the left side is a small core item - ferret-diode cell,
on the right side is a magnet drum for external memory. 

Almost every part of products was built by the manufacture and often required novelty and tackling difficult tasks. Overall there were produced 47 sets of these trinary machines during 1961-1965 in the Kazan computer factory.


Another computer was designed in Yerevan by G. Ovsepyan in 1964. He had a big fight for this machine with the heads of Soviet departments and finally made excellent and robust computer. It was produced in the Kazan factory during 1965-1970.  Nairi was very successful product and very popular in the Soviet Union, the factory engineers made improvements and built overall 500 machines closely working with Ovsepyan's team. Ironically, many Yerevan scientists and the Comunnist party heads stole Ovsepyan's achievements and got awards. He had not been mentioned for decades and some of the cheaters still have political power and are afraid to mention his name.

Nairi computer

Parts of Nairi computer,
in the middle is permanent data storage, 
in the top-right corner is in-line storage


In 1960s IBM made enormous improvement in computer design by spending 5 billion dollars for one project and created IBM 360. The project was twice as expensive as the first nuclear bomb project "Manhattan". The Soviet Union heads wanted to have a copy of this "toy". And, after the contract between USA and USSR, IBM 360 was licensed to the Soviet Union. The government obliged to create very similar machine, so across the country appeared the series of IBM clones ES-1010, ES-1020, ES-1030, ES-10XX computers.
While giving the lecture in museum Badrutdinova remembered Korolev's words, the father of Soviet rocket program, who liked to quote his tutor Bartini: "Can left behind system overtake advanced system? - No, if it tries to catch up moving behind, but what if it takes a short cut?".  The Soviet ministers did not want to see local and new computer versions, they just wanted to have american clones. This was the fatal error in the strategy and the death of true novelty! The engineers struggled and liked to build improved and new competitive versions, but their power was voiceless. The command to produce ES-1030 computers came to the Kazan computer factory. After some years the engineers with V.F. Gusev finally managed to create advanced and more robust version, model ES-1033. The leaders and consumers were impressed and this computer was successfully exported to other countries. In 1970s and 1980s the manufacture produced overall 2600 of ES-1033 machines.
ES-1033 computer

ES-1045.01 computer 
(input model of the improved version of ES-1045)


For the purity I do not cover all computers and electronic devices that were produced by the Kazan computer factory. It is clear that engineers did very good job and made very good and competitive computers, often patching and extending existed models. The factory holds lots of top Soviet awards for its production. It seems unbelievable, but the quality of computers was so good, that some of them still are in use in Russian military agency. The products was used in all Soviet space centres and exported to India, Check Republic, Poland, Bulgaria, Hungary, Mongolia. Thus, even now it is quite possible to meet one of these old workers in real life applications.

Later the factory was merged with the famous UK company ICL. Now it is part of Fujitsu companies.
Modern awards from Russian government.
On the left side the knight is the present from Kaspersky Antivirus for its partnership.

P.S. I am very thankful to Margarita Badrutdinova for the personal lecture and the book about Kazan computer history. You can reach this small but valuable museum at Sibirskiy trakt Street, 34, Kazan city, Russia.

Sources (in Russian and English):

M. Badrutdinova, The Kazan computer factory, 2004, ISBN5-9222-0091-7

Kazan computer museum

M. Badrutdinova and, "Our answer to Steve Jobs and Bill Gates. The development of electronic engineering in USSR"

Setun emulator

Nairi: triumph and drama

Universal systems ES-series

Virtual Russian computer museum

Virtual Ukrainian computer museum


Friday, April 29, 2016

The lifespan of a career as a programmer

In an IT area I often meet a common belief that a programmer should after all become a manger or at least a leading consultant without touching code. This opinion does not give any chance to a hans-on old person and a 50 years old programmer can be regarded as a shame.

The good example against such received thoughts is Dave Cutler.

Wednesday, September 23, 2015

Pronounce it right (for Russian readers)

Disclaimer: Post is in Russian and is about correct pronunciation of programming words. But it also can be useful for any no-english speaking person.

Осознание того, что ты неправильно произносишь английские слова уже многие годы, прилетает как лопата по голове. Носители английского языка просто не понимают термины, которыми русские программисты сыпят каждый день с уверенным advanced-english лицом. Вывести, намотанную годами неверную фразу, не так уж и просто.

Начнем с выборки, которую ошибочно произносят [селект].

SQL запросы ошибочно произносят как [куэри] или [квэри]

Архивацию ошибочно называют чем-то наподобие [а: чив] или [эчив], что больше походит на достижение achieve

В слове backup ошибочно делают ударение на последний слог. Сам был грешен и меня часто переспрашивали, что такое [бэкáп].

Задержку lag ошибочно произносят на русский манер как [лаг]. А lug — это ушко.

Reset произносят почему-то как [резэт], а ведь вас не поймут

Слово command ошибочно называют [кóманд] и, уже лучшим вариантом, но все равно не правильным, [комáнд]

Ну fatal error то мы все знаем. Читаем [фатал эрор], в русском кино слышали
[ˈfeɪt(ə)l] [ˈɛrə]

В слове access ошибочно ставят ударение на последний слог [эксéс] и получает полный access denied от англичан

Русские коллеги конечно поймут слово инсталлятор, а вот далеко не каждый иностранец поймет [инстал]

Со следующим словом страдают как любители доты, так и программисты. На собеседовании претендент сказал «работал с тини мсе», как потом выяснили это был tiny mce plugin

Коллега приобщил к зловредному и неправильному слову [кóнэкт], пришлось долго отвыкать

Отчеты большинство ошибочно произносят [репорт], и меня первое время англоязычные коллеги переспрашивали, что же этот русский хочет сказать

Пойти на ланч с приложением? Конечно, когда launch the app превращается в lunch the app

Binary ошибочно произносят в стиле «бинарники» [бинари]

Статус он и в Африке/за бугром [статус]? Да, вас поймут, но лучше скажите

Ну daemon tools то мы устанавливали, и слышали, что админ Вася называет это [даэмон], а он конечно ошибиться не может

Nuget пакеты ошибочно называют [нагет], т.е самородком nugget

Module ошибочно приравнивают к русскому произношению [модуль]
Две версии

Router произносят на американский манер, но надо знать, что распространен и другой
Две версии
UK/US: ['ru:t] / ['ru:tə]
US: ['raʊt] / ['raʊtə]

Hibernate ошибочно читают как [хибернэйт]

Scheduler ошибочно произносят [шедулер]
Два варианта

CMS Umbraco ошибочно произносят как [умбрако]

Microsoft Word ошибочно произносят как [ворд]

С облаками сложнее. Встречаю два разных типа англоворящих носителей, причем каждый признает только свое произношение. А услышав другое делают круглые глаза и «что ты сказал?».
Два варианта

И бонус для наших читателей. Австралиец Ричард любезно согласился прочитать слова

Sunday, August 16, 2015

Async await idea

In simple words .NET async await syntactic sugar helps to reduce limited resources usage.

Usually UI thread in WPF and WinFroms is only one thread that can update UI controls. If we use UI thread for long running task then user will see freezing window. Same is relevant to ASPNet ThreadPool, which has limited number of threads to process requests. Async Await syntax helps to minimise usage of these resources.

If there is no need to switch back to initial context, you can continue operations on newly created thread (AsyncOperation) by using

Recommend to watch Microsoft videos Essential Tips for Async

Monday, July 13, 2015

Public and private life key

In the dream there was an answer on private and public behaviours that I understood long time ago, but could not explain in a simple form. Accidentally the explanation appeared in amusing form.

Cryptography has the concept of Public/Private keys. And we are holding them in our life:

Rule 1
One cannot know private life key when taking a public key.

One of the nature part that people are hiding from society is a sexual (animal) behaviour, the problem is that it goes in thousands of years in the human history and so takes wide range of private key.
Possibly you dated partner, who is humble in public, but is crazy and hot in private life.
Go to another side.
Chaplin is still a famous comedian, most people think about him as a good and handsome man, but in real life he had relationship mostly with girls and young ladies, and changed them like gloves. His last wife were only 18 and he was 54 when they were married. Or Einstein who married his own cousin. People usually shocked when their lovely and polite neighbour becomes a maniac, these stories appear periodically in the news (Fritzl, Ridgway, Dr Harold Shipman). Or just look at Standard prison experiment, the base for Das Experiment movie.

Take another example, Walt Disney was scared of mice and he is the creator of Micky Mouse.

I believe you have good examples of quiet and ordinary colleagues who are amazing fathers and mothers in their private life.

Rule 2
Life keys are too wide and some cannot guess value with extrapolation.

You meet a girl in a party and she recognises pattern of your public key "he knows computer science", and next question is what? Right, "can you hack Google/Yahoo/Favourite site", or "can you repair my laptop/phone".
Or those surprised faces in work parties when people become to know each other a bit closer: "She is boxing and having professional skiing, wow, but she is so small and kind of quiet mice in the office, impossible", or "our brutal Mark is taking drawing lessons after work, girls, can you believe?".
Expectation of repeating same pattern in other parts of the key does not work and often reveals a surprise.

Guess the authors of the pantings:

The first and the second are painted by [Hitler], and the third is done by [Churchill].
Hint: to see the answer select the white part in [].

Good to see your positive stories of the rules in comments.