Is it okay to have long codes? | SoloLearn: Learn to code for FREE!

+18

Is it okay to have long codes?

I started learning python 1 and a half months ago, and today I was doing a 5 kyu challengeon codewars, and my code was 25 lines long, much longer than anyones. I was wondering if thats okay?

7/22/2020 2:20:55 PM

Kirill Vidov

65 Answers

New Answer

+33

Kirill Vidov your code may be long, but it is clear and readable. Everyone is able to see the logic even there are no commets. I like this style more than a onliner that no one can read and understand. A really good job 👏👏👏

+13

Shorter != Better. But you should of course avoid any clutter, unnecessary computing steps etc. Sometimes it increases the readability to write something a little bit longer, sometimes it's just mess. So it depends on the concrete code.

+9

Here is a code with a bit more complex encryption. I use this program to safe my passwords: https://code.sololearn.com/cxaFu6WWHv8B/?ref=app

+8

♤♢☞ 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 ☜♢♤, you're right. Seems I can't count. 🤣 My question was about UPPER/LOWER though!

+8

I've also tried a version. If it's supposed to be any readable, it can't be too short I suppose... https://code.sololearn.com/cs6qe2lsorWU/?ref=app

+8

Found this one on SL By @V*A*D*I*M Nice! https://code.sololearn.com/c5WBFQ5eiqxj/?ref=app In short, if Python has a library that can do it, don't reinvent the wheel

+7

One benefit of codewars is that you can read other peoples solution to the same problem. Then you can have those Eureka moments like "Oh this is similar to mine, but I could have simplified it like that! Oh, so it can be done with a list comprehension instead of a loop! And there is this library function I had no idea about!" and so on. Eventually come back to the same kata in a few weeks or months, and with the knowledge you will have picked up along the way, your next solution will be more beautiful.

+5

Most important parts for the code are time and space complexity (maybe readability after those 2), length doesn't really matter, I was like that as well, impressed by others short codes but at the end of the day, output is the same and speed is the only difference (tho it feels kinda cool to write a short code xD). Anyway here's my try: from string import ascii_lowercase as ab def rot13(msg): rot = [ab[(ab.index(v) + 13) % 26] if v in ab else v for v in msg.lower()] return ''.join(v if msg[i].islower() else v.upper() for i, v in enumerate(rot))

+5

If it comes to larger projects, readability is not possibly after anything else of importance, it's the #1 as long as it does not have an impact on performance.

+5

well obviously you should always try to write an easily readable code but your first thoughts must be how to optimize the algorithm, even if it is less readable that way, you can add some comments to make it simpler.

+5

Sebastian Keßler thank you so much, and thank you just like all other people in this thread for taking your time, I appreciate all of it.

+5

“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” – Martin Fowler Martin Fowler (born 1963) is a British software developer, author and international public speaker on software development, specialising in object-oriented analysis and design, UML, patterns, and agile software development methodologies, including extreme programming. https://en.wikipedia.org/wiki/Martin_Fowler_(software_engineer)

+4

What upper/lower rule is supposed to be applied?

+4

As long as they are modular and don't involve God objects or methods.

+4

To a point, a short code will likely be more readable. When it's two, three times longer than it has to be, with beginners it usually means, there's useless stuff going on. They have little practice figuring out the direct way to solve a problem, so they are meandering towards their goal. Someone reading the code has to find the meaning in the confusion, see which parts would have been unnecessary and which parts do the actual work, and how. That takes more time as if the algo went to its goal in a more straightforward fashion. However, there's a sweet spot. If you go beyond and make your code shorter, it starts to become cryptic. 'Hey, let's make one function out of two and save a few lines.' Yeah, but now the function is doing two things instead of one, but it has only one name. So your code has just become unnecessarily confusing. 'Hey, I can make one line out of these!' Alright, but do you still understand it afterwards, or is it 200 letters long and full of intertwined sets of parenthesises?

+3

Kirill Vidov Yeah it's ok, will you share the Question & attempt So that we can try to make it shorten

+3

Question: Create a function that takes a string and returns the string ciphered with Rot13. If there are numbers or special characters included in the string, they should be returned as they are. Only letters from the latin/english alphabet should be shifted, like in the original Rot13 "implementation". Please note that using encode is considered cheating. solution: import string def rot13(message): rot = [] alphabet = string.ascii_lowercase + string.ascii_uppercase alphabet = [i for i in alphabet] for char in message: if char.isspace(): rot.append(char) else: if char.isalpha(): pos = alphabet.index(char) try: if char.islower(): rot.append(alphabet[pos+13].lower()) else: rot.append(alphabet[pos+13].upper()) except IndexError: if char.islower(): rot.append(alphabet[pos-13].lower()) else: rot.append(alphabet[pos-13].upper()) else: rot.append(char) return "".join(rot)

+3

I think I would leave out some of those if-else-branches and rather determine the character previously. But in general your code is readable and not bad!

+3

HonFu Extend each letter by 13 letters Like H + 13 = U

+3

So upper 'A' will become 'P', lower 'a' will become 'p'?