How do you understand cryptic code?
-
honey the codewitch wrote:
Do you do this?
Rewrite.
Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
I usually can't understand it enough to rewrite it without porting it first, but that's me. :)
Real programmers use butterflies
-
I'm working on the rasterizer portion of my truetype code, for which I found some public domain code that partially works - the parts I need anyway. it's really hard to follow C code, so I'm porting it to C# before backporting it to C++ so that I can really understand it. This isn't the only time I've done that. In fact, I often find myself going this route when coding something based on a codebase I don't understand at first. Do you do this? More I guess I'm curious how y'all go about decoding code that is either more complicated than you can readily understand, or too ugly to readily understand? I port. :)
Real programmers use butterflies
Understanding an application part - what does this code do (or tries to do). Running many times on different use cases. Adding a lot of logging/trace code and learning a logs in different use cases. Logging depends on the platform - from OutputDebugString or log file to UART-based printf on embedded device. Trying to make some simple changes and see what happens. Running and reading a logs again. And finally, all this doesn't help to understand 10 lines of code written by real C++ guru - template template parameters + traits + 10-20 crazy Boost base classes + SFINAE + RAII + design patters etc. Such code goes to Recycle Bin - it never works. Anyway, I will be in the Recycle Bin soon, so it doesn't matter...
-
Understanding an application part - what does this code do (or tries to do). Running many times on different use cases. Adding a lot of logging/trace code and learning a logs in different use cases. Logging depends on the platform - from OutputDebugString or log file to UART-based printf on embedded device. Trying to make some simple changes and see what happens. Running and reading a logs again. And finally, all this doesn't help to understand 10 lines of code written by real C++ guru - template template parameters + traits + 10-20 crazy Boost base classes + SFINAE + RAII + design patters etc. Such code goes to Recycle Bin - it never works. Anyway, I will be in the Recycle Bin soon, so it doesn't matter...
11917640 Member wrote:
template template parameters + traits + 10-20 crazy Boost base classes
Yeah. I do a lot of generic programming (GP) instead of OOP in C++ so templates are par for the course. However, I dislike boost because of the level of abstraction. I already sideeye the STL for all the allocations it does, and I can't use it when targeting IoT because the Arduino framework doesn't make it available, probably because it can target 8 bit platforms with 8kB of RAM and 256kB of program space. So I tend to agree. Unfortunately I find myself producing code (sans boost and often sans the STL) like that in order to get the compiler to do what I need. I wish it wasn't necessary because the code becomes so abstract it's really hard to follow.
Real programmers use butterflies
-
I'm working on the rasterizer portion of my truetype code, for which I found some public domain code that partially works - the parts I need anyway. it's really hard to follow C code, so I'm porting it to C# before backporting it to C++ so that I can really understand it. This isn't the only time I've done that. In fact, I often find myself going this route when coding something based on a codebase I don't understand at first. Do you do this? More I guess I'm curious how y'all go about decoding code that is either more complicated than you can readily understand, or too ugly to readily understand? I port. :)
Real programmers use butterflies
Quote:
it's really hard to follow C code, so I'm porting it to C# before backporting it to C++ so that I can really understand it.
I try to port it to
C
code. :) I'm doing a lot of this kind of work, with assembly-likeC
code."In testa che avete, Signor di Ceprano?" -- Rigoletto
-
I'm working on the rasterizer portion of my truetype code, for which I found some public domain code that partially works - the parts I need anyway. it's really hard to follow C code, so I'm porting it to C# before backporting it to C++ so that I can really understand it. This isn't the only time I've done that. In fact, I often find myself going this route when coding something based on a codebase I don't understand at first. Do you do this? More I guess I'm curious how y'all go about decoding code that is either more complicated than you can readily understand, or too ugly to readily understand? I port. :)
Real programmers use butterflies
honey the codewitch wrote:
I'm porting it to C# before backporting it to C++
That's an interesting way of addressing the understanding issue... I still think it is easier to port direct to C++, since C is probably closer to C++ than C#, but then, I do not speak C# much-
-
I'm working on the rasterizer portion of my truetype code, for which I found some public domain code that partially works - the parts I need anyway. it's really hard to follow C code, so I'm porting it to C# before backporting it to C++ so that I can really understand it. This isn't the only time I've done that. In fact, I often find myself going this route when coding something based on a codebase I don't understand at first. Do you do this? More I guess I'm curious how y'all go about decoding code that is either more complicated than you can readily understand, or too ugly to readily understand? I port. :)
Real programmers use butterflies
in my head i port c# to c++ and then to c, so i can really understand
-
I'm working on the rasterizer portion of my truetype code, for which I found some public domain code that partially works - the parts I need anyway. it's really hard to follow C code, so I'm porting it to C# before backporting it to C++ so that I can really understand it. This isn't the only time I've done that. In fact, I often find myself going this route when coding something based on a codebase I don't understand at first. Do you do this? More I guess I'm curious how y'all go about decoding code that is either more complicated than you can readily understand, or too ugly to readily understand? I port. :)
Real programmers use butterflies
I just wish to understand how C is more cryptic than C++?
-
I just wish to understand how C is more cryptic than C++?
I never said it was. :confused:
Real programmers use butterflies
-
I never said it was. :confused:
Real programmers use butterflies
No, you didn't, it just seemed that way to me. I wouldn't even bother with C# in between, but that's a personal opinion, still trying to understand the logic though.
-
I've never translated code to another language to help understand it, but I've reformatted it as a way to study it line by line.
Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing.Greg Utas wrote:
I've never translated code to another language to help understand it, but I've reformatted it as a way to study it line by line.
This is what I do. If I don't understand the code, I have no idea how I'd translate it.
-
No, you didn't, it just seemed that way to me. I wouldn't even bother with C# in between, but that's a personal opinion, still trying to understand the logic though.
The reason I move it between unmanaged and managed code is it forces me to restructure it. I can't just get lazy and copypasta. In the process of restructuring it, I grok it's machinations. Furthermore, C# has a library for pretty much everything, so no matter what I'm doing in C++, there is pretty going to be the equiv in the .NET framework that I can rely on, so I can seal it off there and I don't have to import code like say, the code to do an HTTP request from C++, if that's not directly what I'm working on. I hope what I just wrote makes sense! :)
Real programmers use butterflies
-
The reason I move it between unmanaged and managed code is it forces me to restructure it. I can't just get lazy and copypasta. In the process of restructuring it, I grok it's machinations. Furthermore, C# has a library for pretty much everything, so no matter what I'm doing in C++, there is pretty going to be the equiv in the .NET framework that I can rely on, so I can seal it off there and I don't have to import code like say, the code to do an HTTP request from C++, if that's not directly what I'm working on. I hope what I just wrote makes sense! :)
Real programmers use butterflies
Ok, thanks, my bad :) I don't even think of things such as .NET, it doesn't exist in my current life. If I can't find a suitable library, I have to create the functionality. It's just a completely different environment.
-
I'm working on the rasterizer portion of my truetype code, for which I found some public domain code that partially works - the parts I need anyway. it's really hard to follow C code, so I'm porting it to C# before backporting it to C++ so that I can really understand it. This isn't the only time I've done that. In fact, I often find myself going this route when coding something based on a codebase I don't understand at first. Do you do this? More I guess I'm curious how y'all go about decoding code that is either more complicated than you can readily understand, or too ugly to readily understand? I port. :)
Real programmers use butterflies
Yes. Do it often.
-
Ok, thanks, my bad :) I don't even think of things such as .NET, it doesn't exist in my current life. If I can't find a suitable library, I have to create the functionality. It's just a completely different environment.
Besides, "cryptic" usually is a matter of who wrote it and how, not what language it happens to be in. Some of the most cryptic code I have run across was SQL written as a single line. Without parsing it, I would never have figured it out.
-
Besides, "cryptic" usually is a matter of who wrote it and how, not what language it happens to be in. Some of the most cryptic code I have run across was SQL written as a single line. Without parsing it, I would never have figured it out.
Yeah. I'm not trying to imply it's a particular language that's cryptic. It's all a matter of the code.
Real programmers use butterflies
-
Ok, thanks, my bad :) I don't even think of things such as .NET, it doesn't exist in my current life. If I can't find a suitable library, I have to create the functionality. It's just a completely different environment.
UnchainedZA wrote:
It's just a completely different environment.
That's exactly why I do it. :-D
Real programmers use butterflies
-
Greg Utas wrote:
I've never translated code to another language to help understand it, but I've reformatted it as a way to study it line by line.
This is what I do. If I don't understand the code, I have no idea how I'd translate it.
BryanFazekas wrote:
This is what I do. If I don't understand the code, I have no idea how I'd translate it.
If you don't understand a block of code at all, you can't translate it, but translating a code section that you think you understand into another language can be a very good test of whether you really do, since it forces you to concentrate on details of the implementation that you might otherwise skip over.
-
I'm working on the rasterizer portion of my truetype code, for which I found some public domain code that partially works - the parts I need anyway. it's really hard to follow C code, so I'm porting it to C# before backporting it to C++ so that I can really understand it. This isn't the only time I've done that. In fact, I often find myself going this route when coding something based on a codebase I don't understand at first. Do you do this? More I guess I'm curious how y'all go about decoding code that is either more complicated than you can readily understand, or too ugly to readily understand? I port. :)
Real programmers use butterflies
I have this precise issue in a legacy project I have inherited. The author wrote the code in a fashion that virtually guaranteed he was the only possible maintainer [approx. 4,000 words of obscenity-laden rant omitted]. On occasion I have copied the relevant source files to another folder and the reformatted and refactored mercilessly. The reformatting is to correct layout issues since his brace style and tabs weren't consistent (I've found tabs of 2, 3, 4, and 8 with tab characters). The refactoring is to give values meaningful names. The reworked source code lets me understand how the original works when I need to make changes or understand how a feature works. Given the fragility of this towering pile of excreta, I don't use the reformatted code for anything other than my own understanding.
Software Zen:
delete this;
-
I have this precise issue in a legacy project I have inherited. The author wrote the code in a fashion that virtually guaranteed he was the only possible maintainer [approx. 4,000 words of obscenity-laden rant omitted]. On occasion I have copied the relevant source files to another folder and the reformatted and refactored mercilessly. The reformatting is to correct layout issues since his brace style and tabs weren't consistent (I've found tabs of 2, 3, 4, and 8 with tab characters). The refactoring is to give values meaningful names. The reworked source code lets me understand how the original works when I need to make changes or understand how a feature works. Given the fragility of this towering pile of excreta, I don't use the reformatted code for anything other than my own understanding.
Software Zen:
delete this;
I have only one thing to say in response, but it's a mouthful:
static int stbtt__run_charstring(const stbtt_fontinfo *info, int glyph_index, stbtt__csctx *c)
{
int in_header = 1, maskbits = 0, subr_stack_height = 0, sp = 0, v, i, b0;
int has_subrs = 0, clear_stack;
float s[48];
stbtt__buf subr_stack[10], subrs = info->subrs, b;
float f;#define STBTT__CSERR(s) (0)
// this currently ignores the initial width value, which isn't needed if we have hmtx
b = stbtt__cff_index_get(info->charstrings, glyph_index);
while (b.cursor < b.size) {
i = 0;
clear_stack = 1;
b0 = stbtt__buf_get8(&b);
switch (b0) {
// @TODO implement hinting
case 0x13: // hintmask
case 0x14: // cntrmask
if (in_header)
maskbits += (sp / 2); // implicit "vstem"
in_header = 0;
stbtt__buf_skip(&b, (maskbits + 7) / 8);
break;case 0x01: // hstem case 0x03: // vstem case 0x12: // hstemhm case 0x17: // vstemhm maskbits += (sp / 2); break; case 0x15: // rmoveto in\_header = 0; if (sp < 2) return STBTT\_\_CSERR("rmoveto stack"); stbtt\_\_csctx\_rmove\_to(c, s\[sp-2\], s\[sp-1\]); break; case 0x04: // vmoveto in\_header = 0; if (sp < 1) return STBTT\_\_CSERR("vmoveto stack"); stbtt\_\_csctx\_rmove\_to(c, 0, s\[sp-1\]); break; case 0x16: // hmoveto in\_header = 0; if (sp < 1) return STBTT\_\_CSERR("hmoveto stack"); stbtt\_\_csctx\_rmove\_to(c, s\[sp-1\], 0); break; case 0x05: // rlineto if (sp < 2) return STBTT\_\_CSERR("rlineto stack"); for (; i + 1 < sp; i += 2) stbtt\_\_csctx\_rline\_to(c, s\[i\], s\[i+1\]); break; // hlineto/vlineto and vhcurveto/hvcurveto alternate horizontal and vertical // starting from a different place. case 0x07: // vlineto if (sp < 1) return STBTT\_\_CSERR("vlineto stack"); goto vlineto; case 0x06: // hlineto if (sp < 1) return STBTT\_\_CSERR("hlineto stack"); for (;;) { if (i >= sp) break; stbtt\_\_csctx\_rline\_to(c, s\[i\], 0); i++; vlineto: if (i >= sp) break; stbtt\_\_csctx\_rline\_to(c, 0, s\[i\]); i++; } break; case 0x1F: // hvcurveto if (sp < 4) return STBTT\_\_CSERR("hvcurveto stack");
-
I'm working on the rasterizer portion of my truetype code, for which I found some public domain code that partially works - the parts I need anyway. it's really hard to follow C code, so I'm porting it to C# before backporting it to C++ so that I can really understand it. This isn't the only time I've done that. In fact, I often find myself going this route when coding something based on a codebase I don't understand at first. Do you do this? More I guess I'm curious how y'all go about decoding code that is either more complicated than you can readily understand, or too ugly to readily understand? I port. :)
Real programmers use butterflies
I either write code analysis report, where I divide code into sections and write section by section, what I think it does, then verify with debugger, or I create a good old flow diagram, if it is really cryptic (even for OO this will work for extracting actual algorithms, if you can identify sequence first).