Is it good practice to shorten functions? Like this code for instance:
using System;
namespace Hello
{
class Program
{
public static void Main(String[] args)
{
Write("Hello, World!");
Stagger();
}
public static void Write(string s)
{
Console.WriteLine(s);
}
public static void Stagger()
{
Console.ReadLine();
}
}
}
I get lazy and do stuff like that a lot. But is it proper?
2
If you do it to improve code readability, this is a good idea.
If you do it to type less, you probably do it wrong.
In your example, your methods don’t improve readability; I would even assert that they add complexity and decrease readability:
-
What is
Write
? Is it writing something to a file? A log? Right, you should read how the method is implemented (since you don’t even have XML comments) in order to know what is the purpose of the method. -
What do
Stagger
means? Unless this term is used a lot in the application domain, I would avoid it:Console.ReadLine
is much more explicit.
They might help you to type less, if you’re writing your apps in Notepad. If you use Visual Studio, Intellisense will make those methods irrelevant. Not counting the fact that if you count using the official C# style, you would be writing Program.Write
instead of just Write
, which means that the number of keys pressed while using Intellisense will be exactly the same.