简体   繁体   English

注释对android运行时有什么影响?

[英]do comments have any effect on the android runtime?

I like to add... 我喜欢加...

...    
System.out.println(" *description* "); 
...

... lines to my code blocks for debugging purposes (mostly to catch runtime and logic errors. I usually delete them, but lately I have been just adding "//" before them so that they stay there to prevent having to retype them, slash, to use them as a marker reminding myself that I've already debugged that part. ...行到我的代码块中以进行调试(主要是为了捕获运行时和逻辑错误。我通常将其删除,但是最近我一直在它们之前添加“ //”,以便它们停留在此处以防止必须重新输入它们,斜线,将它们用作标记,使自己想起我已经调试了该部分。

Is better to delete these "debug println's" rather than adding "//" before them, or if they would both have the same effect on the app runtime ? 最好删除这些“ debug println's”,而不是在它们之前添加“ //”,或者如果它们对应用程序运行时具有相同的效果,则更好?

Any insights appreciated. 任何见解表示赞赏。

No. By design comments are not used as part of the code (except with certain cases of javadocs appearing in jars). 否。按设计,注释不用作代码的一部分(除非在jar中出现某些javadocs情况)。

The compiler which translates the source code into JVM bytecode will simply ignore the comments, 将源代码转换为JVM字节码的编译器只会忽略注释,

Comments and commented out code have no effect on runtime performance. 注释和注释掉的代码对运行时性能没有影响。 None whatsoever. 没有任何。 (Not even javadoc comments!!) (甚至没有javadoc注释!)

However, leaving "commented out" debug statements in your code is bad practice because it makes your code a lot harder to read. 但是,在代码中保留“注释掉”的调试语句是一种不好的做法,因为这会使您的代码难以阅读。 Obviously, this is not a concern while you are debugging ... but you shouldn't leave them there in the long term. 显然, 在调试时这不是问题……但是从长远来看,您不应该将它们留在那儿。

I recommend two alternatives: 我建议两种选择:

  • Replace the println calls with use of a logging framework, and log those things at "debug" level. 使用日志记录框架替换println调用,并在“调试”级别记录这些内容。 You need to be a bit careful because logging does have an impact on performance. 您需要谨慎一点,因为日志记录确实会影响性能。 But there are ways to minimize the impact ... depending on the framework you are using. 但是有一些方法可以最大程度地减少影响……取决于您使用的框架。

  • Use your version control history to keep a snapshot of the code with the debug statements active. 使用版本控制历史记录来使代码快照保持活动状态,并启用调试语句。 Then delete them. 然后删除它们。

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM