Skip to content

Commit 97d4678

Browse files
committed
ARROW-8882: [C#] Add .editorconfig to C# code
Adding an `.editorconfig` using the rules from dotnet/runtime repo. See: * https://github.com/dotnet/runtime/blob/master/docs/coding-guidelines/coding-style.md * https://github.com/dotnet/runtime/blob/master/.editorconfig Violating these rules won't fail the build, but Visual Studio will respect these rules when formatting the code and make suggestions. The bulk of the violations were `var` usages. There was one unnecessary usage of `this.`. And the rest were naming static fields with `s_`. In `BitUtility.cs`, I made a slight optimization using the pattern recognized by dotnet/roslyn#24621 instead of prefixing the fields with `s_`. Tagging anyone who has contributed to the C# library (please let me know if I missed anyone): @chutchinson @zgramana @nhustler @HashidaTKS @abbotware @pgovind @stephentoub Closes apache#7246 from eerhardt/Editorconfig Authored-by: Eric Erhardt <eric.erhardt@microsoft.com> Signed-off-by: Eric Erhardt <eric.erhardt@microsoft.com>
1 parent f16f76a commit 97d4678

32 files changed

Lines changed: 342 additions & 171 deletions

‎csharp/.editorconfig‎

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
root = true
18+
19+
# Default settings:
20+
# A newline ending every file
21+
# Use 4 spaces as indentation
22+
[*]
23+
insert_final_newline = true
24+
indent_style = space
25+
indent_size = 4
26+
trim_trailing_whitespace = true
27+
28+
# C# files
29+
[*.cs]
30+
# New line preferences
31+
csharp_new_line_before_open_brace = all
32+
csharp_new_line_before_else = true
33+
csharp_new_line_before_catch = true
34+
csharp_new_line_before_finally = true
35+
csharp_new_line_before_members_in_object_initializers = true
36+
csharp_new_line_before_members_in_anonymous_types = true
37+
csharp_new_line_between_query_expression_clauses = true
38+
39+
# Indentation preferences
40+
csharp_indent_block_contents = true
41+
csharp_indent_braces = false
42+
csharp_indent_case_contents = true
43+
csharp_indent_case_contents_when_block = true
44+
csharp_indent_switch_labels = true
45+
csharp_indent_labels = one_less_than_current
46+
47+
# Modifier preferences
48+
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion
49+
50+
# avoid this. unless absolutely necessary
51+
dotnet_style_qualification_for_field = false:suggestion
52+
dotnet_style_qualification_for_property = false:suggestion
53+
dotnet_style_qualification_for_method = false:suggestion
54+
dotnet_style_qualification_for_event = false:suggestion
55+
56+
# Types: use keywords instead of BCL types, and permit var only when the type is clear
57+
csharp_style_var_for_built_in_types = false:suggestion
58+
csharp_style_var_when_type_is_apparent = false:none
59+
csharp_style_var_elsewhere = false:suggestion
60+
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
61+
dotnet_style_predefined_type_for_member_access = true:suggestion
62+
63+
# name all constant fields using PascalCase
64+
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
65+
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
66+
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
67+
dotnet_naming_symbols.constant_fields.applicable_kinds = field
68+
dotnet_naming_symbols.constant_fields.required_modifiers = const
69+
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
70+
71+
# static fields should have s_ prefix
72+
dotnet_naming_rule.static_fields_should_have_prefix.severity = suggestion
73+
dotnet_naming_rule.static_fields_should_have_prefix.symbols = static_fields
74+
dotnet_naming_rule.static_fields_should_have_prefix.style = static_prefix_style
75+
dotnet_naming_symbols.static_fields.applicable_kinds = field
76+
dotnet_naming_symbols.static_fields.required_modifiers = static
77+
dotnet_naming_symbols.static_fields.applicable_accessibilities = private, internal, private_protected
78+
dotnet_naming_style.static_prefix_style.required_prefix = s_
79+
dotnet_naming_style.static_prefix_style.capitalization = camel_case
80+
81+
# internal and private fields should be _camelCase
82+
dotnet_naming_rule.camel_case_for_private_internal_fields.severity = suggestion
83+
dotnet_naming_rule.camel_case_for_private_internal_fields.symbols = private_internal_fields
84+
dotnet_naming_rule.camel_case_for_private_internal_fields.style = camel_case_underscore_style
85+
dotnet_naming_symbols.private_internal_fields.applicable_kinds = field
86+
dotnet_naming_symbols.private_internal_fields.applicable_accessibilities = private, internal
87+
dotnet_naming_style.camel_case_underscore_style.required_prefix = _
88+
dotnet_naming_style.camel_case_underscore_style.capitalization = camel_case
89+
90+
# Code style defaults
91+
csharp_using_directive_placement = outside_namespace:suggestion
92+
dotnet_sort_system_directives_first = true
93+
csharp_prefer_braces = true:refactoring
94+
csharp_preserve_single_line_blocks = true:none
95+
csharp_preserve_single_line_statements = false:none
96+
csharp_prefer_static_local_function = true:suggestion
97+
csharp_prefer_simple_using_statement = false:none
98+
csharp_style_prefer_switch_expression = true:suggestion
99+
100+
# Code quality
101+
dotnet_style_readonly_field = true:suggestion
102+
dotnet_code_quality_unused_parameters = non_public:suggestion
103+
104+
# Expression-level preferences
105+
dotnet_style_object_initializer = true:suggestion
106+
dotnet_style_collection_initializer = true:suggestion
107+
dotnet_style_explicit_tuple_names = true:suggestion
108+
dotnet_style_coalesce_expression = true:suggestion
109+
dotnet_style_null_propagation = true:suggestion
110+
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
111+
dotnet_style_prefer_inferred_tuple_names = true:suggestion
112+
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
113+
dotnet_style_prefer_auto_properties = true:suggestion
114+
dotnet_style_prefer_conditional_expression_over_assignment = true:refactoring
115+
dotnet_style_prefer_conditional_expression_over_return = true:refactoring
116+
csharp_prefer_simple_default_expression = true:suggestion
117+
118+
# Expression-bodied members
119+
csharp_style_expression_bodied_methods = true:refactoring
120+
csharp_style_expression_bodied_constructors = true:refactoring
121+
csharp_style_expression_bodied_operators = true:refactoring
122+
csharp_style_expression_bodied_properties = true:refactoring
123+
csharp_style_expression_bodied_indexers = true:refactoring
124+
csharp_style_expression_bodied_accessors = true:refactoring
125+
csharp_style_expression_bodied_lambdas = true:refactoring
126+
csharp_style_expression_bodied_local_functions = true:refactoring
127+
128+
# Pattern matching
129+
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
130+
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
131+
csharp_style_inlined_variable_declaration = true:suggestion
132+
133+
# Null checking preferences
134+
csharp_style_throw_expression = true:suggestion
135+
csharp_style_conditional_delegate_call = true:suggestion
136+
137+
# Other features
138+
csharp_style_prefer_index_operator = false:none
139+
csharp_style_prefer_range_operator = false:none
140+
csharp_style_pattern_local_over_anonymous_function = false:none
141+
142+
# Space preferences
143+
csharp_space_after_cast = false
144+
csharp_space_after_colon_in_inheritance_clause = true
145+
csharp_space_after_comma = true
146+
csharp_space_after_dot = false
147+
csharp_space_after_keywords_in_control_flow_statements = true
148+
csharp_space_after_semicolon_in_for_statement = true
149+
csharp_space_around_binary_operators = before_and_after
150+
csharp_space_around_declaration_statements = do_not_ignore
151+
csharp_space_before_colon_in_inheritance_clause = true
152+
csharp_space_before_comma = false
153+
csharp_space_before_dot = false
154+
csharp_space_before_open_square_brackets = false
155+
csharp_space_before_semicolon_in_for_statement = false
156+
csharp_space_between_empty_square_brackets = false
157+
csharp_space_between_method_call_empty_parameter_list_parentheses = false
158+
csharp_space_between_method_call_name_and_opening_parenthesis = false
159+
csharp_space_between_method_call_parameter_list_parentheses = false
160+
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
161+
csharp_space_between_method_declaration_name_and_open_parenthesis = false
162+
csharp_space_between_method_declaration_parameter_list_parentheses = false
163+
csharp_space_between_parentheses = false
164+
csharp_space_between_square_brackets = false
165+
166+
# Xml project files
167+
[*.{csproj,props,targets}]
168+
indent_size = 2
169+
charset = utf-8

‎csharp/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ Build from the Apache Arrow project root.
171171

172172
All build artifacts are placed in the **artifacts** folder in the project root.
173173

174+
# Coding Style
175+
176+
This project follows the coding style specified in [Coding Style](https://github.com/dotnet/runtime/blob/master/docs/coding-guidelines/coding-style.md).
177+
174178
# Updating FlatBuffers code
175179

176180
See https://google.github.io/flatbuffers/flatbuffers_guide_use_java_c-sharp.html for how to get the `flatc` executable.

‎csharp/src/Apache.Arrow/Arrays/BinaryArray.cs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public TArray Build(MemoryAllocator allocator = default)
7676
{
7777
ValueOffsets.Append(Offset);
7878

79-
var validityBuffer = NullCount > 0
79+
ArrowBuffer validityBuffer = NullCount > 0
8080
? ValidityBuffer.Build(allocator).ValueBuffer
8181
: ArrowBuffer.Empty;
8282

@@ -114,14 +114,14 @@ public TBuilder Append(ReadOnlySpan<byte> span)
114114

115115
public TBuilder AppendRange(IEnumerable<byte[]> values)
116116
{
117-
foreach (var arr in values)
117+
foreach (byte[] arr in values)
118118
{
119119
if (arr == null)
120120
{
121121
AppendNull();
122122
continue;
123123
}
124-
var len = ValueBuffer.Length;
124+
int len = ValueBuffer.Length;
125125
ValueOffsets.Append(Offset);
126126
ValueBuffer.Append(arr);
127127
ValidityBuffer.Append(true);
@@ -137,9 +137,9 @@ public TBuilder AppendRange(IEnumerable<byte> values)
137137
{
138138
return AppendNull();
139139
}
140-
var len = ValueBuffer.Length;
140+
int len = ValueBuffer.Length;
141141
ValueBuffer.AppendRange(values);
142-
var valOffset = ValueBuffer.Length - len;
142+
int valOffset = ValueBuffer.Length - len;
143143
ValueOffsets.Append(Offset);
144144
Offset += valOffset;
145145
ValidityBuffer.Append(true);
@@ -225,7 +225,7 @@ public int GetValueLength(int index)
225225
return 0;
226226
}
227227

228-
var offsets = ValueOffsets;
228+
ReadOnlySpan<int> offsets = ValueOffsets;
229229
return offsets[index + 1] - offsets[index];
230230
}
231231

‎csharp/src/Apache.Arrow/Arrays/BooleanArray.cs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private Builder NullableAppend(bool? value)
6262

6363
public Builder Append(ReadOnlySpan<bool> span)
6464
{
65-
foreach (var value in span)
65+
foreach (bool value in span)
6666
{
6767
Append(value);
6868
}
@@ -71,7 +71,7 @@ public Builder Append(ReadOnlySpan<bool> span)
7171

7272
public Builder AppendRange(IEnumerable<bool> values)
7373
{
74-
foreach (var value in values)
74+
foreach (bool value in values)
7575
{
7676
Append(value);
7777
}
@@ -85,7 +85,7 @@ public Builder AppendNull()
8585

8686
public BooleanArray Build(MemoryAllocator allocator = default)
8787
{
88-
var validityBuffer = NullCount > 0
88+
ArrowBuffer validityBuffer = NullCount > 0
8989
? ValidityBuffer.Build(allocator)
9090
: ArrowBuffer.Empty;
9191

@@ -154,10 +154,10 @@ public Builder Swap(int i, int j)
154154
{
155155
CheckIndex(i);
156156
CheckIndex(j);
157-
var bi = BitUtility.GetBit(ValueBuffer.Span, i);
158-
var biValid = BitUtility.GetBit(ValidityBuffer.Span, i);
159-
var bj = BitUtility.GetBit(ValueBuffer.Span, j);
160-
var bjValid = BitUtility.GetBit(ValidityBuffer.Span, j);
157+
bool bi = BitUtility.GetBit(ValueBuffer.Span, i);
158+
bool biValid = BitUtility.GetBit(ValidityBuffer.Span, i);
159+
bool bj = BitUtility.GetBit(ValueBuffer.Span, j);
160+
bool bjValid = BitUtility.GetBit(ValidityBuffer.Span, j);
161161
BitUtility.SetBit(ValueBuffer.Span, i, bj);
162162
BitUtility.SetBit(ValidityBuffer.Span, i, bjValid);
163163
BitUtility.SetBit(ValueBuffer.Span, j, bi);

‎csharp/src/Apache.Arrow/Arrays/Date32Array.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public Date32Array(ArrayData data)
5757

5858
public DateTimeOffset? GetDate(int index)
5959
{
60-
var value = GetValue(index);
60+
int? value = GetValue(index);
6161

6262
if (!value.HasValue)
6363
{

‎csharp/src/Apache.Arrow/Arrays/Date64Array.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public Date64Array(ArrayData data)
5757

5858
public DateTimeOffset? GetDate(int index)
5959
{
60-
var value = GetValue(index);
60+
long? value = GetValue(index);
6161

6262
if (!value.HasValue)
6363
{

‎csharp/src/Apache.Arrow/Arrays/ListArray.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public ListArray Build(MemoryAllocator allocator = default)
8181
{
8282
Append();
8383

84-
var validityBuffer = NullCount > 0
84+
ArrowBuffer validityBuffer = NullCount > 0
8585
? ValidityBufferBuilder.Build(allocator).ValueBuffer
8686
: ArrowBuffer.Empty;
8787

@@ -166,7 +166,7 @@ public int GetValueLength(int index)
166166
return 0;
167167
}
168168

169-
var offsets = ValueOffsets;
169+
ReadOnlySpan<int> offsets = ValueOffsets;
170170
return offsets[index + 1] - offsets[index];
171171
}
172172

‎csharp/src/Apache.Arrow/Arrays/PrimitiveArray.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ protected PrimitiveArray(ArrayData data)
4444

4545
public IList<T?> ToList(bool includeNulls = false)
4646
{
47-
var span = Values;
47+
ReadOnlySpan<T> span = Values;
4848
var list = new List<T?>(span.Length);
4949

50-
for (var i = 0; i < span.Length; i++)
50+
for (int i = 0; i < span.Length; i++)
5151
{
52-
var value = GetValue(i);
52+
T? value = GetValue(i);
5353

5454
if (value.HasValue)
5555
{

‎csharp/src/Apache.Arrow/Arrays/PrimitiveArrayBuilder.cs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public TBuilder Append(TFrom value)
4646
public TBuilder Append(ReadOnlySpan<TFrom> span)
4747
{
4848
ArrayBuilder.Reserve(span.Length);
49-
foreach (var value in span)
49+
foreach (TFrom value in span)
5050
{
5151
Append(value);
5252
}
@@ -139,15 +139,15 @@ public TBuilder Append(T value)
139139

140140
public TBuilder Append(ReadOnlySpan<T> span)
141141
{
142-
var len = ValueBuffer.Length;
142+
int len = ValueBuffer.Length;
143143
ValueBuffer.Append(span);
144144
ValidityBuffer.AppendRange(Enumerable.Repeat(true, ValueBuffer.Length - len));
145145
return Instance;
146146
}
147147

148148
public TBuilder AppendRange(IEnumerable<T> values)
149149
{
150-
var len = ValueBuffer.Length;
150+
int len = ValueBuffer.Length;
151151
ValueBuffer.AppendRange(values);
152152
ValidityBuffer.AppendRange(Enumerable.Repeat(true, ValueBuffer.Length - len));
153153
return Instance;
@@ -177,7 +177,7 @@ public TBuilder Set(int index, T value)
177177

178178
public TBuilder Swap(int i, int j)
179179
{
180-
var x = ValueBuffer.Span[i];
180+
T x = ValueBuffer.Span[i];
181181
ValueBuffer.Span[i] = ValueBuffer.Span[j];
182182
ValueBuffer.Span[j] = x;
183183
ValidityBuffer.Swap(i, j);
@@ -186,7 +186,7 @@ public TBuilder Swap(int i, int j)
186186

187187
public TArray Build(MemoryAllocator allocator = default)
188188
{
189-
var validityBuffer = NullCount > 0
189+
ArrowBuffer validityBuffer = NullCount > 0
190190
? ValidityBuffer.Build(allocator).ValueBuffer
191191
: ArrowBuffer.Empty;
192192

‎csharp/src/Apache.Arrow/Arrays/StringArray.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ public Builder Append(string value, Encoding encoding = null)
4141
return AppendNull();
4242
}
4343
encoding = encoding ?? DefaultEncoding;
44-
var span = encoding.GetBytes(value);
44+
byte[] span = encoding.GetBytes(value);
4545
return Append(span.AsSpan());
4646
}
4747

4848
public Builder AppendRange(IEnumerable<string> values, Encoding encoding = null)
4949
{
50-
foreach (var value in values)
50+
foreach (string value in values)
5151
{
5252
Append(value, encoding);
5353
}
@@ -74,7 +74,7 @@ public string GetString(int index, Encoding encoding = default)
7474
{
7575
encoding = encoding ?? DefaultEncoding;
7676

77-
var bytes = GetBytes(index);
77+
ReadOnlySpan<byte> bytes = GetBytes(index);
7878

7979
if (bytes == default)
8080
{

0 commit comments

Comments
 (0)