[ rss / options / help ]
post ]
[ b / iq / g / zoo ] [ e / news / lab ] [ v / nom / pol / eco / emo / 101 / shed ]
[ art / A / beat / boo / com / fat / job / lit / map / mph / poof / £$€¥ / spo / uhu / uni / x / y ] [ * | sfw | o ]
logo
technology

Return ]

Posting mode: Reply
Reply ]
Subject   (reply to 26068)
Message
File  []
close
fizzbuzz.png
260682606826068
>> No. 26068 Anonymous
19th August 2017
Saturday 2:04 pm
26068 spacer
Seriously, what's the most professional code? The first is the coolest but the second is the clearest.

for i in range(1, 101): s = (i % 3 == 0) * 'Fizz' + (i % 5 == 0) * 'Buzz' print(s if s else i)


for i in range(1, 101): s = '' if i % 3 == 0: s += 'Fizz' if i % 5 == 0: s += 'Buzz' if len(s) > 0: print(s) else: print(i)

Expand all images.
>> No. 26069 Anonymous
19th August 2017
Saturday 2:14 pm
26069 spacer
>>26068

for i in range(1, 101): print(((i % 3 == 0) * 'Fizz' + (i % 5 == 0) * 'Buzz') or i)

>> No. 26075 Anonymous
19th August 2017
Saturday 4:54 pm
26075 spacer
Somebody's been watching Tom Scott...
>> No. 26077 Anonymous
19th August 2017
Saturday 5:44 pm
26077 spacer
>>26075
Not OP, but he did get me into fucking about aimlessly with Python and JavaScript.
>> No. 26079 Anonymous
19th August 2017
Saturday 6:14 pm
26079 spacer
>>26068

Clearest is always considered better, unless there is an obvious efficiency factor that makes another piece of code better (either in the sense of scaling the project later, or the runtime).


The example is obviously simple enough for anyone in the know to read that it doesn't matter, but code that someone else can pick up and read with the least amount of head scratching and trying to connect the dots is better.
>> No. 26082 Anonymous
19th August 2017
Saturday 9:28 pm
26082 spacer
>>26079
Exactly this. The code that you write, that other people can read an understand is always the best. If you get to be a professional programmer, you'll understand how you're nearly always working in a team, you're always inheriting legacy code (and therefore technical debt) and it doesn't matter how clever your predecessors were, if others can't pick it up quickly and use it, it is fucking useless.

Most junior programmers think/believe that they're the only people who are going to read their code.

OP I prefer the second version by a long way. It's longer, not as cool, but you can easily figure out what it is doing.
>> No. 26083 Anonymous
19th August 2017
Saturday 10:24 pm
26083 spacer
I'm not into programming, but would it then be advisable to comment on every line you can?
>> No. 26084 Anonymous
19th August 2017
Saturday 10:28 pm
26084 spacer
>>26083
No, too many comments can be as bad as none at all. A line or two at the start of each method/function (depending on what you're programming) and then a line or two above any complex logical blocks. You don't need to overdo it.
>> No. 26085 Anonymous
20th August 2017
Sunday 1:20 pm
26085 spacer
>>26083
No. If used sparingly but judiciously comments can be of immense help for future readers but they're not "free".

Comments act as noise when trying to read code so the value they add must be worth the distraction.

They also require maintenance just like the code does, otherwise they risk drifting from the code they comment. Whoever gets to work on the code next then has to track down whether the comment provides the correct intention but the code is wrong or if the code is correct but the comment's outdated.
>> No. 26086 Anonymous
20th August 2017
Sunday 2:01 pm
26086 spacer
>>26083

Good code should be fairly self-explanatory. If you're writing a lot of comments, it's usually a sign that your code is overly complex and insufficiently modular. Comments should be the exception rather than the rule, used to mark out gotchas or clarify things that are unavoidably complex.
>> No. 26088 Anonymous
23rd August 2017
Wednesday 1:25 am
26088 spacer
Since any "coolness" is voided by its being fizzbuzz, you may as well opt for complete clarity.
>> No. 26089 Anonymous
23rd August 2017
Wednesday 1:42 am
26089 spacer
>>26088
You're quite right. If anyone brought me fizzbuzz in an interview I would walk; lack of imagination.

Return ]
whiteline

Delete Post []
Password