Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to know: is there impact of any kind (for example memory related issues) when I have empty lines between the code blocks in a java class?
Not at all.
Empty lines do not matter at runtime, as the java compiler turns your source code into bytecode in the very first place. The only "consequence" of using empty lines will be the line number information that the compiler can include in the bytecode files. More empty lines, resulting in higher numbers. But that is really of no concern at all.
From that point of view, you strive to use vertical spacing for one purpose, and one purpose only: to communicate intent to human readers of your code. You use empty lines to "group" things that belong together. Of course, you use them with care: too many empty lines don't help with readability a bit. You don't want the reader to scroll around when there is not need to do so.
And just to add the concern by Ernest: yeah, theoretically you could add so many empty lines that your methods get too long/big ( see here). But well, that seems to be a limit for byte code size. As said, no empty lines in byte code, so not even a theoretical problem with that.
There is no impact on the compiled code, but having too much (or too little) whitespace may make your code harder to read.
For example, put blank lines between methods, and you can use it to make 'paragraphs' in your code. Where the paragraph contains closely related code, this can help readability (and it can also help when refactoring a single method into multiple methods).
However overdoing that (eg using a lot of blank lines or using blank lines between each line of code) can actually make it harder to read.
The only impact is for us
Your java code will be compiled in bytecode, i.e. a list of simple computation, ignoring whitespaces and empty lines.
However, that doesn't mean that tabs, spaces and empty lines are useles: they improve a lot code readability, so it's a good practice to indent code, and make some block using empty lines
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Bukkit's setData(data) and getData() are deprecated. But there's no replacement.
Bukkit/Spigot JavaDoc says this about setData():
Deprecated. Magic value
Why is that?
So far, the only way to do it is by using:
Block.setData(byte data);
So, you could do something like this:
myBlock.setData(2); // Set block data to 2
Although Block.setData() is deprecated, it still works, and will continue to work (deprecated methods in Bukkit are rarely removed, especially those for which there is no alternative).
I wish I could give a better answer, but that's the only thing that you can do, as of now.
The reason it is deprecated is because Minecraft is moving away from item IDs, and switching to item names, to make it easier to expand in the future. Where you used to have to run /give player 19, you are now supposed to run /give player minecraft:sponge (although the ID still works). The same thing is going to happen to data values, instead of giving someone 35:14, you now give them red wool.
To get rid of the warning given by using a deprecated method, put #SuppressWarnings("deprecation") above the deprecated method when you use it, or above the method in which it is used.
To set the type of the block, you could use:
Block.setType(Material type);
An example is:
myBlock.setType(Material.GOLD_BLOCK); // Set block to gold block
You could also use MaterialData, but no one really knows how to use it (as far as I know). It's one of the things included in the Bukkit API, but no one knows why.
The source of WorldEdit and most other big plugins look messy because they use a lot of interfaces. To the developers, it seems very organized, but to someone reading it, it looks very messy, unless you can actually visualize the hierarchy.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
There are ways to program that use little text, but many lines of code. There are others that require more typing, but use fewer lines of code. If there is a maximum length for a line of code, this means it exists as a predefined space of memory in the computer, and making many short lines will waste this predefined space. If this is the case, your program can be a lot smaller by putting in the time to consolidate onto fewer lines. Otherwise, many short, easier to program lines would be the obvious choice.
A method has give or take 65k bytes of bytecode. However there are no limits on how many lines you write apart from the system's possible limitations (if any).
However you should always follow code-style guidelines in respect to your language to make code readable.
Read more
To directly answer your question (as I should've done already) - No. There is no maximum length of a line in Java.
There is no maximum length for a line of Java except the maximum your computer can handle however I doubt you'll end up writing a line that long.
If there is a maximum length for a line of code, this means it exists as a predefined space of memory in the computer
Just because there's a limit doesn't mean the memory is preoccupied up to that limit. The allocation could happen dynamically.
And the code is not executed, but compiled into the program.
So the lines of code do not exist in the program.
"If there is a maximum length for a line of code, this means it exists as a predefined space of memory in the computer, and making many short lines will waste this predefined space."
Being able to read code is important. Writing short lines would be better for readability. In fact, this is why we have such practices like DRY (don't repeat yourself) and Object oriented programming for methods (you can class similar functions together if need be).
Imagine writing an entire program, and you write it all up on 1 line without using any white space.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Lots of times I see people naming their methods a(), b(), c(), etc. instead of giving them names that describe what the method actually does. What is the point of this?
My guess is that you were reading obfuscated code.
Here's a nice article.
They shouldn't be doing this. It's bad practice.
Either the developer is being lazy, or they are showing a very simple example(still bad practice).
EDIT: (Given the extra detail in your comments)
In that case this is probably done by some automated program. The code the developer is actually working on wouldn't be using these names.
To minimize the load time for .js files, many developers will "compress" them. Compressing also has an option to obfuscate the code, making it more difficult to steal and change by making it difficult to understand. This happens just as you describe, by changing the variable names and function names to "a", "b", "c", etc. This has the side-effect of making the code smaller, since the names are now shorter. An example of a web tool that does this for you is here: http://www.developerfusion.com/tools/compressjavascript/
The code base that you maintain is certainly NOT the one that has been compressed and/or obfuscated.
Nothing. They are just too lazy to follow the standard naming conventions maybe because they are simply doing some quick POC(proof of concept) and they don't want to waste their time in typing some meaningful method name.
But one must always follow the naming conventions in their actual projects where others might be looking/maintaining their code.
This has always been discouraged by every community into programming and probably is a sign of a bad developer.
For more information on why and how do we use coding conventions see this
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Using a general-purpose programming language like Java, what is the most efficient way to search through a ~20 page document to replace a set of 5000+ strings with some predetermined replacement string? The program should not replace any strings that have already been replaced. What data structure would be optimal to store the 5000+ strings and each of their replacements - two arrays, a dictionary, or something else?
Here are some of the options that I have considered so far:
Iterate through the entire .txt document once time per string using string.replace. The problem is that the algorithm must iterate through the entire .txt document an extra time for each string stored.
Iterate through the .txt once while replacing string as necessary while creating a new string by appending replacements. This seems more efficient, but each step would still require checking the entire set of 5000+ strings for any strings to replace.
Is there a more optimized means of solving this problem, or is one of the above attempts already optimal?
Also, would it be possible to run this algorithm more efficiently in a lower-level language like C?
You want to replace some string in 5000 strings and you want to make it optimal ... Now my question to you is: How will you know if you have to replace a string if you dont read the string? It's not possible, you have to read everything. And the shortest way to do that is to go line by line and replace immediatly. And somebody can correct me if i'm wrong, but reading a file is one of the most basic operations there is so using a library for that besides what is available by default in the programming language seems total overkill to me. Furthermore, every language has basic io and if it doesn't then don't use it.
To store strings, it all depends what you want to do with them. Different data structures have different purposes and some are better suited in some situations then others. If you just need to store them then a simple array is fine. However, if you need more advanced functions then you need to consider your options. But again it's all up to what you want to do with them later.
And there is the memory issue, you need to calculate how much memory your 5000+ strings will take, because you might run out of memory. Then you need to think if it's worth it to use all that memory. check this link
Finally your question about C, ofcourse it will be more efficient. Java runs in a virtual machine that adds considerable overhead. So basically your Java program runs in another Java program and if you know that there is a cost for every single operation then you understand that C will be more efficient then Java in terms of performance.
I would use the commons-lang library, which I think has exactly what you are looking for. Basically you create one array with all the strings you want to substitute and another array with the substitutions. See http://commons.apache.org/proper/commons-lang/javadocs/api-release/index.html for details on the StringUtils#replaceEach method.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
First off all, I know there are several questions about "Java inline". But they are all about how the compiler or JVM inlines function calls. I'm interested in doing this myself, or create some kind of a View for it. I want to define a function call of a class, and want to see everything inlined. Every method call should get inlined. I'm not sure how to handle instantiation of new objects, but it doesn't matter as much.
The goal is manual optimization, i.e. if a parameter is checked too often against null. Is there a tool to to something like this? I would prefer a GUI, but some kind of command line tool where I can specify a class function and it dumps some text somewhere will suffice, too.
EDIT:
For clearification:
Today I argued to use the NullObjectPattern, because some are defensively overchecking for nulls everywhere. This makes the code unreadable and unclean. I dont like it and wanted to have some kind of a tool, to show them how often they are actually checking the very same parameter again and again for null.
As was said: Don't guess, especially when you don't know what the JIT compiler will do after the code has been running for a while. You can waste infinite time infinitely improving something that accounts for 1% of runtime and only save 1%, or you can spend a short time getting a 10% improvement of something that accounts for 20% of your runtime and save 2%; the latter is by far a better choice.
The way you determine what's worth improving is by properly profiling your code after it has been fully warmed up.
And the way you get a significant improvement generally has more to do with improved algorithms than with microtuning of single instructions.