Tuesday, March 09, 2010

KISS Vs DRY

In my last post I talked about the expert programmer knowing when not to implement a feature or widget, so as to keep the product and codebase as maintainable as possible.

This idealogy stems from two of the core principles of good software development practice: KISS - Keep it simple, stupid. And DRY - Don't Repeat Yourself.

But it turns out that these two fundamental practices are occasionally locked in a violent and bloody conflict.

DRY often requires you to enhance the complexity of the software, violating KISS.

Consider the case where you could either implement a new module - and with it a new interface - or attempt to re-use an existing module with a fixed interface.

The first option would be the easiest to code as you have free reign over the interface definition, sticking to KISS. But it would violate DRY.

The second option may require quite a bit of work in coding up a wrapper and violating KISS. But it would conform to DRY.

In this example, which method you choose depends on a number of factors and although the second option is normally the correct one, it won't always be.

4 comments:

Victor said...

Sorry but I have a bad feeling about your article. It is too verbose and not DRY. I see too much repeated letters and even words. Horrible! So lets optimise it.

1) Save text of your article in the text file (1106 bytes) named kiss.txt
2) zip dry.zip kiss.txt -9
3) We have much more concise version in dry.zip(757 bytes) with same amount of information stored. Nothing was lost but now it is so wonderful. Take a look:
PK �tk<y[��O R kiss.txtUT ���K ��Kux
� � ]S�r�0 �� ץ�� 钸Ѹ�Ӹ< G # G �(�}�@* � G$��}��h�(r�4+ g� /��;]*�QHn�d,g 2O�d�$]C h %QRlS
� e�T�� �K �Lk���g*J\l�Edn�������ɩ�؞�C��q �;h���/OO��P(x���F��T��:Q]��o�N�a�� �<�z ���X�r��s�:gv58�No:ћ�
�J� �u��� ���� ���Uz� �S�\$��� n� t'
�� �E�ؠ.�1���F��@��%d�m��ŒFNN;L�-��n�݈���Ղ4������޼��<�0 ��%z���� v��&D��N���=fž�0%��l }����֘,��"OhX(�ǁ� P�pkV ��� =չ�2Z �n�*�QJöBYy���W��"� �Dz= ��) �E �����BG����4�6 �� g4�v���N������bɂ��/3���3�W3��< �Z��'c�'��}� �]]��F��{�lT-&n��QJ�TZ��� «��&sġe ��<���������ʂk� # ;� W
�xy� PK �tk<y[��O R ��kiss.txtUT ���Kux
� � PK N�

Isn't it awesome? :) We stored 32% of store and it is so concise. It is almost Perl.

btw, I have a secret tecnique for opposite process: unzip dry.zip

Victor

fsilber said...

So, you're saying that a wrapper is likely to be more complex than the contents it wraps?

Giorgio said...

Coding a wrapper is not violating KISS if you're not touching the original system. Actually it is the simpler solution, because when you start duplicate code you will acquire a new module to maintain, with possibly a different language and mental module than the old one (adding complexity).

Ian Hickman said...

@Victor - thanks for making me smile, that's the best comment I've had on my blog in ages!

@fsilber - the wrapper shouldn't be more complex, but the complexity comes from adding an extra layer to the architecture.

@Giorgio - normally it would be simpler to code up a wrapper. There are occasions when coding a new module is the simpler situation, but it is rare.