Sponsored Content
Top Forums UNIX for Advanced & Expert Users Detecting unused variables... Post 302973545 by bakunin on Wednesday 18th of May 2016 02:23:44 PM
Old 05-18-2016
Quote:
Originally Posted by wisecracker
However, apart from writing code to do the task, is there a switch to to check which variables have been allocated but have not been used in the remainder of the code?
You already have been given very good advice here, so there is little for me to add - which is why i can as well quit pretending to be helpful and start to outright annoy you:

i think you tackle this from the wrong side alltogether!

Every software engineer learns about structured programming and its values and writing reusable pieces of code, yada, yada. But as every experienced software engineer can tell you structurisation - that is, breaking down the code to simple functions - is not only about reusing pieces of code and all the other tenets. One of its greatest benefits is that it breaks down long parts of the code into small manageable "chapters".

A former colleague of mine, principal senior coder in a large software project, even opined that a function - any function - that is longer than a screen full (he uses the same 80x40 xterm windows i prefer) should be broken down into parts, if not for anything else than for clarity of reading. Now, i think this strict stance is a bit of throwing the baby out with the bath water, but i can understand his sentiment. I try to apply this to my shell scripting all the time and - apart from a few exceptions, like commandline handling in scripts with many options - my functions are mostly fitting on one page of the 80x40 xterm i prefer.

Now, shell programming allows for a lot of bad behavior: undeclared variables made out of thin air, no real distinction between locals and globals and so on. I try to always work as strict as possible, meaning:

if i use a variable, i write a "typeset"-statement declaring it, using "typeset -i" if i intend to use it as an integer.

I admit publicly here that i am a devout user of Hungarian Style Notation, adapted to my needs as a shell programmer: i use, for instance, "f" as a prefix for file/directory names, even if there is no real datatype of a "path", whereas strings that can contain whatever are prefixed "ch" ("char" obviously). I also use "l" for switches, even though they are integers but should only contain 0 or 1. But it doesn't matter so much what you use: be consistent with yourself! If you use UPPERCASE for variables (personally i hate that, but that is just me) then ALWAYS use it - not sometimes this way and sometimes the other.

I ALWAYS stay away from that handy "export" keyword and the dot-feature (. command) - save for a few carefully selected exceptions. If a function needs a certain value from another function calling it, it has to be in the parameters. This way - usually even before i write that function - i have its "interface": which parameters it needs, which return values it produces, which output it creates - already laid out.

So, to sum it up: if you have thousands of lines of code and cannot keep track of your variables i suppose you should do a major code review and rewrite the parts where you needed a variable-tracker in the first place. After some serious restructuring of the code the need for such tools might just go away.

I hope this helps (ahh well, see above).

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How do I get the unused space?

One of my Solaris 8 machines hd was about to die. So I used g4u to create an image of the 9gb drive and I put it in a 36gb drive. That solved my dieing hd problem. But.... How do I get my machine to see the unused 27gb of space? Any help would be greatly appreciated. (1 Reply)
Discussion started by: Spyzic
1 Replies

2. AIX

unused storage on AIX 4.3

Hi, How do I query for unused partition in AIX 4.3 with DAS and SAA storage? I know most unix administrator don't put all the capacity on the system at once. thanks, vene (1 Reply)
Discussion started by: venerayan
1 Replies

3. UNIX for Dummies Questions & Answers

delete the unused file

Hi All, Can you please let me know how to delete any files that have not been accessed in the past 28 days in a directory. Thanks, Arun (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

4. AIX

how to clean Unused semaphore??

How can i clean up my unused semaphore??? (4 Replies)
Discussion started by: abhishek27
4 Replies

5. HP-UX

HP-UX using unused HDD space

Hello, I have a system with HP-UX 11.23 installed on it. There are ~36GB of unused space on the HDD. I did a very basic installation, and it created the usual volume group /dev/vg00. When I look at the output of ioscan -funC disk, I see this (and more, but irrelevant to this post): disk ... (1 Reply)
Discussion started by: goon12
1 Replies

6. Solaris

unused disk space

i Have alloted 20G in my vmware for solaris 10, upon installation, and some distribution of disk space to /,/opt,swap i just use 19G. Can i still use the 1G? How? how to see the 1G? that i did not use? how can i use it? appreciate your responce (17 Replies)
Discussion started by: kenshinhimura
17 Replies

7. AIX

Temporarily disabling unused ethernet adapter

Hi, In our AIX 5.2 server , we have one unused ethernet adapter which doesn't have cable connection . For this interface , we are getting alerts in errpt . Could you suggesthow to stop this alert ? And sametime i would like to keep this device in ODM . Is there... (1 Reply)
Discussion started by: sekarsamy
1 Replies

8. Programming

What Unix do with unused shared memory?

Hello, When creating shared memory in C, should be remove shared memory with shmctl function when don't need it. If it didn't remove, occupied shared memory stay and remain. If we create shared memory repeatedly without removing unusable shared memory, /dev/shm will full. Does Unix or... (1 Reply)
Discussion started by: pronetin
1 Replies

9. Solaris

Solaris 10: how to disable an unused HBA card

Dear all, I have a new Oracle Blade X4-2B server, running Solaris 10. The server comes with a HBA card that will not be used now. It has not fibers connected to it. As a consequence, its leds never stop flashing. My question is: how to disable this HBA card, without removing it physically... (2 Replies)
Discussion started by: Gus1971
2 Replies
All times are GMT -4. The time now is 04:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy