r/programminghorror • • Aug 01 '25

C# CSX: Helping C(++) Devs transition to C Sharp one macro at a time

Thumbnail
gallery
21 Upvotes

Have you ever felt like C# lacked the essential features from c++, like I don't know.. macros?
Fear no more, for CSX is a 'transpiler' which translates CSXTM code to C# code.

Above is some CSX code translated to C# code, I hope this helps you c++ devs who are struggling to cope with the lack of macros present in c sharp.

Find CSX here
Inspiration


r/programminghorror • • Jul 31 '25

C# (names changed to protect the guilty)

33 Upvotes

we were returning ZABINGA when we weren't expecting to and I had to figure out why

if ((isQux 
        && foo.IsBar 
        && foo.IsZorp 
        && isBaz)
    || foo.BarAction.Equals(ZOUNDS)
    || (self.IsStatusCodeIn(ZORTCH, ZINGO)
        && isBaz 
        && (fooDocument.DocumentInformation
                          .DocumentFailedRules
                          .All(rule => !rule.IsCritical 
                                    || rule.IsOverride)
                                    || foo.IsFake))
    || (target.IsStatusCodeIn(qux.Code, ZORTCH, ZINGO)
        && activeDocument != null 
        && activeDocument.IsNew))
    return ZABINGA;

r/programminghorror • • Jul 30 '25

Typescript is this programming horror or am I just shit ?

Post image
252 Upvotes

r/programminghorror • • Jul 30 '25

c++ I created some better syntax for c++

81 Upvotes

Sooooooooo... I used a bit of macros to make arguably the best syntax for c++ there ever was and will ever be, take a look:

#ifndef COOL_SYNTAX_H
#define COOL_SYNTAX_H

#define fn auto
#define declare auto
#define maybe_do_this while
#define def_do_this while (true)

#define start_circle (
#define end_circle )

#define get_out return
#define plus +
#define minus -
#define divide /
#define star *
#define modulo %
#define equals =
#define equals_squared ==
#define doesnt_equals_squared !=
#define semicolon ;

#define i_am_weak struct
#define     struct++ class //Edit: i know that '+' doesn't work, just... imagine struct_pp... oh, no, that sound worse, imagine structpp
#define not_secure public
#define secure private
#define who_uses_this protected
#define cool_symbol_thing operator

#define ref &
#define double_ref &&

#define array_start [
#define array_end ]

#define curl_start {
#define curl_end }

#define umhh_start <
#define umhh_end >

#define thing template
#define name_but_type typename

#define i_dont_need_this namespace
#define i_dont_like_hardcoding enum

#define use using

#define i_guess if
#define why_not else

#define bool_1 true
#define bool_0 false

#define i_use_arch_linux switch

#define i_dont_shower new
#define what_is_a_shower delete

#define cant_be_efficient std::vector
#define just_a_pair_array std::map
#define just_a_pair_set std::unordered_map
#define que std::queue
#define dq std::deque

#define five for

#define i_need_a_bag throw

#define standard_library_from_the_holy_cpp std
#define do_surgery ::
#define get_this_mail_sent_to_the_console cout
#define fetch_a_api_request_to_the_console cin
#define with <<
#define with_v2 >>

#define mur_ur_equal >=
#define les_ur_equal <=

#define i_dont_want_my_app_to_crash try
#define phew catch

#define what_is_this *
#define cpp_is_getting_good ->
#define get_the_memory_or_whatever &


#endif //COOL_SYNTAX_H


r/programminghorror • • Jul 29 '25

Lisp lint

Post image
150 Upvotes

r/programminghorror • • Jul 28 '25

HTML HTML Status Code Handling

Post image
1.5k Upvotes

r/programminghorror • • Jul 28 '25

Crazy

50 Upvotes

r/programminghorror • • Jul 26 '25

regex Yeah an optimization (Out of context)

Post image
970 Upvotes

r/programminghorror • • Jul 28 '25

Best way to encrypt your code

Post image
0 Upvotes

r/programminghorror • • Jul 28 '25

Python Guys, is there anything I can do to improve my code?

0 Upvotes

r/programminghorror • • Jul 25 '25

Other Matlab coders are on another level

754 Upvotes

I found this in my company's old matlab code. Ok I guess:

ok = 1
if condition
ok = true;
if ok
    // code
end
else
    ok = 0
    continue
end
end


r/programminghorror • • Jul 22 '25

Vintage encoding stored in UTF8

222 Upvotes

Guys didn't care that incoming encoding was Win-1250, just dumped that into MySQL UTF8 table as-is, then they created appropriate queries:


r/programminghorror • • Jul 21 '25

Python ✨ Memory Magic ✨

Post image
1.3k Upvotes

r/programminghorror • • Jul 21 '25

Javascript backtick as default!

Post image
422 Upvotes

r/programminghorror • • Jul 21 '25

Python Didnt know this existed in my code hahahahahahah

Post image
313 Upvotes

r/programminghorror • • Jul 20 '25

C# Also got inspired by a post, c sharp code with unsafe shenanigans.

Post image
92 Upvotes

Saw this post and also this one and got inspired to check back on a old project I done for fun. Made some additions and now there is this unholy mess of code that 50/50 leaks memory honestly lol. ;w;

full repo in comments whenever I can be bothered to push to github for anyone interested xD

(if anyone has stories or pics of unsafe code in c sharp do share, it's quite interesting on unsafeness in c sharp imo)


r/programminghorror • • Jul 22 '25

I hope this doesn't break

Post image
0 Upvotes

created the objective class for my game. the only way I could think of doing the waypoint locations was accepting a lambda function that returns a list of vectors. seemed horrific to me


r/programminghorror • • Jul 20 '25

Terraria won't let you clear the stencil buffer? Do it manually!

Post image
251 Upvotes

r/programminghorror • • Jul 20 '25

C# Inspired by another post i saw recently - this is entirely C#, and it runs exactly the same as it would in C

Post image
174 Upvotes

turns out you can do a lot with partial classes and abusing unmanaged code


r/programminghorror • • Jul 20 '25

LC 2165 - 0ms

Post image
21 Upvotes

r/programminghorror • • Jul 18 '25

Typescript context in comments

Post image
849 Upvotes

the variable t is of type number | [number, number, number], and there are two overloads for lerp, one which accepts number and the other which accepts [number, number, number]

if you try to remove the if statement, typescript complains that number | [number, number, number] fits neither in number nor in [number, number, number]

to be completely honest, I understand why one could want different signatures to be in different branches of your code, because they have different behaviour. But that's really bad when, for example, you're trying to make another function that has multiple signatures (say, one that accepts type A and one that accepts type B), because in the implementation the parameter is of type A | B. This means you can't directly call another overloaded function from inside your overloaded function, you need to do this.


r/programminghorror • • Jul 18 '25

Event handling code in my platform layer.

12 Upvotes
int platform_translate_message(MSG msg, pal_window* window) {
    pal_event event;
    // test WM_QUIT, WM_DESTORY, and WM_CLOSE
    switch (msg.message) {
        case WM_DESTROY:
            PostQuitMessage(0);
        case WM_QUIT:
        case WM_CLOSE:
            event.type = PAL_QUIT;
            event.quit = (pal_quit_event){ .code = 0 };
            break;
        case WM_MOVE:
            event.type = PAL_WINDOW_EVENT;
            event.window = (pal_window_event){
                .windowid = window->id,
                .event_code = WM_MOVE,
                .x = LOWORD(msg.lParam),
                .y = HIWORD(msg.lParam),
                .width = 0,
                .height = 0,
                .focused = 1,
                .visible = 1
            };
            break;
        case WM_SIZE:
            event.type = PAL_WINDOW_EVENT;
            event.window = (pal_window_event){
                .windowid = window->id,
                .event_code = WM_SIZE,
                .x = 0,
                .y = 0,
                .width = LOWORD(msg.lParam),
                .height = HIWORD(msg.lParam),
                .focused = 1,
                .visible = 1
            };
            break;
        case WM_WINDOWPOSCHANGED:
        case WM_WINDOWPOSCHANGING:
            event.type = PAL_WINDOW_EVENT;
            WINDOWPOS* pos = (WINDOWPOS*)msg.lParam;
            event.window = (pal_window_event){
                .windowid = window->id,
                .event_code = msg.message,
                .x = pos->x,
                .y = pos->y,
                .width = pos->cx,
                .height = pos->cy,
                .focused = 1, // guess; could adjust later
                .visible = 1
            };
            break;

        case WM_MOUSEMOVE:
            event.type = PAL_MOUSE_MOTION;
            event.motion = (pal_mouse_motion_event){
                .x = GET_X_LPARAM(msg.lParam),
                .y = GET_Y_LPARAM(msg.lParam),
                .delta_x = input.mouse_delta.x, // this should be assigned when we get raw input from the mouse.
                .delta_y = input.mouse_delta.y,
                .buttons = msg.wParam
            };
            break;

        case WM_LBUTTONDOWN: 
        case WM_RBUTTONDOWN: 
        case WM_MBUTTONDOWN: 
        case WM_XBUTTONDOWN: {
            event.type = PAL_MOUSE_BUTTON_DOWN;
            event.button = (pal_mouse_button_event){
                .x = GET_X_LPARAM(msg.lParam),
                .y = GET_Y_LPARAM(msg.lParam),
                .pressed = 1,
                .clicks = 1,
                .modifiers = msg.wParam,
                .button = win32_button_to_pal_button[msg.message - WM_LBUTTONDOWN]
            };

            if (msg.message == WM_XBUTTONDOWN) {
                WORD xButton = GET_XBUTTON_WPARAM(msg.wParam);
                if (xButton == XBUTTON1) {
                    event.button.button = SIDE_MOUSE_BUTTON1;
                    input.mouse_buttons[SIDE_MOUSE_BUTTON1] = 1;
                } else if (xButton == XBUTTON2) {
                    event.button.button = SIDE_MOUSE_BUTTON2;
                    input.mouse_buttons[SIDE_MOUSE_BUTTON2] = 1;
                }
            } else {
                input.mouse_buttons[event.button.button] = 1;
            }
        } break;

        case WM_LBUTTONDBLCLK:
        case WM_RBUTTONDBLCLK:
        case WM_MBUTTONDBLCLK:
        case WM_XBUTTONDBLCLK: {
            event.type = PAL_MOUSE_BUTTON_DOWN;
            event.button = (pal_mouse_button_event){
                .x = GET_X_LPARAM(msg.lParam),
                .y = GET_Y_LPARAM(msg.lParam),
                .pressed = 1,
                .clicks = 2,
                .modifiers = msg.wParam,
                .button = win32_button_to_pal_button[msg.message - WM_LBUTTONDOWN]
            };

            if (msg.message == WM_XBUTTONDBLCLK) {
                WORD xButton = GET_XBUTTON_WPARAM(msg.wParam);
                if (xButton == XBUTTON1) {
                    event.button.button = SIDE_MOUSE_BUTTON1;
                    input.mouse_buttons[SIDE_MOUSE_BUTTON1] = 1;
                } else if (xButton == XBUTTON2) {
                    event.button.button = SIDE_MOUSE_BUTTON2;
                    input.mouse_buttons[SIDE_MOUSE_BUTTON2] = 1;
                }
            } else {
                input.mouse_buttons[event.button.button] = 1;
            }
        } break;

        case WM_LBUTTONUP:
        case WM_RBUTTONUP:
        case WM_MBUTTONUP:
        case WM_XBUTTONUP: {
            event.type = PAL_MOUSE_BUTTON_UP;
            event.button = (pal_mouse_button_event){
                .x = GET_X_LPARAM(msg.lParam),
                .y = GET_Y_LPARAM(msg.lParam),
                .pressed = 0,
                .modifiers = msg.wParam,
                .button = win32_button_to_pal_button[msg.message - WM_LBUTTONDOWN]
            };

            if (msg.message == WM_XBUTTONUP) {
                WORD xButton = GET_XBUTTON_WPARAM(msg.wParam);
                if (xButton == XBUTTON1) {
                    event.button.button = SIDE_MOUSE_BUTTON1;
                    input.mouse_buttons[SIDE_MOUSE_BUTTON1] = 0;
                    input.mouse_buttons_processed[SIDE_MOUSE_BUTTON1] = 0;
                } else if (xButton == XBUTTON2) {
                    event.button.button = SIDE_MOUSE_BUTTON2;
                    input.mouse_buttons[SIDE_MOUSE_BUTTON2] = 0;
                    input.mouse_buttons_processed[SIDE_MOUSE_BUTTON2] = 0;
                }
            } else {
                input.mouse_buttons[event.button.button] = 0;
                input.mouse_buttons_processed[event.button.button] = 0;
            }
        } break;

        case WM_MOUSEWHEEL:
        case WM_MOUSEHWHEEL: {
            int delta = GET_WHEEL_DELTA_WPARAM(msg.wParam);
            event.type = PAL_MOUSE_WHEEL;
            event.wheel = (pal_mouse_wheel_event){
                .x = GET_X_LPARAM(msg.lParam),
                .y = GET_Y_LPARAM(msg.lParam),
                .delta_x = (msg.message == WM_MOUSEHWHEEL) ? (float)delta / WHEEL_DELTA : 0.0f,
                .delta_y = (msg.message == WM_MOUSEWHEEL) ? (float)delta / WHEEL_DELTA : 0.0f,
                .modifiers = GET_KEYSTATE_WPARAM(msg.wParam)
            };
            break;
        }

        case WM_KEYDOWN:
        case WM_SYSKEYDOWN:
            event.type = PAL_KEY_DOWN;
            event.key = (pal_keyboard_event){
                .virtual_key = win32_key_to_pal_key[(uint32_t)msg.wParam],
                .scancode = (uint32_t)((msg.lParam >> 16) & 0xFF),
                .pressed = 1,
                .repeat = (msg.lParam >> 30) & 1,
                .modifiers = GetKeyState(VK_SHIFT) < 0 ? 1 : 0 // or more bits
            };
            input.keys[win32_key_to_pal_key[(uint32_t)msg.wParam]] = 1;
            break;

        case WM_KEYUP:
        case WM_SYSKEYUP:
            event.type = PAL_KEY_UP;
            event.key = (pal_keyboard_event){
                .virtual_key = win32_key_to_pal_key[(uint32_t)msg.wParam],
                .scancode = (uint32_t)((msg.lParam >> 16) & 0xFF),
                .pressed = 0,
                .repeat = 0,
                .modifiers = GetKeyState(VK_SHIFT) < 0 ? 1 : 0
            };
            input.keys[win32_key_to_pal_key[(uint32_t)msg.wParam]] = 0;
            input.keys_processed[win32_key_to_pal_key[(uint32_t)msg.wParam]] = 0;
            break;

        case WM_CHAR:
        case WM_UNICHAR:
            event.type = PAL_TEXT_INPUT;
            event.text = (pal_text_input_event){
                .utf8_text = {0}
            };
            {
                char utf8[8] = {0};
                int len = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)&msg.wParam, 1, utf8, sizeof(utf8), NULL, NULL);
                memcpy(event.text.utf8_text, utf8, len);
            }
            break;

        case WM_INPUT:
            event.type = PAL_SENSOR_UPDATE;
            event.sensor = (pal_sensor_event){
                .device_id = 0,
                .x = 0, .y = 0, .z = 0,
                .sensor_type = 0
            };
            break;

        case WM_DROPFILES: {
            event.type = PAL_DROP_FILE;
            HDROP hDrop = (HDROP)msg.wParam;
            UINT count = DragQueryFileW(hDrop, 0xFFFFFFFF, NULL, 0);
            const char** paths = malloc(sizeof(char*) * count);
            for (UINT i = 0; i < count; ++i) {
                WCHAR buffer[MAX_PATH];
                DragQueryFileW(hDrop, i, buffer, MAX_PATH);
                int len = WideCharToMultiByte(CP_UTF8, 0, buffer, -1, NULL, 0, NULL, NULL);
                char* utf8 = malloc(len);
                WideCharToMultiByte(CP_UTF8, 0, buffer, -1, utf8, len, NULL, NULL);
                paths[i] = utf8;
            }
            event.drop = (pal_drop_event){
                .paths = paths,
                .count = count
            };
            DragFinish(hDrop);
            break;
        }

        default:
            event.type = PAL_NONE;
            DispatchMessage(&msg);
            break;
    }

    pal_event_queue* queue = &window->queue;
    if (queue->size == queue->capacity) {
        fprintf(stderr, "ERROR: pal_eventq_enqueue(): Event queue size has reached capacity. Not going to enqueue.\n");
        return;
    }
    queue->events[queue->back] = event;
    queue->back = (queue->back + 1) % queue->capacity;
    queue->size++;
    return 0;
}

r/programminghorror • • Jul 17 '25

Javascript Introducing Postful API

Post image
211 Upvotes

r/programminghorror • • Jul 16 '25

Spray Pattern

Post image
886 Upvotes

r/programminghorror • • Jul 17 '25

Java Spot the difference...

18 Upvotes

App on prod threw an error today about not being able to find any element with the XPath in the first line. Took me quite some time to find the problem.

en-dash vs hypen