Skip to content

Using interpolated strings for better code readability. #840

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

Merged
merged 1 commit into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions projects/Apigen/apigen/AmqpEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,10 @@ public string DocumentationComment(string prefixSpaces, string docXpath) {
public string DocumentationComment(string prefixSpaces, string docXpath, string tagname) {
string docStr = GetString(docXpath, "").Trim();
if (docStr.Length > 0) {
return (prefixSpaces + "/// <"+tagname+">\n" +
GetString(docXpath, "") + "\n</"+tagname+">")
.Replace("\n", "\n" + prefixSpaces + "/// ");
return $"{prefixSpaces}/// <{tagname}>\n{GetString(docXpath, "")}\n</{tagname}>"
.Replace("\n", $"\n{prefixSpaces}/// ");
} else {
return prefixSpaces + "// (no documentation)";
return $"{prefixSpaces}// (no documentation)";
}
}
}
Expand Down
38 changes: 19 additions & 19 deletions projects/Apigen/apigen/Apigen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static string GetString(XmlNode n0, string path)
string s = GetString(n0, path, null);
if (s == null)
{
throw new Exception("Missing spec XML node: " + path);
throw new Exception($"Missing spec XML node: {path}");
}
return s;
}
Expand Down Expand Up @@ -248,7 +248,7 @@ public void HandleOption(string opt)
}
else
{
Console.Error.WriteLine("Unsupported command-line option: " + opt);
Console.Error.WriteLine($"Unsupported command-line option: {opt}");
Usage();
}
}
Expand Down Expand Up @@ -294,7 +294,7 @@ public void Generate()

public void LoadSpec()
{
Console.WriteLine("* Loading spec from '" + m_inputXmlFilename + "'");
Console.WriteLine($"* Loading spec from '{m_inputXmlFilename}'");
m_spec = new XmlDocument();

using (var stream = new FileStream(m_inputXmlFilename, FileMode.Open, FileAccess.Read))
Expand Down Expand Up @@ -358,12 +358,12 @@ public string MapDomain(string d)

public string VersionToken()
{
return "v" + m_majorVersion + "_" + m_minorVersion;
return $"v{m_majorVersion}_{m_minorVersion}";
}

public void GenerateOutput()
{
Console.WriteLine("* Generating code into '" + m_outputFilename + "'");
Console.WriteLine($"* Generating code into '{m_outputFilename}'");

string directory = Path.GetDirectoryName(m_outputFilename);

Expand Down Expand Up @@ -1069,11 +1069,11 @@ public void EmitContentHeaderFactory(MethodInfo method)
EmitLine(" {");
if (Attribute(method, typeof(AmqpUnsupportedAttribute)) != null)
{
EmitLine(string.Format(" throw new UnsupportedMethodException(\"" + method.Name + "\");"));
EmitLine($" throw new UnsupportedMethodException(\"{method.Name}\");");
}
else
{
EmitLine(" return new " + MangleClass(contentClass) + "Properties();");
EmitLine($" return new {MangleClass(contentClass)}Properties();");
}
EmitLine(" }");
}
Expand Down Expand Up @@ -1101,7 +1101,7 @@ public void MaybeEmitModelMethod(MethodInfo method)
{
EmitModelMethodPreamble(method);
EmitLine(" {");
EmitLine(" throw new UnsupportedMethodException(\"" + method.Name + "\");");
EmitLine($" throw new UnsupportedMethodException(\"{method.Name}\");");
EmitLine(" }");
}
else
Expand Down Expand Up @@ -1198,7 +1198,7 @@ public void LookupAmqpMethod(MethodInfo method,

if (amqpClass == null || amqpMethod == null)
{
throw new Exception("Could not find AMQP class or method for IModel method " + method.Name);
throw new Exception($"Could not find AMQP class or method for IModel method {method.Name}");
}
}

Expand Down Expand Up @@ -1227,7 +1227,7 @@ public void EmitModelMethod(MethodInfo method)
: replyMapping.m_methodName);
if (amqpReplyMethod == null)
{
throw new Exception("Could not find AMQP reply method for IModel method " + method.Name);
throw new Exception($"Could not find AMQP reply method for IModel method {method.Name}");
}
}

Expand Down Expand Up @@ -1269,7 +1269,7 @@ public void EmitModelMethod(MethodInfo method)
string contentHeaderExpr =
contentHeaderParameter == null
? "null"
: " (" + MangleClass(amqpClass.Name) + "Properties) " + contentHeaderParameter.Name;
: $" ({MangleClass(amqpClass.Name)}Properties) {contentHeaderParameter.Name}";
string contentBodyExpr =
contentBodyParameter == null ? "null" : contentBodyParameter.Name;

Expand Down Expand Up @@ -1316,15 +1316,15 @@ public void EmitModelMethod(MethodInfo method)

if (nowaitParameter != null)
{
EmitLine(" if (" + nowaitParameter.Name + ") {");
EmitLine(" ModelSend(__req," + contentHeaderExpr + "," + contentBodyExpr + ");");
EmitLine($" if ({nowaitParameter.Name}) {{");
EmitLine($" ModelSend(__req,{contentHeaderExpr},{contentBodyExpr});");
if (method.ReturnType == typeof(void))
{
EmitLine(" return;");
}
else
{
EmitLine(" return " + nowaitExpression + ";");
EmitLine($" return {nowaitExpression};");
}
EmitLine(" }");
}
Expand All @@ -1334,7 +1334,7 @@ public void EmitModelMethod(MethodInfo method)

if (amqpReplyMethod == null)
{
EmitLine(" ModelSend(__req," + contentHeaderExpr + "," + contentBodyExpr + ");");
EmitLine($" ModelSend(__req,{contentHeaderExpr},{contentBodyExpr});");
}
else
{
Expand All @@ -1358,24 +1358,24 @@ public void EmitModelMethod(MethodInfo method)
string fieldPrefix = IsAmqpClass(method.ReturnType) ? "_" : "";

// No field mapping --> it's assumed to be a struct to fill in.
EmitLine(" " + method.ReturnType + " __result = new " + method.ReturnType + "();");
EmitLine($" {method.ReturnType} __result = new {method.ReturnType}();");
foreach (FieldInfo fi in method.ReturnType.GetFields())
{
if (Attribute(fi, typeof(AmqpFieldMappingAttribute)) is AmqpFieldMappingAttribute returnFieldMapping)
{
EmitLine(" __result." + fi.Name + " = __rep." + fieldPrefix + returnFieldMapping.m_fieldName + ";");
EmitLine($" __result.{fi.Name} = __rep.{fieldPrefix}{returnFieldMapping.m_fieldName};");
}
else
{
EmitLine(" __result." + fi.Name + " = __rep." + fieldPrefix + fi.Name + ";");
EmitLine($" __result.{fi.Name} = __rep.{fieldPrefix}{fi.Name};");
}
}
EmitLine(" return __result;");
}
else
{
// Field mapping --> return just the field we're interested in.
EmitLine(" return __rep._" + returnMapping.m_fieldName + ";");
EmitLine($" return __rep._{returnMapping.m_fieldName};");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion projects/RabbitMQ.Client/client/api/AmqpTcpEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public override int GetHashCode()
/// </remarks>
public override string ToString()
{
return "amqp://" + HostName + ":" + Port;
return $"amqp://{HostName}:{Port}";
}
}
}
2 changes: 1 addition & 1 deletion projects/RabbitMQ.Client/client/api/AmqpTimestamp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public AmqpTimestamp(long unixTime) : this()
/// </summary>
public override string ToString()
{
return "((time_t)" + UnixTime + ")";
return $"((time_t){UnixTime})";
}
}
}
8 changes: 3 additions & 5 deletions projects/RabbitMQ.Client/client/api/ConnectionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ private void SetUri(Uri uri)
}
else
{
throw new ArgumentException("Wrong scheme in AMQP URI: " + uri.Scheme);
throw new ArgumentException($"Wrong scheme in AMQP URI: {uri.Scheme}");
}
string host = uri.Host;
if (!string.IsNullOrEmpty(host))
Expand All @@ -569,7 +569,7 @@ private void SetUri(Uri uri)
string[] userPass = userInfo.Split(':');
if (userPass.Length > 2)
{
throw new ArgumentException("Bad user info in AMQP " + "URI: " + userInfo);
throw new ArgumentException($"Bad user info in AMQP URI: {userInfo}");
}
UserName = UriDecode(userPass[0]);
if (userPass.Length == 2)
Expand All @@ -582,9 +582,7 @@ private void SetUri(Uri uri)
that has at least the path segment "/". */
if (uri.Segments.Length > 2)
{
throw new ArgumentException("Multiple segments in " +
"path of AMQP URI: " +
string.Join(", ", uri.Segments));
throw new ArgumentException($"Multiple segments in path of AMQP URI: {string.Join(", ", uri.Segments)}");
}
if (uri.Segments.Length == 2)
{
Expand Down
2 changes: 1 addition & 1 deletion projects/RabbitMQ.Client/client/api/PlainMechanism.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class PlainMechanism : IAuthMechanism
{
public byte[] handleChallenge(byte[] challenge, IConnectionFactory factory)
{
return Encoding.UTF8.GetBytes("\0" + factory.UserName + "\0" + factory.Password);
return Encoding.UTF8.GetBytes($"\0{factory.UserName}\0{factory.Password}");
}
}
}
2 changes: 1 addition & 1 deletion projects/RabbitMQ.Client/client/api/PublicationAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public static bool TryParse(string uriLikeString, out PublicationAddress result)
/// </summary>
public override string ToString()
{
return ExchangeType + "://" + ExchangeName + "/" + RoutingKey;
return $"{ExchangeType}://{ExchangeName}/{RoutingKey}";
}
}
}
4 changes: 2 additions & 2 deletions projects/RabbitMQ.Client/client/api/ShutdownReportEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public ShutdownReportEntry(string description, Exception exception)

public override string ToString()
{
string output = "Message: " + Description;
return (Exception != null) ? output + " Exception: " + Exception : output;
string description = $"Message: {Description}";
return (Exception != null) ? $"{description} Exception: {Exception}" : description;
}
}
}
2 changes: 1 addition & 1 deletion projects/RabbitMQ.Client/client/impl/AmqpVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public override int GetHashCode()
/// </remarks>
public override string ToString()
{
return Major + "-" + Minor;
return $"{Major}-{Minor}";
}
}
}
20 changes: 7 additions & 13 deletions projects/RabbitMQ.Client/client/impl/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,7 @@ public static IDictionary<string, object> DefaultClientProperties()
["version"] = Encoding.UTF8.GetBytes(s_version),
["platform"] = Encoding.UTF8.GetBytes(".NET"),
["copyright"] = Encoding.UTF8.GetBytes("Copyright (c) 2007-2020 VMware, Inc."),
["information"] = Encoding.UTF8.GetBytes("Licensed under the MPL. " +
"See https://www.rabbitmq.com/")
["information"] = Encoding.UTF8.GetBytes("Licensed under the MPL. See https://www.rabbitmq.com/")
};
return table;
}
Expand Down Expand Up @@ -335,8 +334,7 @@ public void Close(ShutdownEventArgs reason, bool abort, TimeSpan timeout)
}
else
{
LogCloseError("Couldn't close connection cleanly. "
+ "Socket closed unexpectedly", ioe);
LogCloseError("Couldn't close connection cleanly. Socket closed unexpectedly", ioe);
}
}
}
Expand Down Expand Up @@ -378,14 +376,12 @@ public void ClosingLoop()
{
if (_model0.CloseReason == null)
{
LogCloseError("Connection didn't close cleanly. "
+ "Socket closed unexpectedly", eose);
LogCloseError("Connection didn't close cleanly. Socket closed unexpectedly", eose);
}
}
catch (IOException ioe)
{
LogCloseError("Connection didn't close cleanly. "
+ "Socket closed unexpectedly", ioe);
LogCloseError("Connection didn't close cleanly. Socket closed unexpectedly", ioe);
}
catch (Exception e)
{
Expand Down Expand Up @@ -447,7 +443,7 @@ public void HandleMainLoopException(ShutdownEventArgs reason)
}

OnShutdown();
LogCloseError("Unexpected connection closure: " + reason, new Exception(reason.ToString()));
LogCloseError($"Unexpected connection closure: {reason}", new Exception(reason.ToString()));
}

public bool HardProtocolExceptionHandler(HardProtocolException hpe)
Expand All @@ -470,8 +466,7 @@ public bool HardProtocolExceptionHandler(HardProtocolException hpe)
}
else
{
LogCloseError("Hard Protocol Exception occured "
+ "while closing the connection", hpe);
LogCloseError("Hard Protocol Exception occured while closing the connection", hpe);
}

return false;
Expand Down Expand Up @@ -1117,8 +1112,7 @@ void StartAndTune()
IAuthMechanismFactory mechanismFactory = _factory.AuthMechanismFactory(mechanisms);
if (mechanismFactory == null)
{
throw new IOException("No compatible authentication mechanism found - " +
"server offered [" + mechanismsString + "]");
throw new IOException($"No compatible authentication mechanism found - server offered [{mechanismsString}]");
}
IAuthMechanism mechanism = mechanismFactory.GetInstance();
byte[] challenge = null;
Expand Down
2 changes: 1 addition & 1 deletion projects/RabbitMQ.Client/client/impl/Frame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ internal static InboundFrame ReadFrom(Stream reader)
if (frameEndMarker != Constants.FrameEnd)
{
ArrayPool<byte>.Shared.Return(payloadBytes);
throw new MalformedFrameException("Bad frame end marker: " + frameEndMarker);
throw new MalformedFrameException($"Bad frame end marker: {frameEndMarker}");
}

return new InboundFrame((FrameType)type, channel, payload);
Expand Down
4 changes: 1 addition & 3 deletions projects/RabbitMQ.Client/client/impl/ModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -640,9 +640,7 @@ public virtual void HandleBasicDeliver(string consumerTag,
{
if (DefaultConsumer == null)
{
throw new InvalidOperationException("Unsolicited delivery -" +
" see IModel.DefaultConsumer to handle this" +
" case.");
throw new InvalidOperationException("Unsolicited delivery - see IModel.DefaultConsumer to handle this case.");
}
else
{
Expand Down
3 changes: 1 addition & 2 deletions projects/RabbitMQ.Client/client/impl/RecordedBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ public virtual void Recover()

public override string ToString()
{
return string.Format("{0}: source = '{1}', destination = '{2}', routingKey = '{3}', arguments = '{4}'",
GetType().Name, Source, Destination, RoutingKey, Arguments);
return $"{GetType().Name}: source = '{Source}', destination = '{Destination}', routingKey = '{RoutingKey}', arguments = '{Arguments}'";
}

public RecordedBinding WithArguments(IDictionary<string, object> value)
Expand Down
3 changes: 1 addition & 2 deletions projects/RabbitMQ.Client/client/impl/RecordedExchange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public void Recover()

public override string ToString()
{
return string.Format("{0}: name = '{1}', type = '{2}', durable = {3}, autoDelete = {4}, arguments = '{5}'",
GetType().Name, Name, Type, Durable, IsAutoDelete, Arguments);
return $"{GetType().Name}: name = '{Name}', type = '{Type}', durable = {Durable}, autoDelete = {IsAutoDelete}, arguments = '{Arguments}'";
}

public RecordedExchange WithArguments(IDictionary<string, object> value)
Expand Down
3 changes: 1 addition & 2 deletions projects/RabbitMQ.Client/client/impl/RecordedQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ public RecordedQueue ServerNamed(bool value)

public override string ToString()
{
return string.Format("{0}: name = '{1}', durable = {2}, exlusive = {3}, autoDelete = {4}, arguments = '{5}'",
GetType().Name, Name, _durable, _exclusive, IsAutoDelete, _arguments);
return $"{GetType().Name}: name = '{Name}', durable = {_durable}, exlusive = {_exclusive}, autoDelete = {IsAutoDelete}, arguments = '{_arguments}'";
}
}
}
2 changes: 1 addition & 1 deletion projects/RabbitMQ.Client/client/impl/SessionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public virtual void OnSessionShutdown(ShutdownEventArgs reason)

public override string ToString()
{
return GetType().Name + "#" + ChannelNumber + ":" + Connection;
return $"{GetType().Name}#{ChannelNumber}:{Connection}";
}

public void Close(ShutdownEventArgs reason)
Expand Down
2 changes: 1 addition & 1 deletion projects/RabbitMQ.Client/client/impl/TcpClientAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public virtual async Task ConnectAsync(string host, int port)
IPAddress ep = TcpClientAdapterHelper.GetMatchingHost(adds, _sock.AddressFamily);
if (ep == default(IPAddress))
{
throw new ArgumentException("No ip address could be resolved for " + host);
throw new ArgumentException($"No ip address could be resolved for {host}");
}

#if NET461
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public RabbitMqExceptionDetail(IDictionary<string, object> ex)

public override string ToString()
{
return string.Format("Exception: {0}\r\n{1}\r\n\r\n{2}\r\nInnerException:\r\n{3}", Type, Message, StackTrace, InnerException);
return $"Exception: {Type}\r\n{Message}\r\n\r\n{StackTrace}\r\nInnerException:\r\n{InnerException}";
}
}
}
Loading