[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / s / t / u / v / vg / vm / vmg / vr / vrpg / vst / w / wg] [i / ic] [r9k / s4s / vip] [cm / hm / lgbt / y] [3 / aco / adv / an / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / pw / qst / sci / soc / sp / tg / toy / trv / tv / vp / vt / wsg / wsr / x / xs] [Settings] [Search] [Mobile] [Home]
Board
Settings Mobile Home
/g/ - Technology


Thread archived.
You cannot reply anymore.


[Advertise on 4chan]


File: 1787028968146882.png (737 KB, 848x960)
737 KB PNG
i'm just going to say it. goto label functionality is the single most important feature a language can have. if your language doesn't have it, then it's SHIT. completely unusable
>oh but anon, if you design your code well, you can avoid having to use gotos
WRONG!
there are some problems where the use of goto statements, yes even backwards ones, is completely inescapable. the only alternative is mountains of duplicate code and horrible spaghetti
side note, i do wish they had ternary functionality compatible with goto statements in C, but the issue is mostly obviated by the use of a simple macro, so i won't complain too much
>>
>>109595946
goto the base of my cock OP
>>
>I need it
function body too long, try again with better abstractions.
At least post an example of your claim so people can show you how its done. Not me though, I need to go smoke weed and implement operating systems instead of procrastinating.
>>
>>109595946
I'm so fucking glad you're not my coworker
>>
>>109595946
Let me guess, you're heading into your second year of CS?
>>
>>109595990
oh, i'm so glad you asked! say you're working on data transmission and need to handle unmarshalling objects from the wire. however, the structure of these objects has changed over time (say, across versions). not only have fields been added and removed, they've also been reordered. how would you handle this without the use of goto statements? as stated in the OP, one option is shitloads of duplicate code (e.g., one function per version), but that quickly explodes and becomes unmaintainable as the number of versions increases
>>
>>109596036
you use polymorphism and you put the common functionality in a private method in the base class, so that the common logic is not duplicated
>>
Only valid use of goto are noreturn and twice returning, both of which can be achieved better with more modern and vetter language constructs
>>
>>109596036
where da code
>>
>>109596061
not sure that would help. you still end up duplicating code. fundamentally, the issue is that you have some number of functions that need to be called (A, B, C, ...) in varying orders. so even with polymorphism, you end up needing to have a different method per version, because each version will need its own [A, B, C] vs [C, B] vs [A], etc.
with goto statements, you end up with something like
...
COND_GOTO(cond, do_a, do_c);
do_a:
A();
COND_GOTO(cond, do_b, end);
do_b:
B();
COND_GOTO(cond, do_c, end);
do_c:
C();
COND_GOTO(cond, do_b, end);
end:
...

>>109596101
see above for a toy example
>>
>>109596142
I guess it heavily depends on what your priorities are. There isn't a single attribute that determines the quality of your code and you have to make compromises between them depending on what makes sense for your project. The main issue with goto statements is that they make code unreadable af, and it's often preferrable to have a few more lines of code that do the same job in a more readable way. I like the polymorphism approach because it's easily expandable with new versions of the object, it's readable and if done well you're not really duplicating code (though you will be writing more of it than with your approach). If you end up having to differentiate between versions of the object in other parts of the codebase as well, you also avoid duplicated checks for the version everywhere
>>
>>109596036
from chatgpt:
The main idea is to avoid writing a separate unmarshalling function for every version. Instead, separate the logic for reading data from the description of each version's layout.

Ideally, the wire format should identify fields by stable IDs rather than relying on their order. Then one generic decoder can read each field and place it in the appropriate object member. New or unknown fields can be skipped, and missing fields can use defaults.

If you're stuck with old positional formats, keep a small schema for each version that describes the order, types, additions, and removals. Use one generic decoder that follows that schema.

For more complicated changes, decode each version into a common intermediate representation and then convert that representation into the current object.

The important point is that the version differences should be represented as data (schemas/tables), not as separate copies of the decoding algorithm. If the old format contains no version or field identifiers and two versions are indistinguishable from their bytes, however, there is no general solution—the protocol is inherently ambiguous.

seems reasonable
>>
>>109596469
meat proxy

>>109595946
>side note, i do wish they had ternary functionality compatible with goto statements in C,
you can use the labels as values with gcc
https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html
>>
>>109596553
sure snailcat but why would that not be a good solution? you have one generic decoder function you pass a config to alongside your payload
seems like a much more straightforward solution than resorting to gotos which are already widely discouraged for obfuscating the control flow of the program
>>
>>109596680
ask your shatbot, viberetard
every time you ask for solutions, ask then to evaluate said solutions adversarially so you have a full image
its prompting 101 and you fail even at that, lamao
>>
>>109595946
Basically you can Code in C
strictly
Using GOTO
and Make code Clean
and extremely fast ...

They say not to USE
GOTO from simple Reason

Rest of C language is simply redundant if you know how to use GOTO properly !
>>
>>109596718
im not a proompter, the entire point was putting the bare minimum effort and still coming up with a better solution than your convoluted goto autism, but i accept your concession either way
>>
>>109596936
>im not a proompter, the entire point was putting the bare minimum effort and still coming up with a better solution
yeah and you failed, miserably
>>
>>109596959
sure
explain
>>
>>109596758
just use whatever you want, as long as it's not bloated updooter securitard gargabe it's fine, modern software is an abomination either way
>>
>>109595946
Goto is fine but I wouldn't trust most people to use it.
>>
>>109596980
ah, didnt exactly read what the reply chain is about

id actually make a bunch of functions, one for each type of field
and a switch statement that takes the version in, or rather the value of an enum representing versions
in each case, id put the functions needed to decode the package
to avoid unnecessary autism, feed each function a char ** to current position in the raw stream, and each function moves the underlying pointer internally
output goes into a generic struct.

no fukken generic decoder with internal representations whatnot unless you have 10.000 versions and instruction cache pressure becomes a thing

so semi concession if you will
id drop the gotos, they make no sense here
but the shatbot's solution is fucking autistic
>>
>>109596980
>>109597078
>functions
or even macros could do, to avoid function calls although i have actually enforced force inline with gcc,
and the compiler might have an opinon about what to inline and what to leave out
im not sure how the actual field decoding would look like, if theres additional ops that go into it or its just a cast, and moving a pointer
if its the latter i would probably use macros, and benchmark against force inlines if performance is a concern
i could even use non inlined wrapper functions, which in turn would contain the macros/or force inlined funcs, it all depends on the actual code to turn the bytes into data, the number of versions, the number of fields, and the point to which im supposed to optimize the thing
>>
>>109597168
>i could even use non inlined wrapper functions, which in turn would contain the macros/or force inlined funcs, it all depends on the actual code to turn the bytes into data, the number of versions, the number of fields, and the point to which im supposed to optimize the thing...
...and on the whims of the compiler, and the preferences of the cpu

like recently i saw that gcc likes to put force inlines behind jumps
i thought thats retarded so i replaced the relevant instructions verbatim, where they belong, thinking id shave some cycles off the runtime
turns out that no, i was the retard and the thing ran quite slower, and i have no idea why
so yeah, the final form depends on the degree of optimization needed, and on the specificities of the interaction bw the code at hand and the hardware
>>
>>109597071
that's a fair assessment
>>
The goto criticised in Dijkstra's "Goto Considered Harmful" paper was a completely unrestricted goto; you could jump from anywhere to anywhere in your program. Most higher level (than assembly) programming languages that have goto use a more restricted version. Even C's goto is limited to being in the same function.
>>
>>109601364
Most importantly, it was Fortran's goto, which used line numbers offsets, and they even had a three way goto operator.
>>
Most valid use of goto can be achieved by using a simple break:
int foobar(const char *file)
{
int status = 0;
int fd;
struct stat sb;
void *data;

do {
if ((fd = open(file, O_RDONLY)) == -1) break;
status = 1;
if (fstat(fd, &sb) == -1) break;

if ((data = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_ANON | MAP_PRIVATE, fd, 0)) == NULL)
break;
status = 2;

// ...
} while (0);

switch (status)
{
case 2:
munmap(data, (size_t)sb.st_size);
case 1:
close(fd);
default: break;
}
return status == 0;
}
>>
>>109601499
Now how do you break out of two or three levels?
Break with a label would fix it cleanly of course.
>>
>>109603161
C is famously missing that feature. You have to implement some sort of control flow flattening if you want that. At least rust lets you break out of nested loops easily.
>>
>>109596142
my nigger you need a trampoline
>>
>>109595946
you know callbacks exist for a reason right



[Advertise on 4chan]

Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.