Date: 30 Jun 2001 21:26:59 -0000 From: Mike Meyer <mwm@mired.org> To: FreeBSD-gnats-submit@freebsd.org Subject: docs/28555: [PATCH] style(9) isn't explicit about booleans for testing. Message-ID: <20010630212659.72819.qmail@guru.mired.org>
next in thread | raw e-mail | index | archive | help
>Number: 28555 >Category: docs >Synopsis: [PATCH] style(9) isn't explicit about booleans for testing. >Confidential: no >Severity: non-critical >Priority: medium >Responsible: freebsd-doc >State: open >Quarter: >Keywords: >Date-Required: >Class: change-request >Submitter-Id: current-users >Arrival-Date: Sat Jun 30 14:30:01 PDT 2001 >Closed-Date: >Last-Modified: >Originator: Mike Meyer >Release: FreeBSD 4.3-STABLE i386 >Organization: Meyer Consulting >Environment: System: FreeBSD guru.mired.org 4.3-STABLE FreeBSD 4.3-STABLE #22: Sun Jun 24 13:15:49 CDT 2001 mwm@guru.mired.org:/sharetmp/obj/usr/src/sys/GURU i386 >Description: The style(9) page says not to use ! for testing values unless the value is a boolean. It also says to test pointers against NULL. This leaves open the question of how other values that aren't booleans should be tested. >How-To-Repeat: Read the man page to try and decide if you should write "if (x)" or if (x != 0). >Fix: Apply the attached page to the style(9) man page. --- /usr/src/share/man/man9/style.9 Fri May 18 07:27:37 2001 +++ style.9 Sat Jun 30 16:23:34 2001 @@ -449,14 +449,37 @@ !(p = f()) .Ed .Pp -Don't use '!' for tests unless it's a boolean, e.g. use +For tests, always compare the value to the appropriate 0 instead of +checking it directly, unless the value is a boolean. +For pointers, use: +.Bd -literal +if (p != NULL) +.Ed +.Pp +not +.PP +.Bd -literal +if (!p) +.Ed +.Pp +For other values, use: .Bd -literal if (*p == '\e0') .Ed .Pp not .Bd -literal -if (!*p) +if (*p) +.Ed +.Pp +unless the value is a boolean. In that case, use: +.Bd -literal +if (p) +.Ed +.Pp +and +.Bd -literal +if (!p) .Ed .Pp Routines returning void * should not have their return values cast >Release-Note: >Audit-Trail: >Unformatted: To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-doc" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010630212659.72819.qmail>