View profile

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

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 ๐Ÿฅ)
.
.
.
Actually,
>>> print(list(g)) will only give you one element in the list which is -
[8].

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!

-Apoorv;
Did you enjoy this issue?
Apoorv Tyagi

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

๐—ฅ๐—ถ๐˜€๐—ฒ ๐˜๐—ผ ๐˜๐—ต๐—ฒ ๐˜๐—ผ๐—ฝ ๐Ÿญ% ๐˜„๐—ถ๐˜๐—ต ๐—ถ๐—ป๐—ฐ๐—ฟ๐—ฒ๐—บ๐—ฒ๐—ป๐˜๐—ฎ๐—น ๐—ด๐—ฎ๐—ถ๐—ป๐˜€ ๐—ถ๐—ป ๐˜†๐—ผ๐˜‚๐—ฟ ๐—ธ๐—ป๐—ผ๐˜„๐—น๐—ฒ๐—ฑ๐—ด๐—ฒ ๐—ฒ๐˜ƒ๐—ฒ๐—ฟ๐˜† ๐—บ๐—ผ๐—ป๐˜๐—ต.

๐—๐—ผ๐—ถ๐—ป ๐˜‚๐˜€ ๐—ฎ๐—ป๐—ฑ ๐Ÿญ๐Ÿฌ๐Ÿฌ+ ๐—บ๐—ฒ๐—บ๐—ฏ๐—ฒ๐—ฟ๐˜€ ๐ญ๐จ ๐ข๐ฆ๐ฉ๐ซ๐จ๐ฏ๐ž ๐š๐ฌ ๐š ๐๐ž๐ฏ๐ž๐ฅ๐จ๐ฉ๐ž๐ซ ๐—ฎ๐—ป๐—ฑ ๐ญ๐จ ๐ ๐ž๐ญ ๐ญ๐ก๐ž ๐›๐ž๐ฌ๐ญ ๐ญ๐ข๐ฉ๐ฌ ๐š๐ง๐ ๐š๐ซ๐ญ๐ข๐œ๐ฅ๐ž๐ฌ ๐—ฑ๐—ถ๐—ฟ๐—ฒ๐—ฐ๐˜๐—น๐˜† ๐˜๐—ผ ๐˜†๐—ผ๐˜‚๐—ฟ ๐—ถ๐—ป๐—ฏ๐—ผ๐˜… ๐Ÿ“ฉ

P.S. It is free (and fun!) :)

Tweet ย ย ย  Share
If you don't want these updates anymore, please unsubscribe here.
If you were forwarded this newsletter and you like it, you can subscribe here.
Powered by Revue