Naming conventions?

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 1905 Views - Last Post: 08 March 2017 - 07:24 AM Rate Topic: -----

#1 JapanDave  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 218
  • Joined: 01-February 16

Naming conventions?

Posted 21 February 2017 - 03:10 PM

OK, I have advanced enough these days that my applications are getting pretty complicated and sometimes with difficult code to read. Of late, sometimes it can be rather difficult to debug my own code due to the names I give my Class, Method, Function etc. I have been through the typical sites Naming Guidelines - MSDN - Microsoft, but this does not really help.

I am not talking about case as that is not the issue. I tried to find something with google that deals with giving descriptive names for all of these, but everyone seems to deal mainly with casing.

Any real world advice for what you should be naming Class, Method, Function, Parrameter,Property, Variable etc?

Is This A Good Question/Topic? 0
  • +

Replies To: Naming conventions?

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 12824
  • View blog
  • Posts: 50,642
  • Joined: 12-June 08

Re: Naming conventions?

Posted 21 February 2017 - 03:12 PM

Do you have a small example?
For the most part it should describe what it does or is.
Was This Post Helpful? 1
  • +
  • -

#3 JapanDave  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 218
  • Joined: 01-February 16

Re: Naming conventions?

Posted 21 February 2017 - 03:19 PM

OK, something like this,

public class Loop
{
    public class AllStaffSingleSection
    {
        public static void DoShiftCountRefreshBackground(ObservableCollection<StaffCountTbl> countStaffTblSourceObserv, int staffNo, DateTime date)
            {


I put any code that needed to be loop through in the loop class, then when I need to loop through all the staff in a particular section, as oppossed to all the staff in the company, then the method goes through and updates the newly input data in the Database and put it in a collection. The parameters are pretty self explanatory. But it starts getting ridculously long due to the very long names I have given each particular item. Sometimes I may have 2 or 3 internal classes and that is just stupid.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • Build your own boat!
  • member icon

Reputation: 6205
  • View blog
  • Posts: 24,739
  • Joined: 12-December 12

Re: Naming conventions?

Posted 21 February 2017 - 03:26 PM

AllStaffSingleSection(?) I couldn't gauge what this might be for. StaffOfSection or something similar is probably clearer in intent.

DoShiftCountRefreshBackground?! Is it doing, shifting, counting or refreshing? If it is doing more than one then it is too many.



Why is there a Loop class..? It is not a noun (in this context). If classes of certain types need a looping mechanism then there should be an interface and/or abstract class involved. IEnumerable already exists.
Was This Post Helpful? 1
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 12824
  • View blog
  • Posts: 50,642
  • Joined: 12-June 08

Re: Naming conventions?

Posted 21 February 2017 - 03:26 PM

Yup, that's a dumb name and some bad reasoning. Bad static'ing as well. Go back over what OOP is about.

A class describes something and just a junk drawer to hold methods that use loops is silly and wrong.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • Build your own boat!
  • member icon

Reputation: 6205
  • View blog
  • Posts: 24,739
  • Joined: 12-December 12

Re: Naming conventions?

Posted 21 February 2017 - 03:31 PM

StaffCountTbl Why are counts (derived quantities) stored in a table?

Sorry to persist, but I suspect that it is not the naming that is the issue but the appropriateness and scope of what the classes and methods are doing.
Was This Post Helpful? 0
  • +
  • -

#7 JapanDave  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 218
  • Joined: 01-February 16

Re: Naming conventions?

Posted 21 February 2017 - 03:37 PM

@andrewsw, this DoShiftCountRefreshBackground to break it down, it is a void, so Do, ShiftCount this is b/c it counts how many times a staff member has a worked a shift in a month, RefreshBackground refreshes the collection and does it in the background as opposed to on the UI thread.

@#5 modi123_1, I have exhausted my resources for better naming, which is why I am here for help. Any link or any advice on how to go about fixing this. Also, contrary to my name English is not my first language and I would find it much easier to do this in Japanese, but that is not viable of coarse.

@andrewsw, b/c it stores the staffs shift count data. I know I need to change all of this, which why I am here.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 12824
  • View blog
  • Posts: 50,642
  • Joined: 12-June 08

Re: Naming conventions?

Posted 21 February 2017 - 03:42 PM

I am not certain I can offer anything else outside of "oop naming conventions". It is clear you need to review how OOP works if you are going down the path of "any code that needed to be loop through in the loop class". I don't know enough about your project either.

Example:
https://www3.ntu.edu...agramCircle.png
https://www3.ntu.edu...assExamples.png
Was This Post Helpful? 2
  • +
  • -

#9 andrewsw  Icon User is offline

  • Build your own boat!
  • member icon

Reputation: 6205
  • View blog
  • Posts: 24,739
  • Joined: 12-December 12

Re: Naming conventions?

Posted 21 February 2017 - 03:51 PM

I recommend the book Clean Code by Robert Martin. But, yes, reviewing OOP first.
Was This Post Helpful? 1
  • +
  • -

#10 JapanDave  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 218
  • Joined: 01-February 16

Re: Naming conventions?

Posted 21 February 2017 - 04:33 PM

I should not have said "Any" code, I should have said any method that loops and has an abstract function not really tied to anything in particular. But yes, I realize I need to look at this which is why I am asking, there is too much information and every one seems to have their own idea. I don't know what is the gold standard and I was hoping for a link of some kind where I could at least study what is correct. Oh, and to be clear, I am not a programmer, I do this for my enjoyment, so I am not looking for someone to hold my hand while they do everything for me. But, sometimes you just need to be shown the way, think 5 year old and we are on the right track. Although, my kids are 14 and 11 now. LOL.

@andrewsw, will take a look at that . Thanks.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5422
  • View blog
  • Posts: 18,449
  • Joined: 05-May 12

Re: Naming conventions?

Posted 21 February 2017 - 07:24 PM

The gold standard is that your class is named as a noun or a compound noun that represents an object/concept in your problem domain. The method names are verbs or verb-nouns which are actions that you perform on/with the object/concept. Don't be afraid to use overloading with methods. Property names are nouns which are the attributes exhibited by the object/concept. Properties may also be adjectives.

The key thing to keep in mind is encapsulation and being rigorous about the single responsibility principle. This applies to the class in general, and to each method and property. If objects only have a single responsibility, then they will have a limited set of things they can do, so the verbs or verb-nouns that describe those methods that the object can do is quite limited.

I know the above sounds a lot like pronouncements made by a Buddhist monk sitting on top of some remote mountain top. I think that is because there is a little bit Zen involved in well named class hierarchy much like the harmony in a well arranged Zen garden.

I feel the best way to learn this is simply practice, practice, and practice. Taking time to read other people's code also helps. Identify what you like, and what you don't like and see how you would improve it.
Was This Post Helpful? 2
  • +
  • -

#12 JapanDave  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 218
  • Joined: 01-February 16

Re: Naming conventions?

Posted 22 February 2017 - 01:40 AM

Thanks Skydiver, that helps a great deal. That gives you a base to work from.

What do you mean by encapsulation? Are you talking about this?
Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6441
  • View blog
  • Posts: 14,178
  • Joined: 02-June 10

Re: Naming conventions?

Posted 22 February 2017 - 02:07 AM

Skydiver beat me to the first couple ones I was going to mention. But here's my short list.

Objects in the world are things. Thus your properties and classes (objects in code) should be nouns.
Methods *do* action. Thus they should be verbs.
Methods that are targets of threaded operations start with "Do"... DoSyncTime... DoFetchRecords... and usually get called in some way from corresponding methods SyncTime... FetchRecords

Methods that are actions in response to events or commands start with "On_".... "On_SaveDocumentCommand"... "On_AppResume"... "On_AppSleep"

Commands end with "Command". "SaveDocumentCommand"
Evaluations for commands start with "CanExecute". "CanExecuteSaveDocumentCommand"

I like to name properties in a military-ish way with most signifcant fact first so relelated things list together alphabetically in the various Visual Studio drop downs
  • NameFirst
  • NameLast
  • NameMiddle
  • NameAlternate
  • Much easier to locate than spread all over as
  • AlternateName
  • ... 15 other properties stuck in the middle
  • FirstName
  • ... 5 other properties in between
  • MiddleName
  • ... you get the idea


Bools start with "Is" or "Can" as appropriate. "IsDirty"... "IsSuperuser"... "CanAddUsers"... "CanPrintReports"...

ViewModel properies all end in VM... "MainVM.cs"... "MoviesVM.cs"

Pick a term you like and stick with it. It sucks going into code and finding "Create", "Make", "Generate" used interchangeably.
Don't use "get" for retrieving data from REST, user, database etc. Other languages like java have actually "getters" in place of C#'s property get and set. So GetCurrentTime becomes confusing for cross discipline coders. FetchServerTime implies this data is having to be fetched from remote source. LoadSettings implies a load from a local source like xml file or local database.

I name the backing field of properties the same (including case) as the property with just a leading underscore. Purists will scream about this. _NameFirst backs property NameFirst. But there is a simple reason for doing this. Find and replace. It happens more often than we like to admit. So a search for NameFirst that can be case-sensitive will find (and replace) both the backing field and public property and all the comments in code or in your DeveloperNotes.txt (you do leave developer notes in your projects, right?).

Be specific with your names. Time is a bad name. DeviceTimeUTC and DeviceTimeLocal are unmistakable. This is an example of what people mean when they say "Your code should be self documenting". Good names shouldn't requires a comment in code to explain it. Remember than your code is going to compile down to IL anyway so longer names don't make your compiled executable bigger, and you aren't paying for names by the character.
Was This Post Helpful? 4
  • +
  • -

#14 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1252
  • View blog
  • Posts: 3,853
  • Joined: 20-March 10

Re: Naming conventions?

Posted 22 February 2017 - 02:25 AM

View PostJapanDave, on 22 February 2017 - 08:40 AM, said:

What do you mean by encapsulation? Are you talking about this?


Read this blog entry on the single responsibility principle.

Single Responsibilty Principle
Was This Post Helpful? 3
  • +
  • -

#15 JapanDave  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 218
  • Joined: 01-February 16

Re: Naming conventions?

Posted 22 February 2017 - 04:32 AM

Wow, thanks all for the great answers.

@tlhIn`toq, great post. Can I ask what you would name any of these,

  • No. 1: Collection/List/array or another type with a method
  • No. 2: Auto Property or Property

I am not if I am using the correct terminology with No. 1, I mean something like this.
Ie,
public static IEnumerable<T> ReturnSomeList(DatabaseDataContext dataContext)         
    {
        return dataContext.<T>.Where(p => p.ID == 1);
    }


Or this
public static T Foo()         
    {
        return something // return the type specified.
    }



@snoopy11, that was a good read thanks.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2