Shorten API access?

Hey everyone!

Is there a way to shorten the API access?
For example,
“new WaitForSeconds” to “new WFS” or
“Physics.Raycast” to “Phy.RC”?
Of course, I don’t want to
“public static class Phy {void RC () {Physics.Raycast();}}”
because of completely useless calls all over the place. I also don’t have source code access.

I have written quite some code that I re-write later on just to get smaller chunks of code. I rarely use empty lines, I try to reduce white space and shorten names when it’s useful. Of course, clashing has to be prevented and names should still be understandable, but when you go from things like
“character.stats.attackPower” to “char.stats.atk”,
I can still read and understand the code quite well while having much more overview over the entirety of the code and not needing to scroll that much and getting lost.
I understand those aren’t things you’d want to radically go for when writing code others have to read, but when you’re only working on something yourself, you should probably go for what you work with the best.

Best wishes,
Shu

Personal opinion: this is misguided and overall reduces readability of your code. But it is your code, and you can do what you want with it.

You can create shortened aliases for types in C# with a using directive. For example:

using MB = UnityEngine.MonoBehaviour;

public class MyClass : MB {
  // blah
}

Now like I said - personally I believe this decreases readability, but it’s there if you want it.
You could also use the global modifier on such a using statement to apply it to all of your code at once: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive#global-modifier

4 Likes

I also believe that using it is a mistake, except in very specific scenarios. The only time I would realistically want to use this, is when setting specific global gamerules, like a maximum inventory stack size, never to shorthand methods which I use a lot.

One of the reasons why I suggest against it, like @PraetorBlue said, is that it reduces readability of your code. While you may think it’s handy at the time, come a point where you return to your project after a month’s break, you may find out that you don’t remember what any of them stand for, and would waste more time reviewing what they actually mean, than the time you would save from it originally. This is the same for if you brought other people onto your project, as it would take time for them to learn all your short-hands.

1 Like

There are many code editors out there which will take the drudgery and inaccuracy out of typing for you. You don’t have to type everything out yourself.

If you wished the built-into-Unity code editor was better than Notepad, or that you didn’t have to switch between another app and Unity just to edit your code, look into ScriptInspector3, an editor addon available in the Unity Store.

1 Like

Thanks for your replies, PraetorBlue, RadRedPanda and halley!
I know this is an unpopular “opinion” (to me, it’s not an opinion, since I simply do work faster like that, even when reviewing and modifying code I wrote a year ago). Thanks for not being aggressive about this; I have seen quite a lot of annoyed posts about things like that online, since it’s not within “MS Coding Standards” and makes it hard to understand if you haven’t written the code yourself. I do understand that and I strictly only do that when writing code that I know noone else will have to work on.

@PraetorBlue
Thanks for pointing me into the right direction! This sounds great! “global” may or may not work, since it seemss to be introduced in C# 10.0, which I think Unity doesn’t support yet. (?) Not to mention I’m still using Unity 2019.4.

@RadRedPanda
I do understand the concerns, but personally, I just find it easier to work with if the code is shortened. (Of course, not to an unreadable or disguised extent!)

@halley1
Just to clear up any confusion, I am not trying to shorten the code to write less, but to have less text for the same content on screen, so that I get a better overview of the file.
I do use auto complete when writing in VS Code. However, when the project is growing, I am facing two issues with that:

  • There will be a lot of suggestions I have to go through to find what I want VS Code to auto complete to, which, in many cases, makes it faster typing the code out completely.
  • At a few ten thousand lines, I am getting considerable slow down.
    I am even using Sublime Text as an alternative, without auto complete, just to avoid slow down and the auto complete feature when it gets annoying. Most of the time, I just find it better to focus on what I write when there isn’t a window showing up all the time suggesting a bunch of things to go through.

Works great to shorten API. Also, toggling between the used API:

using Hide = UnityEngine.HideInInspector;
// using Hide = System.Serializable; // unhide
public class Example : MonoBehaviour {
    [Hide] public int i0;
    [Hide] public float f0;
}

You’ve got to be kidding -.- You don’t really define an alias named “Hide” and let it point to Serializable…
This is in the same category of C++ programmers that define TRUE as false.

This is not really about “code only you will work on”. The issue is that you get used to such wild hacks and you can’t “turn them off” at will. Abbreviation in general have the issue of increasing ambiguity. The using statement you’re abusing here is there to resolve ambiguities if you have a clash between two namespaces.

Well, you do not program very long then, right? I have code that is 15+ years old. If I had used wile abbreviations all over the place it would take ages to “read back in” to even understand the basics.

-source.

In this blog he talks about why rewriting something from ground up is usually a bad idea and doesn’t yield a better outcome. A lot people always say “I will refactor this later” but reality is that is almost never happens. That’s why you should stick to “good” practises because it’s very unlikely that it will ever be improved.

You said:

Yet you already have made several posts here on the forum where you posted some of your code, so we have to read it ^^. So your initial assumption no one else would ever have to look at it is already broken :slight_smile: I actually do have a lot old code that nobody else ever had an eye on. However you never know when a snippet may be helpful or relevant again.

I also do have a lot of crappy and hacky old code, but I’m aware of it and try to avoid it as much as possible. You seem to seek out gathering more and more bad habits ^^.

Back to the topic:
If you want to use an extra symbol which you can replace, give it a meaningful or neutral name. Naming something one way and having it represent something completely different is one of the worst things you can do.

ps: sorry for the half-rant ^^. Don’t take it personally :slight_smile:

3 Likes

[1] I do try to write differently when posting, but generally, yes, good point.
[2] Not really, I just want to get things done. And the example of “unhiding” a lot of code at once was purely intended for temporal, bug fixing usage. I just placed it within a comment in case I temporarely want to unhide all the data, which can be a pain to do nondestructively. Or I may be missing some concept here, but the point is: When you know a solution, most of the time, it’s best to just implement it rather than trying to find the “perfectly fitting” one that’s exactly intended for that specific use case. Of course, if you know things that fit said scenario and are just as fast to write, you may have a better solution. And it may even be more scalable. Depends. Then again, for some scenarios, I’ve reasearched a lot and ended up writing my own solution anyway. Because of more flexibility, because of performance, because less code to write.
It may also depend on your background. If you have formal education in this, you may have a different perspective than I do.
[3] No worries, I know there can be heated arguments here, which sometimes can be productive, sometimes not. Didn’t read any personal offense into your post, only an objective view.

Just a side note which may possibly change your opinion on using your Hide thing, if you want to view Serializable members in the Inspector, you can always right click the tab and change it to Debug mode. This will make previously hidden members visible, but only in the Inspector. You also won’t be able to change them, but there’s valid reasons behind encapsulation.

2 Likes

@RadRedPanda
Thanks for mentioning! That’s great! I have used that when working with assets, but I didn’t know that’s available for user scripts as well! Well, yes, for my usage, that’s a better solution! Not even having to recompile!

1 Like

I feel like the purpose of using [name] = [some class] is to work around ambiguous naming issues. A common one being object/Object when using both the System and UnityEngine namespaces.

A few of my scripts have using Object = UnityEngine.Object when I need to use the System namespace, but I also need to use UnityEngine.Object a bunch.

2 Likes

Why do you care about shorting or alising things, you don’t have to type it either way but now you got a extra level of indirection.

1 Like

I also didnt get your point of “speed”, any decent IDE will auto-complete WFS to WaitForSecond, so you will need the same time to type in both cases but will lose a lot of readability, why would someone chose this option?

8330910--1094685--a.gif

I’m also a lazy guy, most of us are, but instead of doing this hacks, create a template/snipped to speed up your typing process

8330910--1094688--b.gif

4 Likes

Since we also talk about WaitForSeconds, I’m more concerned about potential garbage allocated than how long the code is. If you’re looking for a garbage free WaitForEndOfFrame / WaitForFixedUpdate, see my post over here . In the following post I present a solution for WaitForSeconds that does not produce garbage. Though that was a lot more trickier and hacky but works.

1 Like

yeah very much agree they are asking for a lot of things that are just best handled by your tools.

@passerbycmc
@Nefisto
There are two points about speed:

  1. Writing:
  • I am working on mobile devices a lot as well, without autocomplete.
  • Unless you’re writing something very often and remember it, you either complete suggestions with the danger of completing to something different or you have to read the suggestions and choose from them. Even with autocomplete available, I don’t use it, because overall, I found it more burdensome than just typing.
  1. Reading
  • I can read it faster, understand it faster.
  • I have a better overview, so I
    → can copy & paste from one place to another faster,
    → can find a specific area faster, having to scroll only about 10% compared to when I didn’t bother shortening things.
    Of course, this may be just my experience and not be representable for all users alike.

@Bunny83
That’s indeed great, I’m already caching that, I was just thinking about it in terms of API that could be shortened.

1 Like

I can’t argue with this.

Anyway, everyone has their preferred way to code, if this fits your use case it’s correct, at least for you

2 Likes

keep in mind works for you, only works if you are a solo dev.

2 Likes

Sure! Different scenarios, different requirements.