1

Is there a way to replace some code with custom (shorter) words?

Example: I don't want to type the words below all the time, but I want to type a shorter word. The /compiler/ pre-processor will replace my custom words with the originals.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
}

I want to write something like this instead:

protected void doGet(banana, apple) foo, bar {
    processRequest(request, response);
}
user3004449
  • 129
  • 1
  • 15
  • 7
    Create your own language called "Fruits" where every keyword is a fruit! :) More seriously, that's the class name, nothing you can do. But note that in a IDE, typing `HttpSer` + Ctrl Space will list you both classes as 1st and 2nd choice. – Tunaki Nov 25 '15 at 19:33
  • Haha yea, would be nice. But is there a way to do this is java? Some mapping between words or something? I use autocomplete, but I type faster than my autocomplete can auto complete :) – user3004449 Nov 25 '15 at 19:34
  • 4
    Use an IDE with code completion so that you don't have to type the complete words all the time. – Jesper Nov 25 '15 at 19:35
  • 1
    Compilers don't do that. – JJF Nov 25 '15 at 19:36
  • 3
    Hopefully, you don't get to type `InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonWindowNotFocusedState` too often. – Tunaki Nov 25 '15 at 19:37
  • :) Nope. But like my example shorter words would be nice for my laziness. – user3004449 Nov 25 '15 at 19:39
  • Maybe your IDE could perform a macro that does find&replace for you? – Fildor Nov 25 '15 at 19:41
  • This might help you http://stackoverflow.com/questions/6202223/auto-code-completion-on-eclipse – Atri Nov 25 '15 at 19:43
  • But how would you remember which fruit is a request and which one is the response? :) – uzilan Nov 25 '15 at 19:48
  • There would be a list in a file with something like this: banana = HttpServletRequest request; apple = HttpServletResponse response; ... – user3004449 Nov 25 '15 at 19:52

4 Answers4

2

To help to reduce your typing work, is not the job of the compiler.
It is the job of the IDE, like Eclipse.
Just type the first 3 letters, and then Ctrl-Space, the IDE shows then a list of suitable names.

That way you have the best of both: few typing and long meaningful and hopefully self-explaining names.

Marty Pitt
  • 28,822
  • 36
  • 122
  • 195
AlexWien
  • 28,470
  • 6
  • 53
  • 83
1

What you're looking for is a job for a pre-processor not a compiler. Unfortunately for you Java doesn't have a pre-processor. If you were using a language with one you could simply use #define statements.

For a few cases you can use static final if the thing you want to shorten can be saved as a variable E.G. strings. See this question for more detail.

Otherwise your best bet is to download an IDE (E.G. IntelliJ or Eclipse) with code completion. you'll only need to type the first few letters and your IDE will complete the rest!

Edit: I see from your comment on @AlexWien's answer that your computer is too slow to keep up with your typing in an IDE. Check out this answer for possible solutions for your machine.

Another possible solution is simply writing the word youwant to use as a shortcut if you've got a sharp memory (or you could store the info on a bit of paper/notepad file). When you get to when you want to compile you could replace all instances of your shortcut using find and replace, probably only a solution for a few certain use cases.

Community
  • 1
  • 1
Knells
  • 827
  • 2
  • 12
  • 24
1

You can't replace the name of a class with something else, but if you have a long method, you could in some situations wrap it in another one like this:

void banana() {
    insanelyLongMethodName();
}

banana();
RaminS
  • 2,208
  • 4
  • 22
  • 30
  • 1
    @Gendarme You could replace the name of the class in a similar way using `extends`. Then do what you suggest with the methods, adding `super` to each one. – Knells Nov 25 '15 at 20:04
  • 1
    @Knells extending HttpServletRequest, HttpServletResponse, ServletException, and IOException is possible I suppose. But I assumed it would be much more pain than gain doing that. But you are correct. – RaminS Nov 25 '15 at 20:07
  • 1
    Just my opinion, but altering program structure to save typing seems like misaligned priorities... – Juan Carlos Coto Nov 25 '15 at 20:12
1

There is also some other JVM languages, as Kotlin, that you can try with less boilerplate. See https://kotlinlang.org/

Prim
  • 2,880
  • 2
  • 15
  • 29