+ 19

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?

22nd Jul 2020, 2:20 PM
Kirill
Kirill - avatar
65 Réponses
+ 35
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 👏👏👏
22nd Jul 2020, 6:34 PM
Sebastian Keßler
Sebastian Keßler - avatar
+ 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.
22nd Jul 2020, 2:40 PM
Sandra Meyer
Sandra Meyer - avatar
+ 12
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
22nd Jul 2020, 7:49 PM
Sebastian Keßler
Sebastian Keßler - avatar
+ 10
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
22nd Jul 2020, 7:51 PM
Louis
Louis - avatar
+ 8
♤♢☞ 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 ☜♢♤, you're right. Seems I can't count. 🤣 My question was about UPPER/LOWER though!
22nd Jul 2020, 6:43 PM
HonFu
HonFu - avatar
+ 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
22nd Jul 2020, 7:21 PM
HonFu
HonFu - avatar
+ 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.
22nd Jul 2020, 8:49 PM
Tibor Santa
Tibor Santa - avatar
+ 6
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))
22nd Jul 2020, 3:24 PM
Bagon
Bagon - avatar
+ 6
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.
22nd Jul 2020, 3:34 PM
Bagon
Bagon - avatar
+ 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.
22nd Jul 2020, 3:28 PM
Sandra Meyer
Sandra Meyer - avatar
+ 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.
22nd Jul 2020, 6:39 PM
Kirill
Kirill - avatar
+ 5
@Jayant Agarwal small codes are not always more efficient than longer ones. The art is to find a well working algorithm.
23rd Jul 2020, 5:19 AM
Sebastian Keßler
Sebastian Keßler - avatar
+ 4
What upper/lower rule is supposed to be applied?
22nd Jul 2020, 6:17 PM
HonFu
HonFu - avatar
+ 4
As long as they are modular and don't involve God objects or methods.
22nd Jul 2020, 10:59 PM
Sonic
Sonic - avatar
+ 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?
23rd Jul 2020, 7:13 AM
HonFu
HonFu - avatar
+ 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)
22nd Jul 2020, 2:43 PM
Kirill
Kirill - avatar
+ 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!
22nd Jul 2020, 2:58 PM
Sandra Meyer
Sandra Meyer - avatar
+ 3
So upper 'A' will become 'P', lower 'a' will become 'p'?
22nd Jul 2020, 6:26 PM
HonFu
HonFu - avatar
+ 3
One of my favorites from Uncle Bob, the father of 'Clean Code': You are not done when it works, you are done when it's right. We may arrive at a solution in roundabout ways that can give correct result, but the code itself is messy. If you want to become a good programmer, don't be satisfied with that much only. Refactor the code, until it looks right. Follow the best practices of the craft such as DRY (don't repeat yourself), KISS (keep it stupid simple) and follow general coding style guidelines such as PEP8 in python. Keep in mind that good code must be easy to read and understand for others. https://mattersmathematical.files.wordpress.com/2015/09/img_1991.jpg
23rd Jul 2020, 7:45 AM
Tibor Santa
Tibor Santa - avatar
+ 3
Yeah it can be long, but just make sure it's readable, and any chance you get to make it shorter and understandable, take it 😉😁
23rd Jul 2020, 11:39 AM
SB5K
SB5K - avatar