TL;DR: You should write code in a way that your colleagues would understand best. After all, they are the ones that will be reading it over and over again. No matter in how much of a hurry you are, you should always take the time to give things meaningful names.
Who do you write code for? It’s a simple question with an obvious answer.
As Martin Fowler bluntly states it:
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
The days of a programmer writing cryptic assembly code that only their computer understands have long gone by. The question deserves attention however, as I seem to keep bumping into examples that prove not everyone shares this belief (or is too lazy to act upon it).
The example
Recently, I came across a bit of SQL code that looked like this:
DECLARE @nvcWFN_SQL NVARCHAR(MAX)
DECLARE @nvcWFN_SQL_1 NVARCHAR(MAX)
DECLARE @nvcWFN_SQL_2 NVARCHAR(MAX)
DECLARE @nvcWFN_SQL_2Bis NVARCHAR(MAX)
DECLARE @nvcWFN_SQL_2Bis2 NVARCHAR(MAX)
DECLARE @nvcWFN_SQL_2Bis3 NVARCHAR(MAX)
DECLARE @nvcWFN_SQL_3 NVARCHAR(MAX)
DECLARE @nvcWFN_SQL_4 NVARCHAR(MAX)
DECLARE @nvcWFN_SQL_4Bis NVARCHAR(MAX)
DECLARE @nvcWFN_SQL_4Bis2 NVARCHAR(MAX)
DECLARE @nvcWFN_SQL_4Bis3 NVARCHAR(MAX)
DECLARE @nvcWFN_SQL_5 NVARCHAR(MAX)
DECLARE @nvcWFN_SQL_6 NVARCHAR(MAX)
--The code goes on by initializing these variables with parts of
--a SQL SELECT query
SET @nvcWFN_SQL =
@nvcWFN_SQL_1 + @nvcWFN_SQL_2 + @nvcWFN_SQL_2Bis +
@nvcWFN_SQL_2Bis2 + @nvcWFN_SQL_2Bis3 + @nvcWFN_SQL_3 +
@nvcWFN_SQL_4 + @nvcWFN_SQL_4Bis + @nvcWFN_SQL_4Bis2 +
@nvcWFN_SQL_4Bis3 + @nvcWFN_SQL_5 + @nvcWFN_SQL_6
--Execute the dynamically generated query
EXEC sp_executesql @nvcWFN_SQL
This is part of some code that generates a complex SQL query at runtime. Ignore the fact that this might not be the best idea ever, but just analyze the code itself.
Continue reading →