DEV Community

Discussion on: Should comments in code be considered failures in coding?

 
stereobooster profile image
stereobooster
doubleCheckIfThisFuelBecauseWeCantStoreItWithoutFireAlarm();

putPauseBecauseBugInZeexel2000MA();

aBitSlowerButMemoryOptimisedDijkstraBecauseWeSkipHeapAllocation();

like this?

Thread Thread
 
rossdrew profile image
Ross • Edited

How did you get from

For example in OOP we extend (or compose I guess) Warehouse with a FuelessWarehouse

to

doubleCheckIfThisFuelBecauseWeCantStoreItWithoutFireAlarm()

fun storeFuel(Item item, Warehouse warehouse) throws FireSafetyException{
 if (item instanceOf Fuel && !warehouse.hasFireAlarm()){
   throw FireSafetyException("Not equiped for fire safety")
 }
}

^ where is the comment required?

Thread Thread
 
stereobooster profile image
stereobooster

Example with business rules. I agree with you here.

Example with code trick (memoryOptimisedDijkstra) we agreed that we can put comment in the top of the function, which is out of your question because you ask only about comments iniside the block.

What if you explain some unexpected behaviour in other system? For example, we need to put pause here because our hardware is not able to proceed signals so fast.

This one is still valid use-case.

Thread Thread
 
rossdrew profile image
Ross • Edited

No, it isn't. Even this hack is better than a comment

fun doSomething(Operation o, Evnironment e){
  if (e.isSlow(){
     slowO =  SlowHardwareDelayOperationFactory.create(o)
     slowO.perform();
  }else{
     o.perform();
  }
}

because
a: it is verifiable, we can write a test to say if the environment is slow we add a delay, unlike a comment
b: it is self documenting
c: it's reusable, unlike a comment
d: if we no longer need the delay and remove it, there's no comment to clean up