LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-19-2024, 01:36 PM   #1
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,141

Rep: Reputation: 392Reputation: 392Reputation: 392Reputation: 392
Note for people just getting into programming of any kind.


Specific variable names! Make it clear what they are. Not just something random!

I'm working on a Python course now. One of the tasks was to write something that can tell if a year is a leap or not. I had the code. The math was exactly matching the algorithm I found. Kept being wrong. Over and over. I was about to put my hand through the wall.

Turned out I was using the same variable to store my True/False as I was collecting my input statement. Why?

I used a for my input statement. Without thinking I just used it again for my bool. I had a all over the place without realizing it. Had I named the bool something like yesno or some such. and the input as year_input or something... I wouldn't have spent the last 30 minutes raising my blood pressure beyond acceptable limits. It would have been obvious from the start.
 
Old 01-19-2024, 02:40 PM   #2
metaed
Member
 
Registered: Apr 2022
Location: US
Distribution: Slackware64 15.0
Posts: 364

Rep: Reputation: 171Reputation: 171
A litprog tool can help -- when naming a variable, take a quick look at your index of variable names.
 
Old 01-19-2024, 02:52 PM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Ha ha!
I did a very similar thing a few days ago and I've been in this game over 25 years!
Some people never learn.
 
Old 01-19-2024, 03:09 PM   #4
rclark
Member
 
Registered: Jul 2008
Location: Montana USA
Distribution: KUbuntu, Fedora (KDE), PI OS
Posts: 482

Rep: Reputation: 179Reputation: 179
Graduated in Computer Science in '86 ... Yes, make your variable names 'mean' what they are in use for. You do have up to 256 chars (usually) to work with in modern languages of today. Doesn't mean you have to use all 256, but you can be a bit more verbose when needed (unlike back when we were using Fortran and early C we were limited to at most 8 chars. We had to get creative sometimes). One of the rules we implemented was variable names should be at least 3 chars long, unless variable name is of a 'common' type. Like coordinates x, y, z are acceptable. Otherwise, row, col, cnt, amps, vars, watts, etc. should be used. Also use camel case or use underscores to separate words in your variables. Example: thisIsAVariable or this_is_a_variable. Up to you. Readability is important in understanding. Also all 'Constants' (or variables treated like constants) should be in uppercase. like PI or MAX_ROW.

Last edited by rclark; 01-19-2024 at 03:45 PM.
 
Old 01-19-2024, 04:03 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,662
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
(Chuckle ... can I have a "raise of hands" here right now?)

No matter how "smart" you are, the computer will always make a fool of you. The closest "emoji" that I can find on this site is either or ...

Especially important, in the case of variable names, is: (1) the "scope" of the variable-definition's "reach," and (2) whether the language will "simply, accept" variable-names that it has never yet seen before. In both cases, programming languages prove themselves susceptible to "a simple tpyo," which is especially hard to take when it has just happened to you ...

Full disclosure: The very first computer program that I ever wrote: (1) was eight lines long; (2) took me six months to write; and (3) had a bug in it. And that was (mumble, mumble ...) years ago.

Last edited by sundialsvcs; 01-19-2024 at 04:06 PM.
 
Old 01-19-2024, 04:43 PM   #6
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,341

Rep: Reputation: Disabled
Quote:
Originally Posted by jmgibson1981 View Post
Specific variable names! Make it clear what they are. Not just something random!
As the saying goes, there are two hard problems in Computer Science: Cache coherency, naming things, and off-by-one errors. You've just touched on an extremely important and also much neglected aspect of not only programming but anything computing-related: Naming conventions.

Having a variable called "i" in a counting loop is fine, but having "i" hold the number of the order currently being processed is most likely not a great idea. "orderNum" would probably be OK, but if you also find "orderNum2" and/or "newOrderNum" in the code, it's a sign that someone didn't really think this through.

And should a variable name also indicate its type (iFoo, bBar etc), especially when using a dynamically typed language? It might make it easier to avoid or catch subtle errors due to dynamic typecasting, as long as the convention is strictly adhered to. (I remember seeing an article on The Daily WTF many years ago where some genius had defined an enum literally called "Bool", with three possible values: "True", "False", and "File Not Found".)

Same goes for networking and servers: "server01" and "gw1" certainly aren't very good server or router names respectively, but what does a good device name look like? Should it reference function, asset number, customer, location, hardware, OS, or perhaps a combination of some or all of the preceding? What about VMs? Switches? Access points? Printers? Barcode scanners? Keycard readers?

It's well worth the time spending a few minutes defining a naming scheme that you'll be comfortable with, and then stick to it. As always, take a look at other people's code and borrow liberally any good ideas that you find. Regardless of your choices, you'll certainly run into situations where your convention will turn out to be suboptimal, but that's fine; you can always revise it for your next project.

(And if you're collaborating on a larger project, follow the existing naming convention with religious fervour, regardless of whether you like it or not.)
 
Old 01-19-2024, 04:57 PM   #7
rclark
Member
 
Registered: Jul 2008
Location: Montana USA
Distribution: KUbuntu, Fedora (KDE), PI OS
Posts: 482

Rep: Reputation: 179Reputation: 179
Quote:
Having a variable called "i" in a counting loop is fine
I bulk a bit at this because 'i' is harder to search for when looking for where the variable is used. Better to use 'cnt', 'idx' or 'index' unless it is in a very 'tight' loop. Another reason to be a bit more verbose ... searching.

Last edited by rclark; 01-19-2024 at 04:59 PM.
 
1 members found this post helpful.
Old 01-19-2024, 05:14 PM   #8
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,341

Rep: Reputation: Disabled
Quote:
Originally Posted by rclark View Post
I bulk a bit at this because 'i' is harder to search for when looking for where the variable is used. Better to use 'cnt', 'idx' or 'index' unless it is in a very 'tight' loop. Another reason to be a bit more verbose ... searching.
That's fair. My point was that a counting variable in a small loop is pretty much the only example I can think of where a nondescript variable name can be justified. And "i" has somehow become a de facto standard name for an integer counter.

But as you say, if the variable is used for fetching items from an array or something similar in the loop, pretty much any name other than "i" would be preferable.
 
Old 01-19-2024, 05:21 PM   #9
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by rclark View Post
... back when we were using Fortran and early C we were limited to at most 8 chars. ...
Fortran II was developed in the heyday of the IBM 7090. It was a 36-bit word machine, variable names were limited to 6 characters. The first character determined Integer or Floating Point. Devising meaningful variable names was challenging, especially in large programs.

Been there, done that, paid those dues!

Daniel B. Martin

.
Attached Thumbnails
Click image for larger version

Name:	Introduction to Fortran.jpg
Views:	10
Size:	39.2 KB
ID:	42441  

Last edited by danielbmartin; 01-19-2024 at 05:25 PM. Reason: Add image
 
Old 01-19-2024, 05:49 PM   #10
rclark
Member
 
Registered: Jul 2008
Location: Montana USA
Distribution: KUbuntu, Fedora (KDE), PI OS
Posts: 482

Rep: Reputation: 179Reputation: 179
Quote:
Fortran II . The first character determined Integer or Floating Point.
Even Fortran 77 which I used... implicit variables starting with the letters 'i' to 'n' are integers. It was recommended not to rely on this, but to always declare your variables. Still in a lot of code I saw plenty of just i, j, k variables! Still 6 char character variable names. Luckily I was introduced to Fortran at college, but never had to write much of it in my career. I did help maintain Fortran code for plasma research (for vehicle re-entry from space) and a few other places but that is it. 'C' was the language we used a lot at work for real-time applications for SCADA projects.

Last edited by rclark; 01-19-2024 at 06:34 PM.
 
Old 01-20-2024, 07:21 AM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Quote:
Originally Posted by rclark View Post
I bulk a bit at this because 'i' is harder to search for when looking for where the variable is used.
I remember reading a suggestion somewhere to use 'ii' because of that, but I think most editors are able to search for a whole symbol easily enough anyway.
 
Old 01-20-2024, 09:22 AM   #12
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,662
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Two things, I think, especially “vex” software today. First, that we continue to use “source-based interpretive languages,” which literally must re-compile the source code with every oncoming request. (I have no idea how this strategy “won,” nor why cockamamie “solutions” to “deal with it on the client side” continue to be taken seriously.)

Second: “typelessness.” Yes, Microsoft probably canonized it with their variant “type.”

Maybe three: “variable declarations.”

Plenty of us still remember when languages were actually “compiled,” even into pseudocode (such as Java), and “strong typing” protected you from yourself.

Lately, I helped a client resolve a very serious problem which literally de-evolved into this ”d-oh!!” situation: (watch the underscores …) ii”

The interpreter didn’t see this as “a tpyo.” Merely as the introduction of a brand-new identifier. In a “better language,” this would have instantly been caught as a “compile error.” Instead, it cost my client many thousands of real Dollars. None of which can ever be recovered.

“The language system is always the agent that is in the best position to protect you from yuorself.” (Did you just catch that syntax error?)

Last edited by sundialsvcs; 01-20-2024 at 09:31 AM.
 
1 members found this post helpful.
Old 01-24-2024, 02:35 PM   #13
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by rclark View Post
Even Fortran 77 which I used... implicit variables starting with the letters 'i' to 'n' are integers. It was recommended not to rely on this, but to always declare your variables.
I used to force explicit declarations by including "IMPLICIT LOGICAL*1" at the top of Fortran code. The compiler complained mightily if variables hadn't specifically been declared to be integers, reals, etc.
 
Old 01-30-2024, 03:16 PM   #14
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,662
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
In a truly-compiled language (particularly ...) the concept of local variables is very important. A variable such as "i," first of all, must be declared. (This would have immediately caught the "ii" problem aforementioned.) But it can also be declared to be "local" to a particular function or subroutine. Even if other routines use the same name, it is not the same variable.

Also, if the routine is recursive, which means that "it can call itself," each instance has its own copy of the local variable.
 
Old 02-08-2024, 01:43 PM   #15
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 267

Rep: Reputation: Disabled
And then there is parallel processing. Effectively, all variables within a thread must be local. Otherwise, you are liable to get collisions.
I can read from a global variable, but variables which will be written to must be strictly defined within each parallel thread.

Quote:
Originally Posted by sundialsvcs View Post
In a truly-compiled language (particularly ...) the concept of local variables is very important. A variable such as "i," first of all, must be declared. (This would have immediately caught the "ii" problem aforementioned.) But it can also be declared to be "local" to a particular function or subroutine. Even if other routines use the same name, it is not the same variable.

Also, if the routine is recursive, which means that "it can call itself," each instance has its own copy of the local variable.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Linux kernel coders propose inclusive terminology coding guidelines, note: 'Arguments about why people should not be offended do not s LXer Syndicated Linux News 0 07-07-2020 10:36 AM
MIDI keyboard note-on and note-off inverted jakykong Linux - Software 0 08-21-2010 03:21 AM
Note for people using SSH from XP to Ubuntu aidansmoker Linux - Software 2 12-16-2007 03:16 AM
kind of a programming quesion...kind of not tho jhorvath Programming 2 06-30-2003 10:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration