LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-17-2021, 07:37 AM   #1
rblampain
Senior Member
 
Registered: Aug 2004
Location: Western Australia
Distribution: Debian 9.2
Posts: 1,208

Rep: Reputation: 51
Is there a convention for naming variables?


When one has to name variables in different contexts, is there a prevailing convention to prefix the variable with a notation indicating where it belongs?
For example, variable contents are passed between Javascript, Local Storage, HTML(forms) and server side scripts.

I got into the habit, possibly a bad habit, of making the variable name slightly different for fear of any conflict when there may be no conflict possible.

If not and if useful, is it an idea to copy the "hungarian notation", which prefixes a variable name with "s" for string, "i" for integer etc and starting the variable name with a capital letter? In my simplistic reasoning, the variable name can be prefixed with "js" or "lc" or "ht" etc (perhaps already applied in "good programming").

Thank you for your help.
 
Old 11-17-2021, 08:36 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,416
Blog Entries: 13

Rep: Reputation: 4522Reputation: 4522Reputation: 4522Reputation: 4522Reputation: 4522Reputation: 4522Reputation: 4522Reputation: 4522Reputation: 4522Reputation: 4522Reputation: 4522
Nothing 100% universal. Hungarian notation is/was one attempt.

What organizations do is to agree upon, document, and use their own coding conventions, which may or may not be in agreement with proposed standards.

Same for you, decide what you feel is a good choice, use it uniformly, and modify as needed.
 
1 members found this post helpful.
Old 11-17-2021, 09:57 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,224
Blog Entries: 1

Rep: Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585
Off: Hungarian notation has a more meaningful version, described by Joel Spolsky:
https://www.joelonsoftware.com/2005/...de-look-wrong/

TL;DR: such prefix is useful if similar but not identical objects have to be handled in parallel, then the prefix emphasizes the difference, e.g.
mileDistance vs kmphSpeed, celsiusWaterBoiling and fahrenheitIceMelding or pixelScreenWidth and mmScreenHeight
 
1 members found this post helpful.
Old 11-17-2021, 10:45 AM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,188

Rep: Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752Reputation: 4752
Don’t use Hungarian notation if you’re using a dynamically typed language.
 
1 members found this post helpful.
Old 11-17-2021, 03:59 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,434
Blog Entries: 4

Rep: Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376Reputation: 3376
I simply suggest that your team, and maybe the higher-level organization, should reach a consensus as to what all of you will choose to do, so that, when anyone in the future reads your source-code, it all looks more or less the same. The future reader, being aware of your consensus, will use it to help him or her understand what you have done.

Personally, when I am passing "variables" between clients and servers, or caching them in storage, I strongly prefer to package them into structures. This gives the reader an immediate clue as to what the variable "belongs to," and it also makes it much less likely that you will be vexed a by "simple typo" that – just as in the current sentence – you might have innocently overlooked because your eyes "knew" what to expect. We have all been extremely annoyed by a bug that we couldn't see when we were in fact looking directly at it.

Last edited by sundialsvcs; 11-17-2021 at 04:00 PM.
 
2 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
usb naming convention hoover93 Linux - Hardware 5 10-19-2005 11:18 PM
fonts.scale/fonts.dir: Naming convention kaon Linux - General 0 08-09-2004 06:02 AM
mail server the naming naming convention problem kashan Linux - Newbie 0 07-16-2004 03:08 PM
A small question in naming convention mnguyen Linux - Software 1 04-17-2004 04:14 AM
What is the naming convention for files in rcX.d? roshko Linux - Newbie 2 09-01-2003 05:41 PM

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

All times are GMT -5. The time now is 11:55 AM.

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