coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] refactoring: users: Remove useless `trimmed_name` variable


From: Alireza Arzehgar
Subject: [PATCH] refactoring: users: Remove useless `trimmed_name` variable
Date: Tue, 22 Nov 2022 02:07:19 +0330

Dear All,

When I was reading users.c source code,  on the `list_entries_users`
function, I saw that some codes can be removed and we can refactor this
codes to simpler and better code.

I think this code is useless:

       if (IS_USER_PROCESS (this))
        {
          char *trimmed_name;

          trimmed_name = extract_trimmed_name (this);

          u[n_entries] = trimmed_name;
          ++n_entries;
        }

We can remove 5 lines of code and just put following code instead of
removed codes:
        if (IS_USER_PROCESS (this))
            u[n_entries++] = extract_trimmed_name (this);

I think replacing previous code can have the following benefits: (a) Less
bulky code. (b) Less binary size.

For less binary size I examined and analyzed binaries before and after
refactoring.

$ wc original-users refactored-users
   951   4295 164400 original-users
   949   4278 164328 refactored-users

In this case binary size was reduced. But the following test shows me that
the text portion size will increase on the refactored version.

$ size original-users refactored-users
   text      data    bss     dec      hex       filename
  29495   1272    440  31207   79e7 original-users
  29527   1272    440  31239   7a07 refactored-users

Beats,
Alireza

-----------------------------------------------------------------------
From: alireza <alirezaarzehgar82@gmail.com>
Date: Tue, 22 Nov 2022 01:19:54 +0330
Subject: [PATCH] refactoring: users: Remove useless `trimmed_name` variable

On the `list_entries_users()` function and condition for checking
`IS_USER_PROCESS()`, the `trimmed_name` is unnecessary.
After removing additional unnecessary codes, following steps will summarize
to
one line of clean and clear code:
 - declaring a variable.
 - defining a variable with output of a function.
 - assign a variable to the index of an array.
 - `index++`
These steps can be summarized into one line of code. assign output of
function to `index++` of array.

Signed-off-by: alireza <alirezaarzehgar82@gmail.com>
---
 src/users.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)

diff --git a/src/users.c b/src/users.c
index 6f88b7526..fde03f3cb 100644
--- a/src/users.c
+++ b/src/users.c
@@ -53,14 +53,7 @@ list_entries_users (size_t n, const STRUCT_UTMP *this)
   while (n--)
     {
       if (IS_USER_PROCESS (this))
-        {
-          char *trimmed_name;
-
-          trimmed_name = extract_trimmed_name (this);
-
-          u[n_entries] = trimmed_name;
-          ++n_entries;
-        }
+          u[n_entries++] = extract_trimmed_name (this);
       this++;
     }

-- 
2.38.1


reply via email to

[Prev in Thread] Current Thread [Next in Thread]