This is a migrated thread and some comments may be shown as answers.

just code - clean code unexpected results

2 Answers 112 Views
Refactorings
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
ManniAT
Top achievements
Rank 2
ManniAT asked on 10 Jun 2011, 02:22 PM
Hi,

I just tried to use "Clean code" - I built a clone and all I marked was
"Remove unused usings"
"Order usings"

First thing - I expected this would handle my whole project :)
I found this works only for the current file.
Which means I could also use "Organize and Add missing usings".
And - see below - both "version" provide the same unwanted result.

First a question - is there a way to do this (remove / sort usings) for all files in a project?
Because else I could also use the "internal" version which works better (of course as long as I don't have to add missing ones).

Why it works better?
The simple reason - JC (no mater if I use "Clean code" ot "Organize....") has a problem when no using is needed in a file.
I breaks my code formatting!

This how the code looks when a using is left (and that's my formatting - choosen in VS):
Snippet created with CBEnhancer
using KBSM.Classes;

namespace KBSM.DataThings {
    public partial class OL4Lock {
        private bool m_bIsByteAssigning;
        CustomerFileHelper cfH;

        partial void OnOL4BytesChanged() {
            if(!m_bIsByteAssigning) {
                OL4_B0 = (byte)(OL4Bytes & 0xFF);
                OL4_B1 = (byte)((OL4Bytes >> 8) & 0xFF);
                OL4_B2 = (byte)((OL4Bytes >> 16) & 0xFF);
                OL4_B3 = (byte)((OL4Bytes >> 24) & 0xFF);
                SendPropertyChanged("OL4_Str");
            }
        }
        partial void OnLoaded() {
            OnOL4BytesChanged();
        }
    }
}
And this is how it looks when no using is left:
Snippet created with CBEnhancer
namespace KBSM.DataThings
{
    public partial class OL4Lock
    {
        private bool m_bIsByteAssigning;
    partial void OnOL4BytesChanged() {
            if(!m_bIsByteAssigning) {
                OL4_B0 = (byte)(OL4Bytes & 0xFF);
                OL4_B1 = (byte)((OL4Bytes >> 8) & 0xFF);
                OL4_B2 = (byte)((OL4Bytes >> 16) & 0xFF);
                OL4_B3 = (byte)((OL4Bytes >> 24) & 0xFF);
                SendPropertyChanged("OL4_Str");
            }
        }
        partial void OnLoaded() {
            OnOL4BytesChanged();
        }
    }
}

Notice the braces.

And a last thing about code formatting.
I have the following (bad formatted) code section:
Snippet created with CBEnhancer
#region OL4_Str
public string OL4_Str {
    get { return OL4Bytes.ToString("X8"); }
    set { }
}
#endregion

#region OL4_B0
private byte m_byOL4_B0;
public byte OL4_B0 {
    get { return m_byOL4_B0; }            set {
        if(m_byOL4_B0 != value) {                    m_byOL4_B0 = value;
            uint nTmp = (uint)OL4Bytes;
            nTmp &= 0xffffff00;
            nTmp |= m_byOL4_B0;
            m_bIsByteAssigning = true;
            OL4Bytes = (int)nTmp;
            m_bIsByteAssigning = false;
            OL4_B0Str = m_byOL4_B0.ToString("X2");
            SendPropertyChanged("OL4_B0");                }
    }
}
#endregion

Navigating to the end of the file - remve the last brace and enter it again (forces VS to format the code) brings this:
Snippet created with CBEnhancer
#region OL4_Str
public string OL4_Str {
    get { return OL4Bytes.ToString("X8"); }
    set { }
}
#endregion

#region OL4_B0
private byte m_byOL4_B0;
public byte OL4_B0 {
    get { return m_byOL4_B0; }
    set {
        if(m_byOL4_B0 != value) {
            m_byOL4_B0 = value;
            uint nTmp = (uint)OL4Bytes;
            nTmp &= 0xffffff00;
            nTmp |= m_byOL4_B0;
            m_bIsByteAssigning = true;
            OL4Bytes = (int)nTmp;
            m_bIsByteAssigning = false;
            OL4_B0Str = m_byOL4_B0.ToString("X2");
            SendPropertyChanged("OL4_B0");
        }
    }
}
#endregion
Using just code - format code brings this:
Snippet created with CBEnhancer
#region OL4_Str
public string OL4_Str {
    get {
        return OL4Bytes.ToString("X8");
    }
    set {
    }
}
#endregion

#region OL4_B0
private byte m_byOL4_B0;

public byte OL4_B0 {
    get {
        return m_byOL4_B0;
    }
    set {
        if(m_byOL4_B0 != value) {
            m_byOL4_B0 = value;
            uint nTmp = (uint)OL4Bytes;
            nTmp &= 0xffffff00;
            nTmp |= m_byOL4_B0;
            m_bIsByteAssigning = true;
            OL4Bytes = (int)nTmp;
            m_bIsByteAssigning = false;
            OL4_B0Str = m_byOL4_B0.ToString("X2");
            SendPropertyChanged("OL4_B0");
        }
    }
}
#endregion

In options I set every brace position to "Same line".
Two things are wrong here:
a.) Singele line setters / getters (event if empty) become multiline
b.) after the backing field I get a newline

Is there a way to set the JC options that it could "reproduce" what VS does?

Thank you
Manfred

2 Answers, 1 is accepted

Sort by
0
ManniAT
Top achievements
Rank 2
answered on 10 Jun 2011, 02:27 PM
About the "formatting issues."
The VS Rules are pretty simple:
If the whole thing (setter / getter) is in one line keep it like this. (it only removes extra blanks and so on which is OK).
Else - format the block like it should be from the settings.
And - don't add an extra line after a backing filed (or in front of a property).

I thought there was a "Use VS settings" in earlier JC version. I can't find it any more (or the presence of this option was just a day dream :)).

Manfred
0
Kaloyan
Telerik team
answered on 16 Jun 2011, 12:41 PM
Hello ManniAT,

To start off, the files that are affected by the code cleaner are determined by your choice of a starting point of the code cleaning functionality. E.g. you can right-click on any node in the Solution Explorer and run the cleaner from there (Just Clean All...), thus, it'll affect all the child nodes of the selected node - in your case all files inside a folder, project, or a solution. So the answer to your question is yes, there's a way to run JC's code cleaner on all files in a project.
As for formatting of single line getters/setters, we definitely do as you describe. We're currently working on improving our formatter and one of the tasks there is to keep the formatting of one-line blocks if they're on the same line with the braces. We'll try to squeeze that in our next major release, so stay tuned.

All the best,
Kaloyan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Refactorings
Asked by
ManniAT
Top achievements
Rank 2
Answers by
ManniAT
Top achievements
Rank 2
Kaloyan
Telerik team
Share this question
or