Subscribe to andrewsw's Blog        RSS Feed
-----

Name Your Stuff

Icon 3 Comments
Please don't name your form "form", or "form1". Please don't call your textbox "textbox1".

Do not leave your controls with their default names of "TextBox1", "TextBox2", "TextBox50000", "ListBox1". Please don't call your application "MyApplication". All of these names are meaningless and unhelpful.

Avoid the name "Name" in particular. Name is already a pre-existing property in most applications. If you have a field for a full-name call it "fullName" (or "Fullname", or "full_name", or "txtFullName", whatever is appropriate). Even "fname" is an improvement from "TextBox1"; but not "fn", it's too brief.

Acquire the habit of re-naming your controls as soon as you add them. Most modern editors (IDEs) are able to reliably propagate a name-change throughout your entire project. Nevertheless, re-naming them as early as possible remains a good, sensible, habit.

I should mention that WPF (Windows Presentation Foundation) is an exception to this discussion. In WPF naming elements is discouraged, as it should be largely unnecessary.



Your language of choice, and possibly the type of application your are building (or framework you are using), is very likely to have its naming conventions or naming standards stated somewhere. Search this out. You should follow this standard unless you have a convincing reason to deviate from it. The most compelling reason is that the team you work with uses different conventions. If this is the case, then clearly your team's conventions should be followed in preference.



On the subject of brevity, you can use i and j (or x and y, depending on the language's custom) for your loop-variables. cmd is commonly used, and accepted, for a Command object, and conn or cnn for a Connection object. But, by and large, use longer more meaningful names. (Not too long though.) Do not use a,b,c,d!

The only possible exception to this last rule is if you are programming a mathematical formula (or algorithm), where a,b, etc., are understood and recognised in the context of the formula's definition. Even so, you should still consider whether they could be given more meaningful names. I know that some programmers won't even agree with this exception!

You can use prefixes, such as "txtFirstName". In some languages this practice is discouraged but I still use prefixes often, in any language. (Some languages, or frameworks, even encourage the use of suffixes, "firstNameTextBox".)

Do not name controls "txtTextBox" or "TextBoxFirst", "TextBoxSecond". This is a waste of time and no better than their default names.

Use some imagination! Imagination is a requirement for a programmer.



When you are searching the topic of naming conventions you will see that people don't always agree about which standards to follow (if any, and for different languages or frameworks), or whether to use prefixes or suffixes, or camelCase or under_scores. The bottom-line is:

The choice of a particular standard to follow is less important than you following it consistently, and that your code can be understood by other programmers.

Again, if you are part of a team, then you probably haven't needed to read this far, because you should be following your team's standards.

3 Comments On This Entry

Page 1 of 1

Michael26 

30 November 2014 - 04:39 AM
Preach it brother :)
0

andrewsw 

04 December 2014 - 12:34 PM
Erm thanks (I think) @Michael.

One reason for this blog-entry is so that I (and perhaps others) can link to it.
0

Michael26 

06 December 2014 - 03:21 AM
I think this should be required subject in schools and colleges, once people get used to default names it's very hard to stop.
I know from personal experience.
0
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

December 2019

S M T W T F S
1234567
8910111213 14
15161718192021
22232425262728
293031    

Tags

    Recent Entries

    Recent Comments

    Search My Blog

    3 user(s) viewing

    3 Guests
    0 member(s)
    0 anonymous member(s)

    Categories