21 votes

Breaking all the rules

In most of my programming, I try and remain professional, and do things in a readable, maintainable way, that doesn't involve pushing the language to breaking point.

But, occasionally, I give myself free reign. What if you didn't care about the programmer who came after you? What if you didn't care about what a programmer should do in a certain circumstance?

For myself, over the years I've written and rewritten a C library, I like to call CNoEvil. Here's a little taste of what you could do:

#define EVIL_IO
#define EVIL_COROUTINE
#include "evil.h"

proc(example, int)
  static int i = 0;
  coroutine();
  While 1 then
    co_return(++i);
  end
  co_end();
  return i;
end

Main then
  displayln(example());
  displayln(example());
end

(And yes, that compiles. Without warnings. Even with -Wpedantic.)

So... Here's the challenge:

Ignoring the rules, and best practices... How can you take your favourite programming language... And make it completely unrecogniseable?

(Might be best to choose a language with macros, like Nim, Rust, any of the Lisps. Though, you can still do some impressively awful things in Java or Python, thanks to overloading inbuilt classes.)

Challenge Ideas:

  • Make Python look like C
  • Make Java look like Python
  • Make anything look like BrainFuck

I don’t know how to really explain my fascination with programming, but I’ll try. To somebody who does it, it’s the most interesting thing in the world. It’s a game much more involved than chess, a game where you can make up your own rules and where the end result is whatever you can make of it. - Linus Torvalds

7 comments

  1. [6]
    pleure
    (edited )
    Link
    int foo(int a) { int sum = 0 ; for (int i = 0; i < a; i++) { if (i % 2 == 0) { sum += i / 2 ;} else { sum += (i - 1) / 2 ;} return sum };} public static void main(String[] args) {...
    int foo(int a)                                                               {
        int sum = 0                                                              ;
        for (int i = 0; i < a; i++)                                              {
            if (i % 2 == 0)                                                      {
                sum += i / 2                                                     ;}
            else                                                                 {
                sum  += (i - 1) / 2                                              ;}
        return sum                                                               };}
                                                                             
    
    public static void main(String[] args)                                       {
        System.out.println("It’s just like python but with static typing!")      ;
        foo(12)                                                                  ;
                                                                                 }
    
    11 votes
    1. mrbig
      Link Parent
      Now make your text editor hide all the ; and {}!

      Now make your text editor hide all the ; and {}!

      4 votes
    2. s4b3r6
      Link Parent
      I can imagine the slow-dawning horror of a fellow coworker stumbling upon this... Though, if you don't mind one tiny change, it might just make it prettier. int foo(int a) {//} int sum = 0 ;//;...

      I can imagine the slow-dawning horror of a fellow coworker stumbling upon this... Though, if you don't mind one tiny change, it might just make it prettier.

      int foo(int a)                                                               {//}
          int sum = 0                                                              ;//;
          for (int i = 0; i < a; i++)                                              {//}
              if (i % 2 == 0)                                                      {//}
                  sum += i / 2                                                     ;}//{;
              else                                                                 {//}
                  sum  += (i - 1) / 2                                              ;}//{;
          return sum                                                               };}//{;{
      
      2 votes
    3. [3]
      Eva
      Link Parent
      The worst, most horrifying and best part of this is how you went out of your way to use ’ instead of '.

      The worst, most horrifying and best part of this is how you went out of your way to use ’ instead of '.

      2 votes
      1. [2]
        pleure
        Link Parent
        Honestly that was because I typed it on my phone, I had to correct the quotations on the string manually.

        Honestly that was because I typed it on my phone, I had to correct the quotations on the string manually.

        3 votes
        1. Emerald_Knight
          Link Parent
          I once had to help someone debug their code because their editor placed smart quotes instead of ordinary quotes. One of those really subtle bugs you don't really think about or notice. Worse...

          I once had to help someone debug their code because their editor placed smart quotes instead of ordinary quotes. One of those really subtle bugs you don't really think about or notice. Worse still, I once had to debug HTML (yes, fucking HTML of all things) because it was unexpectedly broken due to the existence of a non-breaking space in non-entity format--meaning, it looked exactly like every other bit of whitespace on the screen. You can probably imagine how fun it was tracking that one down.

          Kind of a tangent, but this discussion brought those horrible memories to surface :)

          2 votes
  2. Soptik
    Link
    I'll share a bit from wrapper that I wrote about a year ago. It is public repo on GitHub and has about 2500 downloads. public IEnumerable<IEnumerable<O>> Mine<T, O>(IEnumerable<T> input,...

    I'll share a bit from wrapper that I wrote about a year ago. It is public repo on GitHub and has about 2500 downloads.

            public IEnumerable<IEnumerable<O>> Mine<T, O>(IEnumerable<T> input, Func<string, T> getObjectFromTag, IEnumerable<Func<T, IEnumerable<string>>> selectTagsFunction, Func<string, O> resultFunction)
            {
                List<string> tags = new List<string>();
    
                foreach (T inp in input)
                {
                    foreach (Func<T, IEnumerable<string>> function in selectTagsFunction)
                    {
                        tags.AddRange(function(inp));
                    }
                }
    
                List<string> ts = new List<string>();
                ts.AddRange(tags);
                for (int i = 0; i < tags.Count; i++)
                {
                    T result = getObjectFromTag(tags[i]);
                    foreach (Func<T, IEnumerable<string>> function in selectTagsFunction)
                    {
                        foreach (string tag in function(result))
                        {
                            if (!tags.Contains(tag))
                            {
                                tags.Add(tag);
                                ts.Add(tag);
                            }
                        }
                    }
                    yield return ts.Select(tag => resultFunction(tag));
                    ts.Clear();
                }
            }
    

    It mines data from api, here's example:

                // Player is the thing I work with (I mine players), string is the thing mining returns (player tags)
                var result = clashRoyale.Mine<Player, string>(
                    new Player[] { clashRoyale.GetPlayer(clashRoyale.GetTopPlayers().First().tag, false, true) }, // As first data, use best player (here may be more players, even all players in wr.GetTopPlayers())
                    playerTag => clashRoyale.GetPlayer(playerTag, false, true), // How to get object Player from tag
                    new Func<Player, IEnumerable<string>>[]{ // Define array of actions to do with each player object (how to get more tags)
                        // Here, do only three actions:
                        new Func<Player, IEnumerable<string>>( // Get other players from clan
                            player => player.clan == null ? new string[0] : // If player doesn't have clan, return nothing
                                        clashRoyale.GetClan(player.clan.tag) // Get player's clan
                                        .members.Select(member => member.tag) // For each clan member, get his tag (this is used to mine additional tags from known players)
                            ),
                        new Func<Player, IEnumerable<string>>( // Get players from player's battles (from opponents)
                            player => player.battles.SelectMany(battle => battle.opponent.Select(plinfo => plinfo.tag))
                            ),
                        new Func<Player, IEnumerable<string>>(
                            player => player.battles.SelectMany(battle => battle.team.Where(plinfo => plinfo.tag != player.tag).Select(plinfo => plinfo.tag))
                            )
                    },
                    new Func<string, string>( // This functions gets tag (string) and returns required object - in this case, string. If you want to get for example
                                              // full player profile, use something like tag => wr.GetPlayer(tag)
                            resultTag => resultTag
                        )
                    );
    
                // Do not use this in real app, as this code will never end (add some condition)
                // This foreach iterates through all players mined.
                foreach (IEnumerable<string> tags in result)
                {
                    Console.WriteLine(String.Join("\n", tags));
                }
    
    1 vote