The Trojan

By Apoorv Tyagi

Learn to name your variables ๐Ÿ‘จโ€๐Ÿ’ป



Subscribe to our newsletter

By subscribing, you agree with Revueโ€™s Terms of Service and Privacy Policy and understand that The Trojan will receive your email address.

Learn to name your variables ๐Ÿ‘จโ€๐Ÿ’ป
By Apoorv Tyagi • Issue #2 • View online
๐Ÿ”Šย If you want to listen to this newsletter asย audio while you read,ย you can do it by clicking here (Hope you enjoy the audio version๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ)
Welcome to the second issue of The Trojanโ€™s Wednesday Wisdom newsletter.ย I hope you all had an amazing last week.
Today Iโ€™ll be sharing some bits about naming in Software development ๐Ÿ’ป
Names are everywhere in software. Files, packages, variables functions, etc. Because naming things is one of the most common tasks that we do in software development, we have to do it well.
Also as a Software Engineer ๐Ÿ‘ฉโ€๐Ÿ’ป, most of our time is spent reading the existing code rather than writing new one. Therefore, the code should be as readable and clear as possible.
This can be done by selecting words that are close to the human language that we naturally use. Naming is a key part of that.
Moreover, coming up with a good name reveals the nature of an entity, which helps us divide the responsibilities of our components or classes better. This ultimately leads to maintainable and extendable code.
โ€œPrograms are meant to be read by humans and only incidentally for computers to executeโ€ โ€” Donald Knuth
Now Grab a coffee โ˜•, and letโ€™s talk about one of the important points we need to consider while naming software class / functions / variables.

Use Intent Revealing Names ๐Ÿ’ก
The name of a variable, function or class, should answer all the important questions. It should tell you why it exists, what it does, and how it is used.ย If it requires a comment, then the name does not reveal its intent (Exception exists ๐Ÿ˜‰)
Look at the code below ๐Ÿ‘‡
"bad code" example.
"bad code" example.
This code is simple, but create many questions:
  1. What is the content ofย โ€œtheListโ€?
  2. What is the significance of the itemย x[0]ย in the list?
  3. Why we compareย x[0]ย vsย 4?
  4. How would I use the returned list?
The answers to these questions are not present in that code sample, but they could have been.
We can refactor the previous code as follows to get better understanding:
"good code" example.
"good code" example.
Now we know all the information:
  1. โ€œtheListโ€ย represents theย game Board.
  2. x[0]ย represents a cell in the board andย 4ย represents a flagged cell.
  3. The returned list represents theย โ€œflaggedCellsโ€.
Notice that the simplicity of the code has not changed. It still has exactly the same number of operators and constants, with exactly the same number of nesting levels. But the code has become much more explicit.
Further, we can try to improve the code by writing a simple class for cells instead of using an array ofย int.
It can include anย intention-revealing functionย (called itย โ€œisFlaggedโ€) to hide the magic numbers.
"better code" example.
"better code" example.
Finishing off what we left
Last week, I asked you to guess the output of the below python code I also told you that itโ€™s not [1,8,15].
It has something to do with โ€œEvaluation time discrepancy in pythonโ€. So Letโ€™s find out what exactly will get printed -
(Drum roll please ๐Ÿฅ)
>>> print(list(g)) will only give you one element in the list which is -
But Why?? ๐Ÿคฏ
In a generator expression, the โ€œinโ€ clause is evaluated at declaration time, but the conditional clause is evaluated at run time.
So before run time, array is re-assigned to the list [2, 8, 22], and since out of 1, 8 and 15, only element that is left is โ€˜8โ€™ whose count is greater than 0, the generator only yields 8.
hence, in output youโ€™ll see - [8].
๐Ÿ‘‹ Thatโ€™s it for this week. I hope you liked this issue and what I included this time. Again, Iโ€™d love to hear your feedback on how I can make this newsletter better for you.
If you enjoyed this newsletterย consider subscribingย (in case you havenโ€™t already) and share it with those you feel can get benefit out of it.
Have a nice week and enjoy yourselves.
๐Ÿ‘€ See you on the Internet next Wednesday!
Did you enjoy this issue?
Apoorv Tyagi

๐Ÿ“ฉ ๐—” ๐—ป๐—ฒ๐˜„๐˜€๐—น๐—ฒ๐˜๐˜๐—ฒ๐—ฟ ๐—ฎ๐˜ ๐˜๐—ต๐—ฒ ๐—ถ๐—ป๐˜๐—ฒ๐—ฟ๐˜€๐—ฒ๐—ฐ๐˜๐—ถ๐—ผ๐—ป ๐—ผ๐—ณ ๐๐ฌ๐ฒ๐œ๐ก๐จ๐ฅ๐จ๐ ๐ฒ, ๐๐จ๐ง-๐…๐ข๐œ๐ญ๐ข๐จ๐ง ๐๐จ๐จ๐ค๐ฌ ๐—ฎ๐—ป๐—ฑ ๐’๐จ๐Ÿ๐ญ๐ฐ๐š๐ซ๐ž ๐„๐ง๐ ๐ข๐ง๐ž๐ž๐ซ๐ข๐ง๐ .

๐๐จ, ๐ฐ๐ž ๐๐จ๐ง'๐ญ ๐œ๐ฅ๐š๐ข๐ฆ ๐ญ๐จ ๐ฆ๐š๐ค๐ž ๐ฒ๐จ๐ฎ ๐ฌ๐ฆ๐š๐ซ๐ญ๐ž๐ซ ๐ž๐ฏ๐ž๐ซ๐ฒ ๐ฐ๐ž๐ž๐ค. ๐Ž๐ง๐ฅ๐ฒ ๐ฌ๐ฎ๐›๐ฌ๐œ๐ซ๐ข๐›๐ž ๐ข๐Ÿ ๐ฒ๐จ๐ฎ๐ซ ๐ข๐ง๐ญ๐ž๐ซ๐ž๐ฌ๐ญ๐ฌ ๐š๐ฅ๐ข๐ ๐ง ๐ฐ๐ข๐ญ๐ก ๐š๐ง๐ฒ ๐จ๐Ÿ ๐ญ๐ก๐ž ๐ญ๐ก๐ซ๐ž๐ž ๐ญ๐ก๐ข๐ง๐ ๐ฌ ๐ฆ๐ž๐ง๐ญ๐ข๐จ๐ง๐ž๐ ๐š๐›๐จ๐ฏ๐ž!

P.S. It is free (and probably fun)

Tweet ย ย ย  Share
In order to unsubscribe, click here.
If you were forwarded this newsletter and you like it, you can subscribe here.
Powered by Revue