[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / r / 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

Name
Options
Comment
Verification
4chan Pass users can bypass this verification. [Learn More] [Login]
File
  • Please read the Rules and FAQ before posting.
  • You may highlight syntax and preserve whitespace by using [code] tags.

08/21/20New boards added: /vrpg/, /vmg/, /vst/ and /vm/
05/04/17New trial board added: /bant/ - International/Random
10/04/16New board for 4chan Pass users: /vip/ - Very Important Posts
[Hide] [Show All]


[Advertise on 4chan]


File: file.png (6 KB, 1005x42)
6 KB
6 KB PNG
based or cringe?
>>
cringe but not sure of a better way to set a local non-mutable without writing a whole function
>>
>>107854904
I like to separate everything so it's easier to debug. Every time I cram like 5 lines of shit into 1 it bites me in the ass when it's gdb time and I can't print intermediaries.
> and {}
what does this do? doesn't it evaluate to 0 or something?
>>
>>107854904
Dude, don't mix declarations with logic.
>>
>>107855755
if file exists then solution = json of file gets converted into table
if file doesnt exist create the file and set solutions to an empty table
its done this way because the enviroment has no way of saving on close so you have to sync the table with the file manually anyway later in the code
>>
>>107854904
local function get_solutions(path)
local solutions = {}
if isfile(path) then
solutions = HttpService:JSONDecode(readfile(path))
else
makefile(path)
end

return solutions
end


Clean Code > Clever Code
>>
>>107854904
for that specific line of code? cringe. "clever" solutions are the hallmark of retarded devs that are the first to get the axe.
>>
In C this is just
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "cJSON.h"

cJSON *get_solutions(const char *path) {
FILE *fp = fopen(path, "r");
cJSON *solutions = NULL;

if (fp) {
fseek(fp, 0, SEEK_END);
long len = ftell(fp);
rewind(fp);

char *buffer = malloc(len + 1);
if (!buffer) {
fclose(fp);
return NULL;
}

fread(buffer, 1, len, fp);
buffer[len] = '\0';
fclose(fp);

solutions = cJSON_Parse(buffer);
free(buffer);

if (!solutions) {
solutions = cJSON_CreateObject();
}
} else {
// File does not exist: create it
fp = fopen(path, "w");
if (fp) {
fclose(fp);
}

solutions = cJSON_CreateObject();
}

return solutions;
}
>>
>>107854904
Cringe, it’s ugly and difficult to parse. It is very rare that it is a good idea to call functions that have side effects when evaluating logic.
>>
>>107855895
>making a function for something thats only used once
bloat
>>
File: file.png (7 KB, 576x103)
7 KB
7 KB PNG
>>107854904
>>107855755
>>107855895
>>107855916
>>107855960
/thread
>>
>>107854904
hmm
> (A() and B()) or (C() and {})
...
> if A() { B() } else {C(); {}}
are these equivalent, or does the first one run C if B fails or something?
>>
>>107856077
yeah
only a good idea when the language allows a function to be nested inside another function
>>
>>107856422
They are not equivalent. C indeed depends on the return of B.
In simple words, and chain returns either the first false value or the last value (if all of them are true), or chain returns the first true value or the last value
>>
>>107856422
why would they be?



[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.