Code Separation / Printing Format - Official Mentioned

I am looking for a quick and efficient way to split code and make it printable. I work in coder magazine, and it is always very difficult to prepare the code for printing, because the space in one column is limited. Now I do it manually, and it is very tiring. Let me give you an example. This is the code I'm working on right now (already formatted / dictated in my own style), it is for Android:

private void GetFiles() { listBox1.Items.Clear(); var storeFile = IsolatedStorageFile. GetUserStoreForApplication(); string fileString = System.IO.Path.GetFileName("*"); string[] files = storeFile.GetFileNames("*"); for (int i = 0; i < storeFile.GetFileNames("/" + fileString).Length; i++) { String fileName = storeFile.GetFileNames(fileString)[i]; String ext = fileName.Substring(fileName.Length - 3); if (ext == "png") listBox1.Items.Add(fileName); 

}}

note that

 var storeFile = IsolatedStorageFile. GetUserStoreForApplication(); 

- one line. This is too long for a column for the press, so I separate the closest logical gap (here is the period) and the indents of the second part of the line with 4 spaces (inline always indents by 2 spaces). I know this is not the best way, it is difficult to understand. That's why I'm looking for an authoritative answer from a wide range of software developers. I have to work not only with C ++ code, but also with Assembler, JavaScript, Ruby and Python. The latter is a separate issue - it depends on the indentation, so I have to be especially careful with the indentation.

So the question is: which is the best way to format code in a limited log space? How do you prefer? Please suggest your own ideas. Each answer is highly appreciated.

UPD : this is what it looks like in the magazine now: Current look . This can help you solve this problem. Too long lines are broken into '\', and the last part of the line is indented. Not too obvious, is it? = (

+4
source share
2 answers

Why don't you just use the built-in Eclipse code formatter. You can easily adjust maxline length among other things. Then you just use ctrl + shift + f to format it according to your specific codestyle.

And, I did not understand that this is not a specific Java issue. The code you inserted here does not compile, and the editor refuses to format it. But if I use your screenshot, it will look like this by default (I changed the maximum length of the string a bit):

 if (Input.GetKeyDown("space")) { Vector3 prosition = new Vector3( tranform.position.x, transform.position.y + 1, transform.position.z); } Instantiate(ProjectilePrefab, position, Quaternion.Identiy); } 
+3
source

So ... in this loop you want to get a complete list of files, check the length, then get a complete list of files again (except for slightly different ones, because you left the "/" prefix) and hope that there is still a i th element. You repeat the process as many times as there are files to achieve a complex time complexity of O(2n^2) .

You, of course, publish this function as an example of what NOT to do, right?

To give preference to the layout, I would prefer that you split by = if it fits ... But now that you have edited with the sample, it looks like you have even less space for the game than you hinted at first.

If you were actually trying to publish a less expensive algorithm, you can save the search result in a variable and shorten most of the lines in this loop ...

+1
source

Source: https://habr.com/ru/post/1442285/


All Articles