开发者

Why does my source code (written in Eclipse) look different in other text editors?

开发者 https://www.devze.com 2023-04-10 02:03 出处:网络
I\'ve been using Eclipse to do my CS assignments, as recommended by my professor. However, I\'ve noticed that if I open my source code in a different text editor, my beautiful, perfect formatting look

I've been using Eclipse to do my CS assignments, as recommended by my professor. However, I've noticed that if I open my source code in a different text editor, my beautiful, perfect formatting looks wrong. 开发者_C百科I believe the problem lies in tabs. A tab character seems to take up less space in eclipse than in other text editors.

A good chunk of our grade is determined by the neatness of our code. I'm not sure if our programs are graded in eclipse or not, so I'd like to figure out how to make source code have the same formatting regardless of text editor.

Is this a problem with Eclipse? Are there settings I can fiddle with?


This is probably due to your settings for the tab symbol. If you really want to indent using tab, make sure it is set to 8 spaces everywhere.

From the Java coding convention:

Four spaces should be used as the unit of indentation. The exact construction of the indentation (spaces vs. tabs) is unspecified. Tabs must be set exactly every 8 spaces (not 4).

Personally I always use spaces to indent my code due to the fact that some people have their tab symbol set to show as 4 spaces.

To set Eclipse to always use spaces, go to

Window -> Preferences -> Java -> Code style -> Formatter -> Edit

and set Tab policy to Spaces Only.


The tab size is probably different in other editors. It still is in its raw form a \t. Depending on what the editor will display when it encounters one is probably found in the preference. Either way it should be consistent in size all the way across the file.

You could also convert tabs to spaces so it is always the same regardless of editor.


The tab character does not have a defined display width. In notepad, it is displayed as the same width as 8 spaces wide. All code editors should allow the viewer to change the displayed tab width. The convention for code is normally a width equivalent to 4 spaces.

If you're desperate, you could replace all tabs with 4 spaces. However, this is frowned upon by some, and may lose you marks. I'm pretty sure that the advised java coding style advocates the use of tabs, not spaces.

At the end of the day, the marker is a fool if he'll turn down syntax highlighting, and the code editor that comes with it.

0

精彩评论

暂无评论...
验证码 换一张
取 消