Skip to content

Instantly share code, notes, and snippets.

void core1_task(void *ptr)
{
int i = ((thread_args *) ptr)->input;
((thread_args *) ptr)->output = fib(i);
xSemaphoreGive(xSemaphore);
vTaskDelete(NULL);
}
int test_dual_core(int num)
{
int fib(int n)
{
if (n < 2) return n;
else {
int x = fib(n - 1);
int y = fib(n - 2);
return x + y;
}
}
/* GOOD */
// The state is derived by what is passed into the function
function int addOne(int number)
{
return number + 1;
}
/* BAD */
@tuanpmt
tuanpmt / CONDITION BREAKDOWN.c
Created May 2, 2022 10:28
CONDITION BREAKDOWN
/* BAD */
if ((food.status == 'available'
&& user.status == 'active'
&& user.money > food.price
&& shipper.status == 'free')
|| (user.sos && user.call911
&& food.status == 'available'
&& shipper.status == 'free')
) {
@tuanpmt
tuanpmt / USE SEARCHABLE NAMES.c
Created May 2, 2022 10:27
USE SEARCHABLE NAMES
/* BAD */
for (int i = 0; i < 5; i++)
{
s += (t[i] * 4) / 5;
}
/* GOOD */
int realDaysPerIdealDay = 4;
const int WORK_DAYS_PER_WEEK = 5;
@tuanpmt
tuanpmt / USE PRONOUNCEABLE NAMES.c
Created May 2, 2022 10:26
USE PRONOUNCEABLE NAMES
typedef struct {
date_time_t playhhmmdd; /* BAD */
} mp3_audio_data_t;
typedef struct {
date_time_t play_time; /* GOOD */
} mp3_audio_data_t;
@tuanpmt
tuanpmt / REVEALING NAMES.c
Created May 2, 2022 10:25
REVEALING NAMES
/*
Bad: We have a bunch of get_data, and after a few months,
we don't know which get_data is for what
*/
data_t *get_data();
/*
Good
*/
#define AWAIT(a) if (a != OK) continue;
status_t _connect(state_t *state)
{
if (state >= CONNECTED) {
return OK;
}
// do connect, return FAIL if failed
state = CONNECTED;
@tuanpmt
tuanpmt / kss.c
Created May 2, 2022 08:42
Keep it simple, stupid
//for understanding weekday2 there are more details to think about
const char *weekday2(int dayOfWeek)
{
if ((dayOfWeek < 1) || (dayOfWeek > 7)) {
ESP_LOGE(TAG, "dayOfWeek must be in range 1..7");
return NULL;
}
const char *weekdays = {
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
@tuanpmt
tuanpmt / format.java
Last active May 2, 2022 08:17
formating code
//DON'T
public void DoSomething()
{
for (var i = 0; i < 1000; i++)
{
var productCode = $"PRC000{i}";
//...implementation
}
}