[closed] meaningful names for parameters?

In Unit 2, after there is adviced to use meaningful names for parameters, it goes on assigning a parameter the name s.

We programmers know we usually just call s any general string parameter, but for the sake of teaching good programming practices, you shouldn't use that.

asked 29 Feb '12, 13:07

Micro-BIOS's gravatar image

Micro-BIOS
3.1k81864
accept rate: 18%

closed 21 Mar '12, 14:07

The question has been closed for the following reason "The question is answered, right answer was accepted" by Micro-BIOS 21 Mar '12, 14:07


6 Answers:

It's nice to be able to know immediately what exactly a variable represents. Even years later! I tell my math students to name the variable after the thing it represents; even in solving math problems this is useful.

link

answered 29 Feb '12, 13:20

Rafael%20Espericueta's gravatar image

Rafael Esper...
12.8k66128192

edited 29 Feb '12, 13:21

In 20 years of programming, I never called a string variable s. It is not meaningful and makes it hard to maintain the code in the future.

That means that a variable holding a customer name would always be called cust_name or customer_name. Both approaches work though consistency is important. Single character variable names work for loop control because experienced programmers will quickly recognise that this is the intended purpose.

ALways use descriptive variable names with that one exception. Be consistent in naming variables too.

Both will make your code more maintainable in the long run

link

answered 29 Feb '12, 16:32

Ian%20Dixon's gravatar image

Ian Dixon
220411

There is a mutually-agreed-upon Python programing standard, Python Enhancement Proposals, that's been floating around the community. In particular, I think PEP8 is gaining ground. It suggests several programing standards, including variable naming, commenting, etc. If you install a program called pylint, it will go through your code and flag any thing that's not consistent with the PEP8 standard.

link

answered 29 Feb '12, 14:03

David%20Zhang's gravatar image

David Zhang
4.3k112343

Well... I like meaningful names, but sometimes they can get in the way. I have had to work with C code that was full of variables like IndexToTheDataItemTested, and WhereTheDataWasFound and longer. After a few minutes I felt I was going crazy.

So, for a simple loop, the loop counter using "i" would make it easier to understand than a longer name, in my opinion, and has become an accepted pattern for the outermost iterator. "s" is very common for a generic string.

So some balance and reason should be applied, I think.

link

answered 29 Feb '12, 14:05

Bob%20Whitten's gravatar image

Bob Whitten
55418

its a matter of need i think, if you've got a variable that can't be easily recognized by someone looking the code (like 'i' for the loop) its good practice to put a note at where i is initialized or assigned a value for the first time.
that way someone could find where the variable starts and see:

i = 0 #this variable is to keep track of the loop

to make it more easily identifiable. It cant to do that with all variables being created. It seems kind of silly with something small but its a good habit to develop because as you right code, you have to go back and look at it months or years later, or even worse, someone else has to edit it down the road) it makes reviewing the code alot easier

link

answered 29 Feb '12, 14:32

Jeremy%20Tidmore's gravatar image

Jeremy Tidmore
373113

Although there is a bit of sarcasm and hyperbole, I enjoy this classic rant on variable naming conventions.

link

answered 29 Feb '12, 14:55

Carole%20Mah-2's gravatar image

Carole Mah-2
5932719

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×15,330
×218
×28
×13

Asked: 29 Feb '12, 13:07

Seen: 260 times

Last updated: 21 Mar '12, 14:07