Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More .NET 8 code cleanup #116

Merged
merged 25 commits into from
May 10, 2024
Merged

More .NET 8 code cleanup #116

merged 25 commits into from
May 10, 2024

Conversation

shpaass
Copy link
Owner

@shpaass shpaass commented May 8, 2024

It turned out all those inspections that were re-enabled and didn't fix anything were just dormant for some reason. Perhaps I should've restarted Visual Studio for them to work.

This PR includes three parts:

csharp_style_expression_bodied_methods = true:suggestion
csharp_style_expression_bodied_constructors = true:suggestion
csharp_style_expression_bodied_operators = true:suggestion

The main reason for these changes is that we already enabled arrow notation for the properties, and the other parts of the code stop looking weird with arrows if you read them for a bit.

  • The improvements from unmuted suggestions.
  • A bugfix that I found when applying suggestions.

I put the potentially controversial changes at the end, so we can drop them easier if necessary.

QA: checked the basic functionality on an existing Pyanodons project file.

Copy link
Collaborator

@veger veger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not a big fan of the arrow notations, but I guess it is due to me not being used o it. So let's hope I can get used to it as well! 😄

Yafc.UI/Core/DrawingSurface.cs Outdated Show resolved Hide resolved
Yafc/Workspace/ProductionTable/ProductionTableView.cs Outdated Show resolved Hide resolved
@shpaass shpaass force-pushed the feature/more-code-cleanup branch from 6c61e3a to 86294ca Compare May 9, 2024 11:54
@shpaass
Copy link
Owner Author

shpaass commented May 9, 2024

Resolved merge conflict.

@shpaass shpaass force-pushed the feature/more-code-cleanup branch from 6757002 to c3f7b33 Compare May 10, 2024 07:57
@shpaass
Copy link
Owner Author

shpaass commented May 10, 2024

Resolved a merge conflict:

$ git range-diff master..6757002 master..c3f7b33
 1:  bf03b42 =  1:  826c8a3 feat: apply IDE0290 Use primary constructor
 2:  f0bf681 !  2:  c922979 feat: apply IDE0028 and IDE0305 Use collection initializers or expressions
    @@ Yafc/Workspace/ProductionTable/ProductionTableView.cs: goodsHaveNoProduction:;
                  });
     -            var allProduction = goods == null ? Array.Empty<Recipe>() : variants == null ? goods.production : variants.SelectMany(x => x.production).Distinct().ToArray();
     +            var allProduction = goods == null ? [] : variants == null ? goods.production : variants.SelectMany(x => x.production).Distinct().ToArray();
    +             Recipe[] fuelUseList = goods?.fuelFor.AsEnumerable().OfType<EntityCrafter>().SelectMany(e => e.recipes).OfType<Recipe>().Distinct().OrderBy(e => e, DataUtils.DefaultRecipeOrdering).ToArray() ?? [];
                  var fuelDisplayFunc = recipe?.entity?.energy.type == EntityEnergyType.FluidHeat
                      ? (Func<Goods, string>)(g => DataUtils.FormatAmount(g.fluid?.heatValue ?? 0, UnitOfMeasure.Megajoule))
    -                 : g => DataUtils.FormatAmount(g.fuelValue, UnitOfMeasure.Megajoule);
 3:  96e1ee3 =  3:  f4d743b fix: rename a constant to be upper-case
 4:  e18bef7 =  4:  f55a89b feat: apply IDE0251 Member can be made 'readonly'
 5:  e370ff0 =  5:  0c9f368 feat: apply CA1822: Mark members as static
 6:  b1ceb69 =  6:  4300c58 feat: apply CA2211: Non-constant fields should not be visible
 7:  66319e9 =  7:  e51a590 feat: incorporate solver names into their variable names
 8:  145ca73 =  8:  ad5a787 feat: suppress CA1861 for tests
 9:  71a62f1 =  9:  598604a feat: apply CA1865-CA1867: Use char instead of string
10:  0039085 = 10:  148f076 feat: suppress "CA1816: add GC.SuppressFinalize()"
11:  3efc1d3 = 11:  ebfdbd6 feat: run code cleanup on the whole solution
12:  29e3ab2 = 12:  8923db7 feat: enable csharp-style bodied methods
13:  caa6ced = 13:  a48ea63 fix: accumulator was not changing
14:  e637ba1 = 14:  6fd53ad feat: apply IDE0290 Use primary constructor
15:  1cca908 = 15:  c0ca8aa feat: use more collection expressions
16:  aa8f422 = 16:  b3a572a chore: remove unused methods
17:  4f814a0 = 17:  53ee45e chore: rename variable
18:  a01b52a = 18:  d2b983f feat: remove unused variable in FactorioDataSource.CanLoad
19:  83aefad = 19:  6d2dcca feat: use expression bodies for methods
20:  587a1b0 = 20:  4adbb3b chore: improve comment formatting
21:  27a7da0 = 21:  cf36074 chore: suppress "CA2211 Non-constant fields should not be visible" once
22:  af31c76 = 22:  babbbfd feat: convert methods to lambdas where possible
23:  0129d82 = 23:  3556c8f feat: csharp_style_expression_bodied_constructors = true:suggestion
24:  c1ba1f6 = 24:  4b2a303 feat: csharp_style_expression_bodied_operators = true:suggestion
25:  86294ca = 25:  394bb6f chore: rename variable
26:  6757002 = 26:  c3f7b33 feat: use 'using' instead of Dispose, for brevity

The diff above shows that allProduction was changed as previously, when fuelUseList was added, and the last line of the patch was dropped because the context was increased by one line.

shpaass added a commit that referenced this pull request May 10, 2024
shpaass added 21 commits May 10, 2024 10:34
This inspection makes a field or method of a struct readonly if
it's possible, because in general if you use a struct, you want
it to be readonly. Otherwise you should've used a class.
The changed members did not access any instance data, so they could
be marked as static.
Other than that, the solver names were not used anywhere.
Full name: CA1865-CA1867: Use 'string.Method(char)' instead of 'string.Method(string)' for string with single char
Applied this inspection because the char version is faster.
I don't know why it didn't detect all applications of the inspections
 yesterday, so here we go again.

This commit mostly converts empty array inits into []. It also does
some conversions of logic expressions into "is, is not".
I don't understand how exactly this tangle of functions works,
but the fix works and the bug is gone.

Steps to reproduce a bug:
1. Select a sole recipe to produce electricity. You will be given a
recipe through solar panels and accumulators.
2. Change accumulator twice.
3. Notice that after the first change, the accumulator doesn't change
even though you selected a different one.
Merging this commit with the previous one about IDE0290 is pure hell,
so I'm not doing that.
It's weird that there is an unused variable.
tag: potential bug
In other words, write methods as lambdas if applicable.
From one side, it shortens the method descriptions.
From the other side, we need to set max line length around 150-180
for it to be readable. However, Visual Studio seems to not have a
setting for that, so we'll need to curate line length ourselves.

Looking at this lambda spaghetti, I start to suspect that
Inversion of Control might be useful there, so we don't pass a GUI
object through half the files.
csharp_style_expression_bodied_methods = true:suggestion

Now that I read code a bit more, the arrow notation in methods
doesn't look cursed anymore.

The rule of thumb is that if you see "=>" after the method signature,
that means that this method returns the value from what follows after
the arrow.
Same as with methods, the more you work with that code, the less cursed
it feels.
as with the previous changes to editorconfig a couple of commit before,
the more you work with the code, the less you are weirded out by the
arrow notation.
@shpaass shpaass force-pushed the feature/more-code-cleanup branch from c3f7b33 to d2434c0 Compare May 10, 2024 08:42
@shpaass
Copy link
Owner Author

shpaass commented May 10, 2024

Resolved more merge conflicts.

$ git range-diff master..c3f7b33 master..d2434c0
 1:  826c8a3 =  1:  e12d149 feat: apply IDE0290 Use primary constructor
 2:  c922979 =  2:  2b54293 feat: apply IDE0028 and IDE0305 Use collection initializers or expressions
 3:  f4d743b =  3:  e571665 fix: rename a constant to be upper-case
 4:  f55a89b =  4:  f225ef0 feat: apply IDE0251 Member can be made 'readonly'
 5:  0c9f368 =  5:  d50ea07 feat: apply CA1822: Mark members as static
 6:  4300c58 =  6:  7a64837 feat: apply CA2211: Non-constant fields should not be visible
 7:  e51a590 =  7:  49a37b1 feat: incorporate solver names into their variable names
 8:  ad5a787 =  8:  d4020d8 feat: suppress CA1861 for tests
 9:  598604a =  9:  d637b96 feat: apply CA1865-CA1867: Use char instead of string
10:  148f076 <  -:  ------- feat: suppress "CA1816: add GC.SuppressFinalize()"
11:  ebfdbd6 = 10:  adce475 feat: run code cleanup on the whole solution
12:  8923db7 = 11:  13664a3 feat: enable csharp-style bodied methods
13:  a48ea63 = 12:  afdd8ff fix: accumulator was not changing
14:  6fd53ad = 13:  b06c1fd feat: apply IDE0290 Use primary constructor
15:  c0ca8aa = 14:  3c193ba feat: use more collection expressions
16:  b3a572a = 15:  898b161 chore: remove unused methods
17:  53ee45e = 16:  c120194 chore: rename variable
18:  d2b983f = 17:  52ca2aa feat: remove unused variable in FactorioDataSource.CanLoad
19:  6d2dcca = 18:  f5e9bcd feat: use expression bodies for methods
20:  4adbb3b = 19:  2ba415d chore: improve comment formatting
21:  cf36074 = 20:  4361d18 chore: suppress "CA2211 Non-constant fields should not be visible" once
22:  babbbfd ! 21:  b5342bd feat: convert methods to lambdas where possible
    @@ Yafc.UI/Core/DrawingSurface.cs: namespace Yafc.UI {
     -        }
     +        public TextureHandle CreateTextureFromSurface(IntPtr surface) => new TextureHandle(this, SDL.SDL_CreateTextureFromSurface(renderer, surface));

    --        public TextureHandle CreateTexture(uint format, int access, int w, int h) {
    --            return new TextureHandle(this, SDL.SDL_CreateTexture(renderer, format, access, w, h));
    --        }
    -+        public TextureHandle CreateTexture(uint format, int access, int w, int h) => new TextureHandle(this, SDL.SDL_CreateTexture(renderer, format, access, w, h));
    -     }
    -
    -     public abstract class SoftwareDrawingSurface(IntPtr surface, float pixelsPerUnit) : DrawingSurface(pixelsPerUnit) {
    +         public TextureHandle CreateTexture(uint format, int access, int w, int h) {
    +             return new TextureHandle(this, SDL.SDL_CreateTexture(renderer, format, access, w, h));
     @@ Yafc.UI/Core/DrawingSurface.cs: namespace Yafc.UI {

              public override Window window => null;
23:  3556c8f = 22:  1ad832b feat: csharp_style_expression_bodied_constructors = true:suggestion
24:  4b2a303 = 23:  c62e9df feat: csharp_style_expression_bodied_operators = true:suggestion
25:  394bb6f = 24:  63134e0 chore: rename variable
26:  c3f7b33 = 25:  d2434c0 feat: use 'using' instead of Dispose, for brevity

The diff above shows that I dropped one of the arrow-conversions to not bother if I merged correctly or not. Also, the commit about GC.SuppressFinalize got dropped completely because it was superseded by #118

@shpaass shpaass requested a review from Dorus May 10, 2024 08:48
Copy link
Collaborator

@Dorus Dorus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One error suppression we probably wont need anymore. The other comments are probably something for in a less crowded pr 👍

Yafc.Model.Tests/Analysis/Milestones.cs Show resolved Hide resolved
Yafc.Model/Data/SortedList.cs Show resolved Hide resolved
Yafc.Parser/FactorioDataSource.cs Show resolved Hide resolved
@shpaass shpaass merged commit 19b1d24 into master May 10, 2024
1 check passed
@shpaass shpaass deleted the feature/more-code-cleanup branch May 10, 2024 11:07
@@ -215,7 +205,7 @@ private class ExportMaterial {
deflateStream.CopyTo(ms);
byte[] bytes = ms.GetBuffer();
int index = 0;
if (DataUtils.ReadLine(bytes, ref index) != "YAFC" || DataUtils.ReadLine(bytes, ref index) != "ProjectPage") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed this one, but this is not a valid refactoring.

afbeelding

This gives a new error in the latest master:
afbeelding

On a separate note: Export project page to cliborad exports json, import project page from clipboard expect base64. So i couldnt test this function, but as you see in the watch screenshot it's not working as intended.

shpaass added a commit that referenced this pull request May 10, 2024
The pattern matching changed the behavior of the code into the wrong one.
See #116 (comment)

Notice the refactor operator did warn this might not be a correct refactoring:
![afbeelding](https://github.com/have-fun-was-taken/yafc-ce/assets/4461459/5a3702fb-6326-418a-9777-b538867db8de)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants