1

EDIT: I changed the Title to a more appropriate name, base on Comments from David

I am experienced with c# programming and have never really run into this before, though I am surprised I haven't

I am using Long name variables, because I plan to come back to my program in a few months from now, but I am annoyed at how long it is taking me to write code. I considered using shorter names then just replacing them all later. But I was wondering if there is syntax sugar for something like this

Using (var b as TheButtonThatMakesThingsHappen)
{
    b.Name="TheButtonThatMakesThingsHappen";
    b.Location=...
    b.etc...
}

Instead of

TheButtongThatMakesThingsHappen.Name="TheButtonThatMakesThingsHappen";
TheButtongThatMakesThingsHappen.Location=...
TheButtongThatMakesThingsHappen.etc...

I did a google search and tried here on stackoverflow, but I don't know what this is called,

Any Ideas?

General Grey
  • 3,598
  • 2
  • 25
  • 32
  • 2
    You can just set a variable (`b`) to the source variable (`TheButtonThatMakesThingsHappen`), and rely on reference equality to set values on the object. Though I question how much more readable this will actually make the code. Perhaps your variable names just need to be more *concise* without being so *long*. Something like `ThingsHappenButton` instead of `TheButtonThatMakesThingsHappen`. – David Feb 16 '14 at 01:36
  • Not really a duplicate, but maybe you're looking for an [object initializer](http://stackoverflow.com/questions/12842371/is-there-any-benefit-of-using-object-initializer-in-c)? @David: agreed, edited. – CodeCaster Feb 16 '14 at 01:36
  • 4
    @CodeCaster: He's not asking about an object initializer. It looks like he's trying to create a local-scope alias to a variable, which C# doesn't explicitly have functionality to accomplish. Personally, I think he's solving a problem by adding another problem, ultimately making the code harder to support. – David Feb 16 '14 at 01:38
  • @David You nailed my question exactly. And you might be right with the second part of your comment. I am 100% self taught... well taught by you guys. And often there is a standard way of doing something that I totally missed, I was thinking this might be one of those things – General Grey Feb 16 '14 at 01:42
  • TheButtonThatMakesThingsHappen is not type so you can't do what you wrote, but you can do what @David suggested, just use `Button b = TheButtonThatMakesThingsHappen;` and then `b.Location=...` – Nikola Davidovic Feb 16 '14 at 01:43
  • 1
    Another thing to note about variable naming is the *scope* of those variables. As a general rule, long variable names are ok in small scopes where they're not used a lot (in a single small method for example), whereas shorter (more generic, easier to remember) names are good for large scopes (class-level members used in many methods, for example). (I didn't post an answer because I want to avoid turning it into a rant, but good variable naming is *very* important and unfortunately often overlooked by even the most experienced developers. For good reading on the subject, check out Clean Code.) – David Feb 16 '14 at 01:49

2 Answers2

5

As David posted in the comments section on your question, I think that the method that you described for renaming is a needless shortcut to get around the issue of long variable names. Try to name the variables to something more constructive, like SubmitButton or UserSubmitButton instead of TheButtongThatMakesThingsHappen.There is something very wrong with your variable naming scheme... if you don't actually use it.

Another alternative to long variable names is comments with shorter, more descriptive variable names- for example:

button SubmitButton; //this button is used in the second form field of the website
Blue Ice
  • 7,888
  • 6
  • 32
  • 52
  • 2
    +1 for "There is something very wrong with your variable naming scheme if you don't actually use it." Your variable names should always be descriptive, but not needlessly so. You don't need all those articles in the name of your variable. – krillgar Feb 16 '14 at 01:57
  • @krillgar I missed that part totally, and Blue Ice is exactly correct, there is something very wrong, If I am not using it. I will have to rethink this whole endeavor. – General Grey Feb 16 '14 at 02:02
  • 1
    No, you don't have to rethink the entire endeavor. There is only a small superficial problem, and though the fix may take a while, it is a useful way to improve your program for yourself in the future. – Blue Ice Feb 16 '14 at 02:07
1

I don't feel like there is an answer here yet. While I am starting to think the concept itself is flawed, at least for the reasons I want to do it, I think it is unfair not to have a proper solution for someone looking later. I have taking the suggestions From David and others and compiled this as my answer.

#region TheButtonThatMakesThingsHappen
{
    var b = TheButtonThatMakesThingsHappen;
    b.Name = "TheButtonThatMakesThingsHappen";
    b.Location=...;
    b.etc...;
}
#endregion

It allows a local scope alias and the region tags allow for collapsing code, which keeps things neater.

General Grey
  • 3,598
  • 2
  • 25
  • 32