• What's the worst line of code you can remember writing?
    56 replies, posted
[QUOTE=JWki;52175699]I think every bigger engine has. STL just isn't optimised for gamedev which is fine because it is meant to cover a lot of ground. The fact that the spec is questionable sometimes and some implementations are... Even more questionable in places (looking at you, std::map) and also that exceptions are used as a primary error handling mechanism doesn't help there. EASTL is a bit more popular though and there's a movement in the standards committee to get some better support for gamedev needs into the standard. Also here's a horrible piece of code I've written: [CODE] template<class T> T* GetInstance() { static T instance; return &instance ; } [/CODE] Not a single line really but wrong on so many layers. Even ignoring what horrible thing it implements, it's not even a good implementation. Partly because there is no good implementation of this. Still used it way too much.[/QUOTE] Iunno, a lot of the problems with the STL have been improved as of late. Like when doing dev for a console I can see why it wouldn't be ideal, but things like std::algorithm are bloody brilliant and can be really useful. And those are easy to "hook" into by defining the appropriate typedefs + iterators for your custom classes, at least. I do know that Microsoft Excel heavily used std::function, which led to compile-time and executable size bloat before the MSVC team fixed up how it compiles. Found this gross thing in my old n-body project: [cpp] for (auto iter = this->Bodies.cbegin(); iter != this->Bodies.cend();) { auto& b = **iter; this->Mass += b.Mass; // update motion of body b } [/cpp] "Bodies" was a list of shared pointers too, so instead of continuing to live in the safe world of shared pointers and using that standard library item like it was intended I just leapt right into playing with the raw pointer lol.
I couldn't find the absolute "worst" line of code, because then I'd have to pretty much upload every one of my projects in their entirety... But this little snippet of code makes me cringe a little bit, so here you go: [code]#define AGE_API_EXPORT #define MM2_API_EXPORT #include "mm2.h" using namespace MM2; #define DEFINE_PRINT_HOOK(x) \ NAKED void x(LPCSTR str, ...) { \ __asm jmp dword ptr ds:$##x \ } AGEHook<0x4C9720>::Func<void> $Printf; AGEHook<0x4C9750>::Func<void> $Messagef; AGEHook<0x4C9780>::Func<void> $Displayf; AGEHook<0x4C97B0>::Func<void> $Warningf; AGEHook<0x4C97E0>::Func<void> $Errorf; AGEHook<0x4C9810>::Func<void> $Quitf; AGEHook<0x4C9850>::Func<void> $Abortf; AGEHook<0x534790>::Func<char *> $AngelReadString; namespace MM2 { DEFINE_PRINT_HOOK(Printf); DEFINE_PRINT_HOOK(Messagef); DEFINE_PRINT_HOOK(Displayf); DEFINE_PRINT_HOOK(Warningf); DEFINE_PRINT_HOOK(Errorf); DEFINE_PRINT_HOOK(Quitf); DEFINE_PRINT_HOOK(Abortf); char * AngelReadString(UINT stringId) { return $AngelReadString(stringId); } }[/code]
[QUOTE=paindoc;52177222]Iunno, a lot of the problems with the STL have been improved as of late. Like when doing dev for a console I can see why it wouldn't be ideal, but things like std::algorithm are bloody brilliant and can be really useful. And those are easy to "hook" into by defining the appropriate typedefs + iterators for your custom classes, at least. I do know that Microsoft Excel heavily used std::function, which led to compile-time and executable size bloat before the MSVC team fixed up how it compiles. Found this gross thing in my old n-body project: [cpp] for (auto iter = this->Bodies.cbegin(); iter != this->Bodies.cend();) { auto& b = **iter; this->Mass += b.Mass; // update motion of body b } [/cpp] "Bodies" was a list of shared pointers too, so instead of continuing to live in the safe world of shared pointers and using that standard library item like it was intended I just leapt right into playing with the raw pointer lol.[/QUOTE] One of the issues with the STL are the allocation patterns paired with the lacking allocator interface. That's an issue on all platforms really.
[QUOTE=suXin;52151077]actually this happened recently [media]https://twitter.com/suxinjke/status/852498168373817345[/media][/QUOTE] [url]http://www.cplusplus.com/reference/cctype/isdigit/[/url] :3 [editline]3rd May 2017[/editline] [QUOTE=DrTaxi;52173322][code]signed int drawSingleTextLineHook(int textureId, int startX, signed int startY, unsigned int a4, char *string, signed int maxLength, int color, int glyphSize, signed int opacity) { // yolo if (NEEDS_CLEARLIST_TEXT_POSITION_ADJUST) { uintptr_t retaddr; __asm { push eax mov eax, [ebp + 4] mov retaddr, eax pop eax } if (retaddr == gameExeClearlistDrawRet1 || retaddr == gameExeClearlistDrawRet2 || retaddr == gameExeClearlistDrawRet3 || ... [/code] [sp]arguably the most elegant solution for what I'm doing but[/sp] this is what I point people to when I tell them why they shouldn't hire me[/QUOTE] you could use the _ReturnAddress() intrinsic/__builtin_return_address for gcc instead of your inline asm which pretty much do the same
5 Levels nested async task call in my first Android project. (sorry, no source code)
[code] function foo () { dance: for(var k = 0; k < 4; k++){ for(var m = 0; m < 4; m++){ if(m == 2){ break dance; } } } } [/code]
[cpp]__kernel //__attribute__((reqd_work_group_size(DIM_KERNEL3, DIM_KERNEL3, 1))) //__attribute__((vec_type_hint(float3))) void kernel3(__global struct triangle *triangles, float4 c_pos, float4 c_rot, __global uint* depth_buffer, __read_only image2d_t id_buffer, image_3d_read array, __write_only image2d_t screen, __write_only image2d_t backup_screen, __global uint *nums, __global uint *sizes, __global struct obj_g_descriptor* gobj, __global uint* lnum, __global struct light* lights, __global uint* light_depth_buffer, __global uint* static_light_depth_buffer, __global uint * to_clear, __global uint* fragment_id_buffer, __global float4* cutdown_tris, float4 screen_clear_colour, uint frame_id, __global ushort2* screen_normals_optional, int use_optional_blend_buffer, __global uint* optional_blend_buffer_depth, __read_only image2d_t optional_blend_screen, uint mip_start, uint use_linear_rendering )[/cpp]
[QUOTE=Icedshot;52192506][cpp]__kernel //__attribute__((reqd_work_group_size(DIM_KERNEL3, DIM_KERNEL3, 1))) //__attribute__((vec_type_hint(float3))) void kernel3(__global struct triangle *triangles, float4 c_pos, float4 c_rot, __global uint* depth_buffer, __read_only image2d_t id_buffer, image_3d_read array, __write_only image2d_t screen, __write_only image2d_t backup_screen, __global uint *nums, __global uint *sizes, __global struct obj_g_descriptor* gobj, __global uint* lnum, __global struct light* lights, __global uint* light_depth_buffer, __global uint* static_light_depth_buffer, __global uint * to_clear, __global uint* fragment_id_buffer, __global float4* cutdown_tris, float4 screen_clear_colour, uint frame_id, __global ushort2* screen_normals_optional, int use_optional_blend_buffer, __global uint* optional_blend_buffer_depth, __read_only image2d_t optional_blend_screen, uint mip_start, uint use_linear_rendering )[/cpp][/QUOTE] Holy shit...apparently after 8 parameters nobody bothered to ask if a structure pointer would be a good idea :v:
[QUOTE=Icedshot;52192506][cpp]__kernel //__attribute__((reqd_work_group_size(DIM_KERNEL3, DIM_KERNEL3, 1))) //__attribute__((vec_type_hint(float3))) void kernel3(__global struct triangle *triangles, float4 c_pos, float4 c_rot, __global uint* depth_buffer, __read_only image2d_t id_buffer, image_3d_read array, __write_only image2d_t screen, __write_only image2d_t backup_screen, __global uint *nums, __global uint *sizes, __global struct obj_g_descriptor* gobj, __global uint* lnum, __global struct light* lights, __global uint* light_depth_buffer, __global uint* static_light_depth_buffer, __global uint * to_clear, __global uint* fragment_id_buffer, __global float4* cutdown_tris, float4 screen_clear_colour, uint frame_id, __global ushort2* screen_normals_optional, int use_optional_blend_buffer, __global uint* optional_blend_buffer_depth, __read_only image2d_t optional_blend_screen, uint mip_start, uint use_linear_rendering )[/cpp][/QUOTE] This is OpenCL, isn't it? There's similar monstrosities hiding in my CUDA code lol
[QUOTE=CarLuver69;52192965]Holy shit...apparently after 8 parameters nobody bothered to ask if a structure pointer would be a good idea :v:[/QUOTE] Yes, yes, somebody else totally did this, bloody terrible programmer that was *phew* Unfortunately due to OpenCL being OpenCL, you can't pass in a structure containing pointers to globals (easily) in 1.x. You can make a struct... but on the host side you'd have to fill it with pointers to device side memory, which you can't get host side. You could do it in a kernel, but you still end up with indirection which is slow So you're just fucked and you end up with this monstrosity. This is a deferred shading kernel so its by far the worst in the entire codebase. Its not too bad of a maintenance problem though as the entire thing is encapsulated on the host side, it just looks fucking terrible :v:
I wrote a compile-time list class with C++11 magic, it let you write bullshit like this: [cpp] template<typename... Members> class tagged_union { using members = type_pack<Members...>; ... template<typename T> struct value_type_for { using type = typename T::type; }; using body_type = typename members::template map<value_type_for>::get_type::template push_tail<dead_value>::type::template apply<unchecked_variant>::type; ... } [/cpp] Sure wonder why my compile times are fucking horrible and the linker is crying from >1024 char symbol names. That or the 5MB header library included in basically every file :v: (I really need to convert that to a precompiled header) [editline]7th May 2017[/editline] That's basically the most comprehensible part of that file by the way.
For anyone familiar with Angular....I'd love a better way of doing this. [code]<i class="material-icons ng-scope" ng-repeat="val in [1,2,3,4,5]" ng-class="{starred: League.showRatings && (League.indexRating.show(match._id+$parent.$index) ? League.indexRating.hover(match._id+$parent.$index) : match.data[$index].rating) >= val}" ng-mouseenter="League.indexRating.onHover(match._id+$parent.$index, val)" ng-mouseleave="League.indexRating.onUnHover(match._id+$parent.$index)" ng-click="League.showRatings ? Page.rate(match._id, val, $parent.$index) : return;League.showRatings ? match.data[$index].rating = val : League.showRatings = true">star</i>[/code]
My friend accidentally did this in PHP: [code]if (strlen($number > 0))[/code] Instead of [code]if (strlen($number) > 0)[/code] The most amazing thing about this was his program still worked as expected, I guess that's PHP for you
Old, ignorant, maybe a year ago me decided this was a great idea, not the exact code but it was something among these lines: [code] <?php if (isset($_POST['something'])) { $date = new \DateTime(); echo $date->format('d-m-Y H:i:s'); exit; } ?> <span class="the-time"></span> <script> function forTheLoveOfGodDontDoThis() { $.post('index.php', {something: 'yadayada'}, function(data) { $('.the-time').text(data); }); } setInterval(forTheLoveOfGodDontDoThis, 1000); </script> [/code] Badaboom, clock. Needless to say I have learned the errors of my ways.
I think AMD wins this round [cpp]WATTR void \ C(write_image,PT##_fsuf)(read_write IT##_ity i, IT##_##CT##_cty c, int l, IT##_##PT##_pty p) \ { \ C(PFX,C(store,C(PT##_ksuf,C(_lod,IT))))(LOWER_rw##IT(i), c, l, IT##_##PT##_parg); \ }[/cpp] I bet they wish OpenCL 1.2 had templates and function overloading about now
[QUOTE=Icedshot;52223148]I think AMD wins this round [cpp]WATTR void \ C(write_image,PT##_fsuf)(read_write IT##_ity i, IT##_##CT##_cty c, int l, IT##_##PT##_pty p) \ { \ C(PFX,C(store,C(PT##_ksuf,C(_lod,IT))))(LOWER_rw##IT(i), c, l, IT##_##PT##_parg); \ }[/cpp] I bet they wish OpenCL 1.2 had templates and function overloading about now[/QUOTE] [url=https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/basic_string.h]C++ STL code looks no less ugly though[/url]
The worst I can remember is casting a pointer to a __int64 (in c++) to pass it into a FORTRAN77 library, which would be passed through and back out into another C++ function and cast back into the pointer. The better option would have been to just rewrite the age old FORTRAN code, but that was forbidden because 'reasons'
[QUOTE=suXin;52143015]Interpret float as int because engine didn't allow me to send it via it's functions [code]#define WRITE_LONG (*g_engfuncs.pfnWriteLong) inline void WRITE_FLOAT( float value ) { WRITE_LONG( ( ( *( int * ) &value ) ) ); }[/code][/QUOTE] [QUOTE=cartman300;52143961]I would not consider this a bad line of code, it's just a clever little trick to convert to another same-sized structure type, send it and convert back on the other side. [editline]24th April 2017[/editline] There are, after all, no side effects and no other obvious/easier way to do it.[/QUOTE] Just in case anyone thinks this is actually a good idea, it breaks the aliasing rules in C/C++ as an int is not allowed to alias a float. It is therefore undefined behaviour The correct way to do this is: [cpp] union some_union { float f; int i; ///long? Not 100% sure what the original code is doing but you get the point }; some_union converter; converter.f = value; (*g_enginefuncs.pfnWriteLong)(converter.i); ///(or whatever) [/cpp] Which i believe is fine. You could do (char*)&value and write 4 bytes, as chars are allowed to alias any type If you want to do this in a c++ way, you can probably write [cpp] template<typename T, typename U> U convert(const T& t) { template<typename T, typename U> union some_union { T t; U u; }; some_union<T, U> u; u.t = t; return u.u; } [/cpp]
[QUOTE=DrTaxi;52223320][url=https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/basic_string.h]C++ STL code looks no less ugly though[/url][/QUOTE] Going to have to disagree, there's something especially ugly about macros tbh. Templates, especially in standard library implementations, aren't pretty but the meaning and usage of the code is at least somewhat clear. Good luck figuring out complex macros like that.
[QUOTE=paindoc;52228232]Going to have to disagree, there's something especially ugly about macros tbh. Templates, especially in standard library implementations, aren't pretty but the meaning and usage of the code is at least somewhat clear. Good luck figuring out complex macros like that.[/QUOTE] Disagree with the disagreement there, macros ain't pretty but there's a special place in readability hell for most STL code.
This is more than one line but here we go [CODE]#include "reg.h" /* 0C FF int 0xFF * CB retf */ char int_table[] = {0xCD, 0xFF, 0xCB}; typedef void (*intcall)(); inline void doInt(char intnum, REG* reg) { int_table[1] = intnum; __asm { mov ah, byte ptr [reg] mov al, byte ptr [reg+1] mov bh, byte ptr [reg+2] mov bl, byte ptr [reg+3] mov ch, byte ptr [reg+4] mov cl, byte ptr [reg+5] mov dh, byte ptr [reg+6] mov dl, byte ptr [reg+7] } ((intcall)int_table)(); } [/CODE] As you can tell it didn't actually work like it should
[QUOTE=JWki;52228408]Disagree with the disagreement there, macros ain't pretty but there's a special place in readability hell for most STL code.[/QUOTE] What I like about STL code is how they mix tabs and spaces into one big mess. [t]http://i.imgur.com/cfrllgO.jpg[/t]
Not the worst line of code, but lol at my comment here. Last time I touched this project was in September, because yknow after one month programming C++ getting really into intrinsics was a grand idea. [cpp] // Store this vec4's data into data void Store(__m128 _data) { // Somehow, just providing a pointer to the first // element causes the whole vec4 to be copied over _mm_store_ps(&data->m128_f32[0], _data); } [/cpp] [sp]I didn't understand how this worked until maybe 2 months ago, when I was playing with intrinsics again and finally understood "oh, it grabs a pointer to the first element BECAUSE THATS WHAT IT WANTS"[/sp]
[QUOTE=antianan;52230704]What I like about STL code is how they mix tabs and spaces into one big mess. [t]http://i.imgur.com/cfrllgO.jpg[/t][/QUOTE] I find if I'm looking at STL code something in my life has gone wrong. The difference though is that with templates you can [U]technically[/U] debug it, or at least step through it in fear and awe, whereas with the level of horror possible with macros you just have to hope it works, or know it inside-out. But again, by the time you're at the stage of needing to debug either then things aren't looking great for your immediate future. [QUOTE=paindoc;52231228]Not the worst line of code, but lol at my comment here. Last time I touched this project was in September, because yknow after one month programming C++ getting really into intrinsics was a grand idea. [cpp] // Store this vec4's data into data void Store(__m128 _data) { // Somehow, just providing a pointer to the first // element causes the whole vec4 to be copied over _mm_store_ps(&data->m128_f32[0], _data); } [/cpp] [sp]I didn't understand how this worked until maybe 2 months ago, when I was playing with intrinsics again and finally understood "oh, it grabs a pointer to the first element BECAUSE THATS WHAT IT WANTS"[/sp][/QUOTE] So it takes a pointer to a float and I'm assuming it then just happily copies the 16 bytes following that address. This kind of C++ code makes me shiver.
[QUOTE=Facepalm Man;52231472]I find if I'm looking at STL code something in my life has gone wrong. The difference though is that with templates you can [U]technically[/U] debug it, or at least step through it in fear and awe, whereas with the level of horror possible with macros you just have to hope it works, or know it inside-out. But again, by the time you're at the stage of needing to debug either then things aren't looking great for your immediate future. So it takes a pointer to a float and I'm assuming it then just happily copies the 16 bytes following that address. This kind of C++ code makes me shiver.[/QUOTE] I discovered how it actually worked because I was giving the "_mm_store_ps" function offsets into a std::vector of floats, which makes a lot more sense then what I was doing here. Here, I'm doing something worse: taking the first element of the __m128 intrinsic that belongs to this class, then getting the reference/pointer from that :v
[IMG]http://i.imgur.com/sana6xd.png[/IMG] I need to fix something in this.. I don't know what is this all.
Sorry, you need to Log In to post a reply to this thread.