[PATCHv2,1/3] util: Introduce STREQ and STRNEQ macros

Submitted by Radostin Stoyanov on Jan. 29, 2019, 1:11 p.m.

Details

Message ID 20190129131121.26728-1-rstoyanov1@gmail.com
State New
Series "Series without cover letter"
Headers show

Commit Message

Radostin Stoyanov Jan. 29, 2019, 1:11 p.m.
The function strcmp() is often used to check whether two strings are
equal. The macros STREQ and STRNEQ could be used instead to improve
readability of the source code.

- v2: surround the macro-variables with parenthesis (Thanks to Mike Rapoport)

Signed-off-by: Radostin Stoyanov <rstoyanov1@gmail.com>
---
 criu/include/util.h | 4 ++++
 1 file changed, 4 insertions(+)

Patch hide | download patch | download mbox

diff --git a/criu/include/util.h b/criu/include/util.h
index d9a1ff0f5..ea0c757bb 100644
--- a/criu/include/util.h
+++ b/criu/include/util.h
@@ -200,6 +200,10 @@  extern int read_fd_link(int lfd, char *buf, size_t size);
 
 int vaddr_to_pfn(int fd, unsigned long vaddr, u64 *pfn);
 
+/* String equality tests */
+#define STREQ(a, b) (strcmp((a), (b)) == 0)
+#define STRNEQ(a, b) (strcmp((a), (b)) != 0)
+
 /*
  * Check whether @str starts with @sub and report the
  * next character of @str in @end

Comments

Cyrill Gorcunov Jan. 29, 2019, 1:22 p.m.
On Tue, Jan 29, 2019 at 01:11:19PM +0000, Radostin Stoyanov wrote:
> The function strcmp() is often used to check whether two strings are
> equal. The macros STREQ and STRNEQ could be used instead to improve
> readability of the source code.
> 
> - v2: surround the macro-variables with parenthesis (Thanks to Mike Rapoport)

Don't want to disappoint you, but you know strcmp is well known library
function and I don't see any reason to add an additional layer of syntactic
sugar here. I simply don't get how it improves readability. Up to
Andrew though. Don't get it close this is just my opinion.
Radostin Stoyanov Jan. 29, 2019, 2:40 p.m.
On 29/01/2019 13:22, Cyrill Gorcunov wrote:
> On Tue, Jan 29, 2019 at 01:11:19PM +0000, Radostin Stoyanov wrote:
>> The function strcmp() is often used to check whether two strings are
>> equal. The macros STREQ and STRNEQ could be used instead to improve
>> readability of the source code.
>>
>> - v2: surround the macro-variables with parenthesis (Thanks to Mike Rapoport)
> Don't want to disappoint you, but you know strcmp is well known library
> function and I don't see any reason to add an additional layer of syntactic
> sugar here. I simply don't get how it improves readability. Up to
> Andrew though. Don't get it close this is just my opinion.
Thank you for the feedback. Well if it does not improve readability for
others, and only adds additional layer of syntactic sugar, then it would
be better not to introduce these macros.

Radostin
Cyrill Gorcunov Jan. 29, 2019, 2:46 p.m.
On Tue, Jan 29, 2019 at 02:40:14PM +0000, Radostin Stoyanov wrote:
> >> - v2: surround the macro-variables with parenthesis (Thanks to Mike Rapoport)
> > Don't want to disappoint you, but you know strcmp is well known library
> > function and I don't see any reason to add an additional layer of syntactic
> > sugar here. I simply don't get how it improves readability. Up to
> > Andrew though. Don't get it close this is just my opinion.
> Thank you for the feedback. Well if it does not improve readability for
> others, and only adds additional layer of syntactic sugar, then it would
> be better not to introduce these macros.

Up to you guys, I'm not strongly against.
Andrei Vagin Jan. 29, 2019, 5:24 p.m.
On Tue, Jan 29, 2019 at 01:11:19PM +0000, Radostin Stoyanov wrote:
> The function strcmp() is often used to check whether two strings are
> equal. The macros STREQ and STRNEQ could be used instead to improve
> readability of the source code.

It is an arguable question. If I read code, I know what strcmp does, but
STREQ is just a macros and I need to look what is inside of it.

And when a code contains a lot of such macroses it is hard to develop
something, especially when it is a new developer who are not familiar
with a code base.


> 
> - v2: surround the macro-variables with parenthesis (Thanks to Mike Rapoport)
> 
> Signed-off-by: Radostin Stoyanov <rstoyanov1@gmail.com>
> ---
>  criu/include/util.h | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/criu/include/util.h b/criu/include/util.h
> index d9a1ff0f5..ea0c757bb 100644
> --- a/criu/include/util.h
> +++ b/criu/include/util.h
> @@ -200,6 +200,10 @@ extern int read_fd_link(int lfd, char *buf, size_t size);
>  
>  int vaddr_to_pfn(int fd, unsigned long vaddr, u64 *pfn);
>  
> +/* String equality tests */
> +#define STREQ(a, b) (strcmp((a), (b)) == 0)
> +#define STRNEQ(a, b) (strcmp((a), (b)) != 0)
> +
>  /*
>   * Check whether @str starts with @sub and report the
>   * next character of @str in @end
> -- 
> 2.20.1
> 
> _______________________________________________
> CRIU mailing list
> CRIU@openvz.org
> https://lists.openvz.org/mailman/listinfo/criu
Dmitry Safonov Jan. 30, 2019, 3:01 p.m.
On 1/29/19 2:46 PM, Cyrill Gorcunov wrote:
> On Tue, Jan 29, 2019 at 02:40:14PM +0000, Radostin Stoyanov wrote:
>>>> - v2: surround the macro-variables with parenthesis (Thanks to Mike Rapoport)
>>> Don't want to disappoint you, but you know strcmp is well known library
>>> function and I don't see any reason to add an additional layer of syntactic
>>> sugar here. I simply don't get how it improves readability. Up to
>>> Andrew though. Don't get it close this is just my opinion.
>> Thank you for the feedback. Well if it does not improve readability for
>> others, and only adds additional layer of syntactic sugar, then it would
>> be better not to introduce these macros.
> 
> Up to you guys, I'm not strongly against.

+1
not strongly against, but don't see the purpose of hiding strcmp().

Moreover, I believe while writing new code we can forget about existence
of this macro and in result we'll have both variants.