Deutsch English Français Italiano |
<vb7dve$3d5mv$2@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Thiago Adams <thiago.adams@gmail.com> Newsgroups: comp.lang.c Subject: Re: Code guidelines Date: Tue, 3 Sep 2024 13:37:02 -0300 Organization: A noiseless patient Spider Lines: 93 Message-ID: <vb7dve$3d5mv$2@dont-email.me> References: <vb6v1t$3b5mb$1@dont-email.me> <vb726n$3b4rq$1@dont-email.me> <vb736j$3b5mb$2@dont-email.me> <vb75g9$3bntp$1@dont-email.me> <vb77tn$3bu07$3@dont-email.me> <vb7d6l$3d5mv$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Tue, 03 Sep 2024 18:37:03 +0200 (CEST) Injection-Info: dont-email.me; posting-host="15b67f073a3e4783414ee664431ab3cb"; logging-data="3577567"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX197s53q24fHjZX/LPdrLOZ4MHF+FcVfISg=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:dP1C2KBOfItQ4+45bAISyIrMyJY= Content-Language: en-US In-Reply-To: <vb7d6l$3d5mv$1@dont-email.me> Bytes: 3106 On 03/09/2024 13:23, Thiago Adams wrote: .... > > I will give a sample > > In my code I have > > if (obj->member1 && > obj->member1->member2 && > obj->member1->member2->member3) > { > > } > > The contract is > > * obj->member1 CAN be null > * obj->member1->member2 CANNOT be null > * obj->member1->member2->member3 CAN be null > > So I can write just > > if (obj->member1 && > obj->member1->member2->member3) > { > > } > > > but...maybe, is better to be a little redundant here? > I think I prefer to leave "obj->member1->member2 && " even if I know > it should not be null. > > if (obj->member1 && > obj->member1->member2 && > obj->member1->member2->member3) > { > > } > > I also have a interesting sample with linked list struct item { char* title; struct item* next; //can be null }; struct list { struct item* head; //can be null struct item* tail; //can be null }; void list_push_back(struct list* list, struct item* p_item) { if (list->head == NULL) { list->head = p_item; //tail also should be null } else { //When the head is null tail is also null //if head is not null then tail is not null //So tail is not null here.. assert works as assume assert(list->tail != nullptr); //next can be null or not null..however for the tail node //next IS/MUST BE null assert(list->tail->next == nullptr); list->tail->next = p_item; } list->tail = p_item; } assert in this case works as "assume" and "check". For instance, We assume "assert(list->tail != nullptr);" because we access it directly here list->tail->next = p_item; We also assume "assert(list->tail->next == nullptr)" otherwise we could have a memory leak overriding next.