<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Digital Cabin - python</title>
<link rel="alternate" href="https://cabin.digital"/>
<link rel="self" href="https://cabin.digital/tags/python.xml"/>
<id>https://cabin.digital/</id>
<updated>2019-10-08T00:00:00Z</updated>
<generator uri="https://cabin.digital/golog.html" version="1.10.1beta">golog</generator>
<subtitle>Comfy cabin in a rough digital ocean</subtitle>
<author>
<name>dweller</name>
</author>
<category term="c"/>
<category term="python"/>
<category term="haskell"/>
<category term="rant"/>
<category term="dev"/>
<category term="programming"/>
<entry>
<id>https://cabin.digital/tags/python.xml:::https://cabin.digital/log/include-this.html</id>
<link rel="alternate" href="https://cabin.digital/log/include-this.html"/>
<title>#include "this.h"</title>
<updated>2019-10-08T00:00:00Z</updated>
<author>
<name>dweller</name>
</author>
<category term="dev"/>
<category term="programming"/>
<category term="c"/>
<category term="python"/>
<content type="html"><![CDATA[<p>So, I work with a couple of Python developers, and they really like their
Pythonic way. Which is fine, it has many good ideas, but they keep making
<code>import this</code> jokes, and I just had to do one for C. And, well, here we are.</p>

<p>First, let&rsquo;s just see it in action.</p>
<div class="golog-codeblock">
    <div class="golog-linenums"><pre><span>1
2
3
4
5
6
7
8
9
10
11
</span></pre></div>
    <div class="golog-lines"><pre><code><span class='golog-comment'>// main.c</span>
<span class='golog-keyword'>#include</span> <span class='golog-string'>&#34;this.h&#34;</span>


<span class='golog-keyword'>int</span> main(<span class='golog-keyword'>int</span> argc, <span class='golog-keyword'>char</span>** argv)
{
    (<span class='golog-keyword'>void</span>)argc;
    (<span class='golog-keyword'>void</span>)argv;

    <span class='golog-keyword'>return</span> <span class='golog-number'>0</span>;
}
</code></pre></div>
</div>

<p>The first ingredient is our regular C source file. It&rsquo;s just a main function
that does nothing. The 2nd one, is our secret sauce, we will discuss it later.
And of course, the final ingredient, the C compiler, I&rsquo;ll use <code>gcc</code>.</p>

<p>So, let&rsquo;s compile and run it:</p>
<div class="golog-codeblock">
    <div class="golog-linenums"><pre><span>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</span></pre></div>
    <div class="golog-lines"><pre><code>$ gcc -Wall -Wextra -pedantic main.c -o main
$ ./main
The Zen of C, by dweller

Code is better than cliché guidelines.
Working code is better than cute, but broken one.
Simple is better than complex, complex is better than broken.
Simple is not necessarily easy.
Zero cost abstractions is lack of abstractions.
Crash often.
There are usually multiple ways to do it.
Especially if it&#39;s multi-platform.
Avoid undefined behavior.
Know your tools, understand your platform.
Code, profile, optimize. In that order.
If the implementation is hard to explain, it may be a bad idea.
Or it may be hard to explain.
Delete more code than you write.
</code></pre></div>
</div>

<p>Magic, ain&rsquo;t it? :) Well, needless to say the magic is in the <em>this.h</em> file. So
let&rsquo;s inspect it.</p>
<div class="golog-codeblock">
    <div class="golog-linenums"><pre><span>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</span></pre></div>
    <div class="golog-lines"><pre><code><span class='golog-comment'>// this.h</span>
<span class='golog-keyword'>#ifndef</span> THIS_H
<span class='golog-keyword'>#define</span> THIS_H

<span class='golog-keyword'>#include</span> &lt;stdio.h&gt;


<span class='golog-keyword'>#define</span> main                                                                 \
    __dummy(<span class='golog-keyword'>void</span>) {<span class='golog-keyword'>return</span> <span class='golog-number'>0</span>;}                                                \
    <span class='golog-keyword'>int</span> __user_main(<span class='golog-keyword'>int</span>, <span class='golog-keyword'>char</span>**);                                            \
    <span class='golog-keyword'>int</span> main(<span class='golog-keyword'>int</span> argc, <span class='golog-keyword'>char</span>** argv)                                          \
    {                                                                        \
        printf(<span class='golog-string'>&#34;The Zen of C, by dweller\n&#34;</span>                                  \
          <span class='golog-string'>&#34;\n&#34;</span>                                                               \
          <span class='golog-string'>&#34;Code is better than cliché guidelines.\n&#34;</span>                         \
          <span class='golog-string'>&#34;Working code is better than cute, but broken one.\n&#34;</span>              \
          <span class='golog-string'>&#34;Simple is better than complex, complex is better than broken.\n&#34;</span>  \
          <span class='golog-string'>&#34;Simple is not necessarily easy.\n&#34;</span>                                \
          <span class='golog-string'>&#34;Zero cost abstractions is lack of abstractions.\n&#34;</span>                \
          <span class='golog-string'>&#34;Crash often.\n&#34;</span>                                                   \
          <span class='golog-string'>&#34;There are usually multiple ways to <span class='golog-keyword'>do</span> it.\n&#34;</span>                      \
          <span class='golog-string'>&#34;Especially <span class='golog-keyword'>if</span> it&#39;s multi-platform.\n&#34;</span>                             \
          <span class='golog-string'>&#34;Avoid undefined behavior.\n&#34;</span>                                      \
          <span class='golog-string'>&#34;Know your tools, understand your platform.\n&#34;</span>                     \
          <span class='golog-string'>&#34;Code, profile, optimize. In that order.\n&#34;</span>                        \
          <span class='golog-string'>&#34;If the implementation is hard to explain, it may be a bad idea.\n&#34;</span>\
          <span class='golog-string'>&#34;Or it may be hard to explain.\n&#34;</span>                                  \
          <span class='golog-string'>&#34;Delete more code than you write.\n&#34;</span>                               \
          );                                                                 \
        <span class='golog-keyword'>return</span> __user_main(argc, argv);                                      \
    }                                                                        \
    <span class='golog-keyword'>int</span> __user_main

<span class='golog-keyword'>#endif</span> <span class='golog-comment'>// THIS_H</span>
</code></pre></div>
</div>

<p>This is the secret sauce. What it does is replaces any symbol main (which is
<em>usually</em> your program entry) with this mess.
First part is <code>__dummy(void) {return 0;}</code>, it simply closes the <code>int</code> that we assume comes before <code>main</code>. After that, it declares a function <code>__user_main</code>
that has the same signature as regular <code>main</code>. <em>Then</em>, we actually declare and
define actual <code>main</code> function, we print our little joke-ish Zen, call
<code>__user_main</code> function with arguments from the shell and return whatever it
returns. And finally, at the end, we simply start the actual definition of
<code>__user_main</code>, as we expect the user&rsquo;s main implementation after this.</p>

<p>Let&rsquo;s see the output of C preprocessor, ignoring the <code>stdio.h</code> stuff:</p>
<div class="golog-codeblock">
    <div class="golog-linenums"><pre><span>1
</span></pre></div>
    <div class="golog-lines"><pre><code>$ cpp main.c
</code></pre></div>
</div>
<div class="golog-codeblock">
    <div class="golog-linenums"><pre><span>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</span></pre></div>
    <div class="golog-lines"><pre><code># <span class='golog-number'>1</span> <span class='golog-string'>&#34;main.c&#34;</span>
# <span class='golog-number'>1</span> <span class='golog-string'>&#34;&lt;built-in&gt;&#34;</span>
# <span class='golog-number'>1</span> <span class='golog-string'>&#34;&lt;command-line&gt;&#34;</span>
# <span class='golog-number'>31</span> <span class='golog-string'>&#34;&lt;command-line&gt;&#34;</span>
# <span class='golog-number'>1</span> <span class='golog-string'>&#34;/usr/include/stdc-predef.h&#34;</span> <span class='golog-number'>1 3</span> 4
# <span class='golog-number'>32</span> <span class='golog-string'>&#34;&lt;command-line&gt;&#34;</span> <span class='golog-number'>2</span>
# <span class='golog-number'>1</span> <span class='golog-string'>&#34;main.c&#34;</span>
# <span class='golog-number'>1</span> <span class='golog-string'>&#34;this.h&#34;</span> <span class='golog-number'>1</span>

<span class='golog-comment'>// tons of stdio.h stuff...</span>

# <span class='golog-number'>5</span> <span class='golog-string'>&#34;this.h&#34;</span> <span class='golog-number'>2</span>
# <span class='golog-number'>2</span> <span class='golog-string'>&#34;main.c&#34;</span> <span class='golog-number'>2</span>

# <span class='golog-number'>4</span> <span class='golog-string'>&#34;main.c&#34;</span>
<span class='golog-keyword'>int</span> __dummy(<span class='golog-keyword'>void</span>) {<span class='golog-keyword'>return</span> <span class='golog-number'>0</span>;} <span class='golog-keyword'>int</span> __user_main(<span class='golog-keyword'>int</span>, <span class='golog-keyword'>char</span>**); <span class='golog-keyword'>int</span> main(<span class='golog-keyword'>int</span> argc, <span class='golog-keyword'>char</span>** argv) { printf(<span class='golog-string'>&#34;The Zen of C, by dweller\n&#34;</span> <span class='golog-string'>&#34;\n&#34;</span> <span class='golog-string'>&#34;Code is better than cliché guidelines.\n&#34;</span> <span class='golog-string'>&#34;Working code is better than cute, but broken one.\n&#34;</span> <span class='golog-string'>&#34;Simple is better than complex, complex is better than broken.\n&#34;</span> <span class='golog-string'>&#34;Simple is not necessarily easy.\n&#34;</span> <span class='golog-string'>&#34;Zero cost abstractions is lack of abstractions.\n&#34;</span> <span class='golog-string'>&#34;Crash often.\n&#34;</span> <span class='golog-string'>&#34;There are usually multiple ways to <span class='golog-keyword'>do</span> it.\n&#34;</span> <span class='golog-string'>&#34;Especially <span class='golog-keyword'>if</span> it&#39;s multi-platform.\n&#34;</span> <span class='golog-string'>&#34;Avoid undefined behavior.\n&#34;</span> <span class='golog-string'>&#34;Know your tools, understand your platform.\n&#34;</span> <span class='golog-string'>&#34;Code, profile, optimize. In that order.\n&#34;</span> <span class='golog-string'>&#34;If the implementation is hard to explain, it may be a bad idea.\n&#34;</span> <span class='golog-string'>&#34;Or it may be hard to explain.\n&#34;</span> <span class='golog-string'>&#34;Delete more code than you write.\n&#34;</span> ); <span class='golog-keyword'>return</span> __user_main(argc, argv); } <span class='golog-keyword'>int</span> __user_main(<span class='golog-keyword'>int</span> argc, <span class='golog-keyword'>char</span>** argv)
{
    (<span class='golog-keyword'>void</span>)argc;
    (<span class='golog-keyword'>void</span>)argv;

    <span class='golog-keyword'>return</span> <span class='golog-number'>0</span>;
}
</code></pre></div>
</div>

<p>If we just massage the code to make it bit more readable, since all of it is on
the same line, plus remove all the <code>cpp</code> stuff. We get:</p>
<div class="golog-codeblock">
    <div class="golog-linenums"><pre><span>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</span></pre></div>
    <div class="golog-lines"><pre><code><span class='golog-comment'>// stdio.h stuff...</span>

<span class='golog-keyword'>int</span> __dummy(<span class='golog-keyword'>void</span>) {<span class='golog-keyword'>return</span> <span class='golog-number'>0</span>;}
<span class='golog-keyword'>int</span> __user_main(<span class='golog-keyword'>int</span>, <span class='golog-keyword'>char</span>**);

<span class='golog-keyword'>int</span> main(<span class='golog-keyword'>int</span> argc, <span class='golog-keyword'>char</span>** argv)
{
    printf(<span class='golog-string'>&#34;The Zen of C, by dweller\n&#34;</span>
           <span class='golog-string'>&#34;\n&#34;</span>
           <span class='golog-string'>&#34;Code is better than cliché guidelines.\n&#34;</span>
           <span class='golog-string'>&#34;Working code is better than cute, but broken one.\n&#34;</span>
           <span class='golog-string'>&#34;Simple is better than complex, complex is better than broken.\n&#34;</span>
           <span class='golog-string'>&#34;Simple is not necessarily easy.\n&#34;</span>
           <span class='golog-string'>&#34;Zero cost abstractions is lack of abstractions.\n&#34;</span>
           <span class='golog-string'>&#34;Crash often.\n&#34;</span>
           <span class='golog-string'>&#34;There are usually multiple ways to <span class='golog-keyword'>do</span> it.\n&#34;</span>
           <span class='golog-string'>&#34;Especially <span class='golog-keyword'>if</span> it&#39;s multi-platform.\n&#34;</span>
           <span class='golog-string'>&#34;Avoid undefined behavior.\n&#34;</span>
           <span class='golog-string'>&#34;Know your tools, understand your platform.\n&#34;</span>
           <span class='golog-string'>&#34;Code, profile, optimize. In that order.\n&#34;</span>
           <span class='golog-string'>&#34;If the implementation is hard to explain, it may be a bad idea.\n&#34;</span>
           <span class='golog-string'>&#34;Or it may be hard to explain.\n&#34;</span>
           <span class='golog-string'>&#34;Delete more code than you write.\n&#34;</span>);

           <span class='golog-keyword'>return</span> __user_main(argc, argv);
}

<span class='golog-keyword'>int</span> __user_main(<span class='golog-keyword'>int</span> argc, <span class='golog-keyword'>char</span>** argv)
{
    (<span class='golog-keyword'>void</span>)argc;
    (<span class='golog-keyword'>void</span>)argv;

    <span class='golog-keyword'>return</span> <span class='golog-number'>0</span>;
}
</code></pre></div>
</div>

<p>And I think, that&rsquo;s pretty self-explanatory if you know C. Needless to say, this
has a lot of issues. It works only assuming you include it in the file that
contains <code>main</code> function, and that its signature is <code>int (*)(int, char**)</code>,
which is not always true. So this is just a toy to generate a bit of airflow
through some nostrils of a few programmers.</p>

<p>Have a nice one!</p>
]]></content>
</entry>
<entry>
<id>https://cabin.digital/tags/python.xml:::https://cabin.digital/log/whitespace.html</id>
<link rel="alternate" href="https://cabin.digital/log/whitespace.html"/>
<title>Significant Whitespace</title>
<updated>2016-01-11T00:00:00Z</updated>
<author>
<name>dweller</name>
</author>
<category term="programming"/>
<category term="python"/>
<category term="haskell"/>
<category term="rant"/>
<content type="html"><![CDATA[<h1 id="or-more-like-significant-pain-in-the-rear">Or more like significant pain in the rear</h1>

<p>Today I want to talk about something that is bugging me lately. As you might&rsquo;ve guessed from the title, it&rsquo;s significant whitespace in programming languages.</p>

<p>While I was learning Python, I didn&rsquo;t give it much of a thought. Now admittedly I do not know Python as much I&rsquo;d wanted to, but I wrote enough scripts and small tools in it, that I feel <em>comfortable</em>. I haven&rsquo;t thought of significant whitespace much, because it was new, and it seemed cool. (it makes me format my code (not that it ever was a problem)) But now that I tried Haskell, my opinion changed, and in a bad way.</p>

<p>Okay, so you&rsquo;re a programmer? You solve problems, you write the solution to it as such that the dumbest thing can understand it - computer. So most of the time programmers <del>drink coffee and stare at the celling</del> are problem solving, and writing the solution down. Therefore, does it seem like a good idea to make a developer think <em>how</em> to write it down, rather than <em>what</em> to write down. In my humble option, you can always refactor the code and/or prettify it later, after the task have been solved.</p>

<p>When I just started to learn Haskell I got a bunch of errors that complained about indentations. That threw me off the track from solving problem on the hand a few times. I wrote possible solution down, tried it, and got errors that are not telling me what is <em>wrong</em> with my solution, but tell me that my <em>code is misaligned</em>. While I try to fix that, I lose my train of thought. (Also what is it about Haskell and tabs? <code>:unset -fwarn-tabs</code> anyone?)</p>

<p>In the end, all I want to say is that in my opinion tools should help us solve the problems, not get in the way. And significant whitespace gets in the way, especially in the beginning.</p>
]]></content>
</entry>
</feed>
