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);
}
10
  • 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. Commented Nov 25, 2015 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 :) Commented Nov 25, 2015 at 19:34
  • 4
    Use an IDE with code completion so that you don't have to type the complete words all the time. Commented Nov 25, 2015 at 19:35
  • 1
    Compilers don't do that. Commented Nov 25, 2015 at 19:36
  • 3
    Hopefully, you don't get to type InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonWindowNotFocusedState too often. Commented Nov 25, 2015 at 19:37

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. I use Eclipse, but my cheap laptop is often not fast enough for the autocomplete. So I end up typing it anyway.
if eclipse is slow, try to use two generations older versions of eclipse.
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.

3 Comments

cc is fast enough, but his laptop not.
True for both. cc is fast if my laptop would be faster. And for a good cc you have to type half the word sometimes. Would be much better to have a list with words that the IDE will auto replace when starting the program.
FORTUNATLEY: Java doesn't have a pre-processor
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();

3 Comments

@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 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.
Just my opinion, but altering program structure to save typing seems like misaligned priorities...
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.